Skip to content

Latest commit

 

History

History
91 lines (64 loc) · 2.37 KB

creature_spawner.livemd

File metadata and controls

91 lines (64 loc) · 2.37 KB

Creature Spawner

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Creature Spawner

Many video games contain Spawners which spawn enemy creatures.

Typically, the spawner has a limit for how many enemy creatures it spawns. If a creature dies, the spawner then re-spawns that creature.

Sound familiar? We can leverage supervisors to create this effect.

Create three Creature processes. A creature process should store it's health in state, and you should be able to send a message to damage the creature like so.

{:ok, creature} = Creature1.start_link([])
%{health: 100} = :sys.get_state(creature)
Creature1.damage(creature, 10)
%{health: 90} = :sys.get_state(creature)

When the creature's health reaches 0, it should die (crash). When it crashes, the supervisor should restart the creature process.

flowchart
  S[Spawner]
  C1[Creature1]
  C2[Creature2]
  C3[Creature3]

  S --> C1
  S --> C2
  S --> C3
Loading

Enter your solution below.

defmodule Creature1 do
end

defmodule Creature2 do
end

defmodule Creature3 do
end

# start the creatures under a supervisor below:

Bonus: Hero

You may add a Hero process supervised by the supervisor above. The Hero process should automatically damage the three Creature processes on an interval (i.e. 5 seconds)

Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish creature spawner exercise"

Up Next

Previous Next
Dominoes Typing Game