From 9826907fbbf39330396fbe801aae740b3eabb5ef Mon Sep 17 00:00:00 2001 From: ImNotAVirus <17680522+ImNotAVirus@users.noreply.github.com> Date: Fri, 22 Sep 2023 01:07:09 +0200 Subject: [PATCH] :recycle: Small refacto --- apps/game_service/lib/game_service/system.ex | 24 +++++++++++++++++++ .../game_service/systems/entity_visibility.ex | 22 +++++------------ .../systems/entity_visibility_system_test.exs | 18 +++++++------- 3 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 apps/game_service/lib/game_service/system.ex diff --git a/apps/game_service/lib/game_service/system.ex b/apps/game_service/lib/game_service/system.ex new file mode 100644 index 00000000..e71b4767 --- /dev/null +++ b/apps/game_service/lib/game_service/system.ex @@ -0,0 +1,24 @@ +defmodule GameService.System do + @moduledoc """ + TODO: Documentation for GameService.System + """ + + alias ElvenGard.ECS.Query + + alias GameService.EntityComponents.PositionComponent + alias GameService.PlayerComponents.EndpointComponent + + @spec map_event(any(), PositionComponent.t()) :: any() + def map_event(event, %PositionComponent{map_ref: map_ref}) do + # Get all endpoints on the current map + endpoints = + Query.select( + EndpointComponent, + with: [{PositionComponent, [{:==, :map_ref, map_ref}]}] + ) + |> Query.all() + + # Broadcast the entity spawn to players + GameService.broadcast_to(event, endpoints) + end +end diff --git a/apps/game_service/lib/game_service/systems/entity_visibility.ex b/apps/game_service/lib/game_service/systems/entity_visibility.ex index 9be52c08..fee65378 100644 --- a/apps/game_service/lib/game_service/systems/entity_visibility.ex +++ b/apps/game_service/lib/game_service/systems/entity_visibility.ex @@ -12,41 +12,31 @@ defmodule GameService.EntityVisibilitySystem do GameService.Events.EntityDespawned ] - alias ElvenGard.ECS.Query alias GameService.Events.{EntitySpawned, EntityDespawned} - alias GameService.PlayerComponents.EndpointComponent - alias GameService.EntityComponents.PositionComponent + alias GameService.EntityComponents, as: E # System behaviour @impl true def run(%EntitySpawned{entity: entity, components: components}, _delta) do components - |> Enum.find(&match?(%PositionComponent{}, &1)) + |> Enum.find(&match?(%E.PositionComponent{}, &1)) |> then(&broadcast_event(:entity_spawn, entity, components, &1)) end def run(%EntityDespawned{entity: entity, components: components}, _delta) do components - |> Enum.find(&match?(%PositionComponent{}, &1)) + |> Enum.find(&match?(%E.PositionComponent{}, &1)) |> then(&broadcast_event(:entity_despawn, entity, components, &1)) end ## Helpers - defp broadcast_event(event, entity, components, %PositionComponent{map_ref: map_ref}) do - # Get all endpoints on the current map - endpoints = - Query.select( - EndpointComponent, - with: [{PositionComponent, [{:==, :map_ref, map_ref}]}] - ) - |> Query.all() - + defp broadcast_event(event_name, entity, components, %E.PositionComponent{} = position) do # Transform the entity + components to a bundle bundle = GameService.preload_bundle(entity, components) - # Broadcast the entity spawn to players - GameService.broadcast_to({event, bundle}, endpoints) + # Send Events + GameService.System.map_event({event_name, bundle}, position) end end diff --git a/apps/game_service/test/game_service/systems/entity_visibility_system_test.exs b/apps/game_service/test/game_service/systems/entity_visibility_system_test.exs index 12572bc2..3d9eced1 100644 --- a/apps/game_service/test/game_service/systems/entity_visibility_system_test.exs +++ b/apps/game_service/test/game_service/systems/entity_visibility_system_test.exs @@ -2,23 +2,23 @@ defmodule GameService.EntityVisibilitySystemTest do use GameService.EntityCase, async: true alias GameService.PlayerBundle - alias GameService.Events.{EntitySpawned, EntityDespawned} - alias GameService.PlayerComponents.EndpointComponent - alias GameService.EntityComponents.PositionComponent alias GameService.EntityVisibilitySystem + alias GameService.Events, as: Evt + alias GameService.EntityComponents, as: E + alias GameService.PlayerComponents, as: P ## Tests test "system notify on Entity spawn" do # Register our process to receive message ref = make_ref() - position = %PositionComponent{map_ref: ref} - endpoint = %EndpointComponent{pid: self()} + position = %E.PositionComponent{map_ref: ref} + endpoint = %P.EndpointComponent{pid: self()} _ = spawn_player(components: [endpoint, position]) # Call our System with a EntitySpawned event entity = spawn_player(components: [position]) - event = %EntitySpawned{entity: entity, components: [position]} + event = %Evt.EntitySpawned{entity: entity, components: [position]} _ = EntityVisibilitySystem.run(event, 0) # We should receive an event @@ -30,13 +30,13 @@ defmodule GameService.EntityVisibilitySystemTest do test "system notify on Entity despawn" do # Register our process to receive message ref = make_ref() - position = %PositionComponent{map_ref: ref} - endpoint = %EndpointComponent{pid: self()} + position = %E.PositionComponent{map_ref: ref} + endpoint = %P.EndpointComponent{pid: self()} _ = spawn_player(components: [endpoint, position]) # Call our System with a EntitySpawned event entity = spawn_player(components: [position]) - event = %EntityDespawned{entity: entity, components: [position]} + event = %Evt.EntityDespawned{entity: entity, components: [position]} _ = EntityVisibilitySystem.run(event, 0) # We should receive an event