This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NPCs will respawn after a spawn interval
The genserver for them will detect a death and set a timestamp for respawn. After this respawn time has passed it will give max health back to the NPC and enter the room.
- Loading branch information
Showing
14 changed files
with
137 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"priv/repo/migrations":{ "type": "migration", } | ||
"priv/repo/migrations": { "type": "migration" }, | ||
"lib/*.ex": { "alternate": "test/{}_test.exs" }, | ||
"test/*_test.exs": { "alternate": "lib/{}.ex" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Game.NPC.Actions do | ||
@moduledoc """ | ||
""" | ||
|
||
use Game.Room | ||
|
||
@doc """ | ||
Respawn the NPC as a tick happens | ||
""" | ||
@spec tick(state :: map, time :: DateTime.t) :: :ok | {:update, map} | ||
def tick(state = %{npc: %{stats: %{health: health}}}, time) when health < 1 do | ||
state = state |> handle_respawn(time) | ||
{:update, state} | ||
end | ||
def tick(_state, _time), do: :ok | ||
|
||
defp handle_respawn(state = %{respawn_at: respawn_at, npc: npc}, time) when respawn_at != nil do | ||
case Timex.after?(time, respawn_at) do | ||
true -> | ||
npc = %{npc | stats: %{npc.stats | health: npc.stats.max_health}} | ||
npc.room_id |> @room.enter({:npc, npc}) | ||
%{state | npc: npc, respawn_at: nil} | ||
false -> state | ||
end | ||
end | ||
defp handle_respawn(state, time) do | ||
Map.put(state, :respawn_at, time |> Timex.shift(seconds: state.npc.spawn_interval)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
priv/repo/migrations/20170823232426_add_npc_spawn_time.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Data.Repo.Migrations.AddNpcSpawnTime do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:npcs) do | ||
add :spawn_interval, :integer, default: 30, null: false | ||
end | ||
|
||
rename table(:room_items), :interval, to: :spawn_interval | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Game.NPC.ActionsTest do | ||
use ExUnit.Case | ||
|
||
alias Game.NPC.Actions | ||
|
||
@room Test.Game.Room | ||
|
||
describe "tick - respawning the npc" do | ||
setup do | ||
%{time: Timex.now(), npc: %{room_id: 1, stats: %{health: 10, max_health: 15}, spawn_interval: 10}} | ||
end | ||
|
||
test "does nothing if the npc is alive", %{time: time, npc: npc} do | ||
:ok = Actions.tick(%{npc: npc}, time) | ||
end | ||
|
||
test "detecting death", %{time: time, npc: npc} do | ||
{:update, state} = Actions.tick(%{npc: put_in(npc, [:stats, :health], 0)}, time) | ||
assert state.respawn_at == time |> Timex.shift(seconds: 10) | ||
end | ||
|
||
test "doesn't spawn until time", %{time: time, npc: npc} do | ||
respawn_at = time |> Timex.shift(seconds: 2) | ||
{:update, state} = Actions.tick(%{npc: put_in(npc, [:stats, :health], 0), respawn_at: respawn_at}, time) | ||
assert state.respawn_at == respawn_at | ||
assert state.npc.stats.health == 0 | ||
end | ||
|
||
test "respawns the npc", %{time: time, npc: npc} do | ||
respawn_at = time |> Timex.shift(seconds: -31) | ||
{:update, state} = Actions.tick(%{npc: put_in(npc, [:stats, :health], 0), respawn_at: respawn_at}, time) | ||
assert is_nil(state.respawn_at) | ||
assert state.npc.stats.health == 15 | ||
assert [{1, {:npc, _}}] = @room.get_enters() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters