Skip to content

Commit

Permalink
Update clawing of bears when a player claws
Browse files Browse the repository at this point in the history
Whenever a player claws, a timer of 1000ms is set and every 50ms it will
decrease with 50ms until it becomes 0. During this time the bear's
'claw' will be true and it will be nil when it reaches 0. This is needed
to temporarily set a 'clawing' class on bears in the game.
  • Loading branch information
EasterPeanut committed Apr 23, 2019
1 parent 8583a5e commit 3d1ed8d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
10 changes: 7 additions & 3 deletions apps/bear_necessities_web/lib/bear_necessities_web/live/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ defmodule BearNecessitiesWeb.Game do

def handle_event("key_up", key, %{id: id} = socket)
when key in @arrow_keys do
Bear.stop(id)
Bear.stop(id)
{:noreply, socket}
end

def handle_event("key_up", " ", %{id: id} = socket) do
Player.claw(id)
socket =
socket
|> assign(:bear, Player.claw(id))
|> assign(:viewport, ViewPort.get_viewport(id))

{:noreply, socket}
end

Expand Down Expand Up @@ -162,7 +166,7 @@ defmodule BearNecessitiesWeb.Game do
def set_updates(false), do: nil

def set_updates(true) do
{:ok, {:interval, ref}} = :timer.send_interval(50, self(), :update)
{:ok, ref} = :timer.send_interval(50, self(), :update)
ref
end

Expand Down
3 changes: 2 additions & 1 deletion apps/game/lib/bear.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule Bear do
display_name: nil,
started: false,
direction: :down,
moving: false
moving: false,
clawing: false

def create_bear(%{height: height, width: width}, id, display_name, started) do
pos_x = Enum.random(0..height)
Expand Down
10 changes: 6 additions & 4 deletions apps/game/lib/game.ex → apps/game/lib/game_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ defmodule Game do
@impl true
def handle_call({:stop, id}, _pid, %{bears: bears} = state) do
bear = get_bear_from_list(id, bears)
bear = %{bear | moving: false}
bear = %{bear | moving: false, clawing: false}
state = update_state_with(state, bear)

{:reply, bear, state}
Expand All @@ -124,6 +124,8 @@ defmodule Game do
_pid,
state
) do
bear = %{bear | clawing: true}

{bear, state} =
case target(direction, x, y, state) do
%Tree{honey: tree_honey} = tree when tree_honey > 0 ->
Expand Down Expand Up @@ -153,13 +155,13 @@ defmodule Game do
{new_bear, new_state}

%Tree{honey: 0} ->
{bear, state}
{bear, update_state_with(state, bear)}

%Bear{honey: 0} ->
{bear, state}
{bear, update_state_with(state, bear)}

_ ->
{bear, state}
{bear, update_state_with(state, bear)}
end

{:reply, bear, state}
Expand Down
50 changes: 41 additions & 9 deletions apps/game/lib/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ defmodule Player do
use GenServer
require Logger

defstruct [:display_name, :score, :bear]
@claw_time_ms 1000

@enforce_keys [:id]
defstruct [:id, :claw, :timer_pid]

def start_link(default) when is_list(default) do
GenServer.start_link(__MODULE__, default, name: __MODULE__)
end

@impl true
def init([]) do
{:ok, []}
def init(id: id) do
{:ok, %Player{id: id}}
end

@impl true
def handle_call({:action, user_input, id}, _pid, _) do
def handle_call({:action, user_input, id}, _pid, state) do
bear =
case user_input do
:up_arrow ->
Expand All @@ -30,14 +33,43 @@ defmodule Player do
Bear.move(id, :right)
end

{:reply, bear, []}
{:reply, bear, state}
end

@imp true
def handle_call({:claw, id}, _pid, _) do
@impl true
def handle_call({:claw, id}, _pid, state) do
bear = Bear.claw(id)
{:ok, timer_pid} = :timer.send_interval(50, self(), :update_claw)
state = %{state | claw: @claw_time_ms, timer_pid: timer_pid}

{:reply, bear, state}
end

@impl true
def handle_info(:update_claw, %{claw: nil} = state) do
{:noreply, state}
end

@impl true
def handle_info(:update_claw, %{claw: claw_time} = state) when claw_time > 0 do
state = %{state | claw: claw_time - 50}

{:noreply, state}
end

@impl true
def handle_info(:update_claw, %{id: id, claw: claw_time, timer_pid: timer_pid} = state)
when claw_time < 1 do
:timer.cancel(timer_pid)
state = %{state | claw: nil, timer_pid: nil}
Bear.stop(id)

{:noreply, state}
end

{:reply, bear, []}
@impl true
def handle_info(:update_claw, state) do
{:noreply, state}
end

def move(player_id, way) do
Expand All @@ -49,7 +81,7 @@ defmodule Player do
end

def start(display_name, id) do
Player.start_link([])
Player.start_link(id: id)
Game.create_bear(display_name: display_name, id: id, started: true)
end
end

0 comments on commit 3d1ed8d

Please sign in to comment.