162 lines
4.9 KiB
Lua
162 lines
4.9 KiB
Lua
import "CoreLibs/math"
|
|
import "CoreLibs/animation"
|
|
import "CoreLibs/animator"
|
|
import "CoreLibs/timer"
|
|
import "CoreLibs/easing"
|
|
|
|
local velocity = {x=0,y=0}
|
|
local lerpmnt <const> = 0.5
|
|
local grav <const> = 0.2
|
|
scale = 1
|
|
local dead = false
|
|
|
|
local exists = false
|
|
local active = false
|
|
local gfx <const> = playdate.graphics
|
|
local imgRocket = gfx.image.new("gfx/rocket")
|
|
local imgFire = gfx.imagetable.new("gfx/fire")
|
|
assert(imgRocket)
|
|
sprRocket = gfx.sprite.new(imgRocket)
|
|
local loopFire = gfx.animation.loop.new(200,imgFire)
|
|
sprRocket:setCollideRect(9, 9, 10, 10)
|
|
local startpos = {x=0,y=0}
|
|
|
|
local imgBigRocket = gfx.image.new("gfx/bigrocket")
|
|
local imgBigFire = gfx.imagetable.new("gfx/bigrocketfire")
|
|
assert(imgBigRocket)
|
|
assert(imgBigFire)
|
|
local loopBigFire = gfx.animation.loop.new(200,imgBigFire)
|
|
local sprBigRocket = gfx.sprite.new(imgBigRocket)
|
|
sprBigRocket:setCollideRect(8,8,48,48)
|
|
sprBigRocket:setGroups({2})
|
|
|
|
function addPlayer(_x,_y,__x,__y)
|
|
exists = true
|
|
dead = false
|
|
scale = 1
|
|
active = false
|
|
velocity = {x=0,y=0}
|
|
startpos = {x= _x,y=_y}
|
|
sprBigRocket:moveTo(__x,__y)
|
|
sprBigRocket:setImage(imgBigRocket)
|
|
sprBigRocket:add()
|
|
sprRocket:moveTo(_x,_y)
|
|
sprRocket:add()
|
|
sprRocket:setVisible(true)
|
|
end
|
|
|
|
function killPlayer()
|
|
exists = false
|
|
active = false
|
|
sprRocket:remove()
|
|
sprBigRocket:remove()
|
|
gfx.setDrawOffset(0,0)
|
|
end
|
|
|
|
local function playerWin()
|
|
if active then
|
|
song:stop()
|
|
song:load("sfx/song3")
|
|
song:play(1)
|
|
local startPos = playdate.geometry.point.new(sprRocket.x, sprRocket.y)
|
|
local endPos = playdate.geometry.point.new(sprBigRocket.x, sprBigRocket.y)
|
|
sprRocket:setAnimator(gfx.animator.new(800, startPos, endPos))
|
|
|
|
rocketfly = playdate.timer.new(6000, function(timer)
|
|
local stPos = playdate.geometry.point.new(sprBigRocket.x, sprBigRocket.y)
|
|
local ndPos = playdate.geometry.point.new(sprBigRocket.x, sprBigRocket.y - 1000)
|
|
sprBigRocket:setAnimator(gfx.animator.new(3000,stPos,ndPos, playdate.easingFunctions.inCubic))
|
|
end)
|
|
|
|
song:setFinishCallback(function()
|
|
if next then
|
|
map = next
|
|
end
|
|
totalEnergy = 0
|
|
showEnergy = false
|
|
energy = 0
|
|
if mode == "game" and next then
|
|
playdate.datastore.delete("savegame")
|
|
playdate.datastore.write({savedLevel=map,savedDeaths=deaths},"savegame")
|
|
next = nil
|
|
killBlades()
|
|
killPlayer()
|
|
addMapSave(map, false)
|
|
else
|
|
mainMenuCreation()
|
|
createMenu(mainmenu)
|
|
next = nil
|
|
end
|
|
end)
|
|
|
|
end
|
|
active = false
|
|
exists = false
|
|
dead = true
|
|
|
|
end
|
|
|
|
local function die()
|
|
deaths +=1
|
|
active = false
|
|
velocity = {x=0,y=0}
|
|
sprRocket:moveTo(startpos.x, startpos.y)
|
|
end
|
|
|
|
function updatePlayer()
|
|
playdate.timer.updateTimers()
|
|
sprRocket:setImage(imgRocket)
|
|
if active == true then
|
|
if playdate.buttonIsPressed(playdate.kButtonUp) or playdate.buttonIsPressed(playdate.kButtonA) then
|
|
velocity.x = velocity.x + math.sin(math.rad(playdate.getCrankPosition())) /2
|
|
velocity.y = velocity.y - math.cos(math.rad(playdate.getCrankPosition())) /2
|
|
end
|
|
sprRocket:moveBy(velocity.x,velocity.y)
|
|
|
|
local cx, cy = gfx.getDrawOffset()
|
|
gfx.setDrawOffset(playdate.math.lerp(cx,(-sprRocket.x + 200), lerpmnt), playdate.math.lerp(cy,(-sprRocket.y + 120), lerpmnt))
|
|
|
|
sprRocket:setRotation(0)
|
|
sprRocket:update()
|
|
--print(#sprRocket:overlappingSprites())
|
|
|
|
velocity.y += grav
|
|
|
|
if #sprRocket:overlappingSprites() > 0 then
|
|
die()
|
|
end
|
|
|
|
elseif exists == true then
|
|
if (playdate.buttonJustPressed(playdate.kButtonUp) or playdate.buttonJustPressed(playdate.kButtonA)) and playdate.isCrankDocked() == false then
|
|
active = true
|
|
end
|
|
local cx, cy = gfx.getDrawOffset()
|
|
gfx.setDrawOffset(playdate.math.lerp(cx,(-sprRocket.x + 200), lerpmnt), playdate.math.lerp(cy,(-sprRocket.y + 120), lerpmnt))
|
|
elseif dead == true then
|
|
local cx, cy = gfx.getDrawOffset()
|
|
gfx.setDrawOffset(playdate.math.lerp(cx,(-sprRocket.x + 200), 0.1), playdate.math.lerp(cy,(-sprRocket.y + 120), 0.1))
|
|
sprBigRocket:setImage(loopBigFire:image())
|
|
if scale > 0.1 then
|
|
scale -= 0.05
|
|
else
|
|
sprRocket:setVisible(false)
|
|
end
|
|
end
|
|
updateExit()
|
|
|
|
sprRocket:setScale(scale,scale)
|
|
|
|
|
|
sprRocket:setRotation(playdate.getCrankPosition())
|
|
|
|
if active and (playdate.buttonIsPressed(playdate.kButtonA) or playdate.buttonIsPressed(playdate.kButtonUp)) then
|
|
sprRocket:setImage(loopFire:image())
|
|
end
|
|
end
|
|
|
|
function updateExit()
|
|
if sprBigRocket:alphaCollision(sprRocket) and energy == totalEnergy then
|
|
playerWin()
|
|
end
|
|
end
|
|
|