sesopenko ECS v1.0.2 Sesopenko.ECS.Repository View Source

Provides data operations for entities and their components.

Example Usage:

# Instantiate repository:
{:ok, pid} = Sesopenko.ECS.Repository.start_link()

# Create an entity with given component data:
{:ok, entity_id} = Sesopenko.ECS.Repository.add_entity(pid, %{
  first_component_type: %{
    data: 1,
    other: 2,
  },
  second_component_type: %{
    something: "foo",
    other_think: [1, 2, 3]
  }
})

# Fetch the entity:
{:ok, entity_component_data} = Sesopenko.ECS.Repository.fetch_entity(pid, entity_id)

# Get a complete list of entity data for a given component type:
component_type = :second_component_type
{:ok, component_data_list} = Sesopenko.ECS.Repository.list_data_for_component_type(pid, component_type)

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Fetches an entity by entity id.

Provides a list of all data for a given component type.

Starts a Repository process link which can be used to query against.

Link to this section Types

Link to this type

component_data()

View Source
component_data() :: %{required(atom()) => any()}
Link to this type

component_data_set()

View Source
component_data_set() :: %{required(component_type()) => component_data()}
Link to this type

component_type()

View Source
component_type() :: atom()
Link to this type

entity_id()

View Source
entity_id() :: uuid()

Link to this section Functions

Link to this function

add_entity(pid, component_map)

View Source
add_entity(pid(), component_data_set()) :: type :: {:ok, entity_id()}

Adds an entity.

The input component_data_set must be a map, keyed by component type atoms. The component data for each key can be any native elixir type.

Example usage:

{:ok, entity_id} = Sesopenko.ECS.Repository.add_entity(pid, %{
  first_component_type: %{
    data: 1,
    other: 2,
  },
  second_component_type: %{
    something: "foo",
    other_think: [1, 2, 3]
  }
})

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

fetch_entity(pid, entity_id)

View Source
fetch_entity(pid(), entity_id()) :: {:ok, component_data_set()}

Fetches an entity by entity id.

Example usage:

entity_id = "7533af4e-5531-11ea-9263-000c292c6160"
{:ok, entity_component_data} = Sesopenko.ECS.Repository.fetch_entity(pid, entity_id)

The return is a map() keyed by component type, with each value being a map() of component data.

Link to this function

list_data_for_component_type(pid, component_type)

View Source
list_data_for_component_type(pid(), component_type()) ::
  {:ok, [component_data()]}

Provides a list of all data for a given component type.

Example usage:

component_type = :bears_fruit
{:ok, component_data_list} = Sesopenko.ECS.Repository.list_data_for_component_type(pid, component_type)
Link to this function

start_link()

View Source
start_link() :: {:ok, pid()}

Starts a Repository process link which can be used to query against.

Usage:

pid = Sesopenko.ECS.Repository.start_link()