From 0e785f506b070b1dcd5587cc0641313daaa6e53b Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 15 Aug 2016 01:23:14 +0200 Subject: Game is almost working again --- controller.lua | 1 + player.lua | 66 +++++++++++++++++++++++++--------------------------------- world.lua | 24 +++++++++++++-------- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/controller.lua b/controller.lua index e0bdab1..53b1db9 100644 --- a/controller.lua +++ b/controller.lua @@ -59,6 +59,7 @@ end -- Checks if given action of given set is down function Controller.isDown(set, action) + if set == nil then return end if set.joystick == nil then return love.keyboard.isDown(set[action]) else diff --git a/player.lua b/player.lua index 077780c..fe11dbe 100644 --- a/player.lua +++ b/player.lua @@ -38,8 +38,7 @@ Player = { jumptimer = 0.16, jumpnumber = 2, -- Keys - controller = nil, - controller_empty = {isDown = function () return false end}, + controlset = nil, -- HUD portrait_sprite = nil, portrait_sheet = require "portraits", @@ -81,22 +80,16 @@ function Player:delete() self.sprite = nil end --- Controller -function Player:assignController(controller) - self.controller = controller or nil - controller:setParent(self) +-- Control set managment +function Player:assignControlSet(set) + self.controlset = set or nil end - -function Player:getController() - if self.controller ~= nil then - return self.controller - else - return self.controller_empty - end +function Player:getControlSet() + return self.controlset end -- Update callback of `Player` -function Player:update (dt) +function Player:update(dt) -- hotfix? for destroyed bodies if self.body:isDestroyed() then return end -- # LOCALS @@ -120,8 +113,7 @@ function Player:update (dt) -- # HORIZONTAL MOVEMENT -- Walking - local controller = self:getController() - if controller:isDown("left") then + if Controller.isDown(self:getControlSet(), "left") then self.facing = -1 self.body:applyForce(-250, 0) -- Controlled speed limit @@ -129,7 +121,7 @@ function Player:update (dt) self.body:applyForce(250, 0) end end - if controller:isDown("right") then + if Controller.isDown(self:getControlSet(), "right") then self.facing = 1 self.body:applyForce(250, 0) -- Controlled speed limit @@ -139,8 +131,8 @@ function Player:update (dt) end -- Custom linear damping - if not controller:isDown("left") and - not controller:isDown("right") + if not Controller.isDown(self:getControlSet(), "left") and + not Controller.isDown(self:getControlSet(), "right") then local face = nil if x < -12 then @@ -164,7 +156,7 @@ function Player:update (dt) -- Thank you De Morgan! if self.current.repeated or not (self.frame == self.current.frames) then self.frame = (self.frame % self.current.frames) + 1 - elseif controller:isDown("right") or controller:isDown("left") then + elseif Controller.isDown(self:getControlSet(), "right") or Controller.isDown(self:getControlSet(), "left") then -- If nonrepeatable animation is finished and player is walking self:changeAnimation("walk") elseif self.current == self.animations.damage then @@ -209,11 +201,11 @@ function Player:update (dt) end end --- Keypressed callback (I think?) of `Player` -function Player:controllerPressed (key) - local controller = self:getController() +-- Controller callbacks +function Player:controlpressed(set, action, key) + if set ~= self:getControlSet() then return end -- Jumping - if key == "jump" then + if action == "jump" then if self.jumpnumber > 0 then -- General jump logics self.jumpactive = true @@ -239,7 +231,7 @@ function Player:controllerPressed (key) end -- Walking - if (key == "left" or key == "right") and + 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 @@ -247,16 +239,16 @@ function Player:controllerPressed (key) end -- Punching - if key == "attack" and self.punchcd <= 0 then + if action == "attack" and self.punchcd <= 0 then local f = self.facing self.salto = false - if controller:isDown("up") then + if Controller.isDown(self:getControlSet(), "up") then -- Punch up if self.current ~= self.animations.damage then self:changeAnimation("attack_up") end self:hit(-f,-18,4*f,10, 0, -1) - elseif controller:isDown("down") then + elseif Controller.isDown(self:getControlSet(), "down") then -- Punch down if self.current ~= self.animations.damage then self:changeAnimation("attack_down") @@ -272,19 +264,16 @@ function Player:controllerPressed (key) end end end - --- Keyreleased callback (I think?) of `Player` -function Player:controllerReleased (key) - local controller = self:getController() +function Player:controlreleased(set, action, key) + if set ~= self:getControlSet() then return end -- Jumping - if key == "jump" then + if action == "jump" then self.jumpactive = false self.jumptimer = Player.jumptimer -- take initial from metatable end - -- Walking - if (key == "left" or key == "right") and not - (controller:isDown("left") or controller:isDown("right")) and + if (action == "left" or action == "right") and not + (Controller.isDown(self:getControlSet(), "left") or Controller.isDown(self:getControlSet(), "right")) and self.current == self.animations.walk then self:changeAnimation("idle") @@ -292,7 +281,7 @@ function Player:controllerReleased (key) end -- Draw of `Player` -function Player:draw (offset_x, offset_y, scale, debug) +function Player:draw(offset_x, offset_y, scale, debug) -- locals local offset_x = offset_x or 0 local offset_y = offset_y or 0 @@ -320,12 +309,13 @@ end -- Draw HUD of `Player` -- elevation: 1 bottom, 0 top -function Player:drawHUD (x,y,scale,elevation) +function Player:drawHUD(x,y,scale,elevation) -- hud displays only if player is alive if self.alive then love.graphics.setColor(255,255,255,255) love.graphics.draw(self.portrait_sprite, self.portrait_sheet[self.name].normal, x*scale, y*scale, 0, scale, scale) local dy = 30 * elevation + love.graphics.setFont(Font) love.graphics.print((self.combo*10).."%",(x+2)*scale,(y-3+dy)*scale,0,scale,scale) love.graphics.print(math.max(0, self.lives),(x+24)*scale,(y-3+dy)*scale,0,scale,scale) end diff --git a/world.lua b/world.lua index 735f854..e4ade94 100644 --- a/world.lua +++ b/world.lua @@ -121,7 +121,7 @@ function World:spawnNauts(...) for _,naut in pairs(nauts) do local x,y = self:getSpawnPosition() local spawn = self:createNaut(x, y, naut[1]) - spawn:assignController(naut[2]) + spawn:assignControlSet(naut[2]) end end @@ -230,6 +230,12 @@ function World:onNautKilled(naut) end end +function World:getBounce(f) + local f = f or 1 + return math.sin(self.win_move*f*math.pi) +end + +-- LÖVE2D callbacks -- Update ZU WARUDO function World:update(dt) -- Physical world @@ -276,12 +282,6 @@ function World:update(dt) self.win_move = self.win_move - 2 end end - -function World:getBounce(f) - local f = f or 1 - return math.sin(self.win_move*f*math.pi) -end - -- Draw function World:draw() -- Camera stuff @@ -372,6 +372,7 @@ function World:draw() end end +-- Box2D callbacks -- beginContact function World.beginContact(a, b, coll) if a:getCategory() == 1 then @@ -390,7 +391,6 @@ function World.beginContact(a, b, coll) end end end - -- endContact function World.endContact(a, b, coll) if a:getCategory() == 1 then @@ -406,11 +406,17 @@ function World:controlpressed(set, action, key) local map = self:getMapName() local nauts = {} for _,naut in pairs(self:getNautsAll()) do - table.insert(nauts, {naut.name, naut.controller}) + table.insert(nauts, {naut.name, naut:getControlSet()}) end local new = World:new(map, nauts) changeScene(new) end + for k,naut in pairs(self:getNautsAll()) do + naut:controlpressed(set, action, key) + end end function World:controlreleased(set, action, key) + for k,naut in pairs(self:getNautsAll()) do + naut:controlpressed(set, action, key) + end end \ No newline at end of file -- cgit v1.1