abstract class Entitas::Context(TEntity)

Overview

A context manages the lifecycle of entities and groups. You can create and destroy entities and get groups of entities. The prefered way to create a context is to use the generated methods from the code generator, e.g. var context = new GameContext();

Included Modules

Defined in:

entitas.cr
entitas/context.cr
entitas/context/collector.cr
entitas/context/entity_index.cr
entitas/context/groups.cr
entitas/context/info.cr
entitas/generators/context.cr
entitas/generators/context/event_system.cr

Constant Summary

Log = ::Log.for(self)

Constructors

Instance Method Summary

Instance methods inherited from module Entitas::Helper::ComponentPools

clear_component_pool(index : Int32)
clear_component_pool(index : Entitas::Component::ComponentTypes)
clear_component_pool
, clear_component_pools clear_component_pools, component_index_value(index) : Int32 component_index_value, component_pool(index : Int32) : ComponentPool
component_pool(index : Entitas::Component::ComponentTypes) : ComponentPool
component_pool(index : Entitas::Component::Index) : ComponentPool
component_pool
, component_pools : Array(Entitas::ComponentPool) component_pools

Instance methods inherited from module Entitas::Helper::Entities(TEntity)

count : Int32 count, each(&block : TEntity -> Nil) each, get_entities get_entities, has_entity?(entity : TEntity) : Bool has_entity?, size size, sort(&block : TEntity, TEntity -> U) : Array(TEntity) forall U sort

Instance methods inherited from module Entitas::IContext

add_entity_index(entity_index : Entitas::IEntityIndex) add_entity_index, clear_component_pool(index : Int32) clear_component_pool, clear_component_pools clear_component_pools, component_index(index) : Entitas::Component::Index component_index, component_index_class(index) : Entitas::Component::ComponentTypes component_index_class, component_index_value(index) : Int32 component_index_value, component_pools : Array(Entitas::ComponentPool) component_pools, create_entity : IEntity create_entity, destroy_all_entities destroy_all_entities, each(&block : TEntity -> Nil) each, entity_factory : Entitas::Entity entity_factory, get_entities get_entities, get_entity_index(name : String) : Entitas::IEntityIndex get_entity_index, get_entity_index?(name : String) : Entitas::IEntityIndex | Nil get_entity_index?, get_group(matcher : Entitas::Matcher) : Entitas::Group get_group, has_entity?(entity : TEntity) : Bool has_entity?, on_entity_created(&block : Entitas::Events::OnEntityCreated -> Nil) on_entity_created, on_entity_created_event_hooks : Array(Proc(Entitas::Events::OnEntityCreated, Nil)) on_entity_created_event_hooks, on_entity_destroyed(&block : Entitas::Events::OnEntityDestroyed -> Nil) on_entity_destroyed, on_entity_destroyed_event_hooks : Array(Proc(Entitas::Events::OnEntityDestroyed, Nil)) on_entity_destroyed_event_hooks, on_entity_will_be_destroyed(&block : Entitas::Events::OnEntityWillBeDestroyed -> Nil) on_entity_will_be_destroyed, on_entity_will_be_destroyed_event_hooks : Array(Proc(Entitas::Events::OnEntityWillBeDestroyed, Nil)) on_entity_will_be_destroyed_event_hooks, on_group_created(&block : Entitas::Events::OnGroupCreated -> Nil) on_group_created, on_group_created_event_hooks : Array(Proc(Entitas::Events::OnGroupCreated, Nil)) on_group_created_event_hooks, receive_on_entity_created_event(event : Entitas::Events::OnEntityCreated) receive_on_entity_created_event, receive_on_entity_destroyed_event(event : Entitas::Events::OnEntityDestroyed) receive_on_entity_destroyed_event, receive_on_entity_will_be_destroyed_event(event : Entitas::Events::OnEntityWillBeDestroyed) receive_on_entity_will_be_destroyed_event, receive_on_group_created_event(event : Entitas::Events::OnGroupCreated) receive_on_group_created_event, remove_all_event_handlers remove_all_event_handlers, remove_on_entity_created_hook(hook : Proc(Entitas::Events::OnEntityCreated, Nil)) remove_on_entity_created_hook, remove_on_entity_destroyed_hook(hook : Proc(Entitas::Events::OnEntityDestroyed, Nil)) remove_on_entity_destroyed_hook, remove_on_entity_will_be_destroyed_hook(hook : Proc(Entitas::Events::OnEntityWillBeDestroyed, Nil)) remove_on_entity_will_be_destroyed_hook, remove_on_group_created_hook(hook : Proc(Entitas::Events::OnGroupCreated, Nil)) remove_on_group_created_hook, reset reset, reset_creation_index reset_creation_index, size size, to_json(json : JSON::Builder) to_json, total_components : Int32 total_components

Constructor Detail

def self.new(creation_index : Int32 = 0, context_info : Entitas::Context::Info | Nil = nil) #

[View source]

Instance Method Detail

def add_entity_index(entity_index : Entitas::IEntityIndex) #

[View source]
def aerc_factory(entity : TEntity) : Entitas::SafeAERC #

[View source]
def context_info : Entitas::Context::Info #

[View source]
def create_collector(matcher : Entitas::Matcher) : Entitas::Collector(TEntity) #

[View source]
def create_collector(*triggers : Entitas::Events::TriggerOn) : Entitas::Collector(TEntity) #

[View source]
abstract def create_default_context_info : Entitas::Context::Info #

[View source]
def destroy_all_entities #

Destroys all entities in the context. Throws an exception if there are still retained entities.


[View source]
def entity_indices : Hash(String, Entitas::IEntityIndex) #

[View source]
def get_entity_index(name : String) : Entitas::IEntityIndex #

[View source]
def get_entity_index?(name : String) : Entitas::IEntityIndex | Nil #

[View source]
def get_group(matcher : Entitas::Matcher) : Group(TEntity) #

Returns a group for the specified matcher. Calling context.GetGroup(matcher) with the same matcher will always return the same instance of the group.


[View source]
def get_on_component_added_cache : Proc(Entitas::Events::OnComponentAdded, Nil) #

def get_on_component_removed_cache : Proc(Entitas::Events::OnComponentRemoved, Nil) #

def get_on_component_replaced_cache : Proc(Entitas::Events::OnComponentReplaced, Nil) #

def get_on_destroy_entity_cache : Proc(Entitas::Events::OnDestroyEntity, Nil) #

def get_on_entity_created_cache : Proc(Entitas::Events::OnEntityCreated, Nil) #

def get_on_entity_destroyed_cache : Proc(Entitas::Events::OnEntityDestroyed, Nil) #

def get_on_entity_released_cache : Proc(Entitas::Events::OnEntityReleased, Nil) #

def get_on_entity_will_be_destroyed_cache : Proc(Entitas::Events::OnEntityWillBeDestroyed, Nil) #

def get_on_group_created_cache : Proc(Entitas::Events::OnGroupCreated, Nil) #

def groups : Hash(String, Group(TEntity)) #

[View source]

The contextInfo contains information about the context. It's used to provide better error messages.


[View source]
def on_component_added(event : Entitas::Events::OnComponentAdded) #

[View source]
def on_component_added_event_cache : Proc(Entitas::Events::OnComponentAdded, Nil) | Nil #

def on_component_added_event_cache=(on_component_added_event_cache : Proc(Entitas::Events::OnComponentAdded, Nil) | Nil) #

def on_component_removed(event : Entitas::Events::OnComponentRemoved) #

[View source]
def on_component_removed_event_cache : Proc(Entitas::Events::OnComponentRemoved, Nil) | Nil #

def on_component_removed_event_cache=(on_component_removed_event_cache : Proc(Entitas::Events::OnComponentRemoved, Nil) | Nil) #

def on_component_replaced(event : Entitas::Events::OnComponentReplaced) #

[View source]
def on_component_replaced_event_cache : Proc(Entitas::Events::OnComponentReplaced, Nil) | Nil #

def on_component_replaced_event_cache=(on_component_replaced_event_cache : Proc(Entitas::Events::OnComponentReplaced, Nil) | Nil) #

def on_destroy_entity(event : Entitas::Events::OnDestroyEntity) #

Will destroy the entity provided in the Entitas::Events::OnDestroyEntity event. It will delete it from the entities set, emit Entitas::Events::OnEntityWillBeDestroyed before destroying it, as well as emit Entitas::Events::OnEntityDestroyed after distruction.


