Skip to content

Commit

Permalink
Fix dead players doing damage (#1009)
Browse files Browse the repository at this point in the history
* Execute mechanics from player if player is alive only

* Make execute_mechanic/4 private

* Use ubuntu 22.04 instead of latest

* Format elixir code
  • Loading branch information
Nico-Sanchez authored Jan 2, 2025
1 parent 4601bd9 commit 052f0bf
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 additions & 48 deletions apps/arena/lib/arena/game/skill.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ defmodule Arena.Game.Skill do
end)
end

def do_mechanic(
game_state,
entity,
%{type: "circle_hit"} = circle_hit,
%{skill_direction: skill_direction} = _skill_params
) do
def do_mechanic(game_state, entity, mechanic, skill_params) do
if entity.category == :player and not Player.alive?(entity) do
game_state
else
execute_mechanic(game_state, entity, mechanic, skill_params)
end
end

defp execute_mechanic(
game_state,
entity,
%{type: "circle_hit"} = circle_hit,
%{skill_direction: skill_direction} = _skill_params
) do
circle_center_position = get_position_with_offset(entity.position, skill_direction, circle_hit.offset)
circular_damage_area = Entities.make_circular_area(entity.id, circle_center_position, circle_hit.range)

Expand All @@ -37,12 +45,12 @@ defmodule Arena.Game.Skill do
|> maybe_move_player(entity, circle_hit[:move_by])
end

def do_mechanic(
game_state,
entity,
%{type: "cone_hit"} = cone_hit,
%{skill_direction: skill_direction} = _skill_params
) do
defp execute_mechanic(
game_state,
entity,
%{type: "cone_hit"} = cone_hit,
%{skill_direction: skill_direction} = _skill_params
) do
triangle_points =
Physics.calculate_triangle_vertices(
entity.position,
Expand All @@ -58,7 +66,7 @@ defmodule Arena.Game.Skill do
|> maybe_move_player(entity, cone_hit[:move_by])
end

def do_mechanic(game_state, entity, %{type: "multi_cone_hit"} = multi_cone_hit, skill_params) do
defp execute_mechanic(game_state, entity, %{type: "multi_cone_hit"} = multi_cone_hit, skill_params) do
Enum.each(1..(multi_cone_hit.amount - 1), fn i ->
mechanic = %{multi_cone_hit | type: "cone_hit"}

Expand All @@ -69,15 +77,15 @@ defmodule Arena.Game.Skill do
)
end)

do_mechanic(game_state, entity, %{multi_cone_hit | type: "cone_hit"}, skill_params)
execute_mechanic(game_state, entity, %{multi_cone_hit | type: "cone_hit"}, skill_params)
end

def do_mechanic(game_state, entity, %{type: "multi_circle_hit", amount: nil} = multi_circle_hit, skill_params) do
defp execute_mechanic(game_state, entity, %{type: "multi_circle_hit", amount: nil} = multi_circle_hit, skill_params) do
amount = div(multi_circle_hit.duration_ms, multi_circle_hit.interval_ms)
do_mechanic(game_state, entity, Map.put(multi_circle_hit, :amount, amount), skill_params)
execute_mechanic(game_state, entity, Map.put(multi_circle_hit, :amount, amount), skill_params)
end

def do_mechanic(game_state, entity, %{type: "multi_circle_hit"} = multi_circle_hit, skill_params) do
defp execute_mechanic(game_state, entity, %{type: "multi_circle_hit"} = multi_circle_hit, skill_params) do
Enum.each(1..(multi_circle_hit.amount - 1), fn i ->
mechanic = %{multi_circle_hit | type: "circle_hit"}

Expand All @@ -88,15 +96,15 @@ defmodule Arena.Game.Skill do
)
end)

do_mechanic(game_state, entity, %{multi_circle_hit | type: "circle_hit"}, skill_params)
execute_mechanic(game_state, entity, %{multi_circle_hit | type: "circle_hit"}, skill_params)
end

def do_mechanic(
game_state,
entity,
%{type: "dash", speed: speed, duration_ms: duration_ms},
%{skill_direction: skill_direction} = _skill_params
) do
defp execute_mechanic(
game_state,
entity,
%{type: "dash", speed: speed, duration_ms: duration_ms},
%{skill_direction: skill_direction} = _skill_params
) do
Process.send_after(self(), {:stop_dash, entity.id, entity.aditional_info.base_speed}, duration_ms)

## Modifying base_speed rather than speed because effects will reset the speed on game tick
Expand All @@ -113,7 +121,7 @@ defmodule Arena.Game.Skill do
%{game_state | players: players}
end

