Skip to content

Commit

Permalink
better game controls - analogic available for joystick and touch!
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Feb 10, 2018
1 parent 00a117d commit 3dee081
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
30 changes: 28 additions & 2 deletions project/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local Terebi = requireLibrary("terebi")
local Gamestate = requireLibrary("hump/gamestate")

-- Game Version
GAME_VERSION = '1.0.4'
GAME_VERSION = '1.0.5'

-- a variable for debug flags
debug_mode = false
Expand Down Expand Up @@ -289,6 +289,20 @@ end
-- Logic
function love.update( dt )
scrBtn:update(dt)
if scrBtn.pressed.left == true then
keys_pressed['left_amount'] = scrBtn.pressed.left_amount
end
if scrBtn.pressed.right == true then
keys_pressed['right_amount'] = scrBtn.pressed.right_amount
end
if scrBtn.pressed.down == true then
keys_pressed['down_amount'] = scrBtn.pressed.down_amount
end
if scrBtn.pressed.up == true then
keys_pressed['up_amount'] = scrBtn.pressed.up_amount
end


if scrBtn.previouPressed.left ~= true and scrBtn.pressed.left == true then
keys_pressed['left'] = true
elseif scrBtn.previouPressed.left == true and scrBtn.pressed.left ~= true then
Expand Down Expand Up @@ -325,6 +339,9 @@ function love.update( dt )
-- It returns 0 when it is at rest

if p1joystick:getGamepadAxis("leftx")<-0.2 then
keys_pressed['left_amount'] = math.abs(p1joystick:getGamepadAxis("leftx"))
keys_pressed['right_amount'] = nil

if keys_previousGamepad['left']~=true then
keys_pressed['left'] = true
keys_previousGamepad['left'] = true
Expand All @@ -337,7 +354,10 @@ function love.update( dt )
end


if p1joystick:getGamepadAxis("leftx")>0.2 then
if p1joystick:getGamepadAxis("leftx")>0.2 then
keys_pressed['right_amount'] = math.abs(p1joystick:getGamepadAxis("leftx"))
keys_pressed['left_amount'] = nil

if keys_previousGamepad['right']~=true then
keys_pressed['right'] = true
keys_previousGamepad['right'] = true
Expand All @@ -351,6 +371,9 @@ function love.update( dt )


if p1joystick:getGamepadAxis("lefty")>0.2 then
keys_pressed['down_amount'] = math.abs(p1joystick:getGamepadAxis("lefty"))
keys_pressed['up_amount'] = nil

if keys_previousGamepad['down']~=true then
keys_pressed['down'] = true
keys_previousGamepad['down'] = true
Expand All @@ -364,6 +387,9 @@ function love.update( dt )


if p1joystick:getGamepadAxis("lefty")<-0.2 then
keys_pressed['up_amount'] = math.abs(p1joystick:getGamepadAxis("lefty"))
keys_pressed['down_amount'] = nil

if keys_previousGamepad['up']~=true then
keys_pressed['up'] = true
keys_previousGamepad['up'] = true
Expand Down
38 changes: 37 additions & 1 deletion project/src/entities/ScreenButton.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

local Gamestate = requireLibrary("hump.gamestate")
local Class = requireLibrary("hump.class")
local lume = requireLibrary("lume")

local