[View source]
def on_destroy_entity_event_cache : Proc(Entitas::Events::OnDestroyEntity, Nil) | Nil #

def on_destroy_entity_event_cache=(on_destroy_entity_event_cache : Proc(Entitas::Events::OnDestroyEntity, Nil) | Nil) #

def on_entity_created(event : Entitas::Events::OnEntityCreated) : Nil #

Method to process event: Entitas::Events::OnEntityCreated when emited. Will raise Entitas::Error::MethodNotImplemented when not implimented if an Entitas::Events::OnEntityCreated is emitted.

def on_entity_created(event : Entitas::Events::OnEntityCreated) : Nil
  # do something with event
end

def on_entity_created_event_cache : Proc(Entitas::Events::OnEntityCreated, Nil) | Nil #

def on_entity_created_event_cache=(on_entity_created_event_cache : Proc(Entitas::Events::OnEntityCreated, Nil) | Nil) #

def on_entity_destroyed(event : Entitas::Events::OnEntityDestroyed) : Nil #

Method to process event: Entitas::Events::OnEntityDestroyed when emited. Will raise Entitas::Error::MethodNotImplemented when not implimented if an Entitas::Events::OnEntityDestroyed is emitted.

def on_entity_destroyed(event : Entitas::Events::OnEntityDestroyed) : Nil
  # do something with event
end

def on_entity_destroyed_event_cache : Proc(Entitas::Events::OnEntityDestroyed, Nil) | Nil #

def on_entity_destroyed_event_cache=(on_entity_destroyed_event_cache : Proc(Entitas::Events::OnEntityDestroyed, Nil) | Nil) #

def on_entity_released(event : Entitas::Events::OnEntityReleased) #

Will clean the entity provided in the Entitas::Events::OnEntityReleased event. It will remove all release handlers and append it to the reusable_entities cache


[View source]
def on_entity_released_event_cache : Proc(Entitas::Events::OnEntityReleased, Nil) | Nil #

def on_entity_released_event_cache=(on_entity_released_event_cache : Proc(Entitas::Events::OnEntityReleased, Nil) | Nil) #

def on_entity_will_be_destroyed(event : Entitas::Events::OnEntityWillBeDestroyed) : Nil #

Method to process event: Entitas::Events::OnEntityWillBeDestroyed when emited. Will raise Entitas::Error::MethodNotImplemented when not implimented if an Entitas::Events::OnEntityWillBeDestroyed is emitted.

def on_entity_will_be_destroyed(event : Entitas::Events::OnEntityWillBeDestroyed) : Nil
  # do something with event
end

def on_entity_will_be_destroyed_event_cache : Proc(Entitas::Events::OnEntityWillBeDestroyed, Nil) | Nil #

def on_entity_will_be_destroyed_event_cache=(on_entity_will_be_destroyed_event_cache : Proc(Entitas::Events::OnEntityWillBeDestroyed, Nil) | Nil) #

def on_group_created(event : Entitas::Events::OnGroupCreated) : Nil #

Method to process event: Entitas::Events::OnGroupCreated when emited. Will raise Entitas::Error::MethodNotImplemented when not implimented if an Entitas::Events::OnGroupCreated is emitted.

def on_group_created(event : Entitas::Events::OnGroupCreated) : Nil
  # do something with event
end

def on_group_created_event_cache : Proc(Entitas::Events::OnGroupCreated, Nil) | Nil #

def on_group_created_event_cache=(on_group_created_event_cache : Proc(Entitas::Events::OnGroupCreated, Nil) | Nil) #

def remove_all_event_handlers #

Removes all event handlers OnEntityCreated, OnEntityWillBeDestroyed, OnEntityDestroyed and OnGroupCreated


[View source]
def reset #

Resets the context (destroys all entities and resets creationIndex back to 0).


[View source]
def reset_creation_index #

Resets the creationIndex back to 0.


[View source]
def update_groups_component_added_or_removed(entity : TEntity, index : Int32, component : Entitas::IComponent | Nil) #

[View source]
def update_groups_component_replaced(entity : TEntity, index : Int32, prev_component : Entitas::IComponent | Nil, new_component : Entitas::IComponent | Nil) #

[View source]