From a4658e3ef41cc5c9a79d3f207914147e5b07c2c7 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 19 Aug 2024 16:00:16 -0700 Subject: [PATCH] halfway through bullet refactor --- vacuum_gambit.p8 | 119 ++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/vacuum_gambit.p8 b/vacuum_gambit.p8 index e5712bc..3aed13b 100644 --- a/vacuum_gambit.p8 +++ b/vacuum_gambit.p8 @@ -18,6 +18,11 @@ function csv(s) end return ret end +function const_fxn(x) + return function() + return x + end +end -- generate standard "overlay" -- constructor for type tt. @@ -580,9 +585,15 @@ end -->8 -- bullet and gun behaviors -bullet_base = { - enemyspd = 0.5 -} +function player_blt_cat() + return pbullets +end + +function enemy_blt_cat() + return ebullets +end + +bullet_base = { } mknew(bullet_base) gun_base = { @@ -601,18 +612,10 @@ end function bullet_base:move() self.x += self.dx - if self.enemy then - self.y += self.dy - if self.y > 128 then - self:die() - return true - end - else - self.y -= self.dy - if self.y < -8*self.height then - self:die() - return true - end + self.y += self.dy + if (self.y > 128) or (self.y < -8 * self.height) then + self:die() + return true end return false end @@ -621,19 +624,20 @@ function bullet_base:draw() spr(self.sprite, self.x, self.y, self.width, self.height) end -function bullet_base:spawn_at(x, y) - self.x = x - self.center_x_off - if self.enemy then - self.dx *= self.enemyspd - self.dy *= self.enemyspd - self.y = y + self.top_y_off - ebullets:push_back(self) - else - self.y = y - (8 * self.height) + self.bottom_y_off - pbullets:push_back(self) +-- An `actually_shoot` factory +-- for trivial guns +function spawn_one(t) + return function(gun, x, y) + t.new{}:spawn_at(x, y) end end +function bullet_base:spawn_at(x, y) + self.x = x - self.x_off + self.y = y - self.y_off + self.category():push_back(self) +end + function gun_base:shoot(x, y) if (lframe < self.shoot_ready) return false if self.ammo then @@ -645,23 +649,12 @@ function gun_base:shoot(x, y) return true end -function gun_base:actually_shoot(x, y) - local typ = self.t - local b = typ.new{ - enemy = self.enemy, - sprite = self.enemy and typ.esprite or typ.psprite, - } - b:spawn_at(x, y) - return true -end - -->8 -- bullets and guns -zap = bullet_base.new{ +zap_e = bullet_base.new{ --shape - psprite = 8, --index of player ammo sprite - esprite = 9, -- index of enemy ammo sprite + sprite = 9, --index of enemy ammo sprite width = 1, --in 8x8 blocks height = 1, hurt = { -- hurtbox - where this ship can be hit @@ -670,33 +663,42 @@ zap = bullet_base.new{ width = 2, height = 8 }, - center_x_off = 1, -- how to position by ship - bottom_y_off = 0, - top_y_off = 0, + x_off = 1, -- how to position by ship + y_off = 0, damage = 1, dx = 0, -- px/frame - dy = 8, + dy = -4, - hitship = function(_, _) - return true - end + hitship = const_fxn(true), + + category = enemy_blt_cat, } -mknew(zap) +mknew(zap_e) -zap_gun = gun_base.new{ - enemy = false, +zap_p = zap_e.new{ + sprite = 8, + dy = 8, + category = player_blt_cat, +} +mknew(zap_p) + +zap_gun_e = gun_base.new{ power = 20, -- power consumed per shot cooldown = 0x0.000a, -- frames between shots ammo = nil, -- unlimited ammo - main gun - t = zap -- metatable of bullet to fire + actually_shoot = spawn_one(zap_e), } -mknew(zap_gun) +mknew(zap_gun_e) + +zap_gun_p = zap_gun_e.new{ + actually_shoot = spawn_one(zap_p), +} +mknew(zap_gun_p) blast = bullet_base.new{ --shape - psprite = 12, --index of player ammo sprite - esprite = 3, -- index of enemy ammo sprite + sprite = 12, --index of player ammo sprite width = 1, --in 8x8 blocks height = 1, hurt = { -- hurtbox - where this ship can be hit @@ -705,9 +707,8 @@ blast = bullet_base.new{ width = 6, height = 6 }, - center_x_off = 4, -- how to position by ship - bottom_y_off = 0, - top_y_off = 0, + x_off = 4, -- how to position by ship + y_off = 0, damage = 4, dx = 0, -- px/frame @@ -736,18 +737,18 @@ blast = bullet_base.new{ self.awaitcancel = false end) end - end + end, + category=player_blt_cat } mknew(blast) blast_gun = gun_base.new{ icon = 13, - enemy = false, - power = 0, -- ammo, not power + power = 0, -- only cost is ammo cooldown = 0x0.0020, -- frames between shots ammo = 5, maxammo = 5, - t = blast -- type of bullet to fire + actually_shoot = spawn_one(blast), } mknew(blast_gun)