Packages

A distributed ECS (Entity-Component-System) framework in Elixir

Current section

Files

Jump to
distributed_ecs lib distributed_ecs component.ex
Raw

lib/distributed_ecs/component.ex

defmodule DistributedEcs.Component do
alias DistributedEcs.Component
defstruct [:id, :state, :type]
@type params :: map()
@type id :: String.t()
@type component_type :: atom()
@type state :: map()
@type t :: %Component{
type: component_type,
state: state,
id: id
}
defmacro __using__(_options) do
quote location: :keep do
Module.register_attribute(__MODULE__, :default_value, [])
import unquote(__MODULE__)
@before_compile unquote(__MODULE__)
end
end
defmacro __before_compile__(env) do
quote location: :keep do
@default_value @default_value || %{}
def new(initial_state \\ %{}) do
Component.new(
__MODULE__,
Map.merge(@default_value, initial_state)
)
end
end
end
@doc "New component"
@spec new(component_type, state) :: t
def new(component_type, initial_state) do
id = DistributedEcs.ID.new()
struct(
__MODULE__,
%{id: id, type: component_type, state: initial_state}
)
end
# TODO implement a "change" API to fix abstraction leak in systems
end