scenes/levels: fuse all player functions in one file

This commit is contained in:
Kazhnuz 2019-03-03 11:46:07 +01:00
parent 2f08fd25b8
commit 0de079fe61
9 changed files with 302 additions and 290 deletions

View File

@ -1,59 +0,0 @@
function Player:shoot()
if self.gamepad.buttons["B"].press then
self:launchWeapon()
end
if self.gamepad.buttons["C"].press then
self:changeWeapon(self.weaponID+1)
end
end
function Player:actionMove(dt)
local dx, dy = 0, 0
if self.gamepad.buttons["right"].down and not (self.gamepad.buttons["left"].down) then
if (self.xsp < self.topsp) then
if (self.xsp < 0) then
self.xsp = self.xsp + self.dec
else
self.xsp = self.xsp + self.acc
end
end
elseif self.gamepad.buttons["left"].down and not (self.gamepad.buttons["right"].down) then
if (self.xsp > -self.topsp) then
if (self.xsp > 0) then
self.xsp = self.xsp - self.dec
else
self.xsp = self.xsp - self.acc
end
end
else
self:friction(dt)
end
if (self.grav == 0) then
if self.gamepad.buttons["down"].down and (self.y < 1000) then
self.ysp = 160
elseif self.gamepad.buttons["up"].down and (self.y > 0) then
self.ysp = -160
end
end
if (self.onGround == true) then
self.isJumping = false
end
multijump = true
if ((self.onGround == true) or multijump) and self.gamepad.buttons["A"].press then
self.ysp = self.jmp
self.isJumping = true
assets:playSFX("jump")
assets.sprites["cochon"]:resetAnimation(self.animations["jump"])
end
if (self.isJumping == true) and (self.ysp < (-4 * 60)) and (not (self.gamepad.buttons["A"].down)) then
self.ysp = -4 * 60
end
return dx, dy
end

View File

@ -1,22 +0,0 @@
function Player:resolveCollisions(cols, cols_len)
local cols, cols_len = cols, cols_len
for j=1,cols_len do
local other = cols[j].other
if (other.collType == "loot") then
other:takeLoot()
end
end
end
function Player:setFilter()
self.filter = function(item, other)
if (other.collType == "wall") then return 'slide'
elseif (other.collType == "block") then return 'slide'
elseif (other.collType == "platform") and
(self.ysp > 0) and
((self.y+24) <= other.y) then return 'slide'
elseif (other.collType == "loot") then return 'cross'
else return nil
end
end
end

View File

@ -1,44 +0,0 @@
function Player:initAnimations()
self.animations = {}
self.animations["idle"] = 1
self.animations["walk"] = 2
self.animations["brake"] = 3
self.animations["jump"] = 4
self.animations["fall"] = 5
end
function Player:getAnimation()
local animation = 1
if (self.onGround) then
if (self.xsp == 0) then
animation = "idle"
else
if (self.gamepad.buttons["left"].down and (self.xsp > 0)) or
(self.gamepad.buttons["right"].down and (self.xsp < 0)) then
animation = "brake"
else
animation = "walk"
end
end
else
if (self.ysp < 0) and (self.isJumping) then
animation = "jump"
else
animation = "fall"
end
end
if (self.animations[animation] == nil) then
animation = "idle"
end
return self.animations[animation]
end
function Player:draw(dt)
local drawx, drawy = utils.math.floorCoord(self.center.x, self.center.y)
local animation = self:getAnimation()
--if (self.direction == -1) then drawx = drawx + self.w end
assets.sprites[self.stats.race]:draw(animation, drawx, drawy, 0, self.direction, 1, 16, 36)
end

View File

