Skip to content

Commit

Permalink
added a little liveview debugger, bricks may go down now (past gutter)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanyogan committed Feb 6, 2020
1 parent efc8be9 commit 1c251ff
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions lib/tetris_ui_web/live/tetris_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ defmodule TetrisUiWeb.TetrisLive do
use Phoenix.HTML, only: [raw: 1]

alias Tetris.Brick
alias Tetris

@debug true
@box_width 20
@box_height 20

Expand All @@ -18,13 +20,15 @@ defmodule TetrisUiWeb.TetrisLive do
<%= raw boxes(@tetromino) %>
<%= raw svg_foot() %>
</div>
<%= debug(assigns) %>
"""
end

defp new_game(socket) do
assign(socket,
state: :playing,
score: 0
score: 0,
gutter: %{}
)
|> new_block()
|> show_tetromino()
Expand All @@ -33,7 +37,7 @@ defmodule TetrisUiWeb.TetrisLive do
def new_block(socket) do
brick =
Tetris.Brick.new_random()
|> Map.put(:location, {3, 1})
|> Map.put(:location, {3, -2})

assign(socket, brick: brick)
end
Expand Down Expand Up @@ -115,24 +119,39 @@ defmodule TetrisUiWeb.TetrisLive do
defp color(%{name: :o}), do: :orange
defp color(%{name: :z}), do: :grey

defp to_pixels({x, y}), do: {x * @box_width, y * @box_height}
defp to_pixels({x, y}), do: {(x - 1) * @box_width, (y - 1) * @box_height}

def move(direction, socket) do
socket
|> do_move(direction)
|> show_tetromino
end

def do_move(socket, :left) do
assign(socket, brick: socket.assigns.brick |> Brick.left())
def drop(socket) do
socket
|> assign(brick: socket.assigns.brick |> Tetris.Brick.down())
|> show_tetromino
end

def do_move(
%{assigns: %{brick: brick, gutter: gutter}} = socket,
:left
) do
assign(socket, brick: brick |> Tetris.try_left(gutter))
end

def do_move(socket, :right) do
assign(socket, brick: socket.assigns.brick |> Brick.right())
def do_move(
%{assigns: %{brick: brick, gutter: gutter}} = socket,
:right
) do
assign(socket, brick: brick |> Tetris.try_right(gutter))
end

def do_move(socket, :turn) do
assign(socket, brick: socket.assigns.brick |> Tetris.Brick.spin_90())
def do_move(
%{assigns: %{brick: brick, gutter: gutter}} = socket,
:turn
) do
assign(socket, brick: brick |> Tetris.try_spin_90(gutter))
end

def handle_event("keydown", %{"key" => "ArrowLeft"}, socket) do
Expand All @@ -147,6 +166,22 @@ defmodule TetrisUiWeb.TetrisLive do
{:noreply, move(:turn, socket)}
end

def handle_event("keydown", %{"key" => "ArrowDown"}, socket) do
{:noreply, drop(socket)}
end

def handle_event("keydown", _params, socket),
do: {:noreply, socket}

def debug(assigns), do: debug(assigns, @debug, Mix.env())

def debug(assigns, true, :dev) do
~L"""
<pre>
<%= raw @tetromino |> inspect %>
</pre>
"""
end

def debug(_assigns, _, _), do: ""
end

0 comments on commit 1c251ff

Please sign in to comment.