Skip to content

Commit 7af1f52

Browse files
committed
Extract generation code from TileMap
1 parent 9e5f574 commit 7af1f52

File tree

5 files changed

+57
-55
lines changed

5 files changed

+57
-55
lines changed

lib/move_your_cedric/application.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule MoveYourCedric.Application do
1515
{Phoenix.PubSub, name: MoveYourCedric.PubSub},
1616
# Start the Endpoint (http/https)
1717
MoveYourCedricWeb.Endpoint,
18-
MoveYourCedric.Models.Pathfinder
18+
MoveYourCedric.Workers.Pathfinder
1919
]
2020

2121
# See https://hexdocs.pm/elixir/Supervisor.html

lib/move_your_cedric/models/tile_map.ex

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,4 @@ defmodule MoveYourCedric.Models.TileMap do
44
tiles: [],
55
entities: []
66
]
7-
8-
alias MoveYourCedric.Models.Tile
9-
alias MoveYourCedric.Models.Entity
10-
11-
defmodule Randomizer do
12-
defmodule Fake do
13-
def tile_type_from_coords(x, y) do
14-
if x == 4 and y != 3 and y != 4 do
15-
:wall
16-
else
17-
:clear
18-
end
19-
end
20-
end
21-
end
22-
23-
def build(width \\ 8, height \\ 5) do
24-
%__MODULE__{
25-
size: [width, height],
26-
tiles: build_tiles(width, height),
27-
entities: build_entities(width, height)
28-
}
29-
end
30-
31-
defp build_tiles(width, height) do
32-
Enum.map(1..height, fn y ->
33-
Enum.map(1..width, fn x ->
34-
%Tile{
35-
position: [x - 1, y - 1],
36-
type: Randomizer.Fake.tile_type_from_coords(x - 1, y - 1)
37-
}
38-
end)
39-
end)
40-
end
41-
42-
defp build_entities(_width, _height) do
43-
[
44-
scaffold_player()
45-
]
46-
end
47-
48-
defp scaffold_player() do
49-
%Entity{
50-
type: "player",
51-
name: "Cédric",
52-
position: [1, 2],
53-
status: MoveYourCedric.Models.Pathfinder.get_status(),
54-
target: MoveYourCedric.Models.Pathfinder.get_target()
55-
}
56-
end
577
end

lib/move_your_cedric/models/pathfinder.ex renamed to lib/move_your_cedric/workers/pathfinder.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule MoveYourCedric.Models.Pathfinder do
1+
defmodule MoveYourCedric.Workers.Pathfinder do
22
use GenServer
33

44
require Logger
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
defmodule MoveYourCedric.Workers.SmallMapGenerator do
2+
alias MoveYourCedric.Models.Entity
3+
alias MoveYourCedric.Models.Tile
4+
alias MoveYourCedric.Models.TileMap
5+
6+
defmodule Randomizer do
7+
defmodule Fake do
8+
def tile_type_from_coords(x, y) do
9+
if x == 4 and y != 3 and y != 4 do
10+
:wall
11+
else
12+
:clear
13+
end
14+
end
15+
end
16+
end
17+
18+
def build(width \\ 8, height \\ 5) do
19+
%TileMap{
20+
size: [width, height],
21+
tiles: build_tiles(width, height),
22+
entities: build_entities(width, height)
23+
}
24+
end
25+
26+
defp build_tiles(width, height) do
27+
Enum.map(1..height, fn y ->
28+
Enum.map(1..width, fn x ->
29+
%Tile{
30+
position: [x - 1, y - 1],
31+
type: Randomizer.Fake.tile_type_from_coords(x - 1, y - 1)
32+
}
33+
end)
34+
end)
35+
end
36+
37+
defp build_entities(_width, _height) do
38+
[
39+
scaffold_player()
40+
]
41+
end
42+
43+
defp scaffold_player() do
44+
%Entity{
45+
type: "player",
46+
name: "Cédric",
47+
position: [1, 2],
48+
status: MoveYourCedric.Workers.Pathfinder.get_status(),
49+
target: MoveYourCedric.Workers.Pathfinder.get_target()
50+
}
51+
end
52+
end

lib/move_your_cedric_web/live/map_live.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ defmodule MoveYourCedricWeb.MapLive do
33

44
require Logger
55

6-
alias MoveYourCedric.Models.TileMap
7-
alias MoveYourCedric.Models.Pathfinder
6+
alias MoveYourCedric.Workers.Pathfinder
7+
alias MoveYourCedric.Workers.SmallMapGenerator
88

99
def mount(_params, _session, socket) do
10-
tile_map = TileMap.build()
10+
tile_map = SmallMapGenerator.build()
1111

1212
player = tile_map.entities |> Enum.find(fn entity -> entity.type == "player" end)
1313
Pathfinder.set_position(player.position)

0 commit comments

Comments
 (0)