vision/scene/game.lua
2022-10-13 18:18:27 +02:00

132 lines
No EOL
4 KiB
Lua

GameScene = Object.extend(Object)
function GameScene:new()
end
function GameScene:init()
GameScene:playerChanged(currentPlayer)
movementDelta = 0
end
function GameScene:update(dt)
GameScene:updateInput(dt)
GameScene:updateBounds(dt)
end
function GameScene:draw()
love.graphics.setColor(255,255,255, 1)
love.graphics.draw(
currentPlayer.ship.image,
currentPlayer.px,
currentPlayer.py,
currentPlayer.rot*(math.pi/180),
1, 1,
currentPlayer.ship.image:getWidth()/2,
currentPlayer.ship.image:getHeight()/2
)
love.graphics.print(currentPlayer.px..","..currentPlayer.py.." s: " .. currentPlayer.speed .. " a: " ..currentPlayer.acc,10,10)
end
function GameScene:drawHud()
love.graphics.setColor(255,255,255, 1)
love.graphics.draw( gameHud, 0, 600-66 )
love.graphics.setColor(255,255,255, 0.75)
love.graphics.draw( currentPlayer.image, 14, 550, 0, 0.5, 0.5 )
love.graphics.setColor( text.r, text.g, text.b, text.alpha )
love.graphics.print( currentPlayer.name, 48, 550 )
love.graphics.print( currentPlayer:getShipname(), 48, 566 )
end
function GameScene:keypressed(key,unicode)
if key == "1" then
GameScene:playerChanged(CaptainSteve)
elseif key == "2" then
GameScene:playerChanged(CaptainRobert)
elseif key == "3" then
GameScene:playerChanged(CaptainJohn)
end
end
function GameScene:playerChanged(to)
currentPlayer = to
currentPlayer.px = windowWidth/2
currentPlayer.py = windowHeight/2
currentPlayer.rot = 0
currentPlayer.acc = 0
currentPlayer.bounds = { x1= 20, y1= 20, x2= windowWidth-30, y2= windowHeight - 86}
end
function GameScene:updateInput(dt)
movementDelta = movementDelta + dt
if (movementDelta > 0.02) then
movementDelta = 0
local isUp = false
local isLeft = false
local isDown = false
local isRight = false
local rotation = 0
--local hAxis, vAxis, hAxis2, vAxis2 = love.joystick.getAxes(1)
--local axisMargin = 0.5
isUp = love.keyboard.isDown( "w" )-- or (not (vAxis == nil) and vAxis < -(1-axisMargin)) or (not (vAxis2 == nil) and vAxis2 < -(1-axisMargin)) or love.joystick.isDown(1, 1)
isDown = love.keyboard.isDown( "s" )-- or (not (vAxis == nil) and vAxis > (1-axisMargin)) or (not (vAxis2 == nil) and vAxis2 > (1-axisMargin)) or love.joystick.isDown(1, 2)
isLeft = love.keyboard.isDown( "a" )-- or (not (hAxis == nil) and hAxis < -(1-axisMargin)) or (not (hAxis2 == nil) and hAxis2 < -(1-axisMargin)) or love.joystick.isDown(1, 3)
isRight = love.keyboard.isDown( "d" )-- or (not (hAxis == nil) and hAxis > (1-axisMargin)) or (not (hAxis2 == nil) and hAxis2 > (1-axisMargin)) or love.joystick.isDown(1, 4)
if isUp and isLeft then
rotation = 315
elseif isUp and isRight then
rotation = 45
elseif isUp then
rotation = 0
elseif isDown and isLeft then
rotation = 225
elseif isDown and isRight then
rotation = 135
elseif isDown then
rotation = 180
elseif isLeft then
rotation = 270
elseif isRight then
rotation = 90
end
currentPlayer.rot = rotation
if isUp or isDown or isLeft or isRight then
currentPlayer.acc = currentPlayer.acc + currentPlayer.accIncr
elseif currentPlayer.acc > 0 then
currentPlayer.acc = currentPlayer.acc - currentPlayer.accIncr
end
currentPlayer.acc = math.clamp(0, currentPlayer.acc, 1)
if isUp then
currentPlayer.py = currentPlayer.py - (currentPlayer.speed*currentPlayer.acc)
end
if isDown then
currentPlayer.py = currentPlayer.py + (currentPlayer.speed*currentPlayer.acc)
end
if isLeft then
currentPlayer.px = currentPlayer.px - (currentPlayer.speed*currentPlayer.acc)
end
if isRight then
currentPlayer.px = currentPlayer.px + (currentPlayer.speed*currentPlayer.acc)
end
end
end
function GameScene:updateBounds(dt)
currentPlayer.px = math.clamp( currentPlayer.bounds.x1, currentPlayer.px, currentPlayer.bounds.x2 )
currentPlayer.py = math.clamp( currentPlayer.bounds.y1, currentPlayer.py, currentPlayer.bounds.y2 )
end