refactor(levels): let the player manage itself how it respawn

This commit is contained in:
Kazhnuz 2019-06-17 13:01:04 +02:00
parent 76a1099e2d
commit 1c29ae0dfa
3 changed files with 38 additions and 17 deletions

View File

@ -1,15 +1,17 @@
local Entity = require "scenes.levels.entities.parent"
local Player = Entity:extend()
local PLAYER_W, PLAYER_H = 32, 24
local PLAYER_W, PLAYER_H = 32, 24
local TEMP_PLAYERID = 1
-- INIT functions
-- Initialize the player
function Player:new(world, x, y, playerID)
function Player:new(world, x, y)
self.direction = 1
self.startx, self.starty = x, y
self:setSpawnPoint(x, y)
Player.super.new(self, world, "player", x, y, PLAYER_W, PLAYER_H)
@ -21,7 +23,7 @@ function Player:new(world, x, y, playerID)
}
self.stats = {}
self:initStats(playerID)
self:initStats(TEMP_PLAYERID)
self:initWeapon()
self:initHUD()
@ -100,6 +102,12 @@ function Player:updateEnd(dt)
self:setAnimation()
end
function Player:timerResponse(timer)
if (timer == "respawn") then
self:respawn()
end
end
function Player:setFilter()
self.filter = function(item, other)
if (other.type == "wall") then return 'slide'
@ -293,6 +301,23 @@ end
-- DEATH and HIT functions
-- All function related to damage management
function Player:setSpawnPoint(x, y)
self.spawn = {}
self.spawn.x = x
self.spawn.y = y
end
function Player:goToSpawnPoint( )
self.x = self.spawn.x
self.y = self.spawn.y
self.world:changeActorData(self)
end
function Player:respawn()
self.isAlive = true
self:goToSpawnPoint( )
end
function Player:resetLife()
self.hp = self.stats.maxHP
self.mp = self.stats.maxMP
@ -309,8 +334,8 @@ function Player:takeHit(damage)
end
function Player:die()
self.manager:setDeathTimer(1)
self:destroy()
self:addTimer("respawn", 1)
self.isAlive = false
end

View File

@ -4,8 +4,6 @@ local Obj = require "scenes.levels.entities"
function PlayerManager:new(scene)
self.scene = scene
self.players = {}
self.deathTimer = -100
self.activePlayer = -1
self.startx, self.starty = self.scene.world:getStartPosition()
end
@ -65,10 +63,6 @@ function PlayerManager:playerExist(id)
return (self.players[id] ~= nil)
end
function PlayerManager:setDeathTimer(timer)
self.deathTimer = timer
end
function PlayerManager:playerHaveObject(id)
player = self:getPlayerByID(id)
@ -83,12 +77,7 @@ end
-- Update the death timer and respawn the player when it's done
function PlayerManager:update(dt)
if (self.deathTimer > 0) then
self.deathTimer = self.deathTimer - dt
elseif (self.deathTimer > -100) then
self:spawnPlayer(self.activePlayer)
self.deathTimer = -100
end
end
-- DRAW FUNCTIONS

View File

@ -43,6 +43,13 @@ function World:getStartPosition()
return startx, starty
end
-- ACTORS FUNCTIONS
-- Some Bump2D Wrapper not handled by gamecore
function World:changeActorData(actor)
self.actors:update(actor, actor.x, actor.y, actor.w, actor.h)
end
-- MAP FUNCTIONS
-- All map wrappers