Rocket-Bytes/Source/Player.lua
2022-04-23 21:25:48 -06:00

93 lines
No EOL
2.4 KiB
Lua

import "CoreLibs/math"
local velocity = {x=0,y=0}
local lerpmnt <const> = 0.5
local grav <const> = 0.2
scale = 1
local exists = false
local active = false
local gfx <const> = playdate.graphics
local imgRocket = gfx.image.new("gfx/rocket")
assert(imgRocket)
local sprRocket = gfx.sprite.new(imgRocket)
sprRocket:setCollideRect(7, 7, 14, 14)
local startpos = {x=0,y=0}
local imgBigRocket = gfx.image.new("gfx/bigrocket")
assert(imgBigRocket)
local sprBigRocket = gfx.sprite.new(imgBigRocket)
sprBigRocket:setCollideRect(8,8,48,48)
sprBigRocket:setGroups({2})
function addPlayer(_x,_y,__x,__y)
exists = true
scale = 1
active = false
velocity = {x=0,y=0}
startpos = {x= _x,y=_y}
sprBigRocket:moveTo(__x,__y)
sprBigRocket:add()
sprRocket:moveTo(_x,_y)
sprRocket:add()
end
function killPlayer()
exists = false
active = false
sprRocket:remove()
gfx.setDrawOffset(0,0)
end
local function playerWin()
active = false
exists = false
end
local function die()
active = false
velocity = {x=0,y=0}
sprRocket:moveTo(startpos.x, startpos.y)
end
function updatePlayer()
if active == true then
if playdate.buttonIsPressed(playdate.kButtonUp) 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.buttonIsPressed(playdate.kButtonUp) 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))
end
updateExit()
sprRocket:setScale(scale,scale)
sprRocket:setRotation(playdate.getCrankPosition())
end
function updateExit()
if sprBigRocket:alphaCollision(sprRocket) then
playerWin()
end
end