From f8add9c47ff1d378197e27b1abc6559ad347b2a7 Mon Sep 17 00:00:00 2001 From: Matthew Ryan Dillon Date: Thu, 24 Apr 2025 20:04:43 -0400 Subject: [PATCH] simple projectiles --- main.lua | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/main.lua b/main.lua index 3b67b03..0e5d629 100644 --- a/main.lua +++ b/main.lua @@ -11,9 +11,26 @@ local function clamp_angle(value) return value end +local function make_projectile(position) + local projectile = { position=table.deepcopy(position), speed=3 } + + projectile.draw = function(self, id) + local current_width = playdate.graphics.getLineWidth() + playdate.graphics.setLineWidth(2) + local angle_rad = math.rad(self.position.theta) + local dx, dy = self.speed * math.cos(angle_rad), self.speed * math.sin(angle_rad) + local new_x, new_y = self.position.x + dx, self.position.y + dy + playdate.graphics.drawLine(self.position.x,self.position.y, new_x,new_y) + playdate.graphics.setLineWidth(current_width) + self.position.x, self.position.y = new_x, new_y + end + + return projectile +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 } + local player = { position={ x=x, y=y, theta=crankPosition}, width=width, height=height, screen=screen, projectiles={} } player.draw = function(self) local w, h, transform = self.width / 2, self.height / 2, playdate.geometry.affineTransform.new() @@ -22,6 +39,10 @@ local function make_player(x, y, width, height, screen) transform:rotate(self.position.theta, self.position.x, self.position.y) local transformedPolygon = transform:transformedPolygon(polygon) playdate.graphics.drawPolygon(transformedPolygon) + + for i, projectile in ipairs(self.projectiles) do + projectile:draw(i) + end end player.translate = function(self, offset_theta, amount) @@ -42,6 +63,11 @@ local function make_player(x, y, width, height, screen) self:rotate(change) end + player.shoot_projectile = function(self) + local projectile = make_projectile(self.position) + table.insert(self.projectiles, projectile) + end + return player end @@ -58,6 +84,8 @@ function playdate.update() if playdate.buttonIsPressed(playdate.kButtonLeft) then player:translate(270, 1) end if playdate.buttonIsPressed(playdate.kButtonRight) then player:translate(90, 1) end + if playdate.buttonJustPressed(playdate.kButtonA) then player:shoot_projectile() end + player:handle_crank() player:draw() end \ No newline at end of file