Skip to content

Commit 978bc1f

Browse files
use dynamic allocation pattern for player bullets
1 parent 205ab19 commit 978bc1f

1 file changed

Lines changed: 62 additions & 48 deletions

File tree

os/programs/dragonfly.rrbasic

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ SPRITELOAD ship3, "/images/dragonfly/ship3.png"
1010
SPRITELOAD ship4, "/images/dragonfly/ship4.png"
1111
SPRITELOAD ship_bullet, "/images/dragonfly/ship_bullet.png"
1212
SPRITELOAD enemy_bullet, "/images/dragonfly/enemy_bullet.png"
13-
player_bullet_max = 32
13+
player_bullets = 0
14+
player_bullet_max = 1
1415
player_fire_cooldown = 1 ' Starts at one so we dont pick up spurious enter key
1516
player_fire_delay = 6 ' Rapid fire repeat delay in frames
1617
DIM player_bullet_x, player_bullet_max
1718
DIM player_bullet_y, player_bullet_max
1819
DIM player_bullet_type, player_bullet_max
1920
DIM player_bullet_vel_x, player_bullet_max
2021
DIM player_bullet_vel_y, player_bullet_max
21-
player_bullet_next = 0
22-
player_bullet_top = 0
2322
enemy_bullets = 0
2423
enemy_bullet_max = 1
2524
DIM enemy_bullet_x, enemy_bullet_max
@@ -69,6 +68,7 @@ REPEAT
6968
IF VEL_Y < 2 THEN VEL_Y = 2
7069
PROCundraw_bullets()
7170
PROCundraw_enemy_bullets()
71+
PROCmove_bullets()
7272
PROCmove_enemy_bullets()
7373
PROCdraw_bullets()
7474
PROCdraw_enemy_bullets()
@@ -90,63 +90,75 @@ UNTIL (INKEY$(CHR$(27)) <> "") OR (INKEY$("") = CHR$(27))
9090
END
9191

9292
DEF PROCalloc_bullet(type, bullet_x, bullet_y, x_vel, y_vel)
93-
FOR x = player_bullet_next TO player_bullet_max
94-
IF player_bullet_type(x) = 0 THEN
95-
PROCclaim_bullet(x, type, bullet_x, bullet_y, x_vel, y_vel)
96-
x = player_bullet_next * 255
97-
ENDIF
98-
NEXT
99-
IF player_bullet_next * 255 THEN ENDPROC
100-
FOR x = 0 TO player_bullet_next
101-
IF player_bullet_type(x) = 0 THEN
102-
PROCclaim_bullet(x, type, bullet_x, bullet_y, x_vel, y_vel)
103-
x = player_bullet_next * 255
104-
ENDIF
105-
NEXT
93+
player_bullets = player_bullets + 1
94+
IF player_bullets > player_bullet_max THEN
95+
player_bullet_max = player_bullets
96+
REDIM player_bullet_x, player_bullet_max
97+
REDIM player_bullet_y, player_bullet_max
98+
REDIM player_bullet_type, player_bullet_max
99+
REDIM player_bullet_vel_x, player_bullet_max
100+
REDIM player_bullet_vel_y, player_bullet_max
101+
ENDIF
102+
103+
player_bullet_x(player_bullets - 1) = bullet_x
104+
player_bullet_y(player_bullets - 1) = bullet_y
105+
player_bullet_type(player_bullets - 1) = type
106+
player_bullet_vel_x(player_bullets - 1) = x_vel
107+
player_bullet_vel_y(player_bullets - 1) = y_vel
106108
ENDPROC
107109

108-
DEF PROCclaim_bullet(x, type, bullet_x, bullet_y, x_vel, y_vel)
109-
player_bullet_x(x) = bullet_x
110-
player_bullet_y(x) = bullet_y
111-
player_bullet_type(x) = type
112-
player_bullet_vel_x(x) = x_vel
113-
player_bullet_vel_y(x) = y_vel
114-
player_bullet_next = x + 1
115-
IF player_bullet_next >= player_bullet_max THEN player_bullet_next = 0
116-
IF x > player_bullet_top THEN player_bullet_top = x
110+
DEF PROCfree_bullet(i)
111+
POP player_bullet_x, i
112+
POP player_bullet_y, i
113+
POP player_bullet_type, i
114+
POP player_bullet_vel_x, i
115+
POP player_bullet_vel_y, i
116+
117+
player_bullets = player_bullets - 1
118+
119+
IF player_bullets < 1 THEN
120+
player_bullet_max = 1
121+
ELSE
122+
player_bullet_max = player_bullets
123+
ENDIF
124+
125+
REDIM player_bullet_x, player_bullet_max
126+
REDIM player_bullet_y, player_bullet_max
127+
REDIM player_bullet_type, player_bullet_max
128+
REDIM player_bullet_vel_x, player_bullet_max
129+
REDIM player_bullet_vel_y, player_bullet_max
117130
ENDPROC
118131

119-
DEF PROCundraw_bullets()
120-
FOR x = 0 TO player_bullet_top
121-
IF player_bullet_type(x) <> 0 THEN
122-
RECTANGLE player_bullet_x(x) - 4, player_bullet_y(x) - 4, player_bullet_x(x) + 48, player_bullet_y(x) + 48
123-
player_bullet_x(x) = player_bullet_x(x) + player_bullet_vel_x(x)
124-
player_bullet_y(x) = player_bullet_y(x) + player_bullet_vel_y(x)
125-
IF (player_bullet_x(x) < 0) OR (player_bullet_y(x) < 0) OR (player_bullet_x(x) > GRAPHICS_WIDTH) OR (player_bullet_y(x) > GRAPHICS_HEIGHT) THEN
126-
player_bullet_type(x) = 0
127-
ENDIF
128-
ENDIF
129-
NEXT
130-
E = FALSE
132+
DEF PROCmove_bullets()
133+
i = 0
134+
done = FALSE
131135
REPEAT
132-
IF player_bullet_top = 0 THEN
133-
E = TRUE
136+
IF i >= player_bullets THEN
137+
done = TRUE
134138
ELSE
135-
IF player_bullet_type(player_bullet_top) <> 0 THEN
136-
E = TRUE
139+
player_bullet_x(i) = player_bullet_x(i) + player_bullet_vel_x(i)
140+
player_bullet_y(i) = player_bullet_y(i) + player_bullet_vel_y(i)
141+
142+
IF (player_bullet_x(i) < -48) OR (player_bullet_y(i) < -48) OR (player_bullet_x(i) > GRAPHICS_WIDTH + 48) OR (player_bullet_y(i) > GRAPHICS_HEIGHT + 48) THEN
143+
PROCfree_bullet(i)
137144
ELSE
138-
player_bullet_top = player_bullet_top - 1
139-
IF player_bullet_top = 0 THEN E = TRUE
145+
i = i + 1
140146
ENDIF
141147
ENDIF
142-
UNTIL E = TRUE
148+
UNTIL done = TRUE
149+
ENDPROC
150+
151+
DEF PROCundraw_bullets()
152+
IF player_bullets = 0 THEN ENDPROC
153+
FOR i = 0 TO player_bullets - 1
154+
RECTANGLE player_bullet_x(i) - 4, player_bullet_y(i) - 4, player_bullet_x(i) + 48, player_bullet_y(i) + 48
155+
NEXT
143156
ENDPROC
144157

145158
DEF PROCdraw_bullets()
146-
FOR x = 0 TO player_bullet_top
147-
IF player_bullet_type(x) <> 0 THEN
148-
PLOT ship_bullet, player_bullet_x(x), player_bullet_y(x)
149-
ENDIF
159+
IF player_bullets = 0 THEN ENDPROC
160+
FOR i = 0 TO player_bullets - 1
161+
PLOT ship_bullet, player_bullet_x(i), player_bullet_y(i)
150162
NEXT
151163
ENDPROC
152164

@@ -206,12 +218,14 @@ DEF PROCmove_enemy_bullets()
206218
ENDPROC
207219

208220
DEF PROCdraw_enemy_bullets()
221+
IF enemy_bullets = 0 THEN ENDPROC
209222
FOR i = 0 TO enemy_bullets - 1
210223
PLOT enemy_bullet, enemy_bullet_x(i), enemy_bullet_y(i)
211224
NEXT
212225
ENDPROC
213226

214227
DEF PROCundraw_enemy_bullets()
228+
IF enemy_bullets = 0 THEN ENDPROC
215229
FOR i = 0 TO enemy_bullets - 1
216230
RECTANGLE enemy_bullet_x(i) - 4, enemy_bullet_y(i) - 4, enemy_bullet_x(i) + 48, enemy_bullet_y(i) + 48
217231
NEXT

0 commit comments

Comments
 (0)