ScreenButton = Class{
-- initializes the inventory
Expand Down Expand Up @@ -34,6 +37,18 @@ ScreenButton = Class{
self.isTouching = false
self.wasTouching = false
end,

pressedAmount = function (self, pressed, origin)
-- clam uses value, min, max
local valueToReturn = lume.clamp(math.abs(origin-pressed)/((1.6*(self.joy.r^2))^0.5),0,1 )

local deadzone = self.joy.deadzone / ((1.6*(self.joy.r^2))^0.5)
if valueToReturn < deadzone then
valueToReturn = 0
end

return valueToReturn
end,

update = function(self, dt)
if self.osString ~= "Android" and self.osString ~= "iOS" then
Expand Down Expand Up @@ -91,7 +106,8 @@ ScreenButton = Class{
self.pressed.buttona = nil
self.buttonA.color = {128,128,128,64}


-- when the player presses A, the directional must keep working
-- so for multitouch, we need this for to see every place being touched
for i, id in ipairs(touches) do
x, y = love.touch.getPosition(id)
sx = x / self.scale
Expand All @@ -105,31 +121,50 @@ ScreenButton = Class{
if(sx < self.joy.x-self.joy.deadzone ) then
self.pressed.left = true
self.pressed.right = nil
self.pressed.left_amount = self.pressedAmount(self,sx, self.joy.x)
self.pressed.right_amount = nil
elseif (sx > self.joy.x+self.joy.deadzone ) then
self.pressed.right = true
self.pressed.left = nil
self.pressed.right_amount = self.pressedAmount(self, sx, self.joy.x)
self.pressed.left_amount = nil
else
self.pressed.right = nil
self.pressed.left = nil
self.pressed.right_amount = nil
self.pressed.left_amount = nil
end


if(sy > self.joy.y+self.joy.deadzone ) then
self.pressed.down = true
self.pressed.up = nil
self.pressed.down_amount = self.pressedAmount(self, sy, self.joy.y)
self.pressed.up_amount = nil
elseif (sy < self.joy.y-self.joy.deadzone ) then
self.pressed.up = true
self.pressed.down = nil
self.pressed.up_amount = self.pressedAmount(self, sy, self.joy.y)
self.pressed.down_amount = nil
else
self.pressed.up = nil
self.pressed.down = nil
self.pressed.up_amount = nil
self.pressed.down_amount = nil
end

self.touchpos.x = sx
self.touchpos.y = sy

-- evaluates if center of joystick must be moved
if (self.joy.x-sx)^2 + (self.joy.y-sy)^2 > 1.6*(self.joy.r^2) then

-- move the center by the difference dragged. eg:
-- if player started draggin at 100,100 and 50 is limit,
-- when finger goes to 160,100, center moves to 110,100.
--
-- we need to calculate the distance as a modulo and then set
-- the direction vector separetely
local d_modulo = ((self.joy.x-sx)^2 + (self.joy.y-sy)^2 - 1.6*(self.joy.r^2))^0.5
local d_dx = (sx-self.joy.x)/((self.joy.x-sx)^2 + (self.joy.y-sy)^2)
local d_dy = (sy-self.joy.y)/((self.joy.x-sx)^2 + (self.joy.y-sy)^2)
Expand All @@ -142,6 +177,7 @@ ScreenButton = Class{
end
end

-- the accept button is much simpler, we just check for fingers in it's area
if (self.buttonA.x-sx)^2 + (self.buttonA.y-sy)^2 < 2*(self.buttonA.r^2) then
self.pressed.buttona = true
self.buttonA.color = {255,128,255,255}
Expand Down
60 changes: 55 additions & 5 deletions project/src/states/Game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,25 @@ function Game:update(dt)
if player.pos.y > 0 then
-- player.pos.y=player.pos.y-speed*dt
-- force_y = force_y - 400
vy = vy - acc
local amount = 1
if keys_pressed.up_amount ~= nil then
amount = keys_pressed.up_amount
end

vy = vy - acc*amount
end
end

if keys_pressed['down'] then
if player.pos.y < map.height*map.tileheight then
-- player.pos.y=player.pos.y+speed*dt
-- force_y = force_y + 400
vy = vy + acc
local amount = 1
if keys_pressed.down_amount ~= nil then
amount = keys_pressed.down_amount
end

vy = vy + acc*amount
end
end
else
Expand All @@ -679,15 +689,26 @@ function Game:update(dt)
if player.pos.x > 0 then
-- player.pos.x=player.pos.x-speed*dt
-- force_x = force_x - 400
vx = vx - acc
local amount = 1
if keys_pressed.left_amount ~= nil then
amount = keys_pressed.left_amount
end

vx = vx - acc*amount
end
end

if keys_pressed['right'] then
if player.pos.x < map.width*map.tilewidth then
-- player.pos.x=player.pos.x+speed*dt
-- force_x = force_x + 400
vx = vx + acc
local amount = 1
if keys_pressed.right_amount ~= nil then
amount = keys_pressed.right_amount
end


vx = vx + acc*amount
end
end
else
Expand Down Expand Up @@ -890,8 +911,37 @@ local function drawFn()
-- let's draw additional debug info
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.setFont(font_Verdana2)
love.graphics.print("DEBUG MODE",32,32)
love.graphics.print("player x="..player.pos.x..", y="..player.pos.y,32,8)

if keys_pressed.up_amount ~= nil then
love.graphics.setColor( 0, 0, 0, 255 )
love.graphics.print("up_a=" .. keys_pressed.up_amount,32,24)
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.print("up_a=" .. keys_pressed.up_amount,33,25)
end

if keys_pressed.down_amount ~= nil then
love.graphics.setColor( 0, 0, 0, 255 )
love.graphics.print("down_a=" .. keys_pressed.down_amount,32,32)
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.print("down_a=" .. keys_pressed.down_amount,33,33)
end


if keys_pressed.right_amount ~= nil then
love.graphics.setColor( 0, 0, 0, 255 )
love.graphics.print("right_a=" .. keys_pressed.right_amount,32,40)
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.print("right_a=" .. keys_pressed.right_amount,33,41)
end
if keys_pressed.left_amount ~= nil then
love.graphics.setColor( 0, 0, 0, 255 )
love.graphics.print("left_a=" .. keys_pressed.left_amount,32,48)
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.print("left_a=" .. keys_pressed.left_amount,33,49)
end
love.graphics.print("DEBUG MODE",32,150)

end

scrBtn:draw()
Expand Down

0 comments on commit 3dee081

Please sign in to comment.