diff --git a/apps/arena/lib/arena/configuration.ex b/apps/arena/lib/arena/configuration.ex index 79b23848b..b7c48a2b8 100644 --- a/apps/arena/lib/arena/configuration.ex +++ b/apps/arena/lib/arena/configuration.ex @@ -177,6 +177,7 @@ defmodule Arena.Configuration do map_config | radius: maybe_to_float(map_config.radius), initial_positions: Enum.map(map_config.initial_positions, &parse_position/1), + square_wall: map_config.square_wall, obstacles: Enum.map(map_config.obstacles, &parse_obstacle/1), pools: Enum.map(map_config.pools, &parse_entity_values/1), bushes: Enum.map(map_config.bushes, &parse_entity_values/1), diff --git a/apps/arena/lib/arena/game_updater.ex b/apps/arena/lib/arena/game_updater.ex index 5d02cafec..70437c752 100644 --- a/apps/arena/lib/arena/game_updater.ex +++ b/apps/arena/lib/arena/game_updater.ex @@ -590,12 +590,12 @@ defmodule Arena.GameUpdater do item_config = Enum.random(state.game_config.items) position = - random_position_in_map( + random_position_in_square( item_config.radius, state.game_state.external_wall, state.game_state.obstacles, state.game_state.external_wall.position, - state.game_state.external_wall.radius + state.game_state.square_wall ) item = Entities.new_item(last_id, position, item_config) @@ -792,6 +792,7 @@ defmodule Arena.GameUpdater do |> Map.put(:damage_done, %{}) |> Map.put(:crates, %{}) |> Map.put(:external_wall, Entities.new_external_wall(0, config.map.radius)) + |> Map.put(:square_wall, config.map.square_wall) |> Map.put(:zone, %{ radius: config.map.radius, should_start?: config.game.zone_enabled, @@ -1635,10 +1636,24 @@ defmodule Arena.GameUpdater do x = Enum.random(-integer_radius..integer_radius) / 1.0 + initial_position.x y = Enum.random(-integer_radius..integer_radius) / 1.0 + initial_position.y + set_spawn_point(%{x: x, y: y}, object_radius, external_wall, obstacles) + end + + defp random_position_in_square(object_radius, external_wall, obstacles, initial_position, square_wall) do + x = + Enum.random(trunc(square_wall.left)..trunc(square_wall.right)) / 1.0 + initial_position.x + + y = + Enum.random(trunc(square_wall.bottom)..trunc(square_wall.top)) / 1.0 + initial_position.y + + set_spawn_point(%{x: x, y: y}, object_radius, external_wall, obstacles) + end + + defp set_spawn_point(desired_position, object_radius, external_wall, obstacles) do circle = %{ id: 1, shape: :circle, - position: %{x: x, y: y}, + position: desired_position, radius: object_radius, vertices: [], speed: 0.0, diff --git a/apps/game_backend/lib/game_backend/curse_of_mirra/map_configuration.ex b/apps/game_backend/lib/game_backend/curse_of_mirra/map_configuration.ex index a13472b47..eaa8240d7 100644 --- a/apps/game_backend/lib/game_backend/curse_of_mirra/map_configuration.ex +++ b/apps/game_backend/lib/game_backend/curse_of_mirra/map_configuration.ex @@ -6,7 +6,8 @@ defmodule GameBackend.CurseOfMirra.MapConfiguration do import Ecto.Changeset alias GameBackend.Configuration.Version - @derive {Jason.Encoder, only: [:name, :radius, :initial_positions, :obstacles, :bushes, :pools, :crates, :active]} + @derive {Jason.Encoder, + only: [:name, :radius, :initial_positions, :obstacles, :bushes, :pools, :crates, :active, :square_wall]} schema "map_configurations" do field(:name, :string) @@ -18,6 +19,7 @@ defmodule GameBackend.CurseOfMirra.MapConfiguration do embeds_many(:pools, __MODULE__.Pool, on_replace: :delete) embeds_many(:bushes, __MODULE__.Bush, on_replace: :delete) embeds_many(:crates, __MODULE__.Crate, on_replace: :delete) + embeds_one(:square_wall, __MODULE__.SquareWall, on_replace: :delete) belongs_to(:version, Version) @@ -34,9 +36,32 @@ defmodule GameBackend.CurseOfMirra.MapConfiguration do |> cast_embed(:bushes) |> cast_embed(:pools) |> cast_embed(:crates) + |> cast_embed(:square_wall) |> unique_constraint(:name) end + defmodule SquareWall do + @moduledoc """ + SquareWall embedded schema to be used by MapConfiguration + """ + + use GameBackend.Schema + + @derive {Jason.Encoder, only: [:right, :left, :top, :bottom]} + embedded_schema do + field(:right, :integer) + field(:left, :integer) + field(:top, :integer) + field(:bottom, :integer) + end + + def changeset(square_wall, attrs) do + square_wall + |> cast(attrs, [:right, :left, :top, :bottom]) + |> validate_required([:right, :left, :top, :bottom]) + end + end + defmodule Position do @moduledoc """ Position embedded schema to be used by MapConfiguration diff --git a/apps/game_backend/priv/repo/migrations/20241106150352_add_square_wall_restriction.exs b/apps/game_backend/priv/repo/migrations/20241106150352_add_square_wall_restriction.exs new file mode 100644 index 000000000..4a972ccc8 --- /dev/null +++ b/apps/game_backend/priv/repo/migrations/20241106150352_add_square_wall_restriction.exs @@ -0,0 +1,9 @@ +defmodule GameBackend.Repo.Migrations.AddSquareWallRestriction do + use Ecto.Migration + + def change do + alter table(:map_configurations) do + add :square_wall, :map + end + end +end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 54f67cc95..395ff9951 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -3568,7 +3568,7 @@ araban_map_config = %{ merliot_map_config = %{ name: "Merliot", - radius: 10000.0, + radius: 15000.0, active: true, initial_positions: [ %{ @@ -3615,19 +3615,19 @@ merliot_map_config = %{ vertices: [ %{ x: 6400.0, - y: 6800.0 + y: 12000.0 }, %{ - x: 6800.0, - y: 6800.0 + x: 12000.0, + y: 12000.0 }, %{ - x: 6800.0, - y: -6800.0 + x: 12000.0, + y: -12000.0 }, %{ x: 6400.0, - y: -6800.0 + y: -12000.0 } ] }, @@ -3649,11 +3649,11 @@ merliot_map_config = %{ }, %{ x: 6400.0, - y: 6800.0 + y: 12000.0 }, %{ x: -6400.0, - y: 6800.0 + y: 12000.0 }, %{ x: -6400.0, @@ -3675,19 +3675,19 @@ merliot_map_config = %{ vertices: [ %{ x: -6400.0, - y: 6800.0 + y: 12000.0 }, %{ - x: -6800.0, - y: 6800.0 + x: -12000.0, + y: 12000.0 }, %{ - x: -6800.0, - y: -6800.0 + x: -12000.0, + y: -12000.0 }, %{ x: -6400.0, - y: -6800.0 + y: -12000.0 } ] }, @@ -3713,11 +3713,11 @@ merliot_map_config = %{ }, %{ x: 6400.0, - y: -6800.0 + y: -12000.0 }, %{ x: -6400.0, - y: -6800.0 + y: -12000.0 } ] }, @@ -5004,7 +5004,13 @@ merliot_map_config = %{ ], bushes: [], pools: [], - version_id: version.id + version_id: version.id, + square_wall: %{ + right: 6400, + left: -6400, + bottom: -6400, + top: 6400 + } } {:ok, _araban_map_configuration} =