Tuto tiny-ecs demo Part 8 Enemies
We continue with our tiny-ecs demo. It is time to add some "enemies".
Entity enemy1
First we create an entity of type enemy1.
In the entites folder "_E", create a file called "eNme1.lua" for example and copy the following code:
ENme1 = Core.class()
function ENme1:init(xspritelayer, x, y, dx, dy)
-- ids
self.isnme = true
self.isactor = true
-- sprite
self.spritelayer = xspritelayer
self.sprite = Pixel.new(math.random(0xffffff), 1, 32, 64)
-- params
self.x = x
self.y = y
self.health = 10
-- BODY COMPONENT: function CBody:init(xspeed, xjumpspeed)
self.body = CBody.new(4*16, 6*16) -- xspeed, xjumpspeed
self.body.defaultmass = 1
self.body.currmass = self.body.defaultmass
end
We start by adding some ids, a sprite layer and a sprite to see our enemy1. The sprite here is a Pixel with a random color.
We add some more parameters like position and health.
Finally we add the body component so the entity can move.
With ECS it is easy to build your game, you add/remove parameters and components as you need.
World addEntity
We add a couple of enemy1 entities to tiny-ecs world.
Please go to the "LevelX.lua" file and complete the code as follow:
...
-- tiny-ecs
self.tiny = require "classes/tiny-ecs"
self.tiny.world = self.tiny.world()
-- some enemies (xspritelayer, x, y, dx, dy)
local nmes = {}
for i = 1, 10 do -- we create 10 enemies
nmes[i] = ENme1.new(self.camera, math.random(24)*16, 10*16, math.random(12)*16, 0)
self.tiny.world:addEntity(nmes[i])
end
-- the player (xspritelayer, x, y)
self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
self.tiny.world:addEntity(self.player1)
...
You can run the demo and you should be able to see ten enemies placed randomly on the x axis.
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.
Next?
In the next part, we ...
Prev.: Tuto tiny-ecs demo Part 7 Systems to move actors
Next: xxx