35 | 35 |
reserve vector dispatch_logic
|
36 | 36 |
|
37 | 37 |
reserve byte[18] press_fire_msg: "PRESS`FIRE`TO`PLAY"
|
|
38 |
|
|
39 |
// could be routine-local, if they were truly static
|
|
40 |
reserve byte button_down: 0
|
|
41 |
|
|
42 |
/******************************************
|
|
43 |
* Utility routines for manipulating/checking the current actor's
|
|
44 |
* position and delta.
|
|
45 |
******************************************/
|
|
46 |
|
|
47 |
routine reverse_delta {
|
|
48 |
lda #40
|
|
49 |
cmp <delta
|
|
50 |
if beq {
|
|
51 |
// copy #-40 delta
|
|
52 |
lda #216
|
|
53 |
sta <delta
|
|
54 |
lda #255
|
|
55 |
sta >delta
|
|
56 |
} else {
|
|
57 |
// copy #40 delta
|
|
58 |
lda #40
|
|
59 |
sta <delta
|
|
60 |
lda #0
|
|
61 |
sta >delta
|
|
62 |
}
|
|
63 |
}
|
38 | 64 |
|
39 | 65 |
routine calculate_new_position outputs (new_position) {
|
40 | 66 |
clc
|
|
46 | 72 |
sta >new_position
|
47 | 73 |
}
|
48 | 74 |
|
49 | |
// output is carry
|
50 | |
routine compare_new_pos {
|
|
75 |
routine compare_new_pos outputs (.c) {
|
51 | 76 |
lda >new_position
|
52 | 77 |
cmp >compare_target
|
53 | 78 |
if beq {
|
|
57 | 82 |
}
|
58 | 83 |
}
|
59 | 84 |
|
60 | |
// output is carry
|
61 | |
routine check_new_position_in_bounds {
|
|
85 |
routine check_new_position_in_bounds outputs (.c) {
|
62 | 86 |
copy #$07e8 compare_target // just past bottom of screen
|
63 | 87 |
jsr compare_new_pos
|
64 | 88 |
|
|
76 | 100 |
}
|
77 | 101 |
}
|
78 | 102 |
}
|
|
103 |
|
|
104 |
/******************************************
|
|
105 |
* Utility routines for dealing with the current actor's logic routine.
|
|
106 |
******************************************/
|
|
107 |
|
|
108 |
routine indirect_jsr_logic {
|
|
109 |
jmp (dispatch_logic)
|
|
110 |
}
|
|
111 |
|
|
112 |
routine read_stick outputs (delta) {
|
|
113 |
lda #0
|
|
114 |
sta <delta
|
|
115 |
sta >delta
|
|
116 |
ldx joy2
|
|
117 |
txa
|
|
118 |
and #1 // up
|
|
119 |
if beq {
|
|
120 |
lda #216 // -40
|
|
121 |
sta <delta
|
|
122 |
lda #255
|
|
123 |
sta >delta
|
|
124 |
} else {
|
|
125 |
txa
|
|
126 |
and #2 // down
|
|
127 |
if beq {
|
|
128 |
lda #40
|
|
129 |
sta <delta
|
|
130 |
} else {
|
|
131 |
txa
|
|
132 |
and #4 // left
|
|
133 |
if beq {
|
|
134 |
lda #255 // -1
|
|
135 |
sta <delta
|
|
136 |
sta >delta
|
|
137 |
} else {
|
|
138 |
txa
|
|
139 |
and #8 // right
|
|
140 |
if beq {
|
|
141 |
lda #1
|
|
142 |
sta <delta
|
|
143 |
} else { }
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
routine check_fire outputs (.z) {
|
|
150 |
ldx joy2
|
|
151 |
txa
|
|
152 |
and #16
|
|
153 |
}
|
|
154 |
|
|
155 |
/********************
|
|
156 |
*** Actor Logics ***
|
|
157 |
********************/
|
|
158 |
|
|
159 |
routine logic_player {
|
|
160 |
jsr read_stick
|
|
161 |
jsr calculate_new_position
|
|
162 |
jsr check_new_position_in_bounds
|
|
163 |
if bcs {
|
|
164 |
ldy #0
|
|
165 |
lda (new_position), y
|
|
166 |
cmp #32
|
|
167 |
if beq {
|
|
168 |
lda #32
|
|
169 |
ldy #0
|
|
170 |
sta (position), y
|
|
171 |
copy new_position position
|
|
172 |
lda #81
|
|
173 |
ldy #0
|
|
174 |
sta (position), y
|
|
175 |
} else {
|
|
176 |
// copy routine state_game_over to dispatch_state
|
|
177 |
}
|
|
178 |
} else { }
|
|
179 |
}
|
|
180 |
|
|
181 |
routine logic_obstacle {
|
|
182 |
jsr calculate_new_position
|
|
183 |
jsr check_new_position_in_bounds
|
|
184 |
if bcs {
|
|
185 |
ldy #0
|
|
186 |
lda (new_position), y
|
|
187 |
cmp #32
|
|
188 |
if beq {
|
|
189 |
lda #32
|
|
190 |
ldy #0
|
|
191 |
sta (position), y
|
|
192 |
copy new_position position
|
|
193 |
lda #82
|
|
194 |
ldy #0
|
|
195 |
sta (position), y
|
|
196 |
} else {
|
|
197 |
copy routine state_game_over to dispatch_state
|
|
198 |
}
|
|
199 |
} else {
|
|
200 |
jsr reverse_delta
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
/******************************************
|
|
205 |
* Utility routines used in dealing with the game state.
|
|
206 |
******************************************/
|
79 | 207 |
|
80 | 208 |
routine clear_screen {
|
81 | 209 |
ldy #0
|
|
97 | 225 |
}
|
98 | 226 |
}
|
99 | 227 |
|
100 | |
routine read_stick outputs (delta) {
|
101 | |
lda #0
|
102 | |
sta <delta
|
103 | |
sta >delta
|
104 | |
ldx joy2
|
105 | |
txa
|
106 | |
and #1 // up
|
|
228 |
// You can repeatedly (i.e. as part of actor logic or an IRQ handler)
|
|
229 |
// call this routine.
|
|
230 |
// Upon return, if carry is set, the button was pressed then released.
|
|
231 |
|
|
232 |
routine check_button outputs (.c) {
|
|
233 |
lda button_down
|
107 | 234 |
if beq {
|
108 | |
lda #216 // -40
|
109 | |
sta <delta
|
110 | |
lda #255
|
111 | |
sta >delta
|
112 | |
} else {
|
113 | |
txa
|
114 | |
and #2 // down
|
115 | |
if beq {
|
116 | |
lda #40
|
117 | |
sta <delta
|
118 | |
} else {
|
119 | |
txa
|
120 | |
and #4 // left
|
121 | |
if beq {
|
122 | |
lda #255 // -1
|
123 | |
sta <delta
|
124 | |
sta >delta
|
125 | |
} else {
|
126 | |
txa
|
127 | |
and #8 // right
|
128 | |
if beq {
|
129 | |
lda #1
|
130 | |
sta <delta
|
131 | |
} else { }
|
132 | |
}
|
133 | |
}
|
134 | |
}
|
135 | |
}
|
136 | |
|
137 | |
// output is .z flag
|
138 | |
routine check_fire {
|
139 | |
ldx joy2
|
140 | |
txa
|
141 | |
and #16
|
|
235 |
lda joy2
|
|
236 |
and #$10
|
|
237 |
if beq {
|
|
238 |
lda #1
|
|
239 |
sta button_down
|
|
240 |
} else { }
|
|
241 |
clc
|
|
242 |
} else {
|
|
243 |
lda joy2
|
|
244 |
and #$10
|
|
245 |
if bne {
|
|
246 |
lda #0
|
|
247 |
sta button_down
|
|
248 |
sec
|
|
249 |
} else {
|
|
250 |
clc
|
|
251 |
}
|
|
252 |
}
|
142 | 253 |
}
|
143 | 254 |
|
144 | 255 |
routine init_game {
|
|
172 | 283 |
cpy #16
|
173 | 284 |
}
|
174 | 285 |
}
|
|
286 |
|
|
287 |
/*******************
|
|
288 |
*** Game States ***
|
|
289 |
*******************/
|
175 | 290 |
|
176 | 291 |
routine state_title_screen {
|
177 | 292 |
lda #5
|
|
187 | 302 |
iny
|
188 | 303 |
cpy #18
|
189 | 304 |
}
|
190 | |
jsr check_fire
|
191 | |
if beq {
|
|
305 |
jsr check_button
|
|
306 |
if bcs {
|
192 | 307 |
jsr clear_screen
|
193 | 308 |
jsr init_game
|
194 | 309 |
copy routine state_play_game to dispatch_state
|
|
198 | 313 |
|
199 | 314 |
routine state_game_over {
|
200 | 315 |
inc vic_border
|
201 | |
jsr check_fire
|
202 | |
if beq {
|
|
316 |
jsr check_button
|
|
317 |
if bcs {
|
|
318 |
jsr clear_screen
|
203 | 319 |
copy routine state_title_screen to dispatch_state
|
204 | 320 |
} else { }
|
205 | 321 |
jmp (save_cinv)
|
206 | |
}
|
207 | |
|
208 | |
routine logic_player {
|
209 | |
jsr read_stick
|
210 | |
jsr calculate_new_position
|
211 | |
jsr check_new_position_in_bounds
|
212 | |
if bcs {
|
213 | |
ldy #0
|
214 | |
lda (new_position), y
|
215 | |
cmp #32
|
216 | |
if beq {
|
217 | |
lda #32
|
218 | |
ldy #0
|
219 | |
sta (position), y
|
220 | |
copy new_position position
|
221 | |
lda #81
|
222 | |
ldy #0
|
223 | |
sta (position), y
|
224 | |
} else {
|
225 | |
// copy routine state_game_over to dispatch_state
|
226 | |
}
|
227 | |
} else { }
|
228 | |
}
|
229 | |
|
230 | |
routine reverse_delta {
|
231 | |
lda #40
|
232 | |
cmp <delta
|
233 | |
if beq {
|
234 | |
// copy #-40 delta
|
235 | |
lda #216
|
236 | |
sta <delta
|
237 | |
lda #255
|
238 | |
sta >delta
|
239 | |
} else {
|
240 | |
// copy #40 delta
|
241 | |
lda #40
|
242 | |
sta <delta
|
243 | |
lda #0
|
244 | |
sta >delta
|
245 | |
}
|
246 | |
}
|
247 | |
|
248 | |
routine logic_obstacle {
|
249 | |
jsr calculate_new_position
|
250 | |
jsr check_new_position_in_bounds
|
251 | |
if bcs {
|
252 | |
ldy #0
|
253 | |
lda (new_position), y
|
254 | |
cmp #32
|
255 | |
if beq {
|
256 | |
lda #32
|
257 | |
ldy #0
|
258 | |
sta (position), y
|
259 | |
copy new_position position
|
260 | |
lda #82
|
261 | |
ldy #0
|
262 | |
sta (position), y
|
263 | |
} else {
|
264 | |
copy routine state_game_over to dispatch_state
|
265 | |
}
|
266 | |
} else {
|
267 | |
jsr reverse_delta
|
268 | |
}
|
269 | |
}
|
270 | |
|
271 | |
routine indirect_jsr_logic {
|
272 | |
jmp (dispatch_logic)
|
273 | 322 |
}
|
274 | 323 |
|
275 | 324 |
routine state_play_game {
|
|
294 | 343 |
jmp (save_cinv)
|
295 | 344 |
}
|
296 | 345 |
|
|
346 |
/*************************
|
|
347 |
* Main Game Loop Driver *
|
|
348 |
*************************/
|
|
349 |
|
297 | 350 |
routine our_cinv {
|
298 | 351 |
jmp (dispatch_state)
|
299 | 352 |
}
|