Tuto tiny-ecs demo Part 7 Systems to move actors

From GiderosMobile
Revision as of 04:44, 20 December 2023 by MoKaLux (talk | contribs) (Created page with "__TOC__ 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 key...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Please in the systems folder "_S" create a file called "sPlayer1Control.lua" for example.

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.

Finally, in the o

Entity Component

Now we extend our player1 entity with the body component.

Please go to the "ePlayer1.lua" file and add the following code:

	-- ...
	-- params
	self.x = x
	self.y = y
	-- BODY COMPONENTS: function CBody:init(xspeed, xjumpspeed)
	self.body = CBody.new(8*16, 8*16)
	self.body.defaultmass = 1
	self.body.currmass = self.body.defaultmass
	-- ...

The component will also serve as an id for a later System we will create.

You can add more parameters to the body component like mass, ...

A Component can be attached to any entity.

The challenge with the ECS paradigm is to create meaningful components and systems!

Next?

In the next part, we will create systems to move our actors (entities).


Prev.: Tuto tiny-ecs demo Part 5 tiny-ecs System
Next: Tuto tiny-ecs demo Part 7 Systems to move actors


Tutorial - tiny-ecs demo