Add tools/ and manual/, move sources to src/
[dpadhero2.git] / src / starfield.asm
blob5a90946ecbcb380f4158b864aa6b89bf40337861
2 ; Copyright (C) 2010 Kent Hansen.
4 ; This program is free software; you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation; either version 3 of the License, or
7 ; (at your option) any later version.
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ; GNU General Public License for more details.
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 .include "common/sprite.h"
19 .include "target.h"
21 .dataseg
23 spawn_star_timer .db
24 spawn_star_index .db
25 manhattan_length .db
26 prev .db
28 .codeseg
30 .public initialize_starfield
31 .public update_starfield
33 .proc initialize_starfield
34 jsr initialize_target_lists
36 lda #16
37 sta spawn_star_timer
38 lda #0
39 sta spawn_star_index
40 rts
41 .endp
43 .proc abs
44 ora #0
45 bpl +
46 eor #$FF
47 clc : adc #1
48 + rts
49 .endp
51 .proc calculate_manhattan_length
52 lda targets_1.pos_y.int,y
53 jsr abs
54 sta manhattan_length
55 lda targets_1.pos_x.int,y
56 jsr abs
57 clc
58 adc manhattan_length
59 bcc +
60 lda #255
61 + sta manhattan_length
62 rts
63 .endp
65 .proc draw_star
66 ; left half
67 jsr next_sprite_index
68 tax
69 lda manhattan_length
70 cmp #127
71 bcc +
72 lda #127
73 + lsr : lsr
74 and #$FC
75 ora #$60+1
76 pha
77 sta sprites.tile,x
78 and #$18 : lsr : lsr : lsr
79 ora #$20 ; behind bg
80 sta sprites.attr,x
81 lda targets_1.pos_y.int,y
82 clc : adc #120
83 sta sprites._y,x
84 lda targets_1.pos_x.int,y
85 clc : adc #128
86 sta sprites._x,x
87 ; right half
88 jsr next_sprite_index
89 tax
90 pla
91 ora #2
92 sta sprites.tile,x
93 and #$18 : lsr : lsr : lsr
94 ora #$20 ; behind bg
95 sta sprites.attr,x
96 lda targets_1.pos_y.int,y
97 clc : adc #120
98 sta sprites._y,x
99 lda targets_1.pos_x.int,y
100 clc : adc #128+8
101 sta sprites._x,x
103 .endp
105 .proc process_stars
106 lda #$FF
107 sta prev
108 ldy active_targets_head
110 @@loop:
111 cpy #$FF ; end of list?
112 bne @@do_target
115 @@do_target:
116 jsr move_target
118 lda targets_1.pos_y.int,y
119 clc : adc #128
120 cmp #240
121 bcs @@went_offscreen
122 lda targets_1.pos_x.int,y
123 clc : adc #128
124 cmp #240
125 bcs @@went_offscreen
127 jsr calculate_manhattan_length
128 jsr draw_star
130 lda targets_2.next,y
131 sty prev
133 jmp @@loop
135 @@went_offscreen:
136 lda targets_2.next,y
138 ; put on free list
139 lda free_targets_list
140 sta targets_2.next,y
141 sty free_targets_list
143 ; remove from active targets list
144 cpy active_targets_tail
145 bne +
146 sta active_targets_tail
147 + tay
148 ldx prev
149 cpx #$FF
150 bne +
151 sty active_targets_head
152 jmp @@loop
153 + sta targets_2.next,x
154 jmp @@loop
155 .endp
157 .proc maybe_spawn_star
158 dec spawn_star_timer
159 beq +
161 + lda #10
162 sta spawn_star_timer
163 ; grab target from free list
164 ldx free_targets_list
165 cpx #$FF
166 bne +
167 ; don't spawn, no free targets now
169 + lda targets_2.next,x
170 sta free_targets_list
171 ; initialize
172 lda #0
173 sta targets_1.pos_y.frac,x
174 sta targets_1.pos_x.frac,x
175 lda spawn_star_index
176 inc spawn_star_index
177 and #$1F
179 lda @@order_table,y
181 and #$1F
182 lsr : lsr : lsr
184 lda @@quadrant_start_y_hi,y
185 sta targets_1.pos_y.int,x
186 lda @@quadrant_start_x_hi,y
187 sta targets_1.pos_x.int,x
190 and #7
192 lda @@speed_y_lo,y
193 sta targets_2.speed_y.frac,x
194 lda @@speed_y_hi,y
195 sta targets_2.speed_y.int,x
196 lda @@speed_x_lo,y
197 sta targets_2.speed_x.frac,x
198 lda @@speed_x_hi,y
199 sta targets_2.speed_x.int,x
202 and #8
203 beq +
204 ; invert X speed
205 lda targets_2.speed_x.frac,x
206 eor #$FF
207 clc : adc #1
208 sta targets_2.speed_x.frac,x
209 lda targets_2.speed_x.int,x
210 eor #$FF
211 adc #0
212 sta targets_2.speed_x.int,x
213 + pla
214 and #$10
215 beq +
216 ; invert Y speed
217 lda targets_2.speed_y.frac,x
218 eor #$FF
219 clc : adc #1
220 sta targets_2.speed_y.frac,x
221 lda targets_2.speed_y.int,x
222 eor #$FF
223 adc #0
224 sta targets_2.speed_y.int,x
225 + jmp add_to_active_targets_list
226 @@quadrant_start_x_hi:
227 .db 0,-16,0,-16
228 @@quadrant_start_y_hi:
229 .db 0,0,-24,-24
230 @@speed_y_lo:
231 .db $19,$39,$59,$79,$99,$B9,$D9,$F9
232 @@speed_y_hi:
233 .db $00,$00,$00,$00,$00,$00,$00,$00
234 @@speed_x_lo:
235 .db $FE,$F9,$EF,$E1,$CC,$B0,$86,$38
236 @@speed_x_hi:
237 .db $00,$00,$00,$00,$00,$00,$00,$00
238 @@order_table:
239 .if 0
240 .db 0+0,8+6,16+5,24+4
241 .db 0+3,8+2,16+7,24+7
242 .db 0+1,8+5,16+4,24+5
243 .db 0+6,8+7,16+0,24+2
244 .db 0+2,8+4,16+3,24+6
245 .db 0+5,8+0,16+1,24+1
246 .db 0+7,8+3,16+6,24+3
247 .db 0+4,8+1,16+2,24+0
248 .else
249 .db 0+0,8+1,16+2,24+3
250 .db 0+4,8+5,16+6,24+7
251 .db 0+1,8+2,16+3,24+0
252 .db 0+5,8+6,16+7,24+4
253 .db 0+2,8+3,16+0,24+1
254 .db 0+6,8+7,16+4,24+5
255 .db 0+3,8+0,16+1,24+2
256 .db 0+7,8+4,16+5,24+6
257 .endif
258 .endp
260 .proc update_starfield
261 jsr process_stars
262 jmp maybe_spawn_star
263 .endp
265 .end