refactor(levels): let the player manage itself its score and gold

This commit is contained in:
Kazhnuz 2019-06-17 12:26:35 +02:00
parent fd986f6b30
commit 4f0f6a2ea8
7 changed files with 39 additions and 28 deletions

View File

@ -11,7 +11,7 @@ function Block:draw(dt)
self.scene.assets.tileset["block"]:drawTile(1, self.x, self.y)
end
function Block:breakBlock()
function Block:breakBlock(player)
local x, y = self:getCenter()
local spd = 250
local dist = 0
@ -21,7 +21,7 @@ function Block:breakBlock()
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.scene.assets:playSFX("break")
self.scene.playermanager.score = self.scene.playermanager.score + 10
player:getScore(10)
self:destroy()
end

View File

@ -27,14 +27,14 @@ function Ennemy:draw()
utils.graphics.resetColor()
end
function Ennemy:getDamage(base_damage)
function Ennemy:getDamage(base_damage, player)
local damage = math.max(base_damage - self.armor, 1)
self.hp = math.max(0, self.hp - damage)
self.obj.Numbers(self.world, self.x+8, self.y+8, damage, {1,0,0})
if (self.hp == 0) then
self:destroy()
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

View File

@ -8,11 +8,11 @@ function Coin:new(world, x, y, anim, value)
Coin.super.new(self, world, x, y, anim)
end
function Coin:takeLoot()
function Coin:takeLoot(player)
self.obj.GFX(self.world, self.x+8, self.y+8, "sparkle", 1)
self:destroy()
self.scene.playermanager.gold = self.scene.playermanager.gold + self.value
self.scene.playermanager.score = self.scene.playermanager.score + 10
player:getGold(self.value)
player:getScore(10)
self.scene.assets:playSFX("collectcoin")
end

View File

@ -11,8 +11,8 @@ end
function Loot:takeLoot()
self.obj.GFX(self.world, self.x+8, self.y+8, "sparkle", 1)
self:destroy()
self.scene.playermanager.gold = self.scene.playermanager.gold + 1
self.scene.playermanager.score = self.scene.playermanager.score + 10
player:getGold(1)
player:getScore(10)
self.scene.assets:playSFX("collectcoin")
end

View File

@ -21,10 +21,8 @@ function Player:new(world, x, y, playerID)
}
self.stats = {}
self:getStats(playerID)
self:lifeInit()
self:initStats(playerID)
self:initWeapon()
self:initStats()
self:initHUD()
self.currentWeapon = 1
@ -39,12 +37,14 @@ function Player:getStats(playerID)
self.playerID = playerID
end
function Player:lifeInit()
self.hp = self.stats.maxHP
self.mp = self.stats.maxMP
end
function Player:initStats(playerID)
self:getStats(playerID)
self:resetLife()
self.itemList = {}
self.score = 0
self.gold = 0
function Player:initStats()
self.maxxsp = 16*60
self.maxysp = self.maxxsp
self.topsp = 4.5*60
@ -108,7 +108,7 @@ function Player:collisionResponse(collision)
local other = collision.other
if (other.type == "loot") then
other:takeLoot()
other:takeLoot(self)
end
end
@ -185,7 +185,7 @@ function Player:actionMove(dt)
end
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
function Player:changeWeapon(id)
@ -260,16 +260,29 @@ function Player:drawHUD()
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.manager.gold, 4), 373, 25, 96-18, "right")
love.graphics.printf( utils.math.numberToString(self.score, 6), 373, 10, 96, "right")
love.graphics.printf( utils.math.numberToString(self.gold, 4), 373, 25, 96-18, "right")
love.graphics.setColor(1, 1, 85/256)
love.graphics.printf( "G", 373, 25, 96, "right")
utils.graphics.resetColor()
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
-- All function related to damage management
function Player:resetLife()
self.hp = self.stats.maxHP
self.mp = self.stats.maxMP
end
function Player:takeHit(damage)
if (self.hp <= damage) then
self:die()

View File

@ -2,12 +2,14 @@ local Entity = require "scenes.levels.entities.parent"
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)
self.weaponid = id or 0
self.xsp = xsp or 0
self.ysp = ysp or 0
self.rotation = 0
self.creator = creator
end
function Weapon:setFilter()
@ -45,11 +47,11 @@ function Weapon:collisionResponse(collision)
end
if other.type=="block" then
self:destroy()
other:breakBlock()
other:breakBlock(self.creator)
end
if other.type=="ennemy" then
self:destroy()
other:getDamage(1)
other:getDamage(1, self.creator)
end
end

View File

@ -7,10 +7,6 @@ function PlayerManager:new(scene)
self.deathTimer = -100
self.activePlayer = -1
self.startx, self.starty = self.scene.world:getStartPosition()
self.itemList = {}
self.score = 0
self.gold = 0
end
-- PLAYER FUNCTIONS