chore: refactor into mixins the player actor

This commit is contained in:
Kazhnuz 2021-04-10 17:14:41 +02:00
parent 4bf6a9b78a
commit 1281bb30c4
6 changed files with 226 additions and 133 deletions

View file

@ -0,0 +1,49 @@
local PlayerActions = Object:extend()
local BASE_SPEED = 120
local JMP_STRENGHT = 2.5
function PlayerActions:initActions()
self.isJumping = false
end
function PlayerActions:actionMove()
if self.keys["up"].isDown then
self.ysp = -BASE_SPEED
self.charDir = "up"
end
if self.keys["down"].isDown then
self.ysp = BASE_SPEED
self.charDir = "down"
end
if self.keys["left"].isDown then
self.xsp = -BASE_SPEED
self.charDir = "left"
end
if self.keys["right"].isDown then
self.xsp = BASE_SPEED
self.charDir = "right"
end
end
function PlayerActions:actionJump()
if self.keys["B"].isPressed then
if (self.onGround) then
self:goUpward(JMP_STRENGHT)
self.isJumping = true
self.assets.sfx["jump"]:play()
end
end
end
function PlayerActions:actionSwitch()
if self.keys["select"].isPressed and self.onGround then
self:switchActiveCharacter()
end
end
function PlayerActions:endJump()
self.isJumping = false
end
return PlayerActions

View file

@ -0,0 +1,19 @@
local PlayerCharset = Object:extend()
function PlayerCharset:initPlayerCharset()
self:updateCurrentCharset()
end
function PlayerCharset:updateCurrentCharset()
if (not self.isJumping) then
self:setCharset(self.active.data.charset, self.active.data.charId)
self.largeAnim = false
self.isFast = false
else
self:setCharset(self.active.data.charset .. "-jump", self.active.data.charId)
self.largeAnim = true
self.isFast = true
end
end
return PlayerCharset

View file