def do_mechanic(game_state, entity, %{type: "repeated_shot"} = repeated_shot, skill_params) do
defp execute_mechanic(game_state, entity, %{type: "repeated_shot"} = repeated_shot, skill_params) do
remaining_amount = repeated_shot.amount - 1

if remaining_amount > 0 do
Expand Down Expand Up @@ -152,12 +160,12 @@ defmodule Arena.Game.Skill do
|> put_in([:projectiles, projectile.id], projectile)
end

def do_mechanic(
game_state,
entity,
%{type: "multi_shoot"} = multishot,
%{skill_direction: skill_direction} = skill_params
) do
defp execute_mechanic(
game_state,
entity,
%{type: "multi_shoot"} = multishot,
%{skill_direction: skill_direction} = skill_params
) do
entity_player_owner = get_entity_player_owner(game_state, entity)

calculate_angle_directions(multishot.amount, multishot.angle_between, skill_direction)
Expand Down Expand Up @@ -187,12 +195,12 @@ defmodule Arena.Game.Skill do
end)
end

def do_mechanic(
game_state,
entity,
%{type: "simple_shoot"} = simple_shoot,
%{skill_direction: skill_direction, can_pick_destination: true} = skill_params
) do
defp execute_mechanic(
game_state,
entity,
%{type: "simple_shoot"} = simple_shoot,
%{skill_direction: skill_direction, can_pick_destination: true} = skill_params
) do
last_id = game_state.last_id + 1
entity_player_owner = get_entity_player_owner(game_state, entity)

Expand Down Expand Up @@ -233,12 +241,12 @@ defmodule Arena.Game.Skill do
|> put_in([:projectiles, projectile.id], projectile)
end

def do_mechanic(
game_state,
entity,
%{type: "simple_shoot"} = simple_shoot,
%{skill_direction: skill_direction} = skill_params
) do
defp execute_mechanic(
game_state,
entity,
%{type: "simple_shoot"} = simple_shoot,
%{skill_direction: skill_direction} = skill_params
) do
last_id = game_state.last_id + 1
entity_player_owner = get_entity_player_owner(game_state, entity)

Expand All @@ -264,7 +272,7 @@ defmodule Arena.Game.Skill do
|> put_in([:projectiles, projectile.id], projectile)
end

def do_mechanic(game_state, entity, %{type: "leap"} = leap, %{execution_duration: execution_duration}) do
defp execute_mechanic(game_state, entity, %{type: "leap"} = leap, %{execution_duration: execution_duration}) do
Process.send_after(
self(),
{:stop_leap, entity.id, entity.aditional_info.base_speed, leap.on_arrival_mechanic},
Expand All @@ -283,7 +291,7 @@ defmodule Arena.Game.Skill do
put_in(game_state, [:players, player.id], player)
end

def do_mechanic(game_state, entity, %{type: "teleport"}, %{skill_destination: skill_destination}) do
defp execute_mechanic(game_state, entity, %{type: "teleport"}, %{skill_destination: skill_destination}) do
entity =
entity
|> Map.put(:aditional_info, entity.aditional_info)
Expand All @@ -292,7 +300,12 @@ defmodule Arena.Game.Skill do
put_in(game_state, [:players, entity.id], entity)
end

def do_mechanic(game_state, %{category: :projectile} = entity, %{type: "spawn_pool"} = pool_params, skill_params) do
defp execute_mechanic(
game_state,
%{category: :projectile} = entity,
%{type: "spawn_pool"} = pool_params,
skill_params
) do
last_id = game_state.last_id + 1
entity_player_owner = get_entity_player_owner(game_state, entity)

Expand Down Expand Up @@ -324,7 +337,7 @@ defmodule Arena.Game.Skill do
|> put_in([:last_id], last_id)
end

def do_mechanic(game_state, player, %{type: "spawn_pool"} = pool_params, skill_params) do
defp execute_mechanic(game_state, player, %{type: "spawn_pool"} = pool_params, skill_params) do
last_id = game_state.last_id + 1
entity_player_owner = get_entity_player_owner(game_state, player)

Expand Down Expand Up @@ -356,7 +369,7 @@ defmodule Arena.Game.Skill do
|> put_in([:last_id], last_id)
end

def do_mechanic(game_state, entity, {:polygon_hit, polygon_hit}, _skill_params) do
defp execute_mechanic(game_state, entity, {:polygon_hit, polygon_hit}, _skill_params) do
polygon_damage_area = Entities.make_polygon_area(entity.id, polygon_hit.vertices)

entity_player_owner = get_entity_player_owner(game_state, entity)
Expand Down

0 comments on commit 052f0bf

Please sign in to comment.