improvement: refactor subgame Player
This commit is contained in:
parent
1bb9d13e5d
commit
6c7e0caf4b
5 changed files with 131 additions and 91 deletions
|
@ -0,0 +1,13 @@
|
||||||
|
local PlayerActions = Object:extend()
|
||||||
|
|
||||||
|
function PlayerActions:initActions()
|
||||||
|
self.action = "normal"
|
||||||
|
end
|
||||||
|
|
||||||
|
function PlayerActions:doActions()
|
||||||
|
if self.keys["B"].isPressed and (self.onGround) then
|
||||||
|
-- Nothing for the moment
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return PlayerActions
|
|
@ -2,57 +2,27 @@ local cwd = (...):gsub('%.player$', '') .. "."
|
||||||
local Parent = require(cwd .. "parent")
|
local Parent = require(cwd .. "parent")
|
||||||
local Player = Parent:extend()
|
local Player = Parent:extend()
|
||||||
|
|
||||||
|
local Actions = require "game.modules.subgames.world.actors.player.actions"
|
||||||
|
local Movements = require "game.modules.subgames.world.actors.player.movements"
|
||||||
|
local Score = require "game.modules.subgames.world.actors.player.score"
|
||||||
|
local Sprites = require "game.modules.subgames.world.actors.player.sprites"
|
||||||
|
|
||||||
|
Player:implement(Actions)
|
||||||
|
Player:implement(Movements)
|
||||||
|
Player:implement(Score)
|
||||||
|
Player:implement(Sprites)
|
||||||
|
|
||||||
function Player:new(world, x, y, z, id)
|
function Player:new(world, x, y, z, id)
|
||||||
Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true)
|
Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true)
|
||||||
self:setGravity(480*2)
|
self:initMovements()
|
||||||
|
self:initCharactersSprites()
|
||||||
self:initPlayer()
|
self:initScore()
|
||||||
|
self:initActions()
|
||||||
self.action = "normal"
|
|
||||||
|
|
||||||
self.rings = 0
|
|
||||||
self.score = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
function Player:initPlayer()
|
|
||||||
self.charName = game.characters:getActiveCharacter()
|
|
||||||
self.assets:addSprite(self.charName, "datas/gamedata/characters/" .. self.charName .. "/sprites")
|
|
||||||
self:setSprite(self.charName, true, 8, 10)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:updateStart(dt)
|
function Player:updateStart(dt)
|
||||||
self.xfrc, self.yfrc = 480*3, 480*3
|
|
||||||
|
|
||||||
self:basicMovements()
|
self:basicMovements()
|
||||||
|
self:doActions()
|
||||||
if self.keys["A"].isPressed and (self.onGround) then
|
|
||||||
self.zsp = 280*1.33
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.keys["B"].isPressed and (self.onGround) then
|
|
||||||
-- Nothing for the moment
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Player:basicMovements()
|
|
||||||
|
|
||||||
if self.keys["up"].isDown then
|
|
||||||
self.ysp = -160
|
|
||||||
end
|
|
||||||
if self.keys["down"].isDown then
|
|
||||||
self.ysp = 160
|
|
||||||
end
|
|
||||||
if (self.world.autorun == true) then
|
|
||||||
self.xsp = 160
|
|
||||||
else
|
|
||||||
if self.keys["left"].isDown then
|
|
||||||
self.xsp = -160
|
|
||||||
end
|
|
||||||
if self.keys["right"].isDown then
|
|
||||||
self.xsp = 160
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:collisionResponse(collision)
|
function Player:collisionResponse(collision)
|
||||||
|
@ -69,38 +39,6 @@ function Player:updateEnd(dt)
|
||||||
self:setAnimation()
|
self:setAnimation()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:setAnimation()
|
|
||||||
local gsp = utils.math.pointDistance(0, 0, self.xsp, self.ysp)
|
|
||||||
self:setCustomSpeed(math.abs(gsp) / 12)
|
|
||||||
self:setDirection(self.xsp)
|
|
||||||
if (self.action == "punching") then
|
|
||||||
--the animation system is already active
|
|
||||||
else
|
|
||||||
if (self.onGround) then
|
|
||||||
if (math.abs(self.xsp) > 0) or (math.abs(self.ysp) > 0) then
|
|
||||||
self:changeAnimation("walk", false)
|
|
||||||
else
|
|
||||||
self:changeAnimation("idle", false)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if (self.zsp) > 0 then
|
|
||||||
self:changeAnimation("jump", false)
|
|
||||||
else
|
|
||||||
self:changeAnimation("fall", false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Player:setDirection(direction)
|
|
||||||
direction = direction or 0
|
|
||||||
if direction ~= 0 then
|
|
||||||
direction = utils.math.sign(direction)
|
|
||||||
self.direction = direction
|
|
||||||
self.sprite:setScallingX(direction)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Player:getViewCenter()
|
function Player:getViewCenter()
|
||||||
local x, y = Player.super.getViewCenter(self)
|
local x, y = Player.super.getViewCenter(self)
|
||||||
return x, y-16
|
return x, y-16
|
||||||
|
@ -110,18 +48,4 @@ function Player:draw()
|
||||||
Player.super.draw(self)
|
Player.super.draw(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:setRing(value, isRelative)
|
|
||||||
if (isRelative == false) then
|
|
||||||
self.rings = 0
|
|
||||||
end
|
|
||||||
self.rings = self.rings + value
|
|
||||||
end
|
|
||||||
|
|
||||||
function Player:setScore(value, isRelative)
|
|
||||||
if (isRelative == false) then
|
|
||||||
self.score = 0
|
|
||||||
end
|
|
||||||
self.score = self.score + value
|
|
||||||
end
|
|
||||||
|
|
||||||
return Player
|
return Player
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
local PlayerMovement = Object:extend()
|
||||||
|
|
||||||
|
function PlayerMovement:initMovements()
|
||||||
|
self:setGravity(480 * 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
function PlayerMovement:basicMovements()
|
||||||
|
self:setFrc()
|
||||||
|
|
||||||
|
if self.keys["up"].isDown then
|
||||||
|
self.ysp = -160
|
||||||
|
end
|
||||||
|
if self.keys["down"].isDown then
|
||||||
|
self.ysp = 160
|
||||||
|
end
|
||||||
|
if (self.world.autorun == true) then
|
||||||
|
self.xsp = 160
|
||||||
|
else
|
||||||
|
if self.keys["left"].isDown then
|
||||||
|
self.xsp = -160
|
||||||
|
end
|
||||||
|
if self.keys["right"].isDown then
|
||||||
|
self.xsp = 160
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self:jump()
|
||||||
|
end
|
||||||
|
|
||||||
|
function PlayerMovement:setFrc()
|
||||||
|
self.xfrc, self.yfrc = 480 * 3, 480 * 3
|
||||||
|
end
|
||||||
|
|
||||||
|
function PlayerMovement:jump()
|
||||||
|
if self.keys["A"].isPressed and (self.onGround) then
|
||||||
|
self.zsp = 280 * 1.33
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return PlayerMovement
|
|
@ -0,0 +1,22 @@
|
||||||
|
local PlayerScore = Object:extend()
|
||||||
|
|
||||||
|
function PlayerScore:initScore()
|
||||||
|
self.rings = 0
|
||||||
|
self.score = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function PlayerScore:setRing(value, isRelative)
|
||||||
|
if (isRelative == false) then
|
||||||
|
self.rings = 0
|
||||||
|
end
|
||||||
|
self.rings = self.rings + value
|
||||||
|
end
|
||||||
|
|
||||||
|
function PlayerScore:setScore(value, isRelative)
|
||||||
|
if (isRelative == false) then
|
||||||
|
self.score = 0
|
||||||
|
end
|
||||||
|
self.score = self.score + value
|
||||||
|
end
|
||||||
|
|
||||||
|
return PlayerScore
|
|
@ -0,0 +1,41 @@
|
||||||
|
local SpritedPlayer = Object:extend()
|
||||||
|
|
||||||
|
function SpritedPlayer:initCharactersSprites()
|
||||||
|
self.charName = game.characters:getActiveCharacter()
|
||||||
|
self.assets:addSprite(self.charName, "datas/gamedata/characters/" .. self.charName .. "/sprites")
|
||||||
|
self:setSprite(self.charName, true, 8, 10)
|
||||||
|
end
|
||||||
|
|
||||||
|
function SpritedPlayer:setAnimation()
|
||||||
|
local gsp = utils.math.pointDistance(0, 0, self.xsp, self.ysp)
|
||||||
|
self.sprite:setCustomSpeed(math.abs(gsp) / 12)
|
||||||
|
self:setDirection(self.xsp)
|
||||||
|
if (self.action == "punching") then
|
||||||
|
--the animation system is already active
|
||||||
|
else
|
||||||
|
if (self.onGround) then
|
||||||
|
if (math.abs(self.xsp) > 0) or (math.abs(self.ysp) > 0) then
|
||||||
|
self.sprite:changeAnimation("walk", false)
|
||||||
|
else
|
||||||
|
self.sprite:changeAnimation("idle", false)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if (self.zsp) > 0 then
|
||||||
|
self.sprite:changeAnimation("jump", false)
|
||||||
|
else
|
||||||
|
self.sprite:changeAnimation("fall", false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function SpritedPlayer:setDirection(direction)
|
||||||
|
direction = direction or 0
|
||||||
|
if direction ~= 0 then
|
||||||
|
direction = utils.math.sign(direction)
|
||||||
|
self.direction = direction
|
||||||
|
self.sprite:setScallingX(direction)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return SpritedPlayer
|
Loading…
Reference in a new issue