-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Add Entity change direction System
- Loading branch information
1 parent
9826907
commit ca217ac
Showing
6 changed files
with
129 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
apps/game_service/lib/game_service/systems/entity_map_action.ex
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
apps/game_service/lib/game_service/systems/entity_map_actions.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
defmodule GameService.EntityMapActionsSystem do | ||
@moduledoc """ | ||
TODO: Documentation for GameService.EntityMapActionsSystem | ||
""" | ||
|
||
use ElvenGard.ECS.System, | ||
lock_components: [ | ||
GameService.EntityComponents.PositionComponent, | ||
GameService.EntityComponents.DirectionComponent, | ||
GameService.EntityComponents.LevelComponent, | ||
GameService.PlayerComponents.HeroLevelComponent, | ||
GameService.EntityComponents.CombatComponent, | ||
# GameService.EntityComponents.BuffComponent, | ||
GameService.EntityComponents.SpeedComponent, | ||
GameService.EntityComponents.SittingComponent | ||
], | ||
event_subscriptions: [ | ||
GameService.Events.DirectionChanged, | ||
GameService.Events.EntityInfoRequest, | ||
GameService.Events.Movement, | ||
GameService.Events.Sitting | ||
] | ||
|
||
require Logger | ||
|
||
alias ElvenGard.ECS.{Command, Query} | ||
|
||
alias GameService.EntityComponents, as: E | ||
|
||
alias GameService.Events.{ | ||
DirectionChanged | ||
# EntityInfoRequest, | ||
# Movement, | ||
# Sitting | ||
} | ||
|
||
# System behaviour | ||
|
||
@impl true | ||
def run(%DirectionChanged{} = event, _delta) do | ||
%DirectionChanged{ | ||
entity_type: entity_type, | ||
entity_id: entity_id, | ||
value: value | ||
} = event | ||
|
||
ecs_type = GameService.entity_type_to_prefix(entity_type) | ||
ecs_id = {ecs_type, entity_id} | ||
|
||
# Check if the Entity exists | ||
with {:ok, entity} <- Query.fetch_entity(ecs_id), | ||
# Then update it's DirectionComponent | ||
{:ok, _} <- Command.update_component(entity, E.DirectionComponent, value: value), | ||
# Then get the current map | ||
{:ok, position} <- Query.fetch_component(entity, E.PositionComponent) do | ||
# Finally, notify all players on map | ||
event = {:direction_changed, entity_type, entity_id, value} | ||
GameService.System.map_event(event, position) | ||
else | ||
e -> | ||
Logger.error( | ||
"[#{inspect(__MODULE__)}] Can't set direction, " <> | ||
"got #{inspect(e)} for #{inspect(event)}" | ||
) | ||
end | ||
end | ||
|
||
def run(event, _delta) do | ||
Logger.warn("#{inspect(__MODULE__)} unhandled event #{inspect(event)}") | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
apps/game_service/test/game_service/systems/entity_map_actions_system_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule GameService.EntityMapActionsSystemTest do | ||
use GameService.EntityCase, async: true | ||
|
||
alias ElvenGard.ECS.Query | ||
|
||
alias GameService.Events, as: Evt | ||
alias GameService.EntityMapActionsSystem | ||
alias GameService.EntityComponents, as: E | ||
alias GameService.PlayerComponents, as: P | ||
|
||
## Tests | ||
|
||
test "system notify on Entity change direction" do | ||
# Register our process to receive message | ||
ref = make_ref() | ||
position = %E.PositionComponent{map_ref: ref} | ||
endpoint = %P.EndpointComponent{pid: self()} | ||
_ = spawn_player(components: [endpoint, position]) | ||
|
||
# Create our fake Entity with a DirectionComponent | ||
direction = %E.DirectionComponent{value: :south} | ||
entity = spawn_player(components: [position, direction]) | ||
|
||
# Call our System with a DirectionChanged event | ||
event = %Evt.DirectionChanged{ | ||
entity_type: :character, | ||
entity_id: GameService.entity_id(entity), | ||
value: :north | ||
} | ||
|
||
_ = EntityMapActionsSystem.run(event, 0) | ||
|
||
# Check that the DirectionComponent was updated | ||
{:ok, component} = Query.fetch_component(entity, E.DirectionComponent) | ||
assert %E.DirectionComponent{value: :north} = component | ||
|
||
# # We should receive an event | ||
assert_receive {:direction_changed, entity_type, entity_id, value} | ||
assert entity_type == event.entity_type | ||
assert entity_id == event.entity_id | ||
assert value == event.value | ||
end | ||
end |