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 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, 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, 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 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 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, 20, 30, screen) function playdate.update() playdate.graphics.clear() if playdate.isCrankDocked() then playdate.ui.crankIndicator:draw() end if playdate.buttonIsPressed(playdate.kButtonUp) then player:translate(10) end if playdate.buttonIsPressed(playdate.kButtonDown) then player:translate(-10) end if playdate.buttonIsPressed(playdate.kButtonLeft) then player:size(2) end if playdate.buttonIsPressed(playdate.kButtonRight) then player:size(-2) end player:handle_crank() player:draw() end