Skip to content

Latest commit

 

History

History
65 lines (57 loc) · 2.28 KB

design.org

File metadata and controls

65 lines (57 loc) · 2.28 KB

Design

defmodule Dwarf do
  use Ecstatic.Entity
  @default_components [Age, Health.new(%{health: 20})]
end

defmodule Health do
  use Ecstatic.Component
  @default_state %{health: 10}
end

defmodule Poison do
  use Ecstatic.Component
  @default_state %{health_loss: 2}
end

defmodule Age do
  use Ecstatic.Component
  @default_state %{age: 1}
end

defmodule AgingSpell do
  use Ecstatic.Component
  @default_state %{modifier: 1.2}
end

defmodule Poisoned do
  use Ecstatic.Aspect
  entity_with [Health, Poison]
  entity_without [ImmuneToPoison]
end

defmodule PoisonSystem do
  use Ecstatic.System
  aspect Poisoned
  def dispatch(entity) do
    health = Entity.find_component(entity, Health)
    %Ecstatic.Changeset{changed: [%{health | points: health.points - 1}]}
  end
end

defmodule Watchers do
  use Ecstatic.Watcher
  watch Poison do
    run PoisonSystem, every: :timer.seconds(6)
  end
  watch Health do
    run PanicSystem, when: fn(pre, post) -> (pre.points - post.points) > 30 end
    run BleedingSystem, when: fn(_pre, post) -> post.points == 0 end
    run DeathSystem, when: fn(_pre, post) -> post.points == -10 end
  end
end

In words

A valid change to an entity is pushed to the queue (by a producer). All entities (consumers) that care about this change receive the change and the entity to which the change applies. Entities process their changes through the relevant systems, then push out whatever changes may have been generated as a new event to the queue.

So the player types “west”: this is gathered by the input processor and becomes a produced event (changed: location component). It gets consumed by the entity, which calls the system for location component being changed, which actually applies the change. No new change is produced. The player needs to know this change got applied. So.. The system applying the change also notifies observers of the entity? In this case, “you move west”? If Ι want to “arrive from the east” as well, then the system may send multiple messages - that does seem like it belongs to the system. So.. I think some systems may want to know what the change was… And we need to summon the system before the change is applied so we have both pre and post states