vision/scene/game.lua

283 lines
No EOL
8.5 KiB
Lua

GameScene = Object.extend(Object)
function GameScene:new()
end
function GameScene:init()
GameScene:playerChanged(currentPlayer)
movementDelta = 0
love.audio.setPosition( windowWidth/2, windowHeight/2,0)
pSystem = love.graphics.newParticleSystem(boosterParticle,128)
pSystem:setParticleLifetime(0,0.6)
pSystem:setLinearAcceleration(0,20,0,25)
pSystem:setSpeed(40,50)
pSystem:setRotation(10,20)
pSystem:setSpin(10,20)
pSystem:setRadialAcceleration(10,20)
pSystem:setTangentialAcceleration(10,20)
pSystem:setSizeVariation(0.3)
pSystem:setSizes(1,0.9,0.8,0.7,0.5,0.3,0.1,0.05)
pSystem:setColors(
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,0.8,
1,1,1,0.6,
1,1,1,0.5,
1,1,1,0.4,
1,1,1,0.3
)
eSystem = love.graphics.newParticleSystem(explosionParticle,64)
eSystem:setParticleLifetime(0,0.7)
eSystem:setLinearAcceleration(-2,-2,2,2)
eSystem:setSpeed(2,8)
eSystem:setRotation(1,7)
eSystem:setSpin(1,2)
eSystem:setRadialAcceleration(10,20)
eSystem:setTangentialAcceleration(10,20)
eSystem:setSizeVariation(0.9)
eSystem:setSizes(0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 0.6, 0.1)
eSystem:setColors(
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,0.8,
1,1,1,0.6,
1,1,1,0.4,
1,1,1,0.3
)
GameScene.explosionObj = {angle=100, radius=100}
GameScene.godsrayObj = {weight=0}
currentPlanet:init()
currentPlanet:draw()
end
function GameScene:update(dt)
if currentPlayer.death == false and currentPlanet.hasGeneratedMap == true then
pSystem:update(dt)
GameScene:updateInput(dt)
GameScene:updateBounds(dt)
GameScene:checkCollisions()
else
eSystem:update(dt)
end
currentPlanet:update(dt)
self.updateEffects();
end
function GameScene:draw()
love.graphics.setColor(1,1,1, 1)
if currentPlanet ~= nil and currentPlanet.canvas ~= nil then
love.graphics.draw(currentPlanet.canvas,0,0)
end
if currentPlayer.death == false then
love.graphics.setColor(1,1,1, 1)
love.graphics.draw(pSystem, currentPlayer.px-2, currentPlayer.py)
love.graphics.draw(pSystem, currentPlayer.px+2, currentPlayer.py)
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
)
else
love.graphics.draw(eSystem, currentPlayer.px, currentPlayer.py)
end
end
function GameScene:drawHud()
love.graphics.setColor(1,1,1, 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 )
love.graphics.print(
"glbl: " .. currentPlayer.px..","..currentPlayer.py.." s: " .. currentPlayer.ship.speed .. " a: " ..currentPlayer.acc,
10,10
)
local deathval = "0"
if currentPlayer.death then
deathval = "1"
end
love.graphics.print( "death: " .. deathval, 10, 24 )
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 = math.floor(windowWidth/2)
currentPlayer.py = math.floor(windowHeight/2)
currentPlayer.rot = 0
currentPlayer.acc = 0
currentPlayer.bounds = { x1= 20, y1= 20, x2= windowWidth-20, y2= windowHeight - 86}
currentPlayer.death = false
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 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
currentPlayer.rot = 315
particleRot = 45
elseif isUp and isRight then
currentPlayer.rot = 45
particleRot = 135
elseif isUp then
currentPlayer.rot = 0
particleRot = 90
elseif isDown and isLeft then
currentPlayer.rot = 225
particleRot = 315
elseif isDown and isRight then
currentPlayer.rot = 135
particleRot = 225
elseif isDown then
currentPlayer.rot = 180
particleRot = 270
elseif isLeft then
currentPlayer.rot = 270
particleRot = 0
elseif isRight then
currentPlayer.rot = 90
particleRot = 180
end
if isUp or isDown or isLeft or isRight then
currentPlayer.acc = currentPlayer.acc + currentPlayer.ship.accIncr
elseif currentPlayer.acc > 0 then
currentPlayer.acc = currentPlayer.acc - currentPlayer.ship.accIncr
end
currentPlayer.acc = math.clamp(0, currentPlayer.acc, 1)
if currentPlayer.acc > 0 then
pSystem:setDirection(particleRot*(math.pi/180))
pSystem:emit( 64 * currentPlayer.acc )
end
if isUp then
currentPlayer.py = math.floor(currentPlayer.py - (currentPlayer.ship.speed*currentPlayer.acc))
end
if isDown then
currentPlayer.py = math.floor(currentPlayer.py + (currentPlayer.ship.speed*currentPlayer.acc))
end
if isLeft then
currentPlayer.px = math.floor(currentPlayer.px - (currentPlayer.ship.speed*currentPlayer.acc))
end
if isRight then
currentPlayer.px = math.floor(currentPlayer.px + (currentPlayer.ship.speed*currentPlayer.acc))
end
end
end
function GameScene:updateBounds(dt)
currentPlayer.px = math.floor(math.clamp( currentPlayer.bounds.x1, currentPlayer.px, currentPlayer.bounds.x2 ))
currentPlayer.py = math.floor(math.clamp( currentPlayer.bounds.y1, currentPlayer.py, currentPlayer.bounds.y2 ))
end
function GameScene:checkCollisions()
-- Get basic alpha blended collision
local cp = currentPlanet.canvas:newImageData( nil, nil, currentPlayer.px, currentPlayer.py, 1, 1):getPixel( 0,0 )
if cp == 0 then
currentPlayer.death = true
self:generateExplosion( currentPlayer.px, currentPlayer.py )
end
end
function GameScene:generateExplosion(px,py)
GameScene:generateExplosionSound(px,py)
GameScene.explosionObj = {angle=100, radius=100}
GameScene.godsrayObj = {weight=0}
flux.to(GameScene.explosionObj, 0.5, {angle= 100, radius=1600}):ease("quintout"):after( GameScene.explosionObj, 0.5, { angle= 100, radius= 100 })
flux.to(GameScene.godsrayObj, 0.5, {weight=100}):ease("quintout"):after( GameScene.godsrayObj, 0.5, { weight=1 })
eSystem:emit(32)
end
function GameScene:updateEffects()
effect.chromasep.angle= math.floor(GameScene.explosionObj.angle/100)
effect.chromasep.radius= math.floor(GameScene.explosionObj.radius/100)
effect.godsray.weight= GameScene.godsrayObj.weight/100
end
function GameScene:generateExplosionSound(px,py)
local sound = sfxr.newSound()
sound:resetParameters()
sound.waveform = sfxr.WAVEFORM.NOISE
sound.frequency.start = GameScene:variance(0.19907648104897,150)
sound.frequency.slide = -0.057634852747835
sound.repeatspeed = 0
sound.envelope.attack = 0
sound.envelope.sustain = GameScene:variance(0.30484231377123, 150)
sound.envelope.punch = GameScene:variance(0.7393770288859, 150)
sound.envelope.decay = 0.43495283918775
sound.phaser.offset = GameScene:variance(0.53418301267674, 150)
sound.phaser.sweep = -0.26648187020582
sound.vibrato.depth = GameScene:variance(0.24860915303688, 150)
sound.vibrato.speed = 0.44703997451237
sound.change.speed = 0.83105037145914
sound.change.amount = -0.66873272021076
local soundData = sound:generateSoundData()
local source = love.audio.newSource(soundData)
source:setVolume(2)
source:setPosition(px,py)
source:play()
end
function GameScene:variance(seed, variance)
return seed * ((math.random( 100 - (variance/2), 100 + (variance/2)))/100)
end