Refactored player and tilemap into seperate scripts

This commit is contained in:
PossiblyAxolotl 2022-04-19 15:22:15 -06:00
parent 495a01c6ff
commit d06cc122b1
4 changed files with 65 additions and 52 deletions

View file

@ -5,71 +5,28 @@ import "CoreLibs/object"
import "CoreLibs/graphics" import "CoreLibs/graphics"
import "CoreLibs/sprites" import "CoreLibs/sprites"
import "CoreLibs/math" import "CoreLibs/math"
import "Player"
import "Tilemap"
-- Music -- Music
local song1 <const> = playdate.sound.sampleplayer.new("sfx/song1") local song1 <const> = playdate.sound.sampleplayer.new("sfx/song1")
-- Font Setup -- Font Setup
local font <const> = gfx.font.new("gfx/font") local font <const> = gfx.font.new("gfx/font")
gfx.setFont(font) gfx.setFont(font)
-- Tilemap Setup
local tileTable <const> = gfx.imagetable.new("gfx/tiles")
local tilemap <const> = gfx.tilemap.new()
tilemap:setImageTable(tileTable)
tilemap:setSize(100,100)
local level = json.decodeFile("levels/level1.rocketbyte")
for i = 1, #level.tiles, 1 do
tilemap:setTileAtPosition(level.tiles[i].x,level.tiles[i].y,level.tiles[i].t)
end
gfx.sprite.addWallSprites(tilemap, {0,1})
local velocity = {x=0,y=0}
local lerpmnt <const> = 0.5
local grav <const> = 0.2
local imgRocket = gfx.image.new("gfx/rocket")
assert(imgRocket)
local sprRocket = gfx.sprite.new(imgRocket)
sprRocket:setCollideRect(7, 7, 14, 14)
sprRocket:add()
gfx.setBackgroundColor(gfx.kColorBlack) gfx.setBackgroundColor(gfx.kColorBlack)
song1:play(0, 1) song1:play(0, 1)
addTiles("levels/level1.rocketbyte")
addPlayer(10,10)
function playdate.update() function playdate.update()
if playdate.buttonIsPressed(playdate.kButtonUp) then updatePlayer()
velocity.x = velocity.x + math.sin(math.rad(playdate.getCrankPosition()))
velocity.y = velocity.y - math.cos(math.rad(playdate.getCrankPosition()))
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())
sprRocket:setRotation(playdate.getCrankPosition())
velocity.y += grav
-- local stuff = sprRocket:allOverlappingSprites()
-- print(#stuff)
-- gfx.setDrawOffset(-sprRocket.x + 200, -sprRocket.y + 120)
gfx.clear(gfx.kColorBlack) gfx.clear(gfx.kColorBlack)
gfx.sprite.update() gfx.sprite.update()
tilemap:draw(2,1) drawTiles()
--gfx.drawText("AAAAAA LMAO 000 \nTEST",0,0)
end
function _draw()
end end

37
Source/Player.lua Normal file
View file

@ -0,0 +1,37 @@
local velocity = {x=0,y=0}
local lerpmnt <const> = 0.5
local grav <const> = 0.2
local gfx <const> = playdate.graphics
local active = false
local imgRocket = gfx.image.new("gfx/rocket")
assert(imgRocket)
local sprRocket = gfx.sprite.new(imgRocket)
sprRocket:setCollideRect(7, 7, 14, 14)
function addPlayer(x,y)
active = true
sprRocket:moveTo(x,y)
sprRocket:add()
end
function updatePlayer()
if active then
if playdate.buttonIsPressed(playdate.kButtonUp) then
velocity.x = velocity.x + math.sin(math.rad(playdate.getCrankPosition()))
velocity.y = velocity.y - math.cos(math.rad(playdate.getCrankPosition()))
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())
sprRocket:setRotation(playdate.getCrankPosition())
velocity.y += grav
end
end

19
Source/Tilemap.lua Normal file
View file

@ -0,0 +1,19 @@
local tileTable <const> = gfx.imagetable.new("gfx/tiles")
local tilemap <const> = gfx.tilemap.new()
local gfx <const> = playdate.graphics
tilemap:setImageTable(tileTable)
tilemap:setSize(100,100)
function addTiles(_file)
local level = json.decodeFile(_file)
for i = 1, #level.tiles, 1 do
tilemap:setTileAtPosition(level.tiles[i].x,level.tiles[i].y,level.tiles[i].t)
end
gfx.sprite.addWallSprites(tilemap, {0,1})
end
function drawTiles()
tilemap:draw(0,0)
end

View file

@ -1 +1 @@
{"tiles":[{"x":1,"y":1,"t":2},{"x":2,"y":1,"t":3},{"x":3,"y":1,"t":2}]} {"tiles":[{"x":1,"y":1,"t":2},{"x":2,"y":1,"t":3},{"x":3,"y":1,"t":3},{"x":4,"y":1,"t":2}]}