Welcome to toyblock’s 2.0.0 documentation!¶
-
class
toyblock.
Entity
(*instances, pool=None)¶ A bag where you group the components.
Parameters: - *instances (Any) – Instances of any type
- pool (Pool or None) – Pool which this entity belongs to.
Returns: A new Entity instance.
Raises: EntityComponentExistsError
– If the type of a instance is already used.-
__getitem__
(type_)¶ This is a convenient, less verbose, way to get a component and manipulate it.
Parameters: type_ – Type of the instance Returns: Instance of type_ if exists, otherwise None Example
entity = Entity(Body(), Graphic()) entity[Body].x = 7.
-
add_component
(instance)¶ Add a component instance to this entity.
Parameters: instance (Any) – An instance of any class.
Raises: EntityBelongsToPoolError
– If this entity belongs to a Pool.EntityComponentExistsError
– If the type of instance is already used.
-
del_component
(type_)¶ Delete a specific component. Remove and returns the instance of type_.
Parameters: type_ – A instance of type_ Returns: The removed instance from this entity, or None if not exists.
-
free
()¶ Make this entity avaliable from its Pool. The entity it is removed from the systems that are asigned to
Pool
. You can use this method inside aSystem
call.If this entity does not have a Pool then this method does nothing.
Example
@toyblock.System def life(system, entity): if entity[Life].is_over(): entity.free()
-
get_component
(type_)¶ Deprecated since version 2.0.0: Use
__getitem__()
instead.
-
pool
¶ You can check whether this entity belongs to a Pool. Read only.
-
set
(type_, dict_)¶ Convenient method for setting attributes to a component with a dict.
Parameters: - type_ – A instance of type_.
- dict_ – Dict to use for the component.
Example
# This is more easy player.set(Body, {'x': 32., 'y': 64.}) # than player[Body].x = 32. player[Body].y = 64.
-
class
toyblock.
System
(callable_)¶ Define how are entities processed here.
In the constructor is mandatory pass a callable. After you add at least one entity you can run the system calling it.
Use this as a decorator is encouraged. See Example.
Parameters: callable_ – A callable Note
The signature for the callable is
callable(system, entity, *args, **kwargs)
Returns: A System instance which is callable. Raises: TypeError
– If you do not pass a callable.Example
@toyblock.System def physics(system, entity, dt): pass # do your things here physics.add_entity(some_entity) # ... physics(get_delta_time())
-
__call__
(*args, **kwargs)¶ Run the system.
It is perfectly safe add entities to the system or remove entities from the system.
-
entities
¶ Get the entities added to this system.
-
-
class
toyblock.
Pool
(maxlen, types, args_list=(), kwargs_list=(), systems=None)¶ Manage entities and manage systems related to entities.
Parameters: - maxlen (int) – Total number of entities.
- types (iterable of classes) –
- args_list (iterable) – A list of args for the classes.
- kwargs_list (iterable) – A list of kwargs for the classes.
- systems (iterable of System) – Systems related with these entities.
Returns: A instance of Pool.
Example
class A: def __init__(self, a, b): self.a = a self.b = b class B: def __init__(self, a=0): self.a = a class C: def __init__(self): self.things = [] args = ((1, 2),) kwargs = (None, {"a": 7}) pool = toyblock.Pool(10, (A, B, C), args, kwargs, systems=(input, physics, touch, life))
-
clean
(clean_)¶ The clean function is called when an
Entity
is freed.Parameters: clean_ (callable) – Signature is clean_(entity) Returns: The same callable passed as parameter. Raises: TypeError
– if clean_ is not callable.Example
a_pool = toyblock.Pool(4, (Body, Graphic)) @a_pool.clean def reset_car(entity): entity[Damage].damage = 0 # For example
-
free
(entity)¶ Deprecated since version 2.0.0: Use
Entity.free()
instead.
-
free_all
()¶ Release all the used entities.
-
init
(init_)¶ Called when
get()
returns a instance ofEntity
.Parameters: init_ (callable) – Signature is init_(entity) Returns: The same callable passed as parameter. Raises: TypeError
– if init_ is not callable.Example
a_pool = toyblock.Pool(4, (Body, Graphic)) @a_pool.init def init_car(entity): body = entity[Body] body.vel = 0.0 # entity[Body].vel = 0.0