diff --git a/main.lua b/main.lua index a1ce104..3b67b03 100644 --- a/main.lua +++ b/main.lua @@ -5,6 +5,12 @@ local function clamp(value, min, max) return math.max(math.min(value, max), min) end +local function clamp_angle(value) + if value < 0 then value = value + 360 end + if value > 360 then value = value - 360 end + return value +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 } @@ -18,19 +24,17 @@ local function make_player(x, y, width, height, screen) 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)) + player.translate = function(self, offset_theta, amount) + local w, h, theta = self.width / 2, self.height / 2, self.position.theta + local angle_rad = math.rad(clamp_angle(theta + offset_theta)) + local new_x = self.position.x + (amount * math.cos(angle_rad)) + local new_y = self.position.y + (amount * math.sin(angle_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 + self.position.theta = clamp_angle(self.position.theta + degrees) end player.handle_crank = function(self) @@ -38,14 +42,6 @@ local function make_player(x, y, width, height, screen) 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 @@ -56,11 +52,11 @@ 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.kButtonUp) then player:translate(0, 10) end + if playdate.buttonIsPressed(playdate.kButtonDown) then player:translate(180, 10) end - if playdate.buttonIsPressed(playdate.kButtonLeft) then player:size(2) end - if playdate.buttonIsPressed(playdate.kButtonRight) then player:size(-2) end + if playdate.buttonIsPressed(playdate.kButtonLeft) then player:translate(270, 1) end + if playdate.buttonIsPressed(playdate.kButtonRight) then player:translate(90, 1) end player:handle_crank() player:draw()