squash: refactor bullets to remove enemy flag

commit ead2a7d874
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Mon Sep 2 14:45:56 2024 -0700

    fix remaining `vulcan`-family bug

commit 571412b15e
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Mon Sep 2 14:15:56 2024 -0700

    fix chasey xl offsets

commit 907bd8318c
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Mon Sep 2 14:12:35 2024 -0700

    several more fixes, now runs to the end but shot offset is wrong

commit 7170552448
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Mon Sep 2 13:41:42 2024 -0700

    first three waves of bug fixes

commit 01ab6d3969
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Mon Sep 2 12:59:49 2024 -0700

    maybe the rest of the refactor?

commit 7869192dee
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Tue Aug 20 01:21:13 2024 -0700

    partial refactor continued

commit a4658e3ef4
Author: Kistaro Windrider <kistaro@gmail.com>
Date:   Mon Aug 19 16:00:16 2024 -0700

    halfway through bullet refactor
This commit is contained in:
Kistaro Windrider 2024-09-02 14:46:51 -07:00
parent 60b685d94b
commit dd143060ac
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -1,5 +1,5 @@
pico-8 cartridge // http://www.pico-8.com pico-8 cartridge // http://www.pico-8.com
version 41 version 42
__lua__ __lua__
-- vacuum gambit -- vacuum gambit
-- by kistaro windrider -- by kistaro windrider
@ -18,6 +18,11 @@ function csv(s)
end end
return ret return ret
end end
function const_fxn(x)
return function()
return x
end
end
-- generate standard "overlay" -- generate standard "overlay"
-- constructor for type tt. -- constructor for type tt.
@ -118,7 +123,7 @@ end
function _init() function _init()
init_blip_pals() init_blip_pals()
wipe_level() wipe_level()
primary_ship.main_gun = zap_gun.new() primary_ship.main_gun = zap_gun_p.new() -- redundant?
load_level(example_level_csv) load_level(example_level_csv)
state = game state = game
pal(2,129) pal(2,129)
@ -580,9 +585,15 @@ end
-->8 -->8
-- bullet and gun behaviors -- bullet and gun behaviors
bullet_base = { function player_blt_cat()
enemyspd = 0.5 return pbullets
} end
function enemy_blt_cat()
return ebullets
end
bullet_base = { }
mknew(bullet_base) mknew(bullet_base)
gun_base = { gun_base = {
@ -601,18 +612,10 @@ end
function bullet_base:move() function bullet_base:move()
self.x += self.dx self.x += self.dx
if self.enemy then self.y += self.dy
self.y += self.dy if (self.y > 128) or (self.y < -8 * self.height) then
if self.y > 128 then self:die()
self:die() return true
return true
end
else
self.y -= self.dy
if self.y < -8*self.height then
self:die()
return true
end
end end
return false return false
end end
@ -621,19 +624,20 @@ function bullet_base:draw()
spr(self.sprite, self.x, self.y, self.width, self.height) spr(self.sprite, self.x, self.y, self.width, self.height)
end end
function bullet_base:spawn_at(x, y) -- An `actually_shoot` factory
self.x = x - self.center_x_off -- for trivial guns
if self.enemy then function spawn_one(t)
self.dx *= self.enemyspd return function(gun, x, y)
self.dy *= self.enemyspd t.new{}:spawn_at(x, y)
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)
end end
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) function gun_base:shoot(x, y)
if (lframe < self.shoot_ready) return false if (lframe < self.shoot_ready) return false
if self.ammo then if self.ammo then
@ -645,23 +649,12 @@ function gun_base:shoot(x, y)
return true return true
end 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 -->8
-- bullets and guns -- bullets and guns
zap = bullet_base.new{ zap_e = bullet_base.new{
--shape --shape
psprite = 8, --index of player ammo sprite sprite = 9, --index of enemy ammo sprite
esprite = 9, -- index of enemy ammo sprite
width = 1, --in 8x8 blocks width = 1, --in 8x8 blocks
height = 1, height = 1,
hurt = { -- hurtbox - where this ship can be hit hurt = { -- hurtbox - where this ship can be hit
@ -670,33 +663,43 @@ zap = bullet_base.new{
width = 2, width = 2,
height = 8 height = 8
}, },
center_x_off = 1, -- how to position by ship x_off = 1, -- how to position by ship
bottom_y_off = 0, y_off = 8,
top_y_off = 0,
damage = 1, damage = 1,
dx = 0, -- px/frame dx = 0, -- px/frame
dy = 8, dy = 4,
hitship = function(_, _) hitship = const_fxn(true),
return true
end category = enemy_blt_cat,
} }
mknew(zap) mknew(zap_e)
zap_gun = gun_base.new{ zap_p = zap_e.new{
enemy = false, sprite = 8,
dy = -8,
y_off = 0,
category = player_blt_cat,
}
mknew(zap_p)
zap_gun_e = gun_base.new{
power = 20, -- power consumed per shot power = 20, -- power consumed per shot
cooldown = 0x0.000a, -- frames between shots cooldown = 0x0.000a, -- frames between shots
ammo = nil, -- unlimited ammo - main gun 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{ blast = bullet_base.new{
--shape --shape
psprite = 12, --index of player ammo sprite sprite = 12, --index of player ammo sprite
esprite = 3, -- index of enemy ammo sprite
width = 1, --in 8x8 blocks width = 1, --in 8x8 blocks
height = 1, height = 1,
hurt = { -- hurtbox - where this ship can be hit hurt = { -- hurtbox - where this ship can be hit
@ -705,13 +708,12 @@ blast = bullet_base.new{
width = 6, width = 6,
height = 6 height = 6
}, },
center_x_off = 4, -- how to position by ship x_off = 4, -- how to position by ship
bottom_y_off = 0, y_off = 0,
top_y_off = 0,
damage = 4, damage = 4,
dx = 0, -- px/frame dx = 0, -- px/frame
dy = 2, dy = -2,
awaitcancel = false, awaitcancel = false,
-- disable damage for 2 frames -- disable damage for 2 frames
@ -736,25 +738,24 @@ blast = bullet_base.new{
self.awaitcancel = false self.awaitcancel = false
end) end)
end end
end end,
category=player_blt_cat
} }
mknew(blast) mknew(blast)
blast_gun = gun_base.new{ blast_gun = gun_base.new{
icon = 13, icon = 13,
enemy = false, power = 0, -- only cost is ammo
power = 0, -- ammo, not power
cooldown = 0x0.0020, -- frames between shots cooldown = 0x0.0020, -- frames between shots
ammo = 5, ammo = 5,
maxammo = 5, maxammo = 5,
t = blast -- type of bullet to fire actually_shoot = spawn_one(blast),
} }
mknew(blast_gun) mknew(blast_gun)
protron = bullet_base.new{ protron_e = bullet_base.new{
--shape --shape
psprite = 23, --index of player ammo sprite sprite = 24,
esprite = 24, -- index of enemy ammo sprite
width = 1, --in 8x8 blocks width = 1, --in 8x8 blocks
height = 1, height = 1,
hurt = { -- hurtbox - where this ship can be hit hurt = { -- hurtbox - where this ship can be hit
@ -763,56 +764,63 @@ protron = bullet_base.new{
width = 2, width = 2,
height = 2 height = 2
}, },
center_x_off = 1, -- how to position by ship x_off = 1, -- how to position by ship
bottom_y_off = 4, y_off = 4,
top_y_off = 0,
damage = 1, damage = 1,
dx = 0, -- px/frame dym = 0.5, -- gun sets dy;
dy = 3, -- this is mult
category = enemy_blt_cat,
} }
mknew(protron) mknew(protron_e)
protron_gun = gun_base.new{ protron_p = protron_e.new{
sprite=23,
dym = -1,
y_off = 0,
category=player_blt_cat,
}
mknew(protron_p)
protron_gun_e = gun_base.new{
icon = 25, icon = 25,
enemy = false,
power = 60, power = 60,
cooldown = 0x0.000f, -- frames between shots cooldown = 0x0.000f, -- frames between shots
ammo = nil, ammo = nil,
maxammo = nil, maxammo = nil,
actually_shoot = function(self, x, y) munition = protron_e
local sprite = protron.psprite
if (self.enemy) sprite=protron.esprite
for i=1,3 do
local b = protron.new{
enemy=self.enemy,
sprite=sprite,
dx = i,
dy = 4-i
}
b:spawn_at(x,y)
local b2 = protron.new{
enemy=self.enemy,
sprite=sprite,
dx = -i,
dy = 4-i
}
b2:spawn_at(x,y)
end
local bup = protron.new{
enemy=self.enemy,
sprite=sprite,
dy=4
}
bup:spawn_at(x,y)
end
} }
mknew(protron_gun) mknew(protron_gun_e)
vulcan = bullet_base.new{ function protron_gun_e:actually_shoot(x, y)
local m = self.munition.dym
for i=1,3 do
local b = self.munition.new{
dx = i*m,
dy = (4-i)*m,
}
b:spawn_at(x,y)
local b2 = self.munition.new{
dx = -i*m,
dy = (4-i)*m,
}
b2:spawn_at(x,y)
end
local bup = self.munition.new{
dx=0,
dy=4*m,
}
bup:spawn_at(x,y)
end
protron_gun_p = protron_gun_e.new{
munition = protron_p,
}
mknew(protron_gun_p)
vulcan_e = bullet_base.new{
--shape --shape
psprite = 22, --index of player ammo sprite sprite = 21,
esprite = 21, -- index of enemy ammo sprite
width = 1, --in 8x8 blocks width = 1, --in 8x8 blocks
height = 1, height = 1,
hurt = { -- hurtbox - where this ship can be hit hurt = { -- hurtbox - where this ship can be hit
@ -821,31 +829,37 @@ vulcan = bullet_base.new{
width = 1, width = 1,
height = 4 height = 4
}, },
center_x_off = 0.5, -- how to position by ship x_off = 0.5, -- how to position by ship
bottom_y_off = 4, y_off = 0,
top_y_off = 0,
damage = 0.5, damage = 0.5,
dx = 0, -- px/frame -- dx from gun
dy = 4, dy = 2,
category=enemy_blt_cat
} }
mknew(vulcan) mknew(vulcan_e)
vulcan_gun = gun_base.new{ vulcan_p = vulcan_e.new{
sprite=22,
y_off = 4,
dy = -4,
category=player_blt_cat
}
mknew(vulcan_p)
vulcan_gun_e = gun_base.new{
icon = 37, icon = 37,
enemy = false, enemy = false,
power = 8, power = 8,
cooldown = 0x0.0002, -- frames between shots cooldown = 0x0.0002, -- frames between shots
ammo = nil, ammo = nil,
maxammo = nil, maxammo = nil,
munition=vulcan_e,
dxs = {0.35, -0.35, -0.7, 0.7, 0.35, -0.35}, dxs = {0.35, -0.35, -0.7, 0.7, 0.35, -0.35},
xoffs = {1, 0, -1, 1, 0, -1}, xoffs = {1, 0, -1, 1, 0, -1},
dxidx = 1, dxidx = 1,
actually_shoot = function(self, x, y) actually_shoot = function(self, x, y)
local sprite = self.enemy and vulcan.esprite or vulcan.psprite local b = self.munition.new{
local b = vulcan.new{
enemy=self.enemy,
sprite=sprite,
dx = self.dxs[self.dxidx], dx = self.dxs[self.dxidx],
} }
b:spawn_at(self.xoffs[self.dxidx]+x,y) b:spawn_at(self.xoffs[self.dxidx]+x,y)
@ -853,7 +867,12 @@ vulcan_gun = gun_base.new{
if (self.dxidx > #self.dxs) self.dxidx = 1 if (self.dxidx > #self.dxs) self.dxidx = 1
end end
} }
mknew(vulcan_gun) mknew(vulcan_gun_e)
vulcan_gun_p = vulcan_gun_e.new{
munition=vulcan_p,
}
mknew(vulcan_gun_p)
-->8 -->8
--ships, including player --ships, including player
@ -915,7 +934,7 @@ player = ship_m.new{
} }
mknew(player, mknew(player,
function(p) function(p)
p.main_gun = zap_gun.new() p.main_gun = zap_gun_p.new()
-- ONE HIT MODE -- ONE HIT MODE
-- --
-- p.hp = 0 -- p.hp = 0
@ -1000,7 +1019,7 @@ spewy = frownie.new{
end end
} }
mknew(spewy, function(ship) mknew(spewy, function(ship)
ship.main_gun=ship.main_gun or protron_gun.new{enemy=true} ship.main_gun=ship.main_gun or protron_gun_e.new{enemy=true}
end) end)
chasey = ship_m.new{ chasey = ship_m.new{
@ -1028,7 +1047,7 @@ chasey = ship_m.new{
slip = true, slip = true,
} }
mknew(chasey, function(ship) mknew(chasey, function(ship)
ship.main_gun=ship.main_gun or zap_gun.new{enemy=true} ship.main_gun=ship.main_gun or zap_gun_e.new{}
end) end)
function chasey:act() function chasey:act()
@ -1046,6 +1065,8 @@ xl_chasey=chasey.new{
width = 12, width = 12,
height = 10 height = 10
}, },
fire_off_x = 8,
fire_off_y = 15,
hp = 19.5, hp = 19.5,
shield = 5, shield = 5,
boss = true, boss = true,
@ -1062,7 +1083,7 @@ xl_chasey=chasey.new{
end, end,
} }
mknew(xl_chasey, function(ship) mknew(xl_chasey, function(ship)
ship.main_gun=ship.main_gun or zap_gun.new{enemy=true} ship.main_gun=ship.main_gun or zap_gun_e.new{}
end) end)
-->8 -->8
-- collisions -- collisions
@ -1271,9 +1292,9 @@ end
function spawn_bonus_vulcan_chasey() function spawn_bonus_vulcan_chasey()
local c = spawn_chasey() local c = spawn_chasey()
c.main_gun=vulcan_gun.new{enemy=true} c.main_gun=vulcan_gun_e.new{enemy=true}
c.die = function(self) c.die = function(self)
spawn_main_gun_at(self.x-1, self.y-1, vulcan_gun) spawn_main_gun_at(self.x-1, self.y-1, vulcan_gun_p)
chasey.die(self) chasey.die(self)
end end
c.sprite=4 c.sprite=4
@ -1376,7 +1397,7 @@ example_level_csv=[[1,spawn_frownie
310,spawn_blocking_blocky 310,spawn_blocking_blocky
310,spawn_blocking_blocky 310,spawn_blocking_blocky
311,spawn_frownie 311,spawn_frownie
350,spawn_main_gun_at,70,-11,protron_gun 350,spawn_main_gun_at,70,-11,protron_gun_p
401,spawn_frownie 401,spawn_frownie
420,spawn_blocking_frownie 420,spawn_blocking_frownie
430,spawn_bonus_vulcan_chasey 430,spawn_bonus_vulcan_chasey
@ -1486,8 +1507,9 @@ bullets
shots much easier to dodge shots much easier to dodge
* damage - damage per hit; * damage - damage per hit;
used by ships used by ships
* psprite, esprite - index of * sprite - sprite index.
player or enemy sprite. * x_off, y_off - renamed for
the next two vars. may revert
* center_off_x - the horizontal * center_off_x - the horizontal
centerpoint of the bullet, centerpoint of the bullet,
for positioning when firing. for positioning when firing.
@ -1876,7 +1898,7 @@ powerup = bullet_base.new{
-- easy to pick up -- easy to pick up
dx = 0, dx = 0,
dy = 1.5, -- 0.75 after enemyspd dy = 1.5, -- 0.75 after enemyspd
enemy = true, -- collides with player ship category = enemy_blt_cat, -- collides with player ship
damage = 0, damage = 0,
anim_speed = 2, anim_speed = 2,
@ -1909,9 +1931,8 @@ repair = powerup.new{
width = 12, width = 12,
height = 12 height = 12
}, },
center_x_off = 4, x_off = 4,
top_y_off = 0, y_off = 0,
bottom_y_off = 0,
sprites = sheen8x8, sprites = sheen8x8,
hitship = function(self, ship) hitship = function(self, ship)
if (ship ~= primary_ship) return false if (ship ~= primary_ship) return false
@ -1937,9 +1958,8 @@ gun_swap = powerup.new{
height = 16 height = 16
}, },
-- gun = gun_type.new{} -- gun = gun_type.new{}
center_x_off = 6, x_off = 6,
top_y_off = 0, y_off = 0,
bottom_y_off = 4,
width = 2, width = 2,
height = 2, height = 2,
sprites = {64, 66, 68, 70, 72, 74, 76, 78}, sprites = {64, 66, 68, 70, 72, 74, 76, 78},