Difference between revisions of "Tuto tiny-ecs beatemup Part 9 Systems"

From GiderosMobile
(wip)
(wip)
Line 11: Line 11:
 
  *'''function system:filter(entity)''' Returns true if this System should include this Entity, otherwise should return false. If this isn't specified, no Entities are included in the System.
 
  *'''function system:filter(entity)''' Returns true if this System should include this Entity, otherwise should return false. If this isn't specified, no Entities are included in the System.
 
  *'''function system:onAdd(entity)''' Called when an Entity is added to the System.
 
  *'''function system:onAdd(entity)''' Called when an Entity is added to the System.
  *function system:onRemove(entity)''' Called when an Entity is removed from the System.
+
  *'''function system:onRemove(entity)''' Called when an Entity is removed from the System.
 
  *'''function system:onModify(dt)''' Called when the System is modified by adding or removing Entities from the System.
 
  *'''function system:onModify(dt)''' Called when the System is modified by adding or removing Entities from the System.
 
  *'''function system:onAddToWorld(world)''' Called when the System is added to the World, before any entities are added to the system.
 
  *'''function system:onAddToWorld(world)''' Called when the System is added to the World, before any entities are added to the system.
Line 51: Line 51:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
XXX
+
What it does:
 +
* runs only once when it is called
 +
* affects only entities which have a spritelayer variable (id) '''and''' a sprite variable (id)
 +
* when an Entity is added to tiny-ecs '''World''', the System adds the Entity to its sprite layer
 +
* when an Entity is removed from tiny-ecs '''World''', the System removes the Entity from its sprite layer
 +
 
 +
Easy, let's continue.
  
 
== eCollectible.lua ==
 
== eCollectible.lua ==

Revision as of 19:59, 21 November 2024

The Systems

We have our entities, we have our components, now the systems. What is an ECS System?

A System is a wrapper around function callbacks for manipulating Entities. Systems are implemented as tables that contain at least one method; an update function that takes parameters like so:

function system:update(dt)

There are also a few other optional callbacks:
*function system:filter(entity) Returns true if this System should include this Entity, otherwise should return false. If this isn't specified, no Entities are included in the System.
*function system:onAdd(entity) Called when an Entity is added to the System.
*function system:onRemove(entity) Called when an Entity is removed from the System.
*function system:onModify(dt) Called when the System is modified by adding or removing Entities from the System.
*function system:onAddToWorld(world) Called when the System is added to the World, before any entities are added to the system.
*function system:onRemoveFromWorld(world) Called when the System is removed from the world, after all Entities are removed from the System.
*function system:preWrap(dt) Called on each system before update is called on any system.
*function system:postWrap(dt) Called on each system in reverse order after update is called on each system.

Please see Tiny-ecs#System_functions for more information

That looks scary but worry not we won't use all the callback functions :-)

To put it simple a System manipulates entities. Let's see our first System.

sDrawable.lua

Please create a file "sDrawable.lua" in the "_S" folder and the code:

SDrawable = Core.class()

function SDrawable:init(xtiny) -- tiny function
	xtiny.system(self) -- called only once on init (no update)
end

function SDrawable:filter(ent) -- tiny function
	return ent.spritelayer and ent.sprite
end

function SDrawable:onAdd(ent) -- tiny function
--	print("SDrawable:onAdd(ent)")
	ent.spritelayer:addChild(ent.sprite)
end

function SDrawable:onRemove(ent) -- tiny function
--	print("SDrawable:onRemove(ent)")
	ent.spritelayer:removeChild(ent.sprite)
	-- cleaning?
	ent.sprite = nil
	ent = nil
end

What it does:

  • runs only once when it is called
  • affects only entities which have a spritelayer variable (id) and a sprite variable (id)
  • when an Entity is added to tiny-ecs World, the System adds the Entity to its sprite layer
  • when an Entity is removed from tiny-ecs World, the System removes the Entity from its sprite layer

Easy, let's continue.

eCollectible.lua

"eCollectible.lua" in the "_E" folder. The code:

You get the idea ;-)

We are done making all our entities!!!

Next?

The time has come to tackle the systems. I will try to make it easy :-)


Prev.: Tuto tiny-ecs beatemup Part 8 Breakables
Next: Tuto tiny-ecs beatemup Part 10 XXX


Tutorial - tiny-ecs beatemup