refactor(levels): let the player manage itself its score and gold
This commit is contained in:
parent
fd986f6b30
commit
4f0f6a2ea8
|
@ -11,7 +11,7 @@ function Block:draw(dt)
|
||||||
self.scene.assets.tileset["block"]:drawTile(1, self.x, self.y)
|
self.scene.assets.tileset["block"]:drawTile(1, self.x, self.y)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Block:breakBlock()
|
function Block:breakBlock(player)
|
||||||
local x, y = self:getCenter()
|
local x, y = self:getCenter()
|
||||||
local spd = 250
|
local spd = 250
|
||||||
local dist = 0
|
local dist = 0
|
||||||
|
@ -21,7 +21,7 @@ function Block:breakBlock()
|
||||||
self.obj.Debris(self.world, x-dist, y-dist, spd, 180-45)
|
self.obj.Debris(self.world, x-dist, y-dist, spd, 180-45)
|
||||||
self.obj.GFX(self.world, self.x+8, self.y+8, "poof", 1)
|
self.obj.GFX(self.world, self.x+8, self.y+8, "poof", 1)
|
||||||
self.scene.assets:playSFX("break")
|
self.scene.assets:playSFX("break")
|
||||||
self.scene.playermanager.score = self.scene.playermanager.score + 10
|
player:getScore(10)
|
||||||
self:destroy()
|
self:destroy()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -27,14 +27,14 @@ function Ennemy:draw()
|
||||||
utils.graphics.resetColor()
|
utils.graphics.resetColor()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Ennemy:getDamage(base_damage)
|
function Ennemy:getDamage(base_damage, player)
|
||||||
local damage = math.max(base_damage - self.armor, 1)
|
local damage = math.max(base_damage - self.armor, 1)
|
||||||
self.hp = math.max(0, self.hp - damage)
|
self.hp = math.max(0, self.hp - damage)
|
||||||
self.obj.Numbers(self.world, self.x+8, self.y+8, damage, {1,0,0})
|
self.obj.Numbers(self.world, self.x+8, self.y+8, damage, {1,0,0})
|
||||||
if (self.hp == 0) then
|
if (self.hp == 0) then
|
||||||
self:destroy()
|
self:destroy()
|
||||||
self.obj.GFX(self.world, self.x+8, self.y+8, "poof", 1)
|
self.obj.GFX(self.world, self.x+8, self.y+8, "poof", 1)
|
||||||
self.scene.playermanager.score = self.scene.playermanager.score + 100
|
player:getScore(100)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,11 @@ function Coin:new(world, x, y, anim, value)
|
||||||
Coin.super.new(self, world, x, y, anim)
|
Coin.super.new(self, world, x, y, anim)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Coin:takeLoot()
|
function Coin:takeLoot(player)
|
||||||
self.obj.GFX(self.world, self.x+8, self.y+8, "sparkle", 1)
|
self.obj.GFX(self.world, self.x+8, self.y+8, "sparkle", 1)
|
||||||
self:destroy()
|
self:destroy()
|
||||||
self.scene.playermanager.gold = self.scene.playermanager.gold + self.value
|
player:getGold(self.value)
|
||||||
self.scene.playermanager.score = self.scene.playermanager.score + 10
|
player:getScore(10)
|
||||||
self.scene.assets:playSFX("collectcoin")
|
self.scene.assets:playSFX("collectcoin")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ end
|
||||||
function Loot:takeLoot()
|
function Loot:takeLoot()
|
||||||
self.obj.GFX(self.world, self.x+8, self.y+8, "sparkle", 1)
|
self.obj.GFX(self.world, self.x+8, self.y+8, "sparkle", 1)
|
||||||
self:destroy()
|
self:destroy()
|
||||||
self.scene.playermanager.gold = self.scene.playermanager.gold + 1
|
player:getGold(1)
|
||||||
self.scene.playermanager.score = self.scene.playermanager.score + 10
|
player:getScore(10)
|
||||||
self.scene.assets:playSFX("collectcoin")
|
self.scene.assets:playSFX("collectcoin")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,8 @@ function Player:new(world, x, y, playerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.stats = {}
|
self.stats = {}
|
||||||
self:getStats(playerID)
|
self:initStats(playerID)
|
||||||
self:lifeInit()
|
|
||||||
self:initWeapon()
|
self:initWeapon()
|
||||||
self:initStats()
|
|
||||||
self:initHUD()
|
self:initHUD()
|
||||||
|
|
||||||
self.currentWeapon = 1
|
self.currentWeapon = 1
|
||||||
|
@ -39,12 +37,14 @@ function Player:getStats(playerID)
|
||||||
self.playerID = playerID
|
self.playerID = playerID
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:lifeInit()
|
function Player:initStats(playerID)
|
||||||
self.hp = self.stats.maxHP
|
self:getStats(playerID)
|
||||||
self.mp = self.stats.maxMP
|
self:resetLife()
|
||||||
end
|
|
||||||
|
self.itemList = {}
|
||||||
|
self.score = 0
|
||||||
|
self.gold = 0
|
||||||
|
|
||||||
function Player:initStats()
|
|
||||||
self.maxxsp = 16*60
|
self.maxxsp = 16*60
|
||||||
self.maxysp = self.maxxsp
|
self.maxysp = self.maxxsp
|
||||||
self.topsp = 4.5*60
|
self.topsp = 4.5*60
|
||||||
|
@ -108,7 +108,7 @@ function Player:collisionResponse(collision)
|
||||||
local other = collision.other
|
local other = collision.other
|
||||||
|
|
||||||
if (other.type == "loot") then
|
if (other.type == "loot") then
|
||||||
other:takeLoot()
|
other:takeLoot(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -185,7 +185,7 @@ function Player:actionMove(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:launchWeapon()
|
function Player:launchWeapon()
|
||||||
self.obj.Weapon(self.world, self.center.x, self.center.y, self.weapon, 350 * utils.math.sign(self.direction))
|
self.obj.Weapon(self.world, self.center.x, self.center.y, self.weapon, 350 * utils.math.sign(self.direction), 0, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:changeWeapon(id)
|
function Player:changeWeapon(id)
|
||||||
|
@ -260,16 +260,29 @@ function Player:drawHUD()
|
||||||
|
|
||||||
self.scene.assets.fonts["medium"]:set()
|
self.scene.assets.fonts["medium"]:set()
|
||||||
|
|
||||||
love.graphics.printf( utils.math.numberToString(self.manager.score, 6), 373, 10, 96, "right")
|
love.graphics.printf( utils.math.numberToString(self.score, 6), 373, 10, 96, "right")
|
||||||
love.graphics.printf( utils.math.numberToString(self.manager.gold, 4), 373, 25, 96-18, "right")
|
love.graphics.printf( utils.math.numberToString(self.gold, 4), 373, 25, 96-18, "right")
|
||||||
love.graphics.setColor(1, 1, 85/256)
|
love.graphics.setColor(1, 1, 85/256)
|
||||||
love.graphics.printf( "G", 373, 25, 96, "right")
|
love.graphics.printf( "G", 373, 25, 96, "right")
|
||||||
utils.graphics.resetColor()
|
utils.graphics.resetColor()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Player:getScore(score)
|
||||||
|
self.score = self.score + score
|
||||||
|
end
|
||||||
|
|
||||||
|
function Player:getGold(gold)
|
||||||
|
self.gold = self.gold + gold
|
||||||
|
end
|
||||||
|
|
||||||
-- DEATH and HIT functions
|
-- DEATH and HIT functions
|
||||||
-- All function related to damage management
|
-- All function related to damage management
|
||||||
|
|
||||||
|
function Player:resetLife()
|
||||||
|
self.hp = self.stats.maxHP
|
||||||
|
self.mp = self.stats.maxMP
|
||||||
|
end
|
||||||
|
|
||||||
function Player:takeHit(damage)
|
function Player:takeHit(damage)
|
||||||
if (self.hp <= damage) then
|
if (self.hp <= damage) then
|
||||||
self:die()
|
self:die()
|
||||||
|
|
|
@ -2,12 +2,14 @@ local Entity = require "scenes.levels.entities.parent"
|
||||||
|
|
||||||
local Weapon = Entity:extend()
|
local Weapon = Entity:extend()
|
||||||
|
|
||||||
function Weapon:new(world, x, y, id, xsp, ysp)
|
function Weapon:new(world, x, y, id, xsp, ysp, creator)
|
||||||
Weapon.super.new(self, world, "weapon", x-8, y - 8, 16, 16)
|
Weapon.super.new(self, world, "weapon", x-8, y - 8, 16, 16)
|
||||||
self.weaponid = id or 0
|
self.weaponid = id or 0
|
||||||
self.xsp = xsp or 0
|
self.xsp = xsp or 0
|
||||||
self.ysp = ysp or 0
|
self.ysp = ysp or 0
|
||||||
self.rotation = 0
|
self.rotation = 0
|
||||||
|
|
||||||
|
self.creator = creator
|
||||||
end
|
end
|
||||||
|
|
||||||
function Weapon:setFilter()
|
function Weapon:setFilter()
|
||||||
|
@ -45,11 +47,11 @@ function Weapon:collisionResponse(collision)
|
||||||
end
|
end
|
||||||
if other.type=="block" then
|
if other.type=="block" then
|
||||||
self:destroy()
|
self:destroy()
|
||||||
other:breakBlock()
|
other:breakBlock(self.creator)
|
||||||
end
|
end
|
||||||
if other.type=="ennemy" then
|
if other.type=="ennemy" then
|
||||||
self:destroy()
|
self:destroy()
|
||||||
other:getDamage(1)
|
other:getDamage(1, self.creator)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,6 @@ function PlayerManager:new(scene)
|
||||||
self.deathTimer = -100
|
self.deathTimer = -100
|
||||||
self.activePlayer = -1
|
self.activePlayer = -1
|
||||||
self.startx, self.starty = self.scene.world:getStartPosition()
|
self.startx, self.starty = self.scene.world:getStartPosition()
|
||||||
|
|
||||||
self.itemList = {}
|
|
||||||
self.score = 0
|
|
||||||
self.gold = 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- PLAYER FUNCTIONS
|
-- PLAYER FUNCTIONS
|
||||||
|
|
Loading…
Reference in New Issue