Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-604] Spawn power ups outside colliders #623

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ defmodule Arena.GameUpdater do
random_position_in_map(
item_config.radius,
state.game_state.external_wall,
state.game_state.obstacles
state.game_state.obstacles,
state.game_state.external_wall.position,
state.game_state.external_wall.radius
)

item = Entities.new_item(last_id, position, item_config)
Expand Down Expand Up @@ -1138,15 +1140,15 @@ defmodule Arena.GameUpdater do
distance_to_power_up = game_config.power_ups.power_up.distance_to_power_up

Enum.reduce(1..amount//1, game_state, fn _, game_state ->
random_x =
victim.position.x +
Enum.random(-distance_to_power_up..distance_to_power_up)

random_y =
victim.position.y +
Enum.random(-distance_to_power_up..distance_to_power_up)
random_position =
random_position_in_map(
game_config.power_ups.power_up.radius,
game_state.external_wall,
game_state.obstacles,
victim.position,
distance_to_power_up
)

random_position = %{x: random_x, y: random_y}
last_id = game_state.last_id + 1

power_up =
Expand Down Expand Up @@ -1248,10 +1250,10 @@ defmodule Arena.GameUpdater do
end
end

defp random_position_in_map(object_radius, external_wall, obstacles) do
integer_radius = trunc(external_wall.radius - object_radius)
x = Enum.random(-integer_radius..integer_radius) / 1.0
y = Enum.random(-integer_radius..integer_radius) / 1.0
defp random_position_in_map(object_radius, external_wall, obstacles, initial_position, available_radius) do
integer_radius = trunc(available_radius - object_radius)
x = Enum.random(-integer_radius..integer_radius) / 1.0 + initial_position.x
y = Enum.random(-integer_radius..integer_radius) / 1.0 + initial_position.y

circle = %{
id: 1,
Expand All @@ -1274,7 +1276,7 @@ defmodule Arena.GameUpdater do

case Physics.check_collisions(circle, entities_to_collide_with) do
[^external_wall_id | []] -> circle.position
_ -> random_position_in_map(object_radius, external_wall, obstacles)
_ -> random_position_in_map(object_radius, external_wall, obstacles, initial_position, available_radius)
end
end

Expand Down
Loading