Genesis.World (genesis v0.10.0)

Copy Markdown View Source

World is a GenServer that manages the lifecycle of entities at runtime. A world is responsible for creating, cloning, and destroying entities from its own context. It also manages the event routing logic for the components an entity has, ensuring that events are dispatched and handled correctly.

Summary

Functions

Same as Genesis.Context.all/2 but scoped to the world.

Returns a specification to start this module under a supervisor.

Clones an entity with all its components. See Genesis.Manager.clone/2 for more details.

Returns the underlying world context.

Executes a function with the world context synchronously.

Creates a new entity in the world.

Creates a new entity from a prefab. The prefab must be registered before it can be used. Optionally, you can provide overrides to modify component properties.

Same as create/1 but raises on error. Returns the created entity on success.

Same as create/3 but raises on error. Returns the created entity on success.

Destroys an entity from the world. Returns :ok if the entity was successfully destroyed, or :noop if the entity doesn't exist. See Genesis.Context.destroy/2 for more details.

Same as Genesis.Context.exists?/2 but scoped to the world.

Same as fetch/2 but scoped to the world.

List all entities in the world with their respective components.

Sends an event to an entity.

Starts the world process. Same as GenServer.start_link/3.

Functions

all(world, component_type)

Same as Genesis.Context.all/2 but scoped to the world.

at_least(world, component_type, property, value)

Same as Genesis.Context.at_least/4 but scoped to the world.

at_most(world, component_type, property, value)

Same as Genesis.Context.at_most/4 but scoped to the world.

between(world, component_type, property, min, max)

Same as Genesis.Context.between/5 but scoped to the world.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clone(world, entity, opts \\ [])

Clones an entity with all its components. See Genesis.Manager.clone/2 for more details.

context(world)

Returns the underlying world context.

Since both writes and reads in a world are always synchronous, this function creates an escape hatch so users can execute operations using the underlying context. All read operations executed directly in the context should be considered dirty by default.

Example

context = World.context(world)
Genesis.Context.search(context, ...)

context(world, fun)

Executes a function with the world context synchronously.

Differently from context/1, this allows for safe context operations since it's executed synchronously within the world server itself. The function must return a value that will be replied back to the caller.

Example

World.context(world, fn ctx ->
  Genesis.Context.emplace(ctx, entity, component)
end)

create(world)

Creates a new entity in the world.

Examples

Genesis.World.create(world)
#=> {:ok, entity}

create(world, name_or_prefab, overrides \\ %{})

Creates a new entity from a prefab. The prefab must be registered before it can be used. Optionally, you can provide overrides to modify component properties.

Examples

# Using a prefab name
Genesis.World.create(world, "Player")
#=> {:ok, entity}

# Or using a prefab entity directly
Genesis.World.create(world, prefab)
#=> {:ok, entity}

Genesis.World.create(world, "Player", %{"health" => %{current: 50}})
#=> {:ok, entity}

create!(world)

Same as create/1 but raises on error. Returns the created entity on success.

create!(world, name_or_prefab, overrides \\ %{})

Same as create/3 but raises on error. Returns the created entity on success.

destroy(world, entity)

Destroys an entity from the world. Returns :ok if the entity was successfully destroyed, or :noop if the entity doesn't exist. See Genesis.Context.destroy/2 for more details.

exists?(world, entity)

Same as Genesis.Context.exists?/2 but scoped to the world.

fetch(world, entity)

Same as fetch/2 but scoped to the world.

list(world, opts \\ [])

List all entities in the world with their respective components.

Options

  • :format_as - Specifies how to represent the components of each entity. Can be :list (default) to return a list of component structs, or :map to return a map where keys are component aliases and values are component structs.

Examples

Genesis.World.list(world, format_as: :list)
#=> [{entity, [%Health{current: 100}]}]

Genesis.World.list(world, format_as: :map)
#=> [{entity, %{"health" => %Health{current: 100}}}]

match(world, component_type, properties)

Same as Genesis.Context.match/3 but scoped to the world.

send(world, entity, event, args \\ %{})

Sends an event to an entity.

Examples

Genesis.World.send(world, entity, :move, %{direction: :north})
#=> :ok

Genesis.World.send(world, entity, :damage, %{amount: 10})
#=> :ok

start_link(options \\ [])

Starts the world process. Same as GenServer.start_link/3.