3 Commits

Author SHA1 Message Date
507f06fb8c more efficient bullet spawning 2025-06-28 22:59:51 -07:00
14849101dd skip call to sparks if there are no sparks
Then don't check for nil spark colors in sparks itself.

Also uses a more verbose but more efficient exit check, and fixes a bug.
2025-06-28 22:49:10 -07:00
2cd7c64dd9 micro-optimize bullet details
Shots that miss are most likely to not be there yet, so check Y-top first.

If I assume shots will never be larger than 16 pixels per side, there is
no reason to check the ship's genuine width and height.
2025-06-28 22:35:36 -07:00

View File

@ -539,7 +539,7 @@ function ship_m:move()
if (self.dead) return; if (self.dead) return;
self:refresh_shield() self:refresh_shield()
local dx, dy, shoot_spec1, shoot_spec2 = self:act() local dx, dy, shoot_spec1, shoot_spec2 = self:act()
local sg, xm, ym = self.special_guns, self.xmomentum, self.ymomentum local sg, xm, ym, sp = self.special_guns, self.xmomentum, self.ymomentum, self.sparks
dx = self:constrain(self.x, xm, self.xmin, self.xmax, dx) dx = self:constrain(self.x, xm, self.xmin, self.xmax, dx)
dy = self:constrain(self.y, ym, self.ymin, self.ymax, dy) dy = self:constrain(self.y, ym, self.ymin, self.ymax, dy)
self:maybe_shoot(self.main_gun) self:maybe_shoot(self.main_gun)
@ -547,7 +547,7 @@ function ship_m:move()
if (shoot_spec1) self:maybe_shoot(sg[1]) if (shoot_spec1) self:maybe_shoot(sg[1])
if (shoot_spec2) self:maybe_shoot(sg[2]) if (shoot_spec2) self:maybe_shoot(sg[2])
end end
spark(self.sparks, self.x + 4*self.size, self.y + 4*self.size, dx*2.5, dy*2.5, self.sparkodds) if(sp) spark(sp, self.x + 4*self.size, self.y + 4*self.size, dx*2.5, dy*2.5, self.sparkodds)
xm = self:calc_velocity(xm, dx) xm = self:calc_velocity(xm, dx)
ym = self:calc_velocity(ym, dy) ym = self:calc_velocity(ym, dy)
@ -618,14 +618,6 @@ end
-->8 -->8
-- bullet and gun behaviors -- bullet and gun behaviors
function player_blt_cat()
return pbullets
end
function enemy_blt_cat()
return ebullets
end
-- x, y: position -- x, y: position
-- dx, dy: movement (linear) -- dx, dy: movement (linear)
-- f: frames remaining; nil for no limit -- f: frames remaining; nil for no limit
@ -638,9 +630,9 @@ end
-- details, check impl -- details, check impl
-- damage -- damage to do to -- damage -- damage to do to
-- a ship that gets hit -- a ship that gets hit
-- category -- function that -- category -- string naming
-- returns which bullet list -- which bullet list to spawn
-- to spawn onto -- onto, from _ENV
-- hitship -- event handler, -- hitship -- event handler,
-- takes ship as argument. -- takes ship as argument.
-- default: die, return true. -- default: die, return true.
@ -775,7 +767,7 @@ end
function bullet_base:move() function bullet_base:move()
local x,y = self.x + self.dx, self.y+self.dy local x,y = self.x + self.dx, self.y+self.dy
self.x,self.y=x,y self.x,self.y=x,y
return (y<=128) and (y >= -(self.height<<3)) and (x <= 128) and (x >= -(self.width<<3)) return (y<=128) and (y >= -16) and (x <= 128) and (x >= -16)
end end
function bullet_base:draw() function bullet_base:draw()
@ -785,7 +777,7 @@ end
function bullet_base:spawn_at(x, y) function bullet_base:spawn_at(x, y)
self.x = x - self.x_off self.x = x - self.x_off
self.y = y - self.y_off self.y = y - self.y_off
add(self.category(), self) add(_ENV[self.category], self)
end end
function gun_base:shoot(x, y) function gun_base:shoot(x, y)
@ -855,7 +847,7 @@ zap_p = mknew(bullet_base.new{
hitship = const_fxn(true), hitship = const_fxn(true),
category = player_blt_cat, category = "pbullets",
}) })
zap_gun_p = mknew(gun_base.new{ zap_gun_p = mknew(gun_base.new{
@ -914,7 +906,7 @@ blast = mknew(bullet_base.new{
end) end)
end end
end, end,
category=player_blt_cat category="pbullets"
}) })
blast_gun = mknew(gun_base.new{ blast_gun = mknew(gun_base.new{
@ -953,14 +945,14 @@ protron_e = mknew(bullet_base.new{
y_off = 4, y_off = 4,
damage = 1, damage = 1,
category = enemy_blt_cat, category = "ebullets",
}) })
protron_p = mknew(protron_e.new{ protron_p = mknew(protron_e.new{
sprite=23, sprite=23,
dym = -1, dym = -1,
y_off = 0, y_off = 0,
category=player_blt_cat, category="pbullets",
}) })
protron_gun_e = mknew(gun_base.new{ protron_gun_e = mknew(gun_base.new{
@ -1007,13 +999,13 @@ vulcan_e = mknew(bullet_base.new{
y_off = 0, y_off = 0,
damage = 0.5, damage = 0.5,
category=enemy_blt_cat category="ebullets"
}) })
vulcan_p = mknew(vulcan_e.new{ vulcan_p = mknew(vulcan_e.new{
sprite=22, sprite=22,
y_off = 4, y_off = 4,
category=player_blt_cat category="pbullets"
}) })
vulcan_gun_e = mknew(gun_base.new{ vulcan_gun_e = mknew(gun_base.new{
@ -1352,10 +1344,10 @@ end
-- box: x1, y1, x2, y2 -- box: x1, y1, x2, y2
function collides(b1, b2) function collides(b1, b2)
return return
b1.x1<=b2.x2 b1.y1<=b2.y2
and b1.y1<=b2.y2
and b1.x2>=b2.x1
and b1.y2>=b2.y1 and b1.y2>=b2.y1
and b1.x1<=b2.x2
and b1.x2>=b2.x1
end end
collider = mknew{ collider = mknew{
@ -1637,7 +1629,7 @@ function spark_particle:draw()
end end
function spark(sprs, x, y, dx, dy, odds, fg) function spark(sprs, x, y, dx, dy, odds, fg)
if (sprs==nil or flr(rnd(odds) or (abs(dx) < 0.5 and abs(dy))) ~= 0) return if ((dx > -0.5 and dx < 0.5 and dy > -0.5 and dy < 0.5) or rnd(odds) >= 1) return
local target = fg and intangibles_fg or intangibles_bg local target = fg and intangibles_fg or intangibles_bg
target[#target+1] = spark_particle.new{ target[#target+1] = spark_particle.new{
x = x + rnd(4) - 2, x = x + rnd(4) - 2,
@ -1656,7 +1648,7 @@ xp_gem = mknew(bullet_base.new{
dy = 0.75, dy = 0.75,
width=1, -- not used for spr but width=1, -- not used for spr but
height=1,-- bullet_base uses it height=1,-- bullet_base uses it
category = enemy_blt_cat, category = "ebullets",
damage = 0, damage = 0,
hurt = { hurt = {
x_off = -2, x_off = -2,