From e2a30253dd4fa264bcfb5c883087c049fd11cde5 Mon Sep 17 00:00:00 2001 From: annabunches Date: Sun, 1 Oct 2023 01:48:06 -0400 Subject: [PATCH] Add Ika weaponry, lots of cleanup, new art assets. --- src/bullet.lua | 10 +++++----- src/enemy/ebi.lua | 10 +++++----- src/enemy/ika.lua | 26 +++++++++++++++++++++++--- src/entity.lua | 5 +++-- src/kani.lua | 27 ++++++++++++++++----------- src/main.lua | 26 +++++++++++++------------- 6 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/bullet.lua b/src/bullet.lua index bc91f53..91bada1 100644 --- a/src/bullet.lua +++ b/src/bullet.lua @@ -4,18 +4,18 @@ import "CoreLibs/graphics" import "CoreLibs/sprites" local gfx = playdate.graphics +local geom = playdate.geometry class("Bullet").extends(gfx.sprite) - --- Bullet's defaults assume a friendly bullet +-- Bullet's defaults assume a (slow, weak) enemy bullet function Bullet:init(size, damage, vector, collisionMask) Bullet.super.init(self) self.power = power self.damage = damage or 1 - self.vector = vector or {x=5, y=0} - local mask = collisionMask or 0x4 + self.vector = vector or geom.vector2D.new(-1, 0) + local mask = collisionMask or 0x2 local img = gfx.image.new(size, size) gfx.pushContext(img) @@ -31,7 +31,7 @@ function Bullet:init(size, damage, vector, collisionMask) end function Bullet:update() - local collisions = select(3, self:moveWithCollisions(self.x+self.vector.x, self.y+self.vector.y)) + local collisions = select(3, self:moveWithCollisions(self.x+self.vector.dx, self.y+self.vector.dy)) for i=1, #collisions, 1 do -- anything the bullet can collide with should be damaged by it local obj = collisions[i].other diff --git a/src/enemy/ebi.lua b/src/enemy/ebi.lua index 0760638..f03bbf8 100644 --- a/src/enemy/ebi.lua +++ b/src/enemy/ebi.lua @@ -5,12 +5,12 @@ import "CoreLibs/sprites" import "entity" local gfx = playdate.graphics +local geom = playdate.geometry class("Ebi").extends(Entity) function Ebi:init() - local img = gfx.image.new(10, 10, gfx.kColorBlack) - Ebi.super.init(self, img, 5) + Ebi.super.init(self, gfx.image.new("images/ebi.png"), 5) self:setCollidesWithGroupsMask(0x3) local dir = 1 @@ -18,11 +18,11 @@ function Ebi:init() dir = -1 end - self.vector = {x=0, y=dir} + self.vector = geom.vector2D.new(0, dir) self.weaponTimer = playdate.timer.new(2500, function() - local b = Bullet(2, 1, {x=-1, y=0}, 0x2) + local b = Bullet(2, 1) b:moveTo(self.x - (self.width/2) - 1, self.y) b:add() end @@ -34,7 +34,7 @@ function Ebi:update() local collisions = Ebi.super.update(self) for i=1, #collisions, 1 do if collisions[i].other:getGroupMask() == 0x1 then - self.vector.y *= -1 + self.vector.dy *= -1 end end end diff --git a/src/enemy/ika.lua b/src/enemy/ika.lua index 3026391..f7ef6bc 100644 --- a/src/enemy/ika.lua +++ b/src/enemy/ika.lua @@ -2,13 +2,33 @@ import "CoreLibs/object" import "CoreLibs/graphics" import "CoreLibs/sprites" +import "CoreLibs/timer" import "entity" local gfx = playdate.graphics +local geom = playdate.geometry class("Ika").extends(Entity) -function Ika:init() - local img = gfx.image.new(50, 50, gfx.kColorBlack) - Ika.super.init(self, img, 25, 1) +function Ika:init(target) + Ika.super.init(self, gfx.image.new("images/ika.png"), 25, 1) + + self:setCollidesWithGroupsMask(0x2) + + self.weaponTimer = playdate.timer.new(7000, + function() + local b = Bullet(9, 20, self:calculateVector(target)) + b:moveTo(self.x - (self.width/2) - 1, self.y) + b:add() + end + ) + self.weaponTimer.repeats = true +end + +function Ika:calculateVector(target) + local vec = geom.vector2D.new(0,0) + vec.dx = target.x - self.x + vec.dy = target.y - self.y + + return vec:normalized() * 3 end diff --git a/src/entity.lua b/src/entity.lua index 296af25..dcfd8c8 100644 --- a/src/entity.lua +++ b/src/entity.lua @@ -4,6 +4,7 @@ import "CoreLibs/graphics" import "CoreLibs/sprites" local gfx = playdate.graphics +local geom = playdate.geometry class("Entity").extends(gfx.sprite) @@ -14,7 +15,7 @@ function Entity:init(img, health, armor) -- movement direction, every update() the entity will move along this vector and return -- collision data to the subclass - self.vector = {x=0,y=0} + self.vector = geom.vector2D.new(0, 0) self:setCollideRect(0, 0, self:getSize()) @@ -35,7 +36,7 @@ function Entity:damage(amount) end function Entity:update() - local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y)) + local collisions = select(3, self:moveWithCollisions(self.x + self.vector.dx, self.y + self.vector.dy)) return collisions end diff --git a/src/kani.lua b/src/kani.lua index 013bc93..44421c0 100644 --- a/src/kani.lua +++ b/src/kani.lua @@ -8,6 +8,7 @@ import "bullet" import "ui" local gfx = playdate.graphics +local geom = playdate.geometry local BASE_CHARGE_FACTOR = 15 local BASE_WEAPON_CHARGE_SPEED = 1000 @@ -39,8 +40,7 @@ local POWER_DAMAGE_LOOKUP = { class("Kani").extends(Entity) function Kani:init(ui) - local img = gfx.image.new("images/kani.png") - Kani.super.init(self, img, 100) + Kani.super.init(self, gfx.image.new("images/kani.png"), 100) self:setGroupMask(0x2) self:setCollidesWithGroupsMask(0xd) @@ -60,14 +60,14 @@ function Kani:init(ui) self.ui = ui self.inputHandlers = { - upButtonDown = function() self.vector.y = -3 end, - downButtonDown = function() self.vector.y = 3 end, - leftButtonDown = function() self.vector.x = -3 end, - rightButtonDown = function() self.vector.x = 3 end, - upButtonUp = function() self.vector.y = 0 end, - downButtonUp = function() self.vector.y = 0 end, - leftButtonUp = function() self.vector.x = 0 end, - rightButtonUp = function() self.vector.x = 0 end, + upButtonDown = function() self.vector.dy = -3 end, + downButtonDown = function() self.vector.dy = 3 end, + leftButtonDown = function() self.vector.dx = -3 end, + rightButtonDown = function() self.vector.dx = 3 end, + upButtonUp = function() self.vector.dy = 0 end, + downButtonUp = function() self.vector.dy = 0 end, + leftButtonUp = function() self.vector.dx = 0 end, + rightButtonUp = function() self.vector.dx = 0 end, cranked = function(change, accelChange) self:chargeReserve(change) end, AButtonDown = function() self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(self.weaponChargeSpeed / 2, @@ -111,7 +111,12 @@ function Kani:fire() return end - local bullet = Bullet(POWER_SIZE_LOOKUP[self.weaponPower], POWER_DAMAGE_LOOKUP[self.weaponPower]) + local bullet = Bullet( + POWER_SIZE_LOOKUP[self.weaponPower], + POWER_DAMAGE_LOOKUP[self.weaponPower], + geom.vector2D.new(5,0), + 0x4 + ) bullet:moveTo(self.x+16, self.y) bullet:add() self.weaponPower = 0 diff --git a/src/main.lua b/src/main.lua index 859156c..d2ecc52 100644 --- a/src/main.lua +++ b/src/main.lua @@ -25,25 +25,25 @@ function setup() player:add() ui:add() - local enemy = Ika() + local enemy = Ika(player) enemy:moveTo(350, 120) enemy:add() - enemy = Ebi() - enemy:moveTo(270, 50) - enemy:add() + -- enemy = Ebi() + -- enemy:moveTo(270, 50) + -- enemy:add() - enemy = Ebi() - enemy:moveTo(280, 100) - enemy:add() + -- enemy = Ebi() + -- enemy:moveTo(280, 100) + -- enemy:add() - enemy = Ebi() - enemy:moveTo(290, 150) - enemy:add() + -- enemy = Ebi() + -- enemy:moveTo(290, 150) + -- enemy:add() - enemy = Ebi() - enemy:moveTo(300, 200) - enemy:add() + -- enemy = Ebi() + -- enemy:moveTo(300, 200) + -- enemy:add() makeWalls() drawBackground()