Difference between revisions of "Tuto tiny-ecs demo Part 6 tiny-ecs Component"

From GiderosMobile
(Created page with "__TOC__ == 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...")
 
 
Line 48: Line 48:
  
 
== Next? ==
 
== Next? ==
In the next part, we will create...
+
In the next part, we will create systems to move our actors (entities).
  
  
 
Prev.: [[Tuto tiny-ecs demo Part 5 tiny-ecs System]]</br>
 
Prev.: [[Tuto tiny-ecs demo Part 5 tiny-ecs System]]</br>
'''Next: xxx'''
+
'''Next: [[Tuto tiny-ecs demo Part 7 Systems to move actors]]'''
  
  
 
'''[[Tutorial - tiny-ecs demo]]'''
 
'''[[Tutorial - tiny-ecs demo]]'''
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 04:34, 20 December 2023

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