Difference between revisions of "Tuto tiny-ecs demo Part 7 Systems to move actors"

From GiderosMobile
 
Line 93: Line 93:
 
-- the player (xspritelayer, x, y)
 
-- the player (xspritelayer, x, y)
 
self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
 
self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
self.tiny.world:addEntity(self.player1)
+
self.tiny.tworld:addEntity(self.player1)
 
-- add systems to tiny-ecs
 
-- add systems to tiny-ecs
self.tiny.world:add(
+
self.tiny.tworld:add(
 
SDrawable.new(self.tiny),
 
SDrawable.new(self.tiny),
 
SDynamicBodies.new(self.tiny),
 
SDynamicBodies.new(self.tiny),

Latest revision as of 07:44, 21 December 2023

We continue with our tiny-ecs demo. It is time to add some action!

A System to control our player1

We will create a system to control our player1 using the keyboard.

In the systems folder "_S", create a file called "sPlayer1Control.lua" for example and copy the following code:

SPlayer1Control = Core.class()

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

function SPlayer1Control:filter(ent) -- tiny function
	return ent.isplayer1
end

function SPlayer1Control:onAdd(ent) -- tiny function
	stage:addEventListener(Event.KEY_DOWN, function(e) -- is the stage best?
		if e.keyCode == KeyCode.LEFT then ent.isleft = true end
		if e.keyCode == KeyCode.RIGHT then ent.isright = true end
		if e.keyCode == KeyCode.SPACE then ent.isaction1 = true end
	end)
	stage:addEventListener(Event.KEY_UP, function(e) -- is the stage best?
		if e.keyCode == KeyCode.LEFT then ent.isleft = false end
		if e.keyCode == KeyCode.RIGHT then ent.isright = false end
		if e.keyCode == KeyCode.SPACE then ent.isaction1 = false end
	end)
end

In the init function we tell tiny-ecs that this system will be executed only once on init.

Then in the filter function we tell tiny-ecs to execute the system on entities with an id of isplayer1 only.

Finally, in the onAdd function, we add two event listeners, one for the key down event, and one for the key up event. You can choose your key binding here, or even add event listeners for a joypad, mouse, ...

And a System to move our actors

Now we add another system which will be responsible for moving the actors (entities).

In the same systems folder "_S", create a file called sDynamicBodies.lua for example, and copy the following code:

SDynamicBodies = Core.class()

function SDynamicBodies:init(xtiny) -- tiny function
	self.tiny = xtiny -- ref so we can remove entities from tiny system
	self.tiny.processingSystem(self) -- called once on init and every update
end

function SDynamicBodies:filter(ent) -- tiny function
	return ent.isactor and ent.body -- only actors with body component
end

function SDynamicBodies:onAdd(ent) -- tiny function
end

function SDynamicBodies:onRemove(ent) -- tiny function
end

function SDynamicBodies:process(ent, dt) -- tiny function
	-- velocity
	if ent.isleft and not ent.isright then -- LEFT
		ent.body.vx = -ent.body.speed
	elseif ent.isright and not ent.isleft then -- RIGHT
		ent.body.vx = ent.body.speed
	else
		ent.body.vx = 0
	end
	-- move
	ent.x += ent.body.vx * dt
	ent.sprite:setPosition(ent.x, ent.y)
end

All systems follow the same pattern, they are easy to create!

Here, in the init function, we create a reference to the xtiny parameter, so we can use it later to add or remove actors from tiny-ecs world.

Then, we tell tiny-ecs that this system needs to be updated each game loop (cf: Tuto_tiny-ecs_demo_Part_3_tiny-ecs_World, self.tiny.world:update(dt)).

In the filter function, we tell tiny-ecs that this system applies to entities with an id of isactor and body (remember that a component can serve as an id).

Finally, in the process function, we check if an actor is going left or right to add velocity to its body. The velocity will change the actor position.

World addSystem

Last, we add our new systems to tiny-ecs world.

Please go to the "LevelX.lua" file and complete the code as follow:

	...
	-- the player (xspritelayer, x, y)
	self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
	self.tiny.tworld:addEntity(self.player1)
	-- add systems to tiny-ecs
	self.tiny.tworld:add(
		SDrawable.new(self.tiny),
		SDynamicBodies.new(self.tiny),
		SPlayer1Control.new(self.tiny)
	)
	...

I am not sure if the order systems are added is important! Make sure to put them in an order that speaks to you and your team :-)

That's it, you can run the demo and you should be able to control the player1 with the keyboard.

Next?

In the next part, we add some more actors, they will be enemies.


Prev.: Tuto tiny-ecs demo Part 6 tiny-ecs Component
Next: Tuto tiny-ecs demo Part 8 Enemies


Tutorial - tiny-ecs demo