summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2016-05-26 15:33:46 +0200
committerAki <nthirtyone@gmail.com>2016-05-26 15:33:46 +0200
commit2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a (patch)
tree52f2e18d738dea5c3f220535981d350d83d54def
parenteb8c23fb8fff5c8faefa84e6f166bc0f3234a1ec (diff)
downloadroflnauts-2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a.zip
roflnauts-2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a.tar.gz
roflnauts-2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a.tar.bz2
Controller changes
-rw-r--r--controller.lua85
-rw-r--r--main.lua33
-rw-r--r--player.lua85
-rw-r--r--world.lua25
4 files changed, 168 insertions, 60 deletions
diff --git a/controller.lua b/controller.lua
new file mode 100644
index 0000000..808d660
--- /dev/null
+++ b/controller.lua
@@ -0,0 +1,85 @@
+-- `Controller`
+-- Used to manage controls
+
+-- Metashit
+Controller = {
+ joystick = nil,
+ left = "left",
+ right = "right",
+ up = "up",
+ down = "down",
+ attack = "return", -- accept
+ jump = "rshift", -- cancel
+ parent = nil,
+ down = nil
+}
+
+-- Constructor
+function Controller:new(joystick, ...)
+ local o = {}
+ setmetatable(o, self)
+ self.__index = self
+ if joystick ~= nil then
+ o.joystick = joystick
+ end
+ o:setBindings(...)
+ return o
+end
+
+function Controller:setBindings(...)
+ local left, right, up, down, attack, jump = ...
+ self.left = left or "left"
+ self.right = right or "right"
+ self.up = up or "up"
+ self.down = down or "down"
+ self.attack = attack or "return"
+ self.jump = jump or "rshift"
+end
+
+function Controller:setParent(parent)
+ self.parent = parent or nil
+end
+
+function Controller:testControl(control)
+ if control == self.left then
+ return "left"
+ elseif control == self.right then
+ return "right"
+ elseif control == self.up then
+ return "up"
+ elseif control == self.down then
+ return "down"
+ elseif control == self.attack then
+ return "attack"
+ elseif control == self.jump then
+ return "jump"
+ else
+ return nil
+ end
+end
+
+function Controller:keypressed(key, scancode)
+ if self.parent ~= nil and self.joystick == nil then
+ local control = self:testControl(key)
+ if control ~= nil then
+ self.parent:controllerPressed(control)
+ end
+ end
+end
+
+function Controller:keyreleased(key, scancode)
+ if self.parent ~= nil and self.joystick == nil then
+ local control = self:testControl(key)
+ if control ~= nil then
+ self.parent:controllerReleased(control)
+ end
+ end
+end
+
+function Controller:isDown(control)
+ if self.joystick == nil then
+ return love.keyboard.isDown(self[control])
+ else
+ return self.joystick:isGamepadDown(self[control])
+ end
+end \ No newline at end of file
diff --git a/main.lua b/main.lua
index 8a59bba..71ebe06 100644
--- a/main.lua
+++ b/main.lua
@@ -2,15 +2,13 @@
-- WHOLE CODE HAS FLAG OF "need a cleanup"
require "world"
-require "ground"
-require "player"
require "camera"
-require "cloud"
-require "effect"
+--require "menu"
+require "controller"
-- Temporary debug
debug = false
-third = "clunk"
+third = nil --"clunk"
fourth = nil --"yuri"
-- Load
@@ -18,15 +16,21 @@ function love.load ()
-- Graphics
love.graphics.setBackgroundColor(189, 95, 93)
love.graphics.setDefaultFilter("nearest", "nearest")
-
+
-- Font
Font = love.graphics.newImageFont("assets/font2.png", " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-,!:()[]{}<>", -1)
Font:setLineHeight(1)
love.graphics.setFont(Font)
-
+
-- ZU WARUDO!
w = World:new("default", "leon", "lonestar", third, fourth)
-
+
+ -- Controllers
+ cont1 = Controller:new()
+ cont2 = Controller:new(nil, "a", "d", "w", "s", "g", "h")
+ w.Nauts[1]:assignController(cont1)
+ w.Nauts[2]:assignController(cont2)
+
-- Temporary settings for second player
w.Nauts[2].key_left = "a"
w.Nauts[2].key_right = "d"
@@ -34,7 +38,7 @@ function love.load ()
w.Nauts[2].key_down = "s"
w.Nauts[2].key_jump = "g"
w.Nauts[2].key_hit = "f"
-
+
-- Temporary settings for third player
if third ~= nil then
w.Nauts[3].key_left = "kp4"
@@ -44,7 +48,7 @@ function love.load ()
w.Nauts[3].key_jump = "kp2"
w.Nauts[3].key_hit = "kp3"
end
-
+
-- Temporary settings for fourth player
if fourth ~= nil then
w.Nauts[4].key_left = "b"
@@ -54,6 +58,10 @@ function love.load ()
w.Nauts[4].key_jump = "k"
w.Nauts[4].key_hit = "l"
end
+
+ -- Menu bijaczes
+ --m = Menu:new()
+ --m:newSelector()
end
-- Update
@@ -64,6 +72,8 @@ end
-- KeyPressed
function love.keypressed (key)
w:keypressed(key)
+ cont1:keypressed(key)
+ cont2:keypressed(key)
-- Switch hitbox display on/off
if key == "x" then
debug = not debug
@@ -81,11 +91,14 @@ end
-- KeyReleased
function love.keyreleased(key)
w:keyreleased(key)
+ cont1:keyreleased(key)
+ cont2:keyreleased(key)
end
-- Draw
function love.draw ()
w:draw()
+ --m.selectors[1]:draw()
if debug then
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10)
end
diff --git a/player.lua b/player.lua
index 76d694f..3e94221 100644
--- a/player.lua
+++ b/player.lua
@@ -38,12 +38,7 @@ Player = {
jumpdouble = true,
jumptimer = 0.14,
-- Keys
- key_jump = "rshift",
- key_left = "left",
- key_right = "right",
- key_up = "up",
- key_down = "down",
- key_hit = "return",
+ controller = nil,
-- HUD
portrait_sprite = nil,
portrait_sheet = require "portraits"
@@ -78,6 +73,11 @@ function Player:new (game, world, x, y, name)
return o
end
+function Player:assignController(controller)
+ self.controller = controller or nil
+ controller:setParent(self)
+end
+
-- Update callback of `Player`
function Player:update (dt)
-- # VERTICAL MOVEMENT
@@ -87,38 +87,38 @@ function Player:update (dt)
self.body:setLinearVelocity(x,-160)
self.jumptimer = self.jumptimer - dt
end
-
+
-- Salto
if self.salto and (self.current == self.animations.walk or self.current == self.animations.idle) then
self.rotate = (self.rotate + 17 * dt * self.facing) % 360
elseif self.rotate ~= 0 then
self.rotate = 0
end
-
+
-- # HORIZONTAL MOVEMENT
-- Walking
local x,y = self.body:getLinearVelocity()
- if love.keyboard.isDown(self.key_left) then
+ local controller = self.controller
+ if controller:isDown("left") then
self.facing = -1
- self.body:applyForce(-200, 0)
+ self.body:applyForce(-250, 0)
-- Controlled speed limit
if x < -self.max_velocity then
- self.body:applyForce(200, 0)
+ self.body:applyForce(250, 0)
end
end
- if love.keyboard.isDown(self.key_right) then
+ if controller:isDown("right") then
self.facing = 1
- self.body:applyForce(200, 0)
+ self.body:applyForce(250, 0)
-- Controlled speed limit
if x > self.max_velocity then
- self.body:applyForce(-200, 0)
+ self.body:applyForce(-250, 0)
end
end
-
+
-- Custom linear damping
- if not self.inAir and
- not love.keyboard.isDown(self.key_left) and
- not love.keyboard.isDown(self.key_right)
+ if not controller:isDown("left") and
+ not controller:isDown("right")
then
local face = nil
if x < -12 then
@@ -128,9 +128,12 @@ function Player:update (dt)
else
face = 0
end
- self.body:applyForce(120*face,0)
+ self.body:applyForce(40*face,0)
+ if not self.inAir then
+ self.body:applyForce(80*face,0)
+ end
end
-
+
-- # ANIMATIONS
-- Animation
self.delay = self.delay - dt
@@ -139,14 +142,14 @@ 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 love.keyboard.isDown(self.key_right) or love.keyboard.isDown(self.key_left) then
+ elseif controller:isDown("right") or controller:isDown("left") then
-- If nonrepeatable animation is finished and player is walking
self:changeAnimation("walk")
elseif self.current == self.animations.damage then
self:changeAnimation("idle")
end
end
-
+
-- # DEATH
-- We all die in the end.
local m = self.world.map
@@ -164,11 +167,11 @@ function Player:update (dt)
if self.spawntimer <= 0 and not self.alive and self.lives >= 0 then
self:respawn()
end
-
+
-- # PUNCH
-- Cooldown
self.punchcd = self.punchcd - dt
-
+
-- Stop vertical
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
@@ -178,20 +181,21 @@ function Player:update (dt)
self.body:setLinearVelocity(32*self.facing,0)
end
end
-
+
if self.punchcd <= 0 and self.punchdir == 1 then
self.punchdir = 0
end
end
-- Keypressed callback (I think?) of `Player`
-function Player:keypressed (key)
+function Player:controllerPressed (key)
+ local controller = self.controller
-- Jumping
- if key == self.key_jump then
+ if key == "jump" then
if not self.inAir then
self:createEffect("jump")
self.jumpactive = true
- if (self.current == self.animations.attack) or
+ if (self.current == self.animations.attack) or
(self.current == self.animations.attack_up) or
(self.current == self.animations.attack_down) then
self:changeAnimation("idle")
@@ -203,23 +207,23 @@ function Player:keypressed (key)
self.salto = true
end
end
-
+
-- Walking
- if key == self.key_left or key == self.key_right then
+ if key == "left" or key == "right" then
self:changeAnimation("walk")
end
-
+
-- Punching
- if key == self.key_hit and self.punchcd <= 0 then
+ if key == "attack" and self.punchcd <= 0 then
local f = self.facing
self.salto = false
- if love.keyboard.isDown(self.key_up) then
+ if controller:isDown("up") then
-- Punch up
if self.current ~= self.animations.damage then
self:changeAnimation("attack_up")
end
self:hit(2*f,-10,3*f,7, 0, -1)
- elseif love.keyboard.isDown(self.key_down) and self.inAir then
+ elseif controller:isDown("down") and self.inAir then
-- Punch down
if self.current ~= self.animations.damage then
self:changeAnimation("attack_down")
@@ -237,16 +241,17 @@ function Player:keypressed (key)
end
-- Keyreleased callback (I think?) of `Player`
-function Player:keyreleased (key)
+function Player:controllerReleased (key)
+ local controller = self.controller
-- Jumping
- if key == self.key_jump then
+ if key == "jump" then
self.jumpactive = false
self.jumptimer = 0.12
end
-
+
-- Walking
- if (key == self.key_left or key == self.key_right) and not
- (love.keyboard.isDown(self.key_left) or love.keyboard.isDown(self.key_right)) and
+ if (key == "left" or key == "right") and not
+ (controller:isDown("left") or controller:isDown("right")) and
self.current == self.animations.walk
then
self:changeAnimation("idle")
@@ -331,7 +336,7 @@ function Player:damage (horizontal, vertical)
self:createEffect("hit")
local x,y = self.body:getLinearVelocity()
self.body:setLinearVelocity(x,0)
- self.body:applyLinearImpulse((28+12*self.combo)*horizontal, (60+10*self.combo)*vertical + 15)
+ self.body:applyLinearImpulse((32+12*self.combo)*horizontal, (68+10*self.combo)*vertical + 15)
self:changeAnimation("damage")
self.combo = math.min(20, self.combo + 1)
end
diff --git a/world.lua b/world.lua
index 6ef294c..6762bc6 100644
--- a/world.lua
+++ b/world.lua
@@ -3,6 +3,11 @@
-- WHOLE CODE HAS FLAG OF "need a cleanup"
+require "ground"
+require "player"
+require "cloud"
+require "effect"
+
-- Metatable of `World`
-- nils initialized in constructor
World = {
@@ -160,14 +165,14 @@ end
-- Keypressed
function World:keypressed(key)
for _,naut in pairs(self.Nauts) do
- naut:keypressed(key)
+ --naut:keypressed(key)
end
end
-- Keyreleased
function World:keyreleased(key)
for _,naut in pairs(self.Nauts) do
- naut:keyreleased(key)
+ --naut:keyreleased(key)
end
end
@@ -179,32 +184,32 @@ function World:draw()
love.graphics.setColor(self.map.color_mid)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()*0.8)
love.graphics.setColor(self.map.color_top)
- love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()*0.25)
-
+ love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()*0.25)
+
-- Camera stuff
local offset_x, offset_y = self.camera:getOffsets()
local scale = self.camera.scale
-
+
-- Draw clouds
for _,cloud in pairs(self.Clouds) do
cloud:draw(offset_x, offset_y, scale)
end
-
+
-- Draw effects
for _,effect in pairs(self.Effects) do
effect:draw(offset_x,offset_y, scale)
end
-
+
-- Draw player
for _,naut in pairs(self.Nauts) do
naut:draw(offset_x, offset_y, scale, debug)
end
-
+
-- Draw ground
for _,platform in pairs(self.Platforms) do
platform:draw(offset_x, offset_y, scale, debug)
end
-
+
-- draw center
if debug then
local c = self.camera
@@ -229,7 +234,7 @@ function World:draw()
local x2, y2 = c:translatePosition(cx+w, 0)
love.graphics.line(x1,y1,x2,y2)
end
-
+
-- Draw HUDs
for _,naut in pairs(self.Nauts) do
-- I have no idea where to place them T_T