rebooting with playdate sdk

This commit is contained in:
Matthew Ryan Dillon 2025-04-21 16:49:12 -04:00
parent 563818b0b7
commit 7116fcf1ed
2 changed files with 62 additions and 58 deletions

View file

@ -1,8 +1,11 @@
{ {
"runtime.special": { "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"love.filesystem.load": "loadfile" "diagnostics.globals": ["import"],
}, "diagnostics.severity": { "duplicate-set-field": "Hint" },
"workspace.library": [ "format.defaultConfig": { "indent_style": "space", "indent_size": "4" },
"${3rd}/love2d/library" "runtime.builtin": { "io": "disable", "os": "disable", "package": "disable" },
] "runtime.nonstandardSymbol": ["+=", "-=", "*=", "/=", "//=", "%=", "<<=", ">>=", "&=", "|=", "^="],
"runtime.version": "Lua 5.4",
"workspace.preloadFileSize": 1000,
"workspace.library": ["/Users/matthew/Developer/playdate-luacats"]
} }

View file

@ -1,66 +1,67 @@
local lg = love.graphics import "CoreLibs/graphics"
import "CoreLibs/ui"
local function make_player(x, y, width, height, screen) local function clamp(value, min, max)
local player = { position={ x=x, y=y, theta=-math.pi / 2 }, width=width, height=height, screen=screen } return math.max(math.min(value, max), min)
player.draw = function(self)
local w, h = self.width / 2, self.height / 2
lg.push()
lg.setColor(0, 0, 0)
lg.translate(self.position.x, self.position.y)
lg.rotate(self.position.theta)
lg.polygon("fill", -w,-h, -w,h, w,0)
lg.pop()
end end
player.translate = function(self, amount) local function make_player(x, y, width, height, screen)
local w, h = self.width / 2, self.height / 2 local crankPosition = playdate.getCrankPosition()
local new_x = self.position.x + (amount * math.cos(self.position.theta)) local player = { position={ x=x, y=y, theta=crankPosition}, width=width, height=height, screen=screen }
local new_y = self.position.y + (amount * math.sin(self.position.theta))
if new_x + w > self.screen.width then new_x = self.screen.width - w end player.draw = function(self)
if new_x - w < 0 then new_x = w end local w, h, transform = self.width / 2, self.height / 2, playdate.geometry.affineTransform.new()
if new_y + h > self.screen.height then new_y = self.screen.height - h end local polygon = playdate.geometry.polygon.new(-w,-0.5*h, -w,0.5*h, w,0, -w,-0.5*h)
if new_y - h < 0 then new_y = h end transform:translate(self.position.x, self.position.y)
self.position.x = new_x transform:rotate(self.position.theta, self.position.x, self.position.y)
self.position.y = new_y local transformedPolygon = transform:transformedPolygon(polygon)
playdate.graphics.drawPolygon(transformedPolygon)
end
player.translate = function(self, y_amount)
local w, h, theta_rad = self.width / 2, self.height / 2, math.rad(self.position.theta)
local new_x = self.position.x + (y_amount * math.cos(theta_rad))
local new_y = self.position.y + (y_amount * math.sin(theta_rad))
self.position.x = clamp(new_x, w, self.screen.width - w)
self.position.y = clamp(new_y, h, self.screen.height - h)
end end
player.rotate = function(self, degrees) player.rotate = function(self, degrees)
local radians = math.rad(degrees) local new_theta = self.position.theta + degrees
local new_theta = self.position.theta + radians if new_theta < 0 then new_theta = new_theta + 360 end
if new_theta < 0 then new_theta = new_theta + (2 * math.pi) end if new_theta > 360 then new_theta = new_theta - 360 end
if new_theta > 2 * math.pi then new_theta = new_theta - (2 * math.pi) end
self.position.theta = new_theta self.position.theta = new_theta
end end
player.handle_crank = function(self)
local change, _acceleratedChange = playdate.getCrankChange()
self:rotate(change)
end
player.size = function(self, amount)
self.width = clamp(self.width + amount, 2, 80)
self.height = clamp(self.height + amount, 2, 80)
local w, h = self.width / 2, self.height / 2
self.position.x = clamp(self.position.x, w, self.screen.width - w)
self.position.y = clamp(self.position.y, h, self.screen.height - h)
end
return player return player
end end
local screen = { width=400, height=240 } local screen = { width=400, height=240 }
local player = make_player(screen.width / 2, screen.height / 2, 10, 10, screen) local player = make_player(screen.width / 2, screen.height / 2, 20, 30, screen)
function love.load() function playdate.update()
love.window.setMode(screen.width, screen.height) playdate.graphics.clear()
end if playdate.isCrankDocked() then playdate.ui.crankIndicator:draw() end
function love.update(dt) if playdate.buttonIsPressed(playdate.kButtonUp) then player:translate(10) end
if love.keyboard.isDown("w") then if playdate.buttonIsPressed(playdate.kButtonDown) then player:translate(-10) end
player:translate(1)
end
if love.keyboard.isDown("s") then
player:translate(-1)
end
if love.keyboard.isDown("d") then
player:rotate(5)
end
if love.keyboard.isDown("a") then
player:rotate(-5)
end
end
function love.draw() if playdate.buttonIsPressed(playdate.kButtonLeft) then player:size(2) end
lg.setColor(1, 1, 1) if playdate.buttonIsPressed(playdate.kButtonRight) then player:size(-2) end
lg.rectangle("fill", 0, 0, screen.width, screen.height)
player:handle_crank()
player:draw() player:draw()
end end