Tuto tiny-ecs demo Part 6 tiny-ecs Component

From GiderosMobile
Revision as of 03:34, 20 December 2023 by MoKaLux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Component

It is time to create our first Component.

In the components folder "_C", create a file called "cBody.lua" for example. This will be the Component responsible for adding a body to entities.

This component will allow an entity to move left/right, jump, ...

CBody = Core.class()

function CBody:init(xspeed, xjumpspeed)
	-- body physics properties
	self.vx = 0
	self.vy = 0
	self.speed = xspeed
	self.jumpspeed = xjumpspeed
end

In the init function we pass two parameters: a speed and a jump speed.

Did I forget to tell how easy using an ECS can be?

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