summaryrefslogtreecommitdiffhomepage
path: root/not/Player.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-04-05 19:32:26 +0200
committerAki <nthirtyone@gmail.com>2017-04-05 19:32:26 +0200
commit527901f599c79047ab9a12fbc065a93faa8f872e (patch)
treec116ff29b11eca9ae4918f4d266df8514f5c80fc /not/Player.lua
parent8e11bf89f1abe547c30f7d5ac39bf0d7ed555f7e (diff)
downloadroflnauts-527901f599c79047ab9a12fbc065a93faa8f872e.zip
roflnauts-527901f599c79047ab9a12fbc065a93faa8f872e.tar.gz
roflnauts-527901f599c79047ab9a12fbc065a93faa8f872e.tar.bz2
Moved most player-input-related methods from Hero to Player
Diffstat (limited to 'not/Player.lua')
-rw-r--r--not/Player.lua142
1 files changed, 141 insertions, 1 deletions
diff --git a/not/Player.lua b/not/Player.lua
index 5fc2adc..2bee903 100644
--- a/not/Player.lua
+++ b/not/Player.lua
@@ -11,7 +11,6 @@ Player.__index = Player
setmetatable(Player, Hero)
-- Constructor of `Player`.
--- TODO: I'm sure it is a duplicate, but `not.World.create*` methods need to pass proper parameters.
function Player:new (name, game, x, y)
local o = setmetatable({}, self)
o:init(name, game, x, y)
@@ -36,3 +35,144 @@ end
function Player:getControlSet ()
return self.controlset
end
+
+-- Check if control of assigned controller is pressed.
+function Player:isControlDown (control)
+ return Controller.isDown(self:getControlSet(), control)
+end
+
+-- Update of `Player`.
+function Player:update (dt)
+ local x, y = self:getLinearVelocity()
+ Hero.update(self, dt) -- TODO: It would be probably a good idea to add return to update functions to terminate if something goes badly in parent's update.
+
+ -- Jumping.
+ if self.isJumping and self.jumpTimer > 0 then
+ self:setLinearVelocity(x,-160)
+ self.jumpTimer = self.jumpTimer - dt
+ end
+
+ -- Walking.
+ if self:isControlDown("left") then
+ self.facing = -1
+ self:applyForce(-250, 0)
+ -- Controlled speed limit
+ if x < -self.max_velocity then
+ self:applyForce(250, 0)
+ end
+ end
+ if self:isControlDown("right") then
+ self.facing = 1
+ self:applyForce(250, 0)
+ -- Controlled speed limit
+ if x > self.max_velocity then
+ self:applyForce(-250, 0)
+ end
+ end
+
+ -- Limiting walking speed.
+ if not self:isControlDown("left") and
+ not self:isControlDown("right")
+ then
+ local face = nil
+ if x < -12 then
+ face = 1
+ elseif x > 12 then
+ face = -1
+ else
+ face = 0
+ end
+ self:applyForce(40*face,0)
+ if not self.inAir then
+ self:applyForce(80*face,0)
+ end
+ end
+end
+
+-- Controller callbacks.
+function Player:controlpressed (set, action, key)
+ if set ~= self:getControlSet() then return end
+ local isDown = Controller.isDown
+ local controlset = self:getControlSet()
+ -- Jumping
+ if action == "jump" then
+ if self.jumpCounter > 0 then
+ -- General jump logics
+ self.isJumping = true
+ --self:playSound(6)
+ -- Spawn proper effect
+ if not self.inAir then
+ self:createEffect("jump")
+ else
+ self:createEffect("doublejump")
+ end
+ -- Start salto if last jump
+ if self.jumpCounter == 1 then
+ self.salto = true
+ end
+ -- Animation clear
+ if (self.current == self.animations.attack) or
+ (self.current == self.animations.attack_up) or
+ (self.current == self.animations.attack_down) then
+ self:setAnimation("default")
+ end
+ -- Remove jump
+ self.jumpCounter = self.jumpCounter - 1
+ end
+ end
+
+ -- Walking
+ if (action == "left" or action == "right") and
+ (self.current ~= self.animations.attack) and
+ (self.current ~= self.animations.attack_up) and
+ (self.current ~= self.animations.attack_down) then
+ self:setAnimation("walk")
+ end
+
+ -- Punching
+ if action == "attack" and self.punchCooldown <= 0 then
+ local f = self.facing
+ self.salto = false
+ if isDown(controlset, "up") then
+ -- Punch up
+ if self.current ~= self.animations.damage then
+ self:setAnimation("attack_up")
+ end
+ self:punch("up")
+ elseif isDown(controlset, "down") then
+ -- Punch down
+ if self.current ~= self.animations.damage then
+ self:setAnimation("attack_down")
+ end
+ self:punch("down")
+ else
+ -- Punch horizontal
+ if self.current ~= self.animations.damage then
+ self:setAnimation("attack")
+ end
+ if f == 1 then
+ self:punch("right")
+ else
+ self:punch("left")
+ end
+ self.punchdir = 1
+ end
+ end
+end
+function Player:controlreleased (set, action, key)
+ if set ~= self:getControlSet() then return end
+ local isDown = Controller.isDown
+ local controlset = self:getControlSet()
+ -- Jumping
+ if action == "jump" then
+ self.isJumping = false
+ self.jumpTimer = Hero.jumpTimer -- take initial from metatable
+ end
+ -- Walking
+ if (action == "left" or action == "right") and not
+ (isDown(controlset, "left") or isDown(controlset, "right")) and
+ self.current == self.animations.walk
+ then
+ self:setAnimation("default")
+ end
+end