From 603fac3ce18ff7df7b8d2f74d5e57cc728c0abc2 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 22:07:32 +0200 Subject: Created physics functions to influence PhysicalBody; changed Hero to use them. Comments. --- not/Hero.lua | 43 +++++++++++++++++++++++-------------------- not/PhysicalBody.lua | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+), 20 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 1b84b1c..1cb5e93 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -75,6 +75,7 @@ function Hero:init (name, world, x, y) end -- Control set managment +-- TODO: move these two to `not.Player`. function Hero:assignControlSet (set) self.controlset = set end @@ -87,14 +88,14 @@ end function Hero:update (dt) PhysicalBody.update(self, dt) -- locals - local x, y = self.body:getLinearVelocity() + local x, y = self:getLinearVelocity() local isDown = Controller.isDown local controlset = self:getControlSet() -- # VERTICAL MOVEMENT -- Jumping if self.jumpactive and self.jumptimer > 0 then - self.body:setLinearVelocity(x,-160) + self:setLinearVelocity(x,-160) self.jumptimer = self.jumptimer - dt end @@ -109,18 +110,18 @@ function Hero:update (dt) -- Walking if isDown(controlset, "left") then self.facing = -1 - self.body:applyForce(-250, 0) + self:applyForce(-250, 0) -- Controlled speed limit if x < -self.max_velocity then - self.body:applyForce(250, 0) + self:applyForce(250, 0) end end if isDown(controlset, "right") then self.facing = 1 - self.body:applyForce(250, 0) + self:applyForce(250, 0) -- Controlled speed limit if x > self.max_velocity then - self.body:applyForce(-250, 0) + self:applyForce(-250, 0) end end @@ -136,9 +137,9 @@ function Hero:update (dt) else face = 0 end - self.body:applyForce(40*face,0) + self:applyForce(40*face,0) if not self.inAir then - self.body:applyForce(80*face,0) + self:applyForce(80*face,0) end end @@ -163,8 +164,8 @@ function Hero:update (dt) -- # PUNCH -- Cooldown self.punchcd = self.punchcd - dt - if not self.body:isDestroyed() then -- This is weird - for _,fixture in pairs(self.body:getFixtureList()) do + if not self.body:isDestroyed() then -- TODO: This is weird + for _,fixture in pairs(self.body:getFixtureList()) do -- TODO: getFixtures from `PhysicalBody` or similar. if fixture:getUserData() ~= self then fixture:setUserData({fixture:getUserData()[1] - dt, fixture:getUserData()[2]}) if fixture:getUserData()[1] < 0 then @@ -178,9 +179,9 @@ function Hero:update (dt) local c,a = self.current, self.animations if (c == a.attack_up or c == a.attack_down or c == a.attack) and self.frame < c.frames then if self.punchdir == 0 then - self.body:setLinearVelocity(0,0) + self:setLinearVelocity(0,0) else - self.body:setLinearVelocity(38*self.facing,0) + self:setLinearVelocity(38*self.facing,0) end end @@ -281,7 +282,7 @@ end function Hero:getAngle () return self.angle end -function Hero:getHorizontalMirror() +function Hero:getHorizontalMirror () return self.facing end function Hero:getOffset () @@ -289,7 +290,6 @@ function Hero:getOffset () end -- Draw of `Hero` --- TODO: see `not.PhysicalBody.draw` and `not.Sprite.draw`. function Hero:draw (offset_x, offset_y, scale, debug) if not self.alive then return end PhysicalBody.draw(self, offset_x, offset_y, scale, debug) @@ -339,10 +339,12 @@ end -- Punch of `Hero` -- direction: left, right, up, down -- creates temporary fixture for player's body that acts as sensor; fixture is deleted after time set in UserData[1]; deleted by Hero:update(dt) +-- TODO: attack functions needs to be renamed, because even I have problems understanding them. function Hero:hit (direction) -- start cooldown self.punchcd = Hero.punchcd -- INITIAL from metatable -- actual punch + -- TODO: use `PhysicalBody.addFixture`. local fixture if direction == "left" then fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-2,-6, -20,-6, -20,6, -2,6), 0) @@ -382,8 +384,8 @@ function Hero:damage (direction) vertical = 1 end self:createEffect("hit") - local x,y = self.body:getLinearVelocity() - self.body:setLinearVelocity(x,0) + local x,y = self:getLinearVelocity() + self:setLinearVelocity(x,0) self.body:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) self:setAnimation("damage") self.combo = math.min(27, self.combo + 1) @@ -398,21 +400,22 @@ function Hero:die () self.lives = self.lives - 1 self.alive = false self.spawntimer = Hero.spawntimer -- INITIAL from metatable - self.body:setActive(false) + self:setBodyActive(false) self.world:onNautKilled(self) end -- And then respawn. Like Jon Snow. function Hero:respawn () self.alive = true - self.body:setLinearVelocity(0,0) - self.body:setPosition(self.world:getSpawnPosition()) - self.body:setActive(true) + self:setLinearVelocity(0,0) + self:setPosition(self.world:getSpawnPosition()) -- TODO: I'm not convinced about getting new position like this. + self:setBodyActive(true) self:createEffect("respawn") self:playSound(7) end -- Sounds +-- TODO: Possibly export to nonexistent SoundEmitter class. Can be used by World (Stage), too. function Hero:playSound (sfx, force) if self.alive or force then local source = love.audio.newSource(self.sfx[sfx]) diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index a7abcc0..a9ac63b 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -38,6 +38,14 @@ function PhysicalBody:setPosition (x, y) self.body:setPosition(x, y) end +-- Velocity-related methods. +function PhysicalBody:setLinearVelocity (x, y) + self.body:setLinearVelocity(x, y) +end +function PhysicalBody:getLinearVelocity () + return self.body:getLinearVelocity() +end + -- Various setters from Body. -- type: BodyType ("static", "dynamic", "kinematic") function PhysicalBody:setBodyType (type) @@ -46,6 +54,17 @@ end function PhysicalBody:setBodyFixedRotation (bool) self.body:setFixedRotation(bool) end +function PhysicalBody:setBodyActive (bool) + self.body:setActive(bool) +end + +-- Physical influence methods. +function PhysicalBody:applyLinearImpulse (x, y) + self.body:applyLinearImpulse(x, y) +end +function PhysicalBody:applyForce (x, y) + self.body:applyForce(x, y) +end -- Update of `PhysicalBody`. function PhysicalBody:update (dt) -- cgit v1.1