Tuto tiny-ecs 2d platformer Part 6 ECS Components

From GiderosMobile

Components

As we have seen in the previous chapter, components are abilities we add to an Entity. Entities can and will share the same components.

Let's continue adding abilities to our player1.

BODY

The Body Component adds an Entity the ability to move both on the x and y axis. You can create a file called "cBody.lua" in the "_C" folder. The code:

CBody = Core.class()

function CBody:init(xmass, xspeed, xupspeed, xextra)
	-- body physics properties
	self.mass = xmass or 1
	self.currmass = self.mass
	self.vx = 0
	self.vy = 0
	self.speed = xspeed
	self.currspeed = self.speed
	self.upspeed = xupspeed
	self.currupspeed = self.upspeed
	if xextra then
		self.inputbuffer = 1*8 -- 1*8, 2*8
		self.currinputbuffer = self.inputbuffer
		self.coyotetimer = 1.65*8 -- 1.5*8, 2*8, 12
		self.currcoyotetimer = self.coyotetimer
		self.jumpcount = 1 -- 2
		self.currjumpcount = self.jumpcount
		self.candash = true -- true, false XXX
		self.dashtimer = 1*8 -- 1*8 -- 0.5*8
		self.currdashtimer = self.dashtimer
		self.dashmultiplier = 1.5 -- 1.5
		self.dashcooldown = 4*8 -- 4*8, cooldown before performing another dash
		self.currdashcooldown = self.dashcooldown
	end
end

The CBody:init function signature has four parameters: the body mass, the speed on the x axis, the upspeed on the y axis and the extra body variables.

note: some entities will have a body but without the extra parameters, for example a moving platform
Components are usually small pieces of code, which is nice!

COLLISION BOX

The CollisionBox Component adds an Entity a collision box. You can create a file called "cCollisionBox.lua" in the "_C" folder. The code:

CCollisionBox = Core.class()

function CCollisionBox:init(xcollwidth, xcollheight)
	self.w = xcollwidth
	self.h = xcollheight
end

That's all we need to tell a System this Entity has a collision box. We store the Entity collision box width and height to be used by systems.

Shield

This is not an ECS component per se but I added some shield to the player1. I think I should have created a component for it but that will do for now.

When you press the shield action key, the shield is activated.

Next?

The player1 Entity and its components are done, now using almost the same components let's create our enemies...


Prev.: Tuto tiny-ecs 2d platformer Part 5 ePlayer1
Next: Tuto tiny-ecs 2d platformer Part 7 Enemies


Tutorial - tiny-ecs 2d platformer