Difference between revisions of "Tuto tiny-ecs beatemup Part 8 Breakables"
(wip) |
|||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
− | == | + | == The last ones == |
− | + | We have two entities left to make. Some breakable objects entities which will spawn collectible entities. | |
− | As I have mentioned before components are shared amongst entities and this is exactly the case here. The | + | As I have mentioned before components are shared amongst entities and this is exactly the case here too. The breakable objects and the collectibles share the same components as our previous actors and the structure is almost the same as well. |
− | == | + | == eDestructibleObject.lua == |
− | + | '''Excuse my english, I am not an english native speaker. I called the breakable objects destructibles!''' | |
+ | |||
+ | Please create a file "'''eDestructibleObject.lua'''" in the '''"_E"''' folder and the code: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | EDestructibleObject = Core.class() | |
− | function | + | function EDestructibleObject:init(xspritelayer, xpos) |
-- ids | -- ids | ||
− | self. | + | self.isdestructibleobject = true |
− | -- sprite | + | -- sprite layer |
self.spritelayer = xspritelayer | self.spritelayer = xspritelayer | ||
− | |||
-- params | -- params | ||
self.pos = xpos | self.pos = xpos | ||
− | self.sx = 1 | + | self.positionystart = self.pos.y -- for sprite sorting |
+ | self.sx = 1 | ||
self.sy = self.sx | self.sy = self.sx | ||
− | self.totallives = | + | self.flip = math.random(100) |
+ | if self.flip > 50 then self.flip = 1 | ||
+ | else self.flip = -1 | ||
+ | end | ||
+ | self.totallives = 1 | ||
self.totalhealth = 2 | self.totalhealth = 2 | ||
− | |||
− | |||
− | |||
− | |||
if g_difficulty == 0 then -- easy | if g_difficulty == 0 then -- easy | ||
− | + | self.totalhealth *= 0.5 | |
− | self.totalhealth | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
+ | self.currlives = self.totallives | ||
+ | self.currhealth = self.totalhealth | ||
+ | -- recovery | ||
+ | self.washurt = 0 | ||
+ | self.wasbadlyhurt = 0 | ||
+ | self.recovertimer = 10 | ||
self.hitfx = Bitmap.new(Texture.new("gfx/fx/1.png")) | self.hitfx = Bitmap.new(Texture.new("gfx/fx/1.png")) | ||
self.hitfx:setAnchorPoint(0.5, 0.5) | self.hitfx:setAnchorPoint(0.5, 0.5) | ||
-- COMPONENTS | -- COMPONENTS | ||
-- ANIMATION: CAnimation:init(xspritesheetpath, xcols, xrows, xanimspeed, xoffx, xoffy, sx, sy) | -- ANIMATION: CAnimation:init(xspritesheetpath, xcols, xrows, xanimspeed, xoffx, xoffy, sx, sy) | ||
− | local texpath = "gfx/ | + | local texpath = "gfx/breakable/Barrel_02_0012.png" |
− | local framerate = 1 | + | local framerate = 1 |
− | self.animation = CAnimation.new(texpath, | + | self.animation = CAnimation.new(texpath, 1, 1, framerate, 0, 0, self.sx, self.sy) |
self.sprite = self.animation.sprite | self.sprite = self.animation.sprite | ||
+ | self.sprite:setScale(self.sx*self.flip, self.sy) -- for the flip | ||
self.animation.sprite = nil -- free some memory | self.animation.sprite = nil -- free some memory | ||
self.w, self.h = self.sprite:getWidth(), self.sprite:getHeight() | self.w, self.h = self.sprite:getWidth(), self.sprite:getHeight() | ||
− | -- create | + | -- create animations: CAnimation:createAnim(xanimname, xstart, xfinish) |
− | self.animation:createAnim(g_ANIM_DEFAULT, 1, | + | self.animation:createAnim(g_ANIM_DEFAULT, 1, 1) |
− | self.animation:createAnim(g_ANIM_IDLE_R, 1, | + | self.animation:createAnim(g_ANIM_IDLE_R, 1, 1) |
− | + | -- clean up | |
− | + | self.animation.myanimsimgs = nil | |
− | self.animation | ||
− | |||
-- BODY: CBody:init(xspeed, xjumpspeed) | -- 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) | -- COLLISION BOX: CCollisionBox:init(xcollwidth, xcollheight) | ||
− | local collw, collh = self.w | + | local collw, collh = self.w*1, 8*self.sy |
self.collbox = CCollisionBox.new(collw, collh) | self.collbox = CCollisionBox.new(collw, collh) | ||
-- hurt box | -- hurt box | ||
− | local hhbw, hhbh = self.w | + | local hhbw, hhbh = self.w, 1*self.h/2 |
self.headhurtbox = { | self.headhurtbox = { | ||
isactive=false, | isactive=false, | ||
− | x= | + | x=0*self.sx, |
− | y= | + | y=-self.h+hhbh*0.5+self.collbox.h/2, |
w=hhbw, | w=hhbw, | ||
h=hhbh, | h=hhbh, | ||
} | } | ||
− | local shbw, shbh = self.w | + | local shbw, shbh = self.w, 3*self.h/4 |
self.spinehurtbox = { | self.spinehurtbox = { | ||
isactive=false, | isactive=false, | ||
− | x= | + | x=0*self.sx, |
− | y= | + | y=self.collbox.h/2-shbh/2, |
w=shbw, | w=shbw, | ||
h=shbh, | h=shbh, | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
-- SHADOW: CShadow:init(xparentw, xshadowsx, xshadowsy) | -- SHADOW: CShadow:init(xparentw, xshadowsx, xshadowsy) | ||
− | self.shadow = CShadow.new(self.w* | + | self.shadow = CShadow.new(self.w*1.1) |
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | The main differences: | |
− | * | + | * it is a single frame animation |
− | * | + | * no attacks |
− | * | + | * no AI |
− | + | Once a breakable object is destroyed by the player, it spawns a collectible. | |
− | |||
− | |||
− | |||
− | + | == eCollectible.lua == | |
− | + | "'''eCollectible.lua'''" in the '''"_E"''' folder. The code: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | == | ||
− | "''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | ECollectible = Core.class() | |
− | function | + | function ECollectible:init(xspritelayer, xpos) |
-- ids | -- ids | ||
− | self. | + | self.iscollectible = true |
− | -- sprite | + | -- sprite layer |
self.spritelayer = xspritelayer | self.spritelayer = xspritelayer | ||
− | |||
-- params | -- params | ||
self.pos = xpos | self.pos = xpos | ||
− | self.sx = 1 | + | self.positionystart = self.pos.y -- for sprite sorting |
+ | self.sx = 1 | ||
self.sy = self.sx | self.sy = self.sx | ||
− | self. | + | self.flip = math.random(100) |
− | + | if self.flip > 80 then self.flip = 1 | |
− | + | else self.flip = -1 | |
− | |||
− | |||
− | |||
− | if | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | self. | + | self.totallives = 1 |
− | self. | + | self.currlives = self.totallives |
-- COMPONENTS | -- COMPONENTS | ||
-- ANIMATION: CAnimation:init(xspritesheetpath, xcols, xrows, xanimspeed, xoffx, xoffy, sx, sy) | -- ANIMATION: CAnimation:init(xspritesheetpath, xcols, xrows, xanimspeed, xoffx, xoffy, sx, sy) | ||
− | local texpath = "gfx/ | + | local texpath = "gfx/collectible/Dragon Eggs 1.png" |
− | local framerate = 1 | + | local framerate = 1 |
− | self.animation = CAnimation.new(texpath, | + | self.animation = CAnimation.new(texpath, 1, 1, framerate, 0, 0, self.sx, self.sy) |
self.sprite = self.animation.sprite | self.sprite = self.animation.sprite | ||
+ | self.sprite:setScale(self.sx*self.flip, self.sy) | ||
self.animation.sprite = nil -- free some memory | self.animation.sprite = nil -- free some memory | ||
self.w, self.h = self.sprite:getWidth(), self.sprite:getHeight() | self.w, self.h = self.sprite:getWidth(), self.sprite:getHeight() | ||
− | -- create | + | -- create animations: CAnimation:createAnim(xanimname, xstart, xfinish) |
− | self.animation:createAnim(g_ANIM_DEFAULT, 1, | + | self.animation:createAnim(g_ANIM_DEFAULT, 1, 1) |
− | self.animation:createAnim(g_ANIM_IDLE_R, 1, | + | self.animation:createAnim(g_ANIM_IDLE_R, 1, 1) |
− | + | -- clean up | |
− | + | self.animation.myanimsimgs = nil | |
− | self.animation | ||
− | |||
-- BODY: CBody:init(xspeed, xjumpspeed) | -- 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) | -- COLLISION BOX: CCollisionBox:init(xcollwidth, xcollheight) | ||
− | local collw, collh = self.w*0. | + | local collw, collh = self.w*0.6, self.h*0.6 |
self.collbox = CCollisionBox.new(collw, collh) | self.collbox = CCollisionBox.new(collw, collh) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
-- SHADOW: CShadow:init(xparentw, xshadowsx, xshadowsy) | -- SHADOW: CShadow:init(xparentw, xshadowsx, xshadowsy) | ||
self.shadow = CShadow.new(self.w*0.75) | self.shadow = CShadow.new(self.w*0.75) | ||
+ | -- IDEA: CREATE AN OSCILLATION COMPONENT using body xspeed, xjumpspeed! | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | You get the idea ;-) | ||
+ | |||
+ | '''We are done making all our entities!!!''' | ||
== Next? == | == Next? == | ||
− | + | The time has come to tackle the '''systems'''. I will try to make it easy :-) | |
Revision as of 18:29, 20 November 2024
The last ones
We have two entities left to make. Some breakable objects entities which will spawn collectible entities.
As I have mentioned before components are shared amongst entities and this is exactly the case here too. The breakable objects and the collectibles share the same components as our previous actors and the structure is almost the same as well.
eDestructibleObject.lua
Excuse my english, I am not an english native speaker. I called the breakable objects destructibles!
Please create a file "eDestructibleObject.lua" in the "_E" folder and the code:
EDestructibleObject = Core.class()
function EDestructibleObject:init(xspritelayer, xpos)
-- ids
self.isdestructibleobject = 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 > 50 then self.flip = 1
else self.flip = -1
end
self.totallives = 1
self.totalhealth = 2
if g_difficulty == 0 then -- easy
self.totalhealth *= 0.5
end
self.currlives = self.totallives
self.currhealth = self.totalhealth
-- recovery
self.washurt = 0
self.wasbadlyhurt = 0
self.recovertimer = 10
self.hitfx = Bitmap.new(Texture.new("gfx/fx/1.png"))
self.hitfx:setAnchorPoint(0.5, 0.5)
-- COMPONENTS
-- ANIMATION: CAnimation:init(xspritesheetpath, xcols, xrows, xanimspeed, xoffx, xoffy, sx, sy)
local texpath = "gfx/breakable/Barrel_02_0012.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) -- for the flip
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*1, 8*self.sy
self.collbox = CCollisionBox.new(collw, collh)
-- hurt box
local hhbw, hhbh = self.w, 1*self.h/2
self.headhurtbox = {
isactive=false,
x=0*self.sx,
y=-self.h+hhbh*0.5+self.collbox.h/2,
w=hhbw,
h=hhbh,
}
local shbw, shbh = self.w, 3*self.h/4
self.spinehurtbox = {
isactive=false,
x=0*self.sx,
y=self.collbox.h/2-shbh/2,
w=shbw,
h=shbh,
}
-- SHADOW: CShadow:init(xparentw, xshadowsx, xshadowsy)
self.shadow = CShadow.new(self.w*1.1)
end
The main differences:
- it is a single frame animation
- no attacks
- no AI
Once a breakable object is destroyed by the player, it spawns a collectible.
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 beatemup Part 7 Enemies
Next: Tuto tiny-ecs beatemup Part 9 XXX