From 19a19538c205fce74550c20d648fb239e26f0cf3 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 23 Sep 2017 14:08:32 +0200 Subject: Hero walking moved from Player, separated into methods --- not/Hero.lua | 20 ++++++++++++++++++++ not/Player.lua | 14 ++------------ 2 files changed, 22 insertions(+), 12 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index a97a2b1..681d7e6 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -227,6 +227,26 @@ function Hero:land () self:createEffect("land") end +function Hero:walk (face) + local x, y = self:getLinearVelocity() + self.facing = face + self:applyForce(250 * face, 0) + if x > self.MAX_VELOCITY then + self:applyForce(-250, 0) + end + if x < -self.MAX_VELOCITY then + self:applyForce(250, 0) + end +end + +function Hero:walkLeft () + self:walk(-1) +end + +function Hero:walkRight () + self:walk(1) +end + -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. diff --git a/not/Player.lua b/not/Player.lua index b0dac75..6996c9d 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -38,20 +38,10 @@ function Player:update (dt) -- 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 + self:walkLeft() 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 + self:walkRight() end end -- cgit v1.1 From 356054d6dc0fffc280e893ec4385559b4c618de2 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 28 Sep 2017 08:05:15 +0200 Subject: Para-interfacing of walking methods --- not/Hero.lua | 32 +++++++++++++++++++++----------- not/Player.lua | 16 +++++++--------- 2 files changed, 28 insertions(+), 20 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 681d7e6..2e1f1f0 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -43,7 +43,6 @@ function Hero:new (config, x, y, world) self.salto = false self.smoke = false self.isAlive = true - self.isWalking = false self.isJumping = false self.spawntimer = 2 self.punchCooldown = 0 @@ -124,6 +123,13 @@ function Hero:update (dt) end end + if self:isWalkingLeft() then + self:walk(-1) + end + if self:isWalkingRight() then + self:walk(1) + end + -- Stop vertical local currentAnimation = self:getAnimation() if self.frame < currentAnimation.frames then @@ -138,7 +144,7 @@ end --- Damps linear velocity every frame by applying minor force to body. function Hero:dampVelocity (dt) - if not self.isWalking then + if not self:isWalking() then local face local x, y = self:getLinearVelocity() if x < -12 then @@ -199,7 +205,7 @@ end function Hero:goToNextFrame () if self.current.repeated or not (self.frame == self.current.frames) then self.frame = (self.frame % self.current.frames) + 1 - elseif self.isWalking then + elseif self:isWalking() then self:setAnimation("walk") elseif self.current == self.animations.damage then self:setAnimation("default") @@ -227,6 +233,18 @@ function Hero:land () self:createEffect("land") end +function Hero:isWalking () + return self:isWalkingLeft() or self:isWalkingRight() +end + +function Hero:isWalkingLeft () + return false +end + +function Hero:isWalkingRight () + return false +end + function Hero:walk (face) local x, y = self:getLinearVelocity() self.facing = face @@ -239,14 +257,6 @@ function Hero:walk (face) end end -function Hero:walkLeft () - self:walk(-1) -end - -function Hero:walkRight () - self:walk(1) -end - -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. diff --git a/not/Player.lua b/not/Player.lua index 6996c9d..0b56e6d 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -35,14 +35,14 @@ function Player:update (dt) self:setLinearVelocity(x,-160) self.jumpTimer = self.jumpTimer - dt end +end - -- Walking. - if self:isControlDown("left") then - self:walkLeft() - end - if self:isControlDown("right") then - self:walkRight() - end +function Player:isWalkingLeft () + return self:isControlDown("left") +end + +function Player:isWalkingRight () + return self:isControlDown("right") end -- Controller callbacks. @@ -78,7 +78,6 @@ function Player:controlpressed (set, action, key) -- Walking if (action == "left" or action == "right") then - self.isWalking = true if (self.current ~= self.animations.attack) and (self.current ~= self.animations.attack_up) and (self.current ~= self.animations.attack_down) then @@ -125,7 +124,6 @@ function Player:controlreleased (set, action, key) -- Walking if (action == "left" or action == "right") then if not (self:isControlDown("left") or self:isControlDown("right")) then - self.isWalking = false if self.current == self.animations.walk then self:setAnimation("default") end -- cgit v1.1 From 6d61fb66d72bb668d71479d3ed5f40b51041639e Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 29 Sep 2017 13:17:54 +0200 Subject: Moved update part of jumping from Player to Hero isJumping is now a method added debugging information on player jumps --- not/Hero.lua | 19 ++++++++++++++++++- not/Player.lua | 20 ++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 2e1f1f0..b3edfbf 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -43,7 +43,6 @@ function Hero:new (config, x, y, world) self.salto = false self.smoke = false self.isAlive = true - self.isJumping = false self.spawntimer = 2 self.punchCooldown = 0 -- TODO: Pass loaded portrait from menu to Hero. @@ -140,6 +139,13 @@ function Hero:update (dt) self:setLinearVelocity(38*self.facing, 0) end end + + -- Jumping. + if self:isJumping() and self.jumpTimer > 0 and (self.jumpCounter == 0 or self.jumpCounter == 1) then + local x = self:getLinearVelocity() + self:setLinearVelocity(x,-160) + self.jumpTimer = self.jumpTimer - dt + end end --- Damps linear velocity every frame by applying minor force to body. @@ -175,6 +181,13 @@ end function Hero:draw (debug) if not self.isAlive then return end Hero.__super.draw(self, debug) + if debug then + local x, y = self:getPosition() + love.graphics.setColor(255, 50, 50) + love.graphics.setFont(Font) + local msg = string.format("%d %s %s", self.jumpCounter, tostring(self.jumpTimer > 0), tostring(self:isJumping())) + love.graphics.print(msg, x + 10, y) + end end -- TODO: Hero@drawTag's printf is not readable. @@ -233,6 +246,10 @@ function Hero:land () self:createEffect("land") end +function Hero:isJumping () + return false +end + function Hero:isWalking () return self:isWalkingLeft() or self:isWalkingRight() end diff --git a/not/Player.lua b/not/Player.lua index 0b56e6d..3d15f31 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -25,16 +25,8 @@ function Player:isControlDown (control) return Controller.isDown(self:getControllerSet(), control) end --- Update of `Player`. -function Player:update (dt) - Player.__super.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. - if self.body:isDestroyed() then return end - local x, y = self:getLinearVelocity() - -- Jumping. - if self.isJumping and self.jumpTimer > 0 then - self:setLinearVelocity(x,-160) - self.jumpTimer = self.jumpTimer - dt - end +function Player:isJumping () + return self:isControlDown("jump") end function Player:isWalkingLeft () @@ -51,10 +43,9 @@ function Player:controlpressed (set, action, key) self.smoke = false -- TODO: temporary -- Jumping if action == "jump" then + self.jumpCounter = self.jumpCounter - 1 if self.jumpCounter > 0 then - -- General jump logics - self.isJumping = true - --self:playSound(6) + -- self:playSound(6) -- Spawn proper effect if not self.inAir then self:createEffect("jump") @@ -71,8 +62,6 @@ function Player:controlpressed (set, action, key) (self.current == self.animations.attack_down) then self:setAnimation("default") end - -- Remove jump - self.jumpCounter = self.jumpCounter - 1 end end @@ -118,7 +107,6 @@ function Player:controlreleased (set, action, key) if set ~= self:getControllerSet() then return end -- Jumping if action == "jump" then - self.isJumping = false self.jumpTimer = Hero.jumpTimer -- take initial from metatable end -- Walking -- cgit v1.1 From 08271656cbe18fbc4432fc758ac42750e818da84 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 30 Sep 2017 19:27:04 +0200 Subject: Jump moved completely out of Player (I think) --- not/Hero.lua | 39 +++++++++++++++++++++++++++++++++++---- not/Player.lua | 23 ----------------------- 2 files changed, 35 insertions(+), 27 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index b3edfbf..21c4e27 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -141,10 +141,41 @@ function Hero:update (dt) end -- Jumping. - if self:isJumping() and self.jumpTimer > 0 and (self.jumpCounter == 0 or self.jumpCounter == 1) then - local x = self:getLinearVelocity() - self:setLinearVelocity(x,-160) - self.jumpTimer = self.jumpTimer - dt + if self:isJumping() and self.jumpTimer > 0 then + if not self._jumpevent then + self._jumpevent = true + self:onJump() + end + if self.jumpCounter == 0 or self.jumpCounter == 1 then + local x = self:getLinearVelocity() + self:setLinearVelocity(x,-160) + self.jumpTimer = self.jumpTimer - dt + end + else + self._jumpevent = false + end +end + +function Hero:onJump () + -- Start salto if last jump + if self.jumpCounter == 1 then + self.salto = true + end + self.jumpCounter = self.jumpCounter - 1 + if self.jumpCounter > 0 then + -- self:playSound(6) + -- Spawn proper effect + if not self.inAir then + self:createEffect("jump") + else + self:createEffect("doublejump") + 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 end end diff --git a/not/Player.lua b/not/Player.lua index 3d15f31..27ace75 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -41,29 +41,6 @@ end function Player:controlpressed (set, action, key) if set ~= self:getControllerSet() then return end self.smoke = false -- TODO: temporary - -- Jumping - if action == "jump" then - self.jumpCounter = self.jumpCounter - 1 - if self.jumpCounter > 0 then - -- 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 - end - end -- Walking if (action == "left" or action == "right") then -- cgit v1.1 From d900efc15b373d065666f891851085d70ec52667 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 01:42:34 +0200 Subject: Added method call when Hero starts walking --- not/Hero.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7ef7b4e..37e61c0 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -122,6 +122,16 @@ function Hero:update (dt) end end + --- Walking + -- TODO: Walking is still not satisfactiory. Think of way to improve it. + if self:isWalking() then + if not self._already_walking then + self._already_walking = true + self:onWalkingStarted() + end + else + self._already_walking = false + end if self:isWalkingLeft() then self:walk(-1) end @@ -156,6 +166,12 @@ function Hero:update (dt) end end +--- Called each time Hero starts walking. +-- Is not called when only direction of walking is changed. +function Hero:onWalkingStarted () + +end + function Hero:onJump () -- Start salto if last jump if self.jumpCounter == 1 then -- cgit v1.1 From 5ce7f80fcdbc16ed74d56865df3114a800b3788d Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 01:48:55 +0200 Subject: Walking animation start fires now inside Hero's onWalkingStarted --- not/Hero.lua | 6 +++++- not/Player.lua | 10 +--------- 2 files changed, 6 insertions(+), 10 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 37e61c0..650eed4 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -169,7 +169,11 @@ end --- Called each time Hero starts walking. -- Is not called when only direction of walking is changed. function Hero:onWalkingStarted () - + if (self.current ~= self.animations.attack) and + (self.current ~= self.animations.attack_up) and + (self.current ~= self.animations.attack_down) then + self:setAnimation("walk") + end end function Hero:onJump () diff --git a/not/Player.lua b/not/Player.lua index 27ace75..c6c4431 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -42,15 +42,6 @@ function Player:controlpressed (set, action, key) if set ~= self:getControllerSet() then return end self.smoke = false -- TODO: temporary - -- Walking - if (action == "left" or action == "right") then - if (self.current ~= self.animations.attack) and - (self.current ~= self.animations.attack_up) and - (self.current ~= self.animations.attack_down) then - self:setAnimation("walk") - end - end - -- Punching if action == "attack" and self.punchCooldown <= 0 then local f = self.facing @@ -80,6 +71,7 @@ function Player:controlpressed (set, action, key) end end end + function Player:controlreleased (set, action, key) if set ~= self:getControllerSet() then return end -- Jumping -- cgit v1.1 From 6124f00707856980f1c5a7809ebf03351783f4a4 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 01:56:08 +0200 Subject: Punch animations firing now also happens in Hero --- not/Hero.lua | 33 ++++++++++++++++++++++++++++----- not/Player.lua | 13 ------------- 2 files changed, 28 insertions(+), 18 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 650eed4..f49ed14 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -328,14 +328,37 @@ end -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. +-- TODO: While it's good that punch animation changes are here, it is still bad. There is too much similar code in this method. function Hero:punch (direction) self.punchCooldown = Hero.PUNCH_COOLDOWN - -- Choose shape based on punch direction. + self.salto = false + local shape - if direction == "left" then shape = Hero.PUNCH_LEFT end - if direction == "right" then shape = Hero.PUNCH_RIGHT end - if direction == "up" then shape = Hero.PUNCH_UP end - if direction == "down" then shape = Hero.PUNCH_DOWN end + if direction == "left" then + shape = Hero.PUNCH_LEFT + if self.current ~= self.animations.damage then + self:setAnimation("attack") + end + end + if direction == "right" then + shape = Hero.PUNCH_RIGHT + if self.current ~= self.animations.damage then + self:setAnimation("attack") + end + end + if direction == "up" then + shape = Hero.PUNCH_UP + if self.current ~= self.animations.damage then + self:setAnimation("attack_up") + end + end + if direction == "down" then + shape = Hero.PUNCH_DOWN + if self.current ~= self.animations.damage then + self:setAnimation("attack_down") + end + end + -- Create and set sensor fixture. local fixture = self:addFixture(shape, 0) fixture:setSensor(true) diff --git a/not/Player.lua b/not/Player.lua index c6c4431..3722cb3 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -45,24 +45,11 @@ function Player:controlpressed (set, action, key) -- Punching if action == "attack" and self.punchCooldown <= 0 then local f = self.facing - self.salto = false if self:isControlDown("up") then - -- Punch up - if self.current ~= self.animations.damage then - self:setAnimation("attack_up") - end self:punch("up") elseif self:isControlDown("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 -- cgit v1.1 From c36b981f54d22c3891856c64b6be780815e8b7df Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 02:03:21 +0200 Subject: Changed jump-related-variables to match to newer parts of code --- not/Hero.lua | 8 +++++--- not/Player.lua | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index f49ed14..118b9f1 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -4,8 +4,6 @@ Hero = require "not.PhysicalBody":extends() -- Few are left... -Hero.jumpTimer = 0.16 -Hero.jumpCounter = 2 Hero.sfx = require "config.sounds" Hero.QUAD_FRAME = love.graphics.newQuad(0, 15, 32,32, 80,130) @@ -19,6 +17,8 @@ Hero.PUNCH_LEFT = {-2,-6, -20,-6, -20,6, -2,6} Hero.PUNCH_RIGHT = {2,-6, 20,-6, 20,6, 2,6} Hero.PUNCH_UP = {-8,-4, -8,-20, 8,-20, 8,-4} Hero.PUNCH_DOWN = {-8,4, -8,20, 8,20, 8,4} +Hero.JUMP_TIMER = 0.16 +Hero.JUMP_COUNT = 2 -- TODO: Portrait managment in Hero and config passed from Menu should be reviewed! -- TODO: Clean-up, see `menus/select`. @@ -44,6 +44,8 @@ function Hero:new (config, x, y, world) self.smoke = false self.isAlive = true self.spawntimer = 2 + self.jumpTimer = Hero.JUMP_TIMER + self.jumpCounter = Hero.JUMP_COUNT self.punchCooldown = 0 -- TODO: Pass loaded portrait from menu to Hero. self.portrait = love.graphics.newImage(config.portrait) @@ -291,7 +293,7 @@ end -- Called by World when Hero starts contact with Platform (lands). function Hero:land () self.inAir = false - self.jumpCounter = 2 + self.jumpCounter = Hero.JUMP_COUNT self.salto = false self.smoke = false self:createEffect("land") diff --git a/not/Player.lua b/not/Player.lua index 3722cb3..5e73e25 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -63,7 +63,7 @@ function Player:controlreleased (set, action, key) if set ~= self:getControllerSet() then return end -- Jumping if action == "jump" then - self.jumpTimer = Hero.jumpTimer -- take initial from metatable + self.jumpTimer = Hero.JUMP_TIMER end -- Walking if (action == "left" or action == "right") then -- cgit v1.1 From 4e2ad54590449037e4c35e26b285638029c8282d Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 02:23:13 +0200 Subject: Added onWalkingStopped and moved animation changes there from Player --- not/Hero.lua | 17 +++++++++++++++-- not/Player.lua | 8 -------- 2 files changed, 15 insertions(+), 10 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 118b9f1..1181080 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -132,7 +132,10 @@ function Hero:update (dt) self:onWalkingStarted() end else - self._already_walking = false + if self._already_walking then + self._already_walking = false + self:onWalkingStopped() + end end if self:isWalkingLeft() then self:walk(-1) @@ -169,7 +172,7 @@ function Hero:update (dt) end --- Called each time Hero starts walking. --- Is not called when only direction of walking is changed. +-- Is not called when direction of walking is changed. function Hero:onWalkingStarted () if (self.current ~= self.animations.attack) and (self.current ~= self.animations.attack_up) and @@ -178,6 +181,16 @@ function Hero:onWalkingStarted () end end +--- Called when Hero stops walking. +-- Is not called when direction of walking is changed. +function Hero:onWalkingStopped () + if not (self:isControlDown("left") or self:isControlDown("right")) then + if self.current == self.animations.walk then + self:setAnimation("default") + end + end +end + function Hero:onJump () -- Start salto if last jump if self.jumpCounter == 1 then diff --git a/not/Player.lua b/not/Player.lua index 5e73e25..b33e229 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -65,14 +65,6 @@ function Player:controlreleased (set, action, key) if action == "jump" then self.jumpTimer = Hero.JUMP_TIMER end - -- Walking - if (action == "left" or action == "right") then - if not (self:isControlDown("left") or self:isControlDown("right")) then - if self.current == self.animations.walk then - self:setAnimation("default") - end - end - end end return Player -- cgit v1.1 From 0baba0172f916a70042a5ddadf2a30957afa33d4 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 03:14:51 +0200 Subject: Clean-ups for Player --- not/Player.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'not') diff --git a/not/Player.lua b/not/Player.lua index b33e229..1cf6018 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -1,13 +1,11 @@ -require "not.Hero" - --- `Player` --- Special `not.Hero` controllable by a player. --- TODO: move functions and properties related to controls from `not.Hero`. -Player = Hero:extends() +-- Right now this is more or less wrapper for Hero and various methods related to players' input. +-- TODO: Few more things should be exchanged between Player and Hero. +-- TODO: In the end this class could be implemented in form of more verbose and functional Controller class. Think about it. +Player = require "not.Hero":extends() Player.controllerSet =--[[Controller.sets.*]]nil --- Constructor of `Player`. function Player:new (name, x, y, world) Player.__super.new(self, name, x, y, world) end @@ -16,6 +14,7 @@ end function Player:assignControllerSet (set) self.controllerSet = set end + function Player:getControllerSet () return self.controllerSet end -- cgit v1.1 From 6bad1609ca28ce88dbe75207e524ce8d67ec2a61 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 03:21:33 +0200 Subject: Added onJumpStopped, moved jump timer reset there; renamed onJump to onJumpStarted --- not/Hero.lua | 31 ++++++++++++++++++++----------- not/Player.lua | 4 ---- 2 files changed, 20 insertions(+), 15 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 1181080..38eb749 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -156,18 +156,23 @@ function Hero:update (dt) end -- Jumping. - if self:isJumping() and self.jumpTimer > 0 then - if not self._jumpevent then - self._jumpevent = true - self:onJump() - end - if self.jumpCounter == 0 or self.jumpCounter == 1 then - local x = self:getLinearVelocity() - self:setLinearVelocity(x,-160) - self.jumpTimer = self.jumpTimer - dt + if self:isJumping() then + if self.jumpTimer > 0 then + if not self._jumping then + self._jumping = true + self:onJumpStarted() + end + if self.jumpCounter == 0 or self.jumpCounter == 1 then + local x = self:getLinearVelocity() + self:setLinearVelocity(x,-160) + self.jumpTimer = self.jumpTimer - dt + end end else - self._jumpevent = false + if self._jumping then + self._jumping = false + self:onJumpStopped() + end end end @@ -191,7 +196,7 @@ function Hero:onWalkingStopped () end end -function Hero:onJump () +function Hero:onJumpStarted () -- Start salto if last jump if self.jumpCounter == 1 then self.salto = true @@ -214,6 +219,10 @@ function Hero:onJump () end end +function Hero:onJumpStopped () + self.jumpTimer = Hero.JUMP_TIMER +end + --- Damps linear velocity every frame by applying minor force to body. function Hero:dampVelocity (dt) if not self:isWalking() then diff --git a/not/Player.lua b/not/Player.lua index 1cf6018..72d64bb 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -60,10 +60,6 @@ end function Player:controlreleased (set, action, key) if set ~= self:getControllerSet() then return end - -- Jumping - if action == "jump" then - self.jumpTimer = Hero.JUMP_TIMER - end end return Player -- cgit v1.1 From 21880f5d084f04f0403301bac53dc9587eea198d Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 20:27:54 +0200 Subject: Restructured Hero a little bit more --- not/Hero.lua | 85 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 42 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 38eb749..7c818ee 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -176,6 +176,37 @@ function Hero:update (dt) end end +function Hero:walk (face) + local x, y = self:getLinearVelocity() + self.facing = face + self:applyForce(250 * face, 0) + if x > self.MAX_VELOCITY then + self:applyForce(-250, 0) + end + if x < -self.MAX_VELOCITY then + self:applyForce(250, 0) + end +end + +--- Damps linear velocity every frame by applying minor force to body. +function Hero:dampVelocity (dt) + if not self:isWalking() then + local face + local x, y = self:getLinearVelocity() + 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 + --- Called each time Hero starts walking. -- Is not called when direction of walking is changed. function Hero:onWalkingStarted () @@ -223,36 +254,6 @@ function Hero:onJumpStopped () self.jumpTimer = Hero.JUMP_TIMER end ---- Damps linear velocity every frame by applying minor force to body. -function Hero:dampVelocity (dt) - if not self:isWalking() then - local face - local x, y = self:getLinearVelocity() - 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 - --- TODO: comment them and place them somewhere properly -function Hero:getAngle () - return self.angle -end -function Hero:getHorizontalMirror () - return self.facing -end -function Hero:getOffset () - return 12,15 -end - function Hero:draw (debug) if not self.isAlive then return end Hero.__super.draw(self, debug) @@ -321,6 +322,18 @@ function Hero:land () self:createEffect("land") end +function Hero:getAngle () + return self.angle +end + +function Hero:getHorizontalMirror () + return self.facing +end + +function Hero:getOffset () + return 12,15 +end + function Hero:isJumping () return false end @@ -337,18 +350,6 @@ function Hero:isWalkingRight () return false end -function Hero:walk (face) - local x, y = self:getLinearVelocity() - self.facing = face - self:applyForce(250 * face, 0) - if x > self.MAX_VELOCITY then - self:applyForce(-250, 0) - end - if x < -self.MAX_VELOCITY then - self:applyForce(250, 0) - end -end - -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. -- cgit v1.1 From 3d2dcce88a19798a5b5e2bd563b1fe2dfa1b80d2 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 20:42:06 +0200 Subject: Last clean-ups of Player, I guess --- not/Player.lua | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'not') diff --git a/not/Player.lua b/not/Player.lua index 72d64bb..5da4f5f 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -6,11 +6,8 @@ Player = require "not.Hero":extends() Player.controllerSet =--[[Controller.sets.*]]nil -function Player:new (name, x, y, world) - Player.__super.new(self, name, x, y, world) -end - --- Controller set manipulation. +--- Assigns controller set to Player. +-- @param set one of `Controller.sets` function Player:assignControllerSet (set) self.controllerSet = set end @@ -19,29 +16,34 @@ function Player:getControllerSet () return self.controllerSet end --- Check if control of assigned controller is pressed. +--- Wrapper for checking if passed control is currently pressed. function Player:isControlDown (control) return Controller.isDown(self:getControllerSet(), control) end +--- Overridden from Hero, used by Hero:update. function Player:isJumping () return self:isControlDown("jump") end +--- Overridden from Hero, used by Hero:update. function Player:isWalkingLeft () return self:isControlDown("left") end +--- Overridden from Hero, used by Hero:update. function Player:isWalkingRight () return self:isControlDown("right") end --- Controller callbacks. +--- Called when control is pressed. +-- @param set ControllerSet that owns pressed control +-- @param action action assigned to control +-- @param key parent key of control function Player:controlpressed (set, action, key) if set ~= self:getControllerSet() then return end self.smoke = false -- TODO: temporary - -- Punching if action == "attack" and self.punchCooldown <= 0 then local f = self.facing if self:isControlDown("up") then @@ -58,8 +60,7 @@ function Player:controlpressed (set, action, key) end end -function Player:controlreleased (set, action, key) - if set ~= self:getControllerSet() then return end -end +--- Called when control is released. +function Player:controlreleased (set, action, key) end return Player -- cgit v1.1 From d6444b3a69fa194e49fe9a207547821b5cd6728d Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 20:54:28 +0200 Subject: All smoke resetting logic moved to Hero; cleanups --- not/Hero.lua | 32 +++++++++++++++++++------------- not/Player.lua | 2 -- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7c818ee..676dc4a 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -70,13 +70,18 @@ function Hero:newFixture () fixture:setGroupIndex(self.group) end --- Update callback of `Hero` +--- Called each game iteration. +-- @param dt time since last iteration +-- TODO: Cut this method into smaller parts. function Hero:update (dt) Hero.__super.update(self, dt) + if self.body:isDestroyed() then return end + self:dampVelocity(dt) + -- Salto if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then self.angle = (self.angle + 17 * dt * self.facing) % 360 @@ -84,7 +89,7 @@ function Hero:update (dt) self.angle = 0 end - -- Could you please die? + -- Death -- TODO: World/Map function for testing if Point is inside playable area. local m = self.world.map local x, y = self:getPosition() @@ -95,7 +100,7 @@ function Hero:update (dt) self:die() end - -- Respawn timer. + -- Respawn if self.spawntimer > 0 then self.spawntimer = self.spawntimer - dt end @@ -103,15 +108,14 @@ function Hero:update (dt) self:respawn() end - -- Trail spawner + -- Trail -- TODO: lower the frequency of spawning - currently it is each frame. if self.smoke and self.inAir then local dx, dy = love.math.random(-5, 5), love.math.random(-5, 5) self:createEffect("trail", dx, dy) end - -- # PUNCH - -- Cooldown + -- Punch cooldown self.punchCooldown = self.punchCooldown - dt if not self.body:isDestroyed() then -- TODO: This is weird for _,fixture in pairs(self.body:getFixtures()) do -- TODO: getFixtures from `PhysicalBody` or similar. @@ -124,7 +128,7 @@ function Hero:update (dt) end end - --- Walking + -- Walking -- TODO: Walking is still not satisfactiory. Think of way to improve it. if self:isWalking() then if not self._already_walking then @@ -144,7 +148,7 @@ function Hero:update (dt) self:walk(1) end - -- Stop vertical + -- Set predefined velocity when attack animations are playing local currentAnimation = self:getAnimation() if self.frame < currentAnimation.frames then if currentAnimation == self.animations.attack_up or currentAnimation == self.animations.attack_down then @@ -155,7 +159,7 @@ function Hero:update (dt) end end - -- Jumping. + -- Jumping if self:isJumping() then if self.jumpTimer > 0 then if not self._jumping then @@ -228,20 +232,21 @@ function Hero:onWalkingStopped () end function Hero:onJumpStarted () - -- Start salto if last jump + self.smoke = false + if self.jumpCounter == 1 then self.salto = true end + self.jumpCounter = self.jumpCounter - 1 + if self.jumpCounter > 0 then - -- self:playSound(6) - -- Spawn proper effect if not self.inAir then self:createEffect("jump") else self:createEffect("doublejump") end - -- Animation clear + if (self.current == self.animations.attack) or (self.current == self.animations.attack_up) or (self.current == self.animations.attack_down) then @@ -357,6 +362,7 @@ end function Hero:punch (direction) self.punchCooldown = Hero.PUNCH_COOLDOWN self.salto = false + self.smoke = false local shape if direction == "left" then diff --git a/not/Player.lua b/not/Player.lua index 5da4f5f..ac5d6aa 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -42,8 +42,6 @@ end -- @param key parent key of control function Player:controlpressed (set, action, key) if set ~= self:getControllerSet() then return end - self.smoke = false -- TODO: temporary - if action == "attack" and self.punchCooldown <= 0 then local f = self.facing if self:isControlDown("up") then -- cgit v1.1