@ -1,91 +1,57 @@
local Parent = require "scenes.overworld.actors.parent" local Parent = require "scenes.overworld.actors.parent"
local Player = Parent:extend() local Player = Parent:extend()
local Emblem = require "game.modules.gui.emblem"
local TweenManager = require "game.modules.tweenmanager" local TweenManager = require "game.modules.tweenmanager"
local Team = require "scenes.overworld.actors.player.team"
local Interactions = require "scenes.overworld.actors.player.interactions"
local Actions = require "scenes.overworld.actors.player.actions"
local Charset = require "scenes.overworld.actors.player.charset"
local Map = require "scenes.overworld.actors.player.map"
Player:implement(Team)
Player:implement(Interactions)
Player:implement(Actions)
Player:implement(Charset)
Player:implement(Map)
local FRICTION = 480 * 3
local GRAV = 10
function Player:new(world, x, y, id) function Player:new(world, x, y, id)
Player.super.new(self, world, "player", x, y, 16, 16, true) Player.super.new(self, world, "player", x, y, 16, 16, true)
self.active = game.characters:getActiveCharacterData()
self.emblems = {}
for i, name in ipairs(game.characters.team) do
game.characters:loadSprite(self.assets, name)
self.emblems[i] = Emblem(game.characters.list[name], self.scene)
end
self.activeVisible = game.characters.active
self.canChangeActive = true
self.tweens = TweenManager(self) self.tweens = TweenManager(self)
self.lastCollision = -1
self.haveCollided = false
self.previousMap = 0
self:updateCurrentCharset()
self.onGround = true self.onGround = true
self.isJumping = false self.xfrc, self.yfrc = FRICTION, FRICTION
self:initTeam()
self:initInteractions()
self:initActions()
self:initPlayerCharset()
self:initMap()
end end
function Player:updateStart(dt) function Player:updateStart(dt)
self.xfrc, self.yfrc = 480*3, 480*3
if self.keys["up"].isDown then
self.ysp = -120
self.charDir = "up"
end
if self.keys["down"].isDown then
self.ysp = 120
self.charDir = "down"
end
if self.keys["left"].isDown then
self.xsp = -120
self.charDir = "left"
end
if self.keys["right"].isDown then
self.xsp = 120
self.charDir = "right"
end
if self.keys["select"].isPressed and self.canChangeActive then
if (self.onGround) then
game.characters:setActiveCharacter()
self.canChangeActive = false
self.tweens:newTimer(0.3, "changeCharacter")
self.tweens:newTween(0, 0.3, {activeVisible = self.activeVisible + 1}, "inQuad")
end
end
if self.keys["B"].isPressed then
if (self.onGround) then
self.zsp = 2.5
self.grav = 10
self.onGround = false
self.isJumping = true
self.assets.sfx["jump"]:play()
end
end
self.tweens:update(dt) self.tweens:update(dt)
self:actionMove()
self:actionJump()
self:actionSwitch()
self.world:getTileTypeAtPoint(self.x, self.y) self.world:getTileTypeAtPoint(self.x, self.y)
if (self.haveCollided == false) then self:updateInteraction()
self.lastCollision = nil
end
self.haveCollided = false
local currentMap = self.world.map:getMapAtPoint(self.x, self.y)
if (currentMap ~= nil) then
if (self.previousMap ~= currentMap.id) then
self.previousMap = currentMap.id
self.scene:updateCurrentMap(currentMap)
end
end
self:updateCurrentCharset() self:updateCurrentCharset()
self:updateCurrentMap()
end
-- PHYSICS FUNCTIONS
-- Some functions to hook up the physic system
function Player:goUpward(zsp)
self.zsp = zsp
self.grav = GRAV
self.onGround = false
end end
function Player:applyGravity(dt) function Player:applyGravity(dt)
@ -100,9 +66,9 @@ end
function Player:checkGround() function Player:checkGround()
if (self.z + self.zsp <= 0) then if (self.z + self.zsp <= 0) then
self.onGround = true self.onGround = true
self.isJumping = false
self.z = 0 self.z = 0
self.zsp = 0 self.zsp = 0
self:endJump()
end end
end end
@ -111,84 +77,29 @@ function Player:autoMove(dt)
self.z = self.z + self.zsp self.z = self.z + self.zsp
end end
-- RESPONSES
function Player:updateCurrentCharset() -- Reponse to timer and collisions
if (not self.isJumping) then
self:setCharset(self.active.data.charset, self.active.data.charId)
self.largeAnim = false
self.isFast = false
else
self:setCharset(self.active.data.charset .. "-jump", self.active.data.charId)
self.largeAnim = true
self.isFast = true
end
end
function Player:collisionResponse(col) function Player:collisionResponse(col)
local hitbox = col.other local hitbox = col.other
local other = col.other.owner local other = col.other.owner
if (not other.isDestroyed) then if (not other.isDestroyed) then
if (hitbox.type == "gizmo") then if (hitbox.type == "gizmo") then
if (other.needButton) then self:collideWithGizmo(other)
if (self.keys["A"].isPressed) then elseif (hitbox.type == "btnInput" and other.needButton) then
other:doAction() self:talkToGizmo(other)
self.haveCollided = true
self.lastCollision = other.creationID
end end
else
if (self.lastCollision ~= other.creationID) then
other:doAction()
end
self.haveCollided = true
self.lastCollision = other.creationID
end
end
if (hitbox.type == "btnInput" and other.needButton) then
if (self.keys["A"].isPressed and (self:faceRightDirection(other))) then
other:doAction()
self.haveCollided = true
self.lastCollision = other.creationID
end
end
end
end
function Player:faceRightDirection(other)
if (self.charDir == "up") then
return (self.y >= other.y + other.h)
end
if (self.charDir == "down") then
return (self.y + self.h <= other.y)
end
if (self.charDir == "left") then
return (self.x >= other.x + other.w)
end
if (self.charDir == "right") then
return (self.x + self.w <= other.x)
end end
end end
function Player:timerResponse(response) function Player:timerResponse(response)
if (response == "changeCharacter") then if (response == "changeCharacter") then
self.canChangeActive = true self:endCharacterSwitchAnimation()
self.activeVisible = game.characters.active
self.active = game.characters:getActiveCharacterData()
self:updateCurrentCharset()
end end
end end
function Player:drawHUD(id) function Player:drawHUD(id)
--love.graphics.print(id .. " test", 4, 4)
self:drawEmblems(self.scene:getEmblemsPosition(), 24) self:drawEmblems(self.scene:getEmblemsPosition(), 24)
end end
function Player:drawEmblems(x, y)
for i,emblem in ipairs(self.emblems) do
local angle = ((i-self.activeVisible) * (360/#self.emblems)) - 90
local rad = math.rad(angle)
local emblemX, emblemY = utils.math.lengthdir(18, rad)
emblem:draw(x + emblemX, y + emblemY)
end
end
return Player return Player

View file

@ -0,0 +1,54 @@
local PlayerInteractions = Object:extend()
function PlayerInteractions:initInteractions()
self.lastCollision = -1
self.haveCollided = false
end
function PlayerInteractions:updateInteraction()
if (self.haveCollided == false) then
self.lastCollision = nil
end
self.haveCollided = false
end
function PlayerInteractions:collideWithGizmo(other)
if (other.needButton) then
if (self.keys["A"].isPressed) then
other:doAction()
self.haveCollided = true
self.lastCollision = other.creationID
end
else
if (self.lastCollision ~= other.creationID) then
other:doAction()
end
self.haveCollided = true
self.lastCollision = other.creationID
end
end
function PlayerInteractions:talkToGizmo(other)
if (self.keys["A"].isPressed and (self:faceRightDirection(other))) then
other:doAction()
self.haveCollided = true
self.lastCollision = other.creationID
end
end
function PlayerInteractions:faceRightDirection(other)
if (self.charDir == "up") then
return (self.y >= other.y + other.h)
end
if (self.charDir == "down") then
return (self.y + self.h <= other.y)
end
if (self.charDir == "left") then
return (self.x >= other.x + other.w)
end
if (self.charDir == "right") then
return (self.x + self.w <= other.x)
end
end
return PlayerInteractions

View file

@ -0,0 +1,17 @@
local PlayerMap = Object:extend()
function PlayerMap:initMap()
self.previousMap = 0
end
function PlayerMap:updateCurrentMap()
local currentMap = self.world.map:getMapAtPoint(self.x, self.y)
if (currentMap ~= nil) then
if (self.previousMap ~= currentMap.id) then
self.previousMap = currentMap.id
self.scene:updateCurrentMap(currentMap)
end
end
end
return PlayerMap

View file

@ -0,0 +1,43 @@
local Team = Object:extend()
local Emblem = require "game.modules.gui.emblem"
function Team:initTeam()
self.active = game.characters:getActiveCharacterData()
self.emblems = {}
for i, name in ipairs(game.characters.team) do
game.characters:loadSprite(self.assets, name)
self.emblems[i] = Emblem(game.characters.list[name], self.scene)
end
self.activeVisible = game.characters.active
self.canChangeActive = true
end
function Team:switchActiveCharacter()
if (self.canChangeActive) then
game.characters:setActiveCharacter()
self.canChangeActive = false
self.tweens:newTimer(0.3, "changeCharacter")
self.tweens:newTween(0, 0.3, {activeVisible = self.activeVisible + 1}, "inQuad")
end
end
function Team:endCharacterSwitchAnimation()
self.canChangeActive = true
self.activeVisible = game.characters.active
self.active = game.characters:getActiveCharacterData()
self:updateCurrentCharset()
end
function Team:drawEmblems(x, y)
for i,emblem in ipairs(self.emblems) do
local angle = ((i-self.activeVisible) * (360/#self.emblems)) - 90
local rad = math.rad(angle)
local emblemX, emblemY = utils.math.lengthdir(18, rad)
emblem:draw(x + emblemX, y + emblemY)
end
end
return Team