refactor(levels): let the player manage itself how it respawn
This commit is contained in:
parent
76a1099e2d
commit
1c29ae0dfa
|
@ -1,15 +1,17 @@
|
||||||
local Entity = require "scenes.levels.entities.parent"
|
local Entity = require "scenes.levels.entities.parent"
|
||||||
|
|
||||||
local Player = Entity:extend()
|
local Player = Entity:extend()
|
||||||
local PLAYER_W, PLAYER_H = 32, 24
|
local PLAYER_W, PLAYER_H = 32, 24
|
||||||
|
local TEMP_PLAYERID = 1
|
||||||
|
|
||||||
-- INIT functions
|
-- INIT functions
|
||||||
-- Initialize the player
|
-- Initialize the player
|
||||||
|
|
||||||
function Player:new(world, x, y, playerID)
|
function Player:new(world, x, y)
|
||||||
self.direction = 1
|
self.direction = 1
|
||||||
|
|
||||||
self.startx, self.starty = x, y
|
self.startx, self.starty = x, y
|
||||||
|
self:setSpawnPoint(x, y)
|
||||||
|
|
||||||
Player.super.new(self, world, "player", x, y, PLAYER_W, PLAYER_H)
|
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.stats = {}
|
||||||
self:initStats(playerID)
|
self:initStats(TEMP_PLAYERID)
|
||||||
self:initWeapon()
|
self:initWeapon()
|
||||||
self:initHUD()
|
self:initHUD()
|
||||||
|
|
||||||
|
@ -100,6 +102,12 @@ function Player:updateEnd(dt)
|
||||||
self:setAnimation()
|
self:setAnimation()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Player:timerResponse(timer)
|
||||||
|
if (timer == "respawn") then
|
||||||
|
self:respawn()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Player:setFilter()
|
function Player:setFilter()
|
||||||
self.filter = function(item, other)
|
self.filter = function(item, other)
|
||||||
if (other.type == "wall") then return 'slide'
|
if (other.type == "wall") then return 'slide'
|
||||||
|
@ -293,6 +301,23 @@ end
|
||||||
-- DEATH and HIT functions
|
-- DEATH and HIT functions
|
||||||
-- All function related to damage management
|
-- 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()
|
function Player:resetLife()
|
||||||
self.hp = self.stats.maxHP
|
self.hp = self.stats.maxHP
|
||||||
self.mp = self.stats.maxMP
|
self.mp = self.stats.maxMP
|
||||||
|
@ -309,8 +334,8 @@ function Player:takeHit(damage)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:die()
|
function Player:die()
|
||||||
self.manager:setDeathTimer(1)
|
self:addTimer("respawn", 1)
|
||||||
self:destroy()
|
self.isAlive = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,6 @@ local Obj = require "scenes.levels.entities"
|
||||||
function PlayerManager:new(scene)
|
function PlayerManager:new(scene)
|
||||||
self.scene = scene
|
self.scene = scene
|
||||||
self.players = {}
|
self.players = {}
|
||||||
self.deathTimer = -100
|
|
||||||
self.activePlayer = -1
|
|
||||||
self.startx, self.starty = self.scene.world:getStartPosition()
|
self.startx, self.starty = self.scene.world:getStartPosition()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,10 +63,6 @@ function PlayerManager:playerExist(id)
|
||||||
return (self.players[id] ~= nil)
|
return (self.players[id] ~= nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
function PlayerManager:setDeathTimer(timer)
|
|
||||||
self.deathTimer = timer
|
|
||||||
end
|
|
||||||
|
|
||||||
function PlayerManager:playerHaveObject(id)
|
function PlayerManager:playerHaveObject(id)
|
||||||
player = self:getPlayerByID(id)
|
player = self:getPlayerByID(id)
|
||||||
|
|
||||||
|
@ -83,12 +77,7 @@ end
|
||||||
-- Update the death timer and respawn the player when it's done
|
-- Update the death timer and respawn the player when it's done
|
||||||
|
|
||||||
function PlayerManager:update(dt)
|
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
|
end
|
||||||
|
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
|
|
|
@ -43,6 +43,13 @@ function World:getStartPosition()
|
||||||
return startx, starty
|
return startx, starty
|
||||||
end
|
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
|
-- MAP FUNCTIONS
|
||||||
-- All map wrappers
|
-- All map wrappers
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue