Tuto tiny-ecs 2d platformer Part 8 more entities
Some more Entities
A game is made of many "actors" besides the player and the enemies. Here we will add more "actors" aka entities (collectibles, doors, keys, ...).
As I have mentioned before, components are shared amongst entities and this is exactly the case here too
ECollectibles.lua
Let's add some collectibles, shall we? Please create a file "eCollectibles.lua" in the "_E" folder. The code:
ECollectibles = Core.class()
function ECollectibles:init(xid, xspritelayer, xpos, xspeed, xdx, xdy)
-- ids
self.iscollectible = true
self.eid = xid
self.doanimate = true -- to save some cpu
-- sprite layer
self.spritelayer = xspritelayer
-- params
self.pos = xpos
self.sx = 1
self.sy = self.sx
self.flip = 1
self.totallives = 1
self.currlives = self.totallives
-- default to coin
local framerate = 1/10 -- magik XXX
local texpath = "gfx/collectibles/Coin_A_0_0001.png"
local cols, rows = 4, 3
self.sx = 0.7
if self.eid == "lives" then -- heart
texpath = "gfx/collectibles/Heart_2_0001.png"
cols, rows = 4, 3
self.sx = 0.8
elseif self.eid:find("door") then -- key
texpath = "gfx/collectibles/Key_3_0001.png"
cols, rows = 4, 3
self.sx = 0.8
end
self.sy = self.sx
-- COMPONENTS
-- ANIMATION
local anims = {} -- table to hold actor animations
local animsimgs = {} -- table to hold actor animations images
-- CAnimation:init(xanimspeed)
self.animation = CAnimation.new(framerate)
-- CAnimation:cutSpritesheet(xspritesheetpath, xcols, xrows, xanimsimgs, xoffx, xoffy, sx, sy)
self.animation:cutSpritesheet(texpath, cols, rows, animsimgs, 0, 0, self.sx, self.sy)
-- 1st set of animations: CAnimation:createAnim(xanims, xanimname, xanimsimgs, xtable, xstart, xfinish)
self.animation:createAnim(anims, g_ANIM_DEFAULT, animsimgs, nil, 1, cols*rows)
self.animation:createAnim(anims, g_ANIM_IDLE_R, animsimgs, nil, 1, cols*rows)
-- end animations
self.animation.anims = anims
self.sprite = self.animation.sprite
self.sprite:setScale(self.sx*self.flip, self.sy)
self.animation.sprite = nil -- free some memory
self.w, self.h = self.sprite:getWidth(), self.sprite:getHeight() -- with applied scale
-- COLLISION BOX: CCollisionBox:init(xcollwidth, xcollheight)
local collw, collh = self.w//1, self.h//1 -- must be round numbers for cbump physics!
self.collbox = CCollisionBox.new(collw, collh)
-- COscillation:init(xspeed, xamplitudex, xamplitudey)
self.oscillation = COscillation.new(xspeed, xdx, xdy)
end
When we create a collectible, we assign it an entity id (eid). Depending on the eid we will either spawn a coin, a life (to heal the player) or a key (to open doors).
The code is quite small and follows the same principle as the previous entities (player1 and enemies). The only thing we add here is an oscillation Component to give our collectibles some juice. Please create a file called "cOscillation.lua" in the "_C" folder. The code:
COscillation = Core.class()
function COscillation:init(xspeed, xamplitudex, xamplitudey)
self.vx = 0
self.vy = 0
self.speed = xspeed
self.amplitudex = xamplitudex
self.amplitudey = xamplitudey
end
An oscillation System will look for it and act accordingly. Easy as E C S ;-)
eCollectible.lua
"eCollectible.lua" in the "_E" folder. The code:
ECollectible = Core.class()
function ECollectible:init(xspritelayer, xpos)
-- ids
self.iscollectible = true
-- sprite layer
self.spritelayer = xspritelayer
-- params
self.pos = xpos
self.positionystart = self.pos.y -- for sprite sorting
self.sx = 1
self.sy = self.sx
self.flip = math.random(100)
if self.flip > 80 then self.flip = 1
else self.flip = -1
end
self.totallives = 1
self.currlives = self.totallives
-- COMPONENTS
-- ANIMATION: CAnimation:init(xspritesheetpath, xcols, xrows, xanimspeed, xoffx, xoffy, sx, sy)
local texpath = "gfx/collectible/Dragon Eggs 1.png"
local framerate = 1
self.animation = CAnimation.new(texpath, 1, 1, framerate, 0, 0, self.sx, self.sy)
self.sprite = self.animation.sprite
self.sprite:setScale(self.sx*self.flip, self.sy)
self.animation.sprite = nil -- free some memory
self.w, self.h = self.sprite:getWidth(), self.sprite:getHeight()
-- create animations: CAnimation:createAnim(xanimname, xstart, xfinish)
self.animation:createAnim(g_ANIM_DEFAULT, 1, 1)
self.animation:createAnim(g_ANIM_IDLE_R, 1, 1)
-- clean up
self.animation.myanimsimgs = nil
-- BODY: CBody:init(xspeed, xjumpspeed)
self.body = CBody.new(0, 0) -- xspeed, xjumpspeed
self.body.defaultmass = 1
self.body.currmass = self.body.defaultmass
-- COLLISION BOX: CCollisionBox:init(xcollwidth, xcollheight)
local collw, collh = self.w*0.6, self.h*0.6
self.collbox = CCollisionBox.new(collw, collh)
-- SHADOW: CShadow:init(xparentw, xshadowsx, xshadowsy)
self.shadow = CShadow.new(self.w*0.75)
-- IDEA: CREATE AN OSCILLATION COMPONENT using body xspeed, xjumpspeed!
end
You get the idea ;-)
We are done making all our entities!!!
Next?
The time has come to tackle the systems. I will try to make it easy :-)
Prev.: Tuto tiny-ecs 2d platformer Part 7 Enemies
Next: Tuto tiny-ecs 2d platformer Part 9 Systems
Tutorial - tiny-ecs 2d platformer