I am trying to paint a room with walls into a map of %{{x, y} => [items]}
and I am using a case statement to figure out what type of wall I want to draw.
However it sems to try to do pattern matching and assign pos
to the values on left. (throwing illegal pattern
compilation error)
I know I can prevent assignment with ^
, but what about the cases where I need to do (from_y+sizey)
?
def place_room({from_x, from_y}, {size_x, size_y}, original_map) do
Enum.reduce from_x..(from_x + size_x-1), original_map, fn x, map ->
Enum.reduce from_y..(from_y + size_y-1), map, fn y, map ->
pos = {x, y}
case pos do
{from_x, from_y} ->
place(map, pos, :wall, %{type: :room_wall_tl})
{from_x, (from_y+size_y)} ->
place(map, pos, :wall, %{type: :room_wall_tr})
{from_x, (from_y+size_y)} ->
place(map, pos, :wall, %{type: :room_wall_bl})
{(from_x+size_x), (from_y+size_y)} ->
place(map, pos, :wall, %{type: :room_wall_br})
{_, _} ->
place(map, pos, :floor, %{type: :room_floor})
weird ->
IO.inspect weird
end
end
end
end
How would you write this?