Rocket-Bytes/Source/Player.lua

165 lines
4.9 KiB
Lua
Raw Normal View History

2022-04-19 21:31:46 +00:00
import "CoreLibs/math"
2022-05-14 16:34:53 +00:00
import "CoreLibs/animation"
2022-05-15 05:42:52 +00:00
import "CoreLibs/animator"
import "CoreLibs/timer"
import "CoreLibs/easing"
2022-04-19 21:31:46 +00:00
local velocity = {x=0,y=0}
local lerpmnt <const> = 0.5
local grav <const> = 0.2
2022-04-23 23:54:00 +00:00
scale = 1
2022-04-24 18:15:38 +00:00
local dead = false
local exists = false
local active = false
local gfx <const> = playdate.graphics
local imgRocket = gfx.image.new("gfx/rocket")
2022-05-14 16:34:53 +00:00
local imgFire = gfx.imagetable.new("gfx/fire")
assert(imgRocket)
2022-05-15 00:31:57 +00:00
sprRocket = gfx.sprite.new(imgRocket)
2022-05-14 16:34:53 +00:00
local loopFire = gfx.animation.loop.new(200,imgFire)
2022-05-18 05:09:02 +00:00
sprRocket:setCollideRect(9, 9, 10, 10)
local startpos = {x=0,y=0}
2022-04-23 23:54:00 +00:00
local imgBigRocket = gfx.image.new("gfx/bigrocket")
2022-05-18 05:09:02 +00:00
local imgBigFire = gfx.imagetable.new("gfx/bigrocketfire")
2022-04-23 23:54:00 +00:00
assert(imgBigRocket)
2022-05-18 05:09:02 +00:00
assert(imgBigFire)
local loopBigFire = gfx.animation.loop.new(200,imgBigFire)
2022-04-23 23:54:00 +00:00
local sprBigRocket = gfx.sprite.new(imgBigRocket)
sprBigRocket:setCollideRect(8,8,48,48)
sprBigRocket:setGroups({2})
function addPlayer(_x,_y,__x,__y)
2022-04-27 02:40:39 +00:00
song:stop()
song:load("sfx/song2")
song:play(0)
exists = true
2022-04-24 18:15:38 +00:00
dead = false
2022-04-23 23:54:00 +00:00
scale = 1
active = false
velocity = {x=0,y=0}
startpos = {x= _x,y=_y}
2022-04-23 23:54:00 +00:00
sprBigRocket:moveTo(__x,__y)
2022-05-18 05:09:02 +00:00
sprBigRocket:setImage(imgBigRocket)
2022-04-23 23:54:00 +00:00
sprBigRocket:add()
sprRocket:moveTo(_x,_y)
sprRocket:add()
2022-05-07 20:31:34 +00:00
sprRocket:setVisible(true)
end
function killPlayer()
exists = false
active = false
sprRocket:remove()
2022-04-23 06:54:45 +00:00
gfx.setDrawOffset(0,0)
end
2022-04-23 23:54:00 +00:00
local function playerWin()
2022-05-14 16:34:53 +00:00
if active then
song:stop()
song:load("sfx/song3")
song:play(1)
2022-05-15 05:42:52 +00:00
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
2022-05-18 05:09:02 +00:00
map = next
2022-05-15 05:42:52 +00:00
end
2022-05-18 05:09:02 +00:00
playdate.datastore.delete("savegame")
2022-05-15 05:42:52 +00:00
playdate.datastore.write({savedLevel=map,savedDeaths=deaths},"savegame")
totalEnergy = 0
showEnergy = false
energy = 0
if next == nil then
mainMenuCreation()
createMenu(mainmenu)
next = nil
else
next = nil
killBlades()
killPlayer()
2022-05-18 05:09:02 +00:00
addMapSave(map, false)
2022-05-15 05:42:52 +00:00
end
end)
2022-05-14 16:34:53 +00:00
end
2022-04-23 23:54:00 +00:00
active = false
exists = false
2022-04-24 18:15:38 +00:00
dead = true
2022-05-15 05:42:52 +00:00
2022-04-23 23:54:00 +00:00
end
local function die()
2022-05-07 20:31:34 +00:00
deaths +=1
active = false
velocity = {x=0,y=0}
sprRocket:moveTo(startpos.x, startpos.y)
end
function updatePlayer()
2022-05-15 05:42:52 +00:00
playdate.timer.updateTimers()
2022-05-14 16:40:45 +00:00
sprRocket:setImage(imgRocket)
if active == true then
2022-05-13 04:34:12 +00:00
if playdate.buttonIsPressed(playdate.kButtonUp) or playdate.buttonIsPressed(playdate.kButtonA) then
2022-04-23 06:54:45 +00:00
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
2022-05-20 17:01:59 +00:00
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))
2022-04-24 18:15:38 +00:00
elseif dead == true then
2022-05-18 05:09:02 +00:00
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())
2022-04-24 18:15:38 +00:00
if scale > 0.1 then
scale -= 0.05
else
sprRocket:setVisible(false)
2022-04-24 18:15:38 +00:00
end
end
2022-04-23 23:54:00 +00:00
updateExit()
sprRocket:setScale(scale,scale)
2022-05-14 16:34:53 +00:00
sprRocket:setRotation(playdate.getCrankPosition())
2022-05-18 05:09:02 +00:00
if active and (playdate.buttonIsPressed(playdate.kButtonA) or playdate.buttonIsPressed(playdate.kButtonUp)) then
sprRocket:setImage(loopFire:image())
end
2022-04-23 23:54:00 +00:00
end
function updateExit()
2022-05-15 00:31:57 +00:00
if sprBigRocket:alphaCollision(sprRocket) and energy == totalEnergy then
2022-04-23 23:54:00 +00:00
playerWin()
end
2022-05-15 05:42:52 +00:00
end