From 7116fcf1edc8a4cbeca6dfeea698a013f34db251 Mon Sep 17 00:00:00 2001 From: Matthew Ryan Dillon Date: Mon, 21 Apr 2025 16:49:12 -0400 Subject: [PATCH] rebooting with playdate sdk --- .luarc.json | 15 +++++--- main.lua | 105 ++++++++++++++++++++++++++-------------------------- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/.luarc.json b/.luarc.json index 0e02584..ad2360a 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,8 +1,11 @@ { - "runtime.special": { - "love.filesystem.load": "loadfile" - }, - "workspace.library": [ - "${3rd}/love2d/library" - ] + "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json", + "diagnostics.globals": ["import"], + "diagnostics.severity": { "duplicate-set-field": "Hint" }, + "format.defaultConfig": { "indent_style": "space", "indent_size": "4" }, + "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"] } \ No newline at end of file diff --git a/main.lua b/main.lua index 9c0b755..a1ce104 100644 --- a/main.lua +++ b/main.lua @@ -1,66 +1,67 @@ -local lg = love.graphics +import "CoreLibs/graphics" +import "CoreLibs/ui" + +local function clamp(value, min, max) + return math.max(math.min(value, max), min) +end local function make_player(x, y, width, height, screen) - local player = { position={ x=x, y=y, theta=-math.pi / 2 }, width=width, height=height, screen=screen } + local crankPosition = playdate.getCrankPosition() + local player = { position={ x=x, y=y, theta=crankPosition}, width=width, height=height, screen=screen } - 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 + player.draw = function(self) + local w, h, transform = self.width / 2, self.height / 2, playdate.geometry.affineTransform.new() + local polygon = playdate.geometry.polygon.new(-w,-0.5*h, -w,0.5*h, w,0, -w,-0.5*h) + transform:translate(self.position.x, self.position.y) + transform:rotate(self.position.theta, self.position.x, self.position.y) + local transformedPolygon = transform:transformedPolygon(polygon) + playdate.graphics.drawPolygon(transformedPolygon) + end - player.translate = function(self, amount) - local w, h = self.width / 2, self.height / 2 - local new_x = self.position.x + (amount * math.cos(self.position.theta)) - 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 - if new_x - w < 0 then new_x = w end - if new_y + h > self.screen.height then new_y = self.screen.height - h end - if new_y - h < 0 then new_y = h end - self.position.x = new_x - self.position.y = new_y - 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 - player.rotate = function(self, degrees) - local radians = math.rad(degrees) - local new_theta = self.position.theta + radians - if new_theta < 0 then new_theta = new_theta + (2 * math.pi) end - if new_theta > 2 * math.pi then new_theta = new_theta - (2 * math.pi) end - self.position.theta = new_theta - end + player.rotate = function(self, degrees) + local new_theta = self.position.theta + degrees + if new_theta < 0 then new_theta = new_theta + 360 end + if new_theta > 360 then new_theta = new_theta - 360 end + self.position.theta = new_theta + end - return player + 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 end 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() - love.window.setMode(screen.width, screen.height) -end +function playdate.update() + playdate.graphics.clear() + if playdate.isCrankDocked() then playdate.ui.crankIndicator:draw() end -function love.update(dt) - if love.keyboard.isDown("w") then - 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 + if playdate.buttonIsPressed(playdate.kButtonUp) then player:translate(10) end + if playdate.buttonIsPressed(playdate.kButtonDown) then player:translate(-10) end -function love.draw() - lg.setColor(1, 1, 1) - lg.rectangle("fill", 0, 0, screen.width, screen.height) + if playdate.buttonIsPressed(playdate.kButtonLeft) then player:size(2) end + if playdate.buttonIsPressed(playdate.kButtonRight) then player:size(-2) end - player:draw() + player:handle_crank() + player:draw() end \ No newline at end of file