Skip to content

Instantly share code, notes, and snippets.

@halfdan
Created September 22, 2014 10:03
Show Gist options
  • Save halfdan/65552741fff9103a9c00 to your computer and use it in GitHub Desktop.
Save halfdan/65552741fff9103a9c00 to your computer and use it in GitHub Desktop.
defmodule HealthComponent do
use GenEvent
### Public API
def get_hp(entity) do
:gen_event.call(entity, HealthComponent, :get_hp)
end
def alive?(entity) do
:gen_event.call(entity, HealthComponent, :alive?)
end
### GenEvent API
def init(hp) do
{:ok, hp}
end
def handle_call(:get_hp, hp) do
{:ok, hp, hp}
end
def handle_call(:alive?, hp) do
{:ok, hp > 0, hp}
end
def handle_event({:hit, amount}, hp) do
{:ok, hp - amount}
end
def handle_event({:heal, amount}, hp) do
{:ok, hp + amount}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment