simple projectiles

This commit is contained in:
Matthew Ryan Dillon 2025-04-24 20:04:43 -04:00
parent 0c553b0739
commit f8add9c47f

View file

@ -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