Created
September 22, 2014 10:03
-
-
Save halfdan/65552741fff9103a9c00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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