@ -1,13 +1,6 @@
Player = Entity:extend()
require "scenes.levels.entities.player.draw"
require "scenes.levels.entities.player.actions"
require "scenes.levels.entities.player.collisions"
require "scenes.levels.entities.player.update"
require "scenes.levels.entities.player.pad"
require "scenes.levels.entities.player.movement"
require "scenes.levels.entities.player.life"
require "scenes.levels.entities.player.weapon"
-- Initialisation functions
function Player:new(level, playerID, x, y)
@ -46,4 +39,305 @@ function Player:getStats(playerID)
self.playerID = playerID
end
function Player:lifeInit()
self.hp = self.stats.maxHP
self.mp = self.stats.maxMP
end
function Player:initMovement()
self.maxxsp = 16*60
self.maxysp = self.maxxsp
self.topsp = 4.5*60
self.acc = 0.046875*60*1.5
self.dec = 0.5*60
self.frc = self.acc
self.grav = 1
self.jmp = - 6*60
self.isJumping = false
end
function Player:initWeapon()
self.weaponID = 1
self:setWeapon()
end
function Player:initAnimations()
self.animations = {}
self.animations["idle"] = 1
self.animations["walk"] = 2
self.animations["brake"] = 3
self.animations["jump"] = 4
self.animations["fall"] = 5
end
function Player:padInit()
self:padReset()
self:padAddTouch("up")
self:padAddTouch("left")
self:padAddTouch("right")
self:padAddTouch("down")
self:padAddTouch("A")
self:padAddTouch("B")
self:padAddTouch("C")
self:padAddAnalogue()
self:padAddTouch("select")
end
function Player:padReset()
self.gamepad = {}
self.gamepad.buttons = {}
self.gamepad.analogs = {}
end
function Player:padAddTouch(touchname)
self.gamepad.buttons[touchname] = {}
self.gamepad.buttons[touchname].down = false
self.gamepad.buttons[touchname].rel = false
self.gamepad.buttons[touchname].press = false
end
function Player:padAddAnalogue()
local id = #self.gamepad.analogs + 1
self.gamepad.analogs[id] = {}
self.gamepad.analogs[id].angle = 0
self.gamepad.analogs[id].strenght = 0
end
-- UPDATE and COLLISION functions
function Player:update(dt)
self:actionMove(dt)
self:shoot(dt)
self:gravity(dt)
self:limitMovement()
self:setFilter()
local cols, cols_len = self:move(dt)
self:resolveCollisions(cols, cols_len)
self.center.x, self.center.y = self:getCenter()
self:getDirection()
if self.y >= ((self.level.map.height * self.level.map.tileheight) + 64) then
self:die()
end
end
function Player:setFilter()
self.filter = function(item, other)
if (other.collType == "wall") then return 'slide'
elseif (other.collType == "block") then return 'slide'
elseif (other.collType == "platform") and
(self.ysp > 0) and
((self.y+24) <= other.y) then return 'slide'
elseif (other.collType == "loot") then return 'cross'
else return nil
end
end
end
function Player:resolveCollisions(cols, cols_len)
local cols, cols_len = cols, cols_len
for j=1,cols_len do
local other = cols[j].other
if (other.collType == "loot") then
other:takeLoot()
end
end
end
function Player:limitMovement()
if (math.abs(self.xsp) >= self.maxxsp) then
self.xsp = self.maxxsp * math.sign(self.xsp)
end
if (math.abs(self.ysp) >= self.maxysp) then
self.ysp = self.maxysp * math.sign(self.ysp)
end
end
-- GAMEPLAY functions
function Player:padPush(touchname, value)
if (value == true) then
-- Si le bouton est enfoncé, alors on ne peut pas l'avoir relaché
self.gamepad.buttons[touchname].rel = false
if (self.gamepad.buttons[touchname].down == true) then
-- Si le bouton était déjà pressé au début, alors on indique
-- qu'il ne vient pas d'être pressé.
self.gamepad.buttons[touchname].down = true
self.gamepad.buttons[touchname].press = false
else
-- Sinon, on indique qu'il viens d'être pressé, et
-- qu'il est enfoncé
self.gamepad.buttons[touchname].down = true
self.gamepad.buttons[touchname].press = true
end
else
-- Si le bouton est relaché, alors on ne peut pas l'avoir pressé
self.gamepad.buttons[touchname].press = false
if (self.gamepad.buttons[touchname].down == true) then
-- Si le bouton était déjà pressé au début, alors on indique
-- qu'il vient d'être relaché, et qu'il n'est plus enfoncé
self.gamepad.buttons[touchname].down = false
self.gamepad.buttons[touchname].rel = true
else
-- Sinon, on indique qu'il ne viens plus d'être relaché, et
-- qu'il n'est pas enfoncé, au cas ou.
self.gamepad.buttons[touchname].down = false
self.gamepad.buttons[touchname].rel = false
end
end
end
function Player:shoot()
if self.gamepad.buttons["B"].press then
self:launchWeapon()
end
if self.gamepad.buttons["C"].press then
self:changeWeapon(self.weaponID+1)
end
end
function Player:actionMove(dt)
local dx, dy = 0, 0
if self.gamepad.buttons["right"].down and not (self.gamepad.buttons["left"].down) then
if (self.xsp < self.topsp) then
if (self.xsp < 0) then
self.xsp = self.xsp + self.dec
else
self.xsp = self.xsp + self.acc
end
end
elseif self.gamepad.buttons["left"].down and not (self.gamepad.buttons["right"].down) then
if (self.xsp > -self.topsp) then
if (self.xsp > 0) then
self.xsp = self.xsp - self.dec
else
self.xsp = self.xsp - self.acc
end
end
else
self:friction(dt)
end
if (self.grav == 0) then
if self.gamepad.buttons["down"].down and (self.y < 1000) then
self.ysp = 160
elseif self.gamepad.buttons["up"].down and (self.y > 0) then
self.ysp = -160
end
end
if (self.onGround == true) then
self.isJumping = false
end
multijump = true
if ((self.onGround == true) or multijump) and self.gamepad.buttons["A"].press then
self.ysp = self.jmp
self.isJumping = true
assets:playSFX("jump")
assets.sprites["cochon"]:resetAnimation(self.animations["jump"])
end
if (self.isJumping == true) and (self.ysp < (-4 * 60)) and (not (self.gamepad.buttons["A"].down)) then
self.ysp = -4 * 60
end
return dx, dy
end
function Player:launchWeapon()
Weapon(self.level, self.weapon, self.center.x, self.center.y, 350 * utils.math.sign(self.direction))
end
function Player:changeWeapon(id)
local maxWeapon = #self.stats.weaponList
self.weaponID = wrap(id, maxWeapon)
self:setWeapon()
end
function Player:setWeapon()
self.weapon = self.stats.weaponList[self.weaponID]
end
-- DRAW functions
function Player:getAnimation()
local animation = 1
if (self.onGround) then
if (self.xsp == 0) then
animation = "idle"
else
if (self.gamepad.buttons["left"].down and (self.xsp > 0)) or
(self.gamepad.buttons["right"].down and (self.xsp < 0)) then
animation = "brake"
else
animation = "walk"
end
end
else
if (self.ysp < 0) and (self.isJumping) then
animation = "jump"
else
animation = "fall"
end
end
if (self.animations[animation] == nil) then
animation = "idle"
end
return self.animations[animation]
end
function Player:draw(dt)
local drawx, drawy = utils.math.floorCoord(self.center.x, self.center.y)
local animation = self:getAnimation()
--if (self.direction == -1) then drawx = drawx + self.w end
assets.sprites[self.stats.race]:draw(animation, drawx, drawy, 0, self.direction, 1, 16, 36)
end
-- Death and hit functions
function Player:takeHit(damage)
if (self.hp <= damage) then
self:die()
else
self.hp = self.hp - damage
end
end
function Player:die()
--self.hp = self.stats.maxHP
--self.x = self.startx-16
--self.y = self.starty-12
--self.xsp = 0
--self.ysp = 0
--currentLevel.world:update(self, self.x,self.y)
self.level:setDeathTimer(1)
self:destroy()
end
return Player

View File

@ -1,23 +0,0 @@
function Player:lifeInit()
self.hp = self.stats.maxHP
self.mp = self.stats.maxMP
end
function Player:takeHit(damage)
if (self.hp <= damage) then
self:die()
else
self.hp = self.hp - damage
end
end
function Player:die()
--self.hp = self.stats.maxHP
--self.x = self.startx-16
--self.y = self.starty-12
--self.xsp = 0
--self.ysp = 0
--currentLevel.world:update(self, self.x,self.y)
self.level:setDeathTimer(1)
self:destroy()
end

View File

@ -1,22 +0,0 @@
function Player:initMovement()
self.maxxsp = 16*60
self.maxysp = self.maxxsp
self.topsp = 4.5*60
self.acc = 0.046875*60*1.5
self.dec = 0.5*60
self.frc = self.acc
self.grav = 1
self.jmp = - 6*60
self.isJumping = false
end
function Player:limitMovement()
if (math.abs(self.xsp) >= self.maxxsp) then
self.xsp = self.maxxsp * math.sign(self.xsp)
end
if (math.abs(self.ysp) >= self.maxysp) then
self.ysp = self.maxysp * math.sign(self.ysp)
end
end

View File

@ -1,74 +0,0 @@
function Player:padInit()
self:padReset()
self:padAddTouch("up")
self:padAddTouch("left")
self:padAddTouch("right")
self:padAddTouch("down")
self:padAddTouch("A")
self:padAddTouch("B")
self:padAddTouch("C")
self:padAddAnalogue()
self:padAddTouch("select")
end
function Player:padReset()
self.gamepad = {}
self.gamepad.buttons = {}
self.gamepad.analogs = {}
end
function Player:padAddTouch(touchname)
self.gamepad.buttons[touchname] = {}
self.gamepad.buttons[touchname].down = false
self.gamepad.buttons[touchname].rel = false
self.gamepad.buttons[touchname].press = false
end
function Player:padAddAnalogue()
local id = #self.gamepad.analogs + 1
self.gamepad.analogs[id] = {}
self.gamepad.analogs[id].angle = 0
self.gamepad.analogs[id].strenght = 0
end
function Player:padPush(touchname, value)
if (value == true) then
-- Si le bouton est enfoncé, alors on ne peut pas l'avoir relaché
self.gamepad.buttons[touchname].rel = false
if (self.gamepad.buttons[touchname].down == true) then
-- Si le bouton était déjà pressé au début, alors on indique
-- qu'il ne vient pas d'être pressé.
self.gamepad.buttons[touchname].down = true
self.gamepad.buttons[touchname].press = false
else
-- Sinon, on indique qu'il viens d'être pressé, et
-- qu'il est enfoncé
self.gamepad.buttons[touchname].down = true
self.gamepad.buttons[touchname].press = true
end
else
-- Si le bouton est relaché, alors on ne peut pas l'avoir pressé
self.gamepad.buttons[touchname].press = false
if (self.gamepad.buttons[touchname].down == true) then
-- Si le bouton était déjà pressé au début, alors on indique
-- qu'il vient d'être relaché, et qu'il n'est plus enfoncé
self.gamepad.buttons[touchname].down = false
self.gamepad.buttons[touchname].rel = true
else
-- Sinon, on indique qu'il ne viens plus d'être relaché, et
-- qu'il n'est pas enfoncé, au cas ou.
self.gamepad.buttons[touchname].down = false
self.gamepad.buttons[touchname].rel = false
end
end
end

View File

@ -1,20 +0,0 @@
function Player:update(dt)
self:actionMove(dt)
self:shoot(dt)
self:gravity(dt)
self:limitMovement()
self:setFilter()
local cols, cols_len = self:move(dt)
self:resolveCollisions(cols, cols_len)
self.center.x, self.center.y = self:getCenter()
self:getDirection()
if self.y >= ((self.level.map.height * self.level.map.tileheight) + 64) then
self:die()
end
end

View File

@ -1,18 +0,0 @@
function Player:initWeapon()
self.weaponID = 1
self:setWeapon()
end
function Player:launchWeapon()
Weapon(self.level, self.weapon, self.center.x, self.center.y, 350 * utils.math.sign(self.direction))
end
function Player:changeWeapon(id)
local maxWeapon = #self.stats.weaponList
self.weaponID = wrap(id, maxWeapon)
self:setWeapon()
end
function Player:setWeapon()
self.weapon = self.stats.weaponList[self.weaponID]
end