-
Notifications
You must be signed in to change notification settings - Fork 3
/
petaxian.p8
366 lines (304 loc) · 8.92 KB
/
petaxian.p8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
%import syslib
%import textio
%import base
%import title
%import roller
%import decor
%import game_over
%import stage
%import gun
%import enemy
%import bombs
%import seeker_bombs
%import cluster_bombs
%import explosion
main {
const ubyte CLR = $20
ubyte[] start_msg_cols = [6, 14, 14, 3, 3, 3, 1, 1, 1, 1,
3, 3, 3, 14, 14, 6, 6, 0, 0, 0]
ubyte[] end_msg_cols = [2, 8, 8, 7, 7, 7, 1, 1, 1, 1,
7, 7, 7, 8, 8, 2, 2, 0, 0, 0]
; Used for coding two bool's in one byte
const ubyte LEFTMOST = 1
const ubyte TOPMOST = 2
const ubyte NOT_LEFTMOST = ~1 ; For asm
const ubyte NOT_TOPMOST = ~2 ; For asm
; Game "loop" variables
uword hiscore
uword score
uword bonus_score ; Track score from time bonuses for "stats"
uword next_new_life
ubyte cur_stage
ubyte player_lives
uword wave_ticks
ubyte bonus
ubyte wave_time
ubyte seeker_delay
; This variable is used to allow bullets and explosions to complete at
; the end of a stage before starting the next. And prevent new bullets
; during this delay
ubyte stage_start_delay = 0
; Variable enemy speed
const ubyte ENEMY_SPEED = 3
ubyte enemy_sub_counter
; Fixed player speed? Power ups? Split gun/bullet speed?
const ubyte PLAYER_SPEED = 2
ubyte player_sub_counter
const ubyte GLIDE_MOVEMENT = 0; Keyboard "glide" mechanic for now off
; Delay for animation
const ubyte ANIMATION_SPEED = 5
ubyte animation_sub_counter
; Delay between bullets
ubyte bullet_delay
sub start() {
base.platform_setup()
repeat {
game_title()
game_loop()
game_end()
}
}
sub game_title() {
txt.clear_screen()
title.draw()
roller.setup()
; Add startup delay to prevent "start" button press from
; immediately trigger start of game
sys.wait(50)
wait_key(32, ">>> press fire or start to begin <<<", 2, 23,
&start_msg_cols, true);
}
sub game_loop() {
txt.clear_screen()
decor.draw()
base.draw_extra_border() ; Mark unused area on XC16
player_lives = 3
score = 0
bonus_score = 0
next_new_life = 1000
cur_stage = 0
bullet_delay = 0
seeker_delay = 0
attack.set_data()
enemy.set_data()
gun.set_data()
gun_bullets.set_data()
bombs.set_data()
seeker_bombs.set_data()
cluster_bombs.set_data()
repeat {
ubyte time_lo = lsb(cbm.RDTIM16())
; May needed to find a better timer
if time_lo >= 1 {
cbm.SETTIM(0,0,0)
wave_ticks++
; controll sound effects
sound.check()
; Player movements
player_sub_counter++
if player_sub_counter == PLAYER_SPEED {
; Check joystick
joystick.pull_info()
keyboard.pull_info();
if joystick.pushing_fire() or keyboard.pushing_fire() {
if bullet_delay == 0 {
gun.fire()
bullet_delay = 3
} else
bullet_delay--
}
if joystick.pushing_left() or keyboard.pushing_left() {
gun.set_left()
} else if joystick.pushing_right() or keyboard.pushing_right() {
gun.set_right()
}
gun_bullets.move()
gun.move()
if GLIDE_MOVEMENT == 0 ; If joystic "mode" is set avoid gliding
gun.direction = 0
player_sub_counter = 0
}
; Enemy movement
enemy_sub_counter++
if enemy_sub_counter == ENEMY_SPEED {
enemy.trigger_attack()
enemy.move_all()
enemy_sub_counter = 0
bombs.move()
cluster_bombs.move()
; Make seekers slower than other bombs
if seeker_delay > 0 {
seeker_bombs.move()
seeker_delay = 0
} else {
seeker_delay = 1
}
enemy.spawn_bomb()
}
if (enemy.enemies_left == 0) {
if stage_start_delay == 0 { ; Increase stage at start of counter
if cur_stage == 0
stage_start_delay = 150 ; Skip stage bonus display at start
else {
wave_time = wave_ticks / 60 as ubyte
if wave_time >= stage.bonus_times[cur_stage-1]
bonus = 0
else
bonus = stage.bonus_times[cur_stage-1] - wave_time
}
cur_stage++
}
stage_start_delay++
if stage_start_delay < 150 {
stage_bonus()
} else if stage_start_delay < 250 {
if cur_stage > stage.MAX_STAGE { ; Game won we return (after bonus)
stage_start_delay = 0
return
}
stage_announce()
} else {
enemy.setup_stage(cur_stage - 1)
printStage()
stage_start_delay = 0
wave_ticks = 0
}
}
; explosions etc.
animation_sub_counter++
if animation_sub_counter == ANIMATION_SPEED {
animation_sub_counter = 0
explosion.animate()
}
if player_lives == 0
return
}
}
}
sub game_end() {
; Let explosion animation finish loop
ubyte end_counter = 50
endloop:
ubyte time_lo = lsb(cbm.RDTIM16())
if time_lo >= 1 {
cbm.SETTIM(0,0,0)
; explosions etc.
animation_sub_counter++
if animation_sub_counter == ANIMATION_SPEED {
animation_sub_counter = 0
explosion.animate()
}
end_counter--
}
if end_counter > 0
goto endloop
txt.clear_screen()
if cur_stage > stage.MAX_STAGE
game_over.draw_victory()
else
game_over.draw_defeat()
wait_key(32, ">> press fire or start to continue <<", 1, 23,
&end_msg_cols, false)
}
sub wait_key(ubyte key, uword strRef, ubyte x, ubyte y,
uword colRef, bool do_usage) {
ubyte time_lo = lsb(cbm.RDTIM16())
ubyte col = 0
ubyte inp = 0
while inp != key {
inp = cbm.GETIN2()
if time_lo >= 2 {
cbm.SETTIM(0,0,0)
write( colRef[col], x, y, strRef )
col++
if col == 20 {
col = 0
if do_usage
roller.draw()
}
}
; Let's also check joystick start (push up on c64) or fire
joystick.pull_info()
if joystick.pushing_start() or joystick.pushing_fire()
return
time_lo = lsb(cbm.RDTIM16())
}
}
sub stage_bonus() {
when stage_start_delay {
10,35 -> {
printNumber(4, 5, bonus as uword, 2)
write( 3, 7, 5, "bonus sec" )
}
70 -> {
write( 1, 17, 5, "--- points" )
uword score_bonus = bonus as uword * 50
add_score(score_bonus)
bonus_score += score_bonus
printNumber(17, 5, score_bonus, 3)
}
140 -> {
write( 4, 2, 5, " " )
}
}
}
sub stage_announce() {
when stage_start_delay {
170,200,230 -> {
write( 3, 12, 5, "stage:" )
printNumber(19, 5, cur_stage, 2)
}
190,220,249 -> {
write( 3, 12, 5, " " )
}
}
}
sub add_score(uword points) {
score += points
if score >= next_new_life {
player_lives++
next_new_life += 3000
printLives()
}
printScore()
}
; To be replaced when a better solution is found
sub printHiscore() {
void conv.str_uw0(hiscore)
write(1, 12, 10, "hiscore:")
write(1, 21, 10, conv.string_out)
}
; Convert/display uword value as desimal on screen. Uses function
; from Prog conv library to convert from uword to decimal string
sub printNumber(ubyte x, ubyte y, uword val, ubyte digits) {
ubyte pos_adj = 5 - digits
ubyte i
void conv.str_uw0(val)
for i in pos_adj to 4 {
txt.setcc(base.LBORDER + x + i - pos_adj, base.UBORDER + y,
conv.string_out[i], 1)
}
}
; Similar to printNumber but we want the "reversed" number chars
sub printScore() {
void conv.str_uw0(main.score)
ubyte i
for i in 0 to 4 {
txt.setcc(base.RBORDER + 4 + i, base.UBORDER + 2,
conv.string_out[i] + 128, 1)
}
}
sub printLives() {
txt.setcc(base.RBORDER + 8, base.UBORDER + 4, player_lives + 176, 1 )
}
; Get "reversed" numbers
sub printStage() {
txt.setcc(base.RBORDER + 7, base.UBORDER + 6, cur_stage / 10 + 176, 1)
txt.setcc(base.RBORDER + 8, base.UBORDER + 6, cur_stage % 10 + 176, 1)
}
sub write(ubyte col, ubyte x, ubyte y, uword messageptr) {
txt.color(col)
txt.plot( base.LBORDER + x, base.UBORDER + y )
txt.print( messageptr )
}
}