Virtual hearts system
Posted: Mon Jun 12, 2017 4:46 pm
As I found a code for additional hearts over at PGE forums I decided to modify if to replace the text by pictures as well as adding the heart container and heart piece system of LoZ.
However, the following code throws a no matcing overload error even though I've checked my naming carefully.As I don't know when else such an error can appear I appreciated your help now.For better following which effect I want each conditional to have I've also posted the commentary I made (marked by these signs--):
However, the following code throws a no matcing overload error even though I've checked my naming carefully.As I don't know when else such an error can appear I appreciated your help now.For better following which effect I want each conditional to have I've also posted the commentary I made (marked by these signs--):
Code: Select all
local hearts = {}
local vhearts = 3
local heartLimit = 3
local heartPieces = 0
local limit3 = Graphics.loadImage(Misc.resolveFile("hearts/limit3"))
local limit4 = Graphics.loadImage(Misc.resolveFile("hearts/limit4"))
local limit5 = Graphics.loadImage(Misc.resolveFile("hearts/limit5"))
local limit6 = Graphics.loadImage(Misc.resolveFile("hearts/limit6"))
local limit7 = Graphics.loadImage(Misc.resolveFile("hearts/limit7"))
local limit8 = Graphics.loadImage(Misc.resolveFile("hearts/limit8"))
local limit9 = Graphics.loadImage(Misc.resolveFile("hearts/limit9"))
local limit10 = Graphics.loadImage(Misc.resolveFile("hearts/limit10"))
local heart = Graphics.loadImage(Misc.resolveFile("hearts/heart.png"))
local piece1 = Graphics.loadImage(Misc.resolveFile("hearts/piece1.png"))
local piece2 = Graphics.loadImage(Misc.resolveFile("hearts/piece2.png"))
local piece3 = Graphics.loadImage(Misc.resolveFile("hearts/piece3.png"))
local piece4 = Graphics.loadImage(Misc.resolveFile("hearts/piece4.png"))
local piecebg = Graphics.loadImage(Misc.resolveFile("hearts/piecebg.png"))
function hearts.onInitAPI()
registerEvent(hearts, "onTick", "onTick", true)
registerEvent(hearts, "onHUDDraw", "onHUDDraw",true)
end
function hearts.onTick()
--Detect extra hearts. It'll add an infinite number of hearts.
if ((player:mem(0x16, FIELD_WORD)) > 2) then
vhearts = vhearts + 1;
player:mem(0x16, FIELD_WORD, 2);
end
--You've defined a number of hearts for some reason, no?
if (vhearts > heartLimit) then
vhearts = heartLimit;
end
--If damaged, take extra hearts from virtual hearts
if ((player:mem(0x16, FIELD_WORD)) == 1) and (vhearts > 1) then
vhearts = vhearts - 1;
end
--This keeps the player alive
if (player.powerup == PLAYER_SMALL) and (vhearts > 2)
or (player.powerup == PLAYER_SMALL) and (vhearts == 2) then
player.powerup = PLAYER_BIG;
end
for k,v in pairs(NPC.get(250,-1)) do --functions like a heart container in LoZ: extends the maximal hearts by 1
heartLimit = heartLimit + 1
vhearts = heartLimit
end
for k,v in pairs(NPC.get(249,-1)) do
heartPieces = heartPieces + 1
vhearts = vhearts --avoids that heart pieces heal you
end
if heartPieces == 4 then
heartLimit = heartLimit + 1
heartPieces = 0
vhearts = heartLimit -- turns 4 heart pieces into an extra heart
end
if heartLimit > 10 then
heartLimit = 10 -- to limit maximal hearts even if they'd be extended by a container
end
function hearts.onHUDDraw() --this draws everything.See comments below for further details
if vhearts == 1 then
Graphics.drawImageWP(heart,36,38,5) --draws full red hearts to indicate current hp
end
if vhearts == 2 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
end
if vhearts == 3 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
end
if vhearts == 4 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
end
if vhearts == 5 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
end
if vhearts == 6 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
end
if vhearts == 7 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
end
if vhearts == 8 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
Graphics.drawImageWP(heart,36,74,5)
end
if vhearts == 9 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
Graphics.drawImageWP(heart,36,74,5)
Graphics.drawImageWP(heart,72,74,5)
end
if vhearts == 10 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
Graphics.drawImageWP(heart,36,74,5)
Graphics.drawImageWP(heart,72,74,5)
Graphics.drawImageWP(heart,108,74,5) --draws full red hearts to indicate current hp
end
if heartLimit == 3 then
Graphics.drawImageWP(limit3,36,38,5) --draws empty grey hearts to indicate the current maximum
end
if heartLimit == 4 then
Graphics.drawImageWP(limit4,36,38,5)
end
if heartLimit == 5 then
Graphics.drawImageWP(limit5,36,38,5)
end
if heartLimit == 6 then
Graphics.drawImageWP(limit6,36,38,5)
end
if heartLimit == 7 then
Graphics.drawImageWP(limit7,36,38,5)
end
if heartLimit == 8 then
Graphics.drawImageWP(limit8,36,38,5)
end
if heartLimit == 9 then
Graphics.drawImageWP(limit9,36,38,5)
end
if heartLimit == 10 then
Graphics.drawImageWP(limit10,36,38,5) --draws empty grey hearts to indicate the current maximum
end
if heartPieces == 0 or
heartPieces > 0 then
Graphics.drawImageWP(piecebg,262,38,4) --draws the player's current heart pieces and their background
end
if heartPieces == 1 then
Graphics.drawImageWP(piece1,262,38,5)
end
if heartPieces == 2 then
Graphics.drawImageWP(piece1,262,38,5)
Graphics.drawImageWP(piece2,279,38,5)
end
if heartPieces == 3 then
Graphics.drawImageWP(piece1,262,38,5)
Graphics.drawImageWP(piece2,279,38,5)
Graphics.drawImageWP(piece3,262,55,5)
end
if heartPieces == 4 then
Graphics.drawImageWP(piece1,262,38,5)
Graphics.drawImageWP(piece2,279,38,5)
Graphics.drawImageWP(piece3,262,55,5)
Graphics.drawImageWP(piece4,279,55,5) -- draws the heart pieces the player currently has
end
end
end
return hearts