Module CEnt

module CEnt: sig .. end
Entity/Component Oriented Game/Multimedia Module


Note: This is an experimentation, not a final work.

Entities


type id = Ent.id 
id of an entity, entities have an id only after been added to the world
type entity = Comp.entity 
this is the base type of the entity / component concept
val new_entity : unit -> entity
generic game element
val get_id : entity -> id
raises an exception if the entity has not been added to the world
val get_id_opt : entity -> id option
same than get_id but returns None instead of raising an exception

Components



A component handles a property / attribute of an entity.

An entity may have as many components as needed, but only one component of each type.

val has_component : entity -> Comp.component_type -> bool
val has_components : entity -> Comp.component_type list -> bool
retruns true if the entity contains all the given component types

(this function does not tell if the entity contains other components or not)

val has_any_component : entity -> Comp.component_type list -> bool
returns true if the entity contains at least one of the given component types
val iter_components : entity -> (Comp.component_type -> Comp.component -> unit) -> unit
val get_components : entity -> Comp.component_type list
val cmp_components : entity -> Comp.component_type list -> int
val components_match : entity -> Comp.component_type list -> bool

Constructors / Accessors / Modifiers



The constructors, accessors and modifiers are in the module Comp, which have been generated (from the type definition of component) with the gen_comp command line tool.

World


type ('delta, 'fld) world = ('delta, 'fld) Comp.world 
for the 'delta type parameter see the doc of the function world_step

and see world_step_fold about the 'fld type

val new_world : unit -> ('a, 'b) world
val add_entity : ('a, 'b) world -> entity -> ('a, 'b) world
val add_entities : ('a, 'b) world -> entity list -> ('a, 'b) world
val add_entity_id : ('a, 'b) world -> entity -> ('a, 'b) world * id
same than add_entity but also return the id that was given to this entity
val add_entities_id : ('a, 'b) world -> entity list -> ('a, 'b) world * id list
val add_entities_init : w:('a, 'b) world ->
n:int -> f:(int -> entity) -> ('a, 'b) world
val has_entity : ('a, 'b) world -> id -> bool
does an entity exists with the given id
val replace_entity : ('a, 'b) world -> id -> entity -> ('a, 'b) world
val remove_entity : ('a, 'b) world -> entity -> ('a, 'b) world
val remove_entity_id : ('a, 'b) world -> id -> ('a, 'b) world

World Getters


val get_entity : ('a, 'b) world -> id -> entity
val get_entity_opt : ('a, 'b) world -> id -> entity option
get an entity by its id
val get_entities : ('a, 'b) world -> id list -> entity list
ids not found are just skipped
val do_get_entities : ('a, 'b) world -> id list -> entity list
raises Not_found if an id is not found
val get_entities_with_components : ('a, 'b) world -> Comp.component_type list -> entity list

World Step



the heart beats here
type system_label = Ent.system_label 
val world_step : ('delta, unit) world ->
?labels:system_label list -> 'delta -> ('delta, unit) world
'delta is the input parameter that is given to the systems

Only the systems with one of the given system_label will be applied, except if no labels are given, then all systems are applied.

val world_step_fold : ('delta, 'fld) world ->
?labels:system_label list ->
'delta -> 'fld -> ('delta, 'fld) world * 'fld
same than world_step but with an additional folded parameter, see foldable_systems

World Iterators


val iter_entities : (entity -> unit) -> ('a, 'b) world -> unit
val fold_entities : (entity -> 'p -> 'p) -> ('a, 'b) world -> 'p -> 'p
val num_entities : ('a, 'b) world -> int
val num_entities_with_components : ('a, 'b) world -> Comp.component_type list -> int

Mappers



a system will be only applied to the entities that contain all the component types of the associated mapper
type mapper = Comp.component_type Ent.mapper 
val make_mapper : Comp.component_type list -> mapper

Systems



A system may update or remove entities, or create new ones, each time world_step is called.
type 'a update = 'a Ent.update 
type ('a, 'b) system = (Comp.component_type, Comp.component, 'a, 'b) Ent.system 
the first returned value should be the input entity with eventual changes and the second returned value is eventual new entities that should be added to the world
val add_system : ('delta, 'a) world ->
mapper ->
?label:system_label ->
('delta, 'a) system -> ('delta, 'a) world
val add_systems : ('delta, 'a) world ->
(mapper * system_label option * ('delta, 'a) system) list ->
('delta, 'a) world

Foldable Systems


type ('delta, 'fld) foldable_system = (Comp.component_type, Comp.component, 'delta, 'fld) Ent.foldable_system 
similar than system but a foldable_system has an additional folded parameter that can be given with world_step_fold
val add_foldable_system : ('delta, 'fld) world ->
mapper ->
?label:system_label ->
('delta, 'fld) foldable_system -> ('delta, 'fld) world
val add_foldable_systems : ('delta, 'fld) world ->
(mapper * system_label option * ('delta, 'fld) foldable_system)
list -> ('delta, 'fld) world