-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel-data.fth
More file actions
242 lines (208 loc) · 5.54 KB
/
level-data.fth
File metadata and controls
242 lines (208 loc) · 5.54 KB
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
REQUIRE std.fth
REQUIRE snes-std.fth
REQUIRE wram.fth
\ This file describes the data structures used to store and manipulate level
\ data. It's separated from the level gameplay/drawing code itself because I
\ want to store the data for levels in a separate file and that file must come
\ _after_ the data descriptions but _before_ the level gameplay code.
\ == Enums and data structures ==
\ These are in order such that 3 bits indicate color combinations, e.g. a color
\ with bit 1 set always has red in it, bit 2 for green, and bit 3 for blue.
1 CONSTANT RED
2 CONSTANT GREEN
3 CONSTANT YELLOW
4 CONSTANT BLUE
5 CONSTANT MAGENTA
6 CONSTANT CYAN
7 CONSTANT WHITE
: BALL-ENABLED ; \ First cell.
: BALL-Y 1 CELLS + ;
: BALL-X 2 CELLS + ;
: BALL-COLOR 3 CELLS + ;
: BALLS 2* 2* CELLS ;
8 CONSTANT MAX-BALLS
: GOAL-ENABLED ; \ First cell.
: GOAL-Y 1 CELLS + ;
: GOAL-X 2 CELLS + ;
: GOAL-COLOR 3 CELLS + ;
: GOALS 2* 2* CELLS ;
8 CONSTANT MAX-GOALS
: MAP-TILES
\ 1 byte per entry
;
\ For now we're limited to one BGTILEMAP's-worth of tiles in a map.
BGTILEMAP-TILE-COUNT CONSTANT MAP-TILES-COUNT
BANK@
LOWRAM BANK!
CREATE LEVEL 1 CELLS ALLOT
CREATE LEVEL-STATE 1 CELLS ALLOT
CREATE LEVEL-NMI-STATE 1 CELLS ALLOT
CREATE LEVEL-TICKS 1 CELLS ALLOT
\ Level data is contiguous so we can initialize it in a single DMA call.
HERE
DUP CONSTANT LEVEL-DATA-START
CREATE PLAYER-X 1 CELLS ALLOT
CREATE PLAYER-Y 1 CELLS ALLOT
CREATE BALL-ARRAY MAX-BALLS BALLS ALLOT
CREATE GOAL-ARRAY MAX-GOALS GOALS ALLOT
CREATE LEVEL-MAP MAP-TILES-COUNT MAP-TILES ALLOT
HERE
SWAP - CONSTANT LEVEL-DATA-SIZE-BYTES
BANK!
\ == Ball words ==
: CLEAR-BALLS
BALL-ARRAY MAX-BALLS BALLS EACH DO
FALSE I BALL-ENABLED !
1 BALLS +LOOP
;
: NEXT-FREE-BALL-ID
MAX-BALLS 0 DO
BALL-ARRAY I BALLS +
( &ball )
BALL-ENABLED @ 0= IF
I UNLOOP EXIT
THEN
LOOP
\ TODO: ABORT?
BREAKPOINT
;
( y x color -- ball-index )
: ADD-BALL
NEXT-FREE-BALL-ID
>R R@ BALLS BALL-ARRAY +
TRUE OVER BALL-ENABLED !
TUCK BALL-COLOR !
TUCK BALL-X !
BALL-Y !
R>
;
\ == Goal words ==
: CLEAR-GOALS
GOAL-ARRAY MAX-GOALS GOALS EACH DO
FALSE I GOAL-ENABLED !
1 GOALS +LOOP
;
( -- &goal )
: NEXT-FREE-GOAL
MAX-GOALS 0 DO
GOAL-ARRAY I GOALS +
( &goal )
DUP GOAL-ENABLED @ 0= IF
UNLOOP EXIT
THEN
DROP
LOOP
\ TODO: ABORT?
BREAKPOINT
;
( y x color -- )
: ADD-GOAL
NEXT-FREE-GOAL
( y x color &goal )
TRUE OVER GOAL-ENABLED !
TUCK GOAL-COLOR !
TUCK GOAL-X !
GOAL-Y !
;
\ == Player words ==
( y x -- )
: SET-PLAYER-COORDS
PLAYER-X !
PLAYER-Y !
;
\ == Tilemap words ==
\ These are word constants, but will be stored as bytes in the level map.
0x0000 CONSTANT EMPTY-TILE
0x0001 CONSTANT WALL-TILE
0x0010 CONSTANT TUTORIAL-TILES
0x0020 CONSTANT BALL-0-TILE \ Ball 1 is BALL-0-TILE 1+, etc
( y x -- &tile )
: TILE-ADDR
SWAP 32 PPU-MULT DROP + MAP-TILES
LEVEL-MAP +
;
( y x -- tile )
: TILE-AT
TILE-ADDR C@
;
( tile-id -- is-ball )
: IS-BALL-TILE?
DUP BALL-0-TILE >=
SWAP BALL-0-TILE MAX-BALLS + < AND
;
( ball-tile-id -- ball-id )
: TILE-BALL-INDEX
BALL-0-TILE -
;
\ == Loading and compiling levels ==
\ Skips linefeeds.
( -- next-char )
: GET-NEXT-CHAR
BEGIN
KEY
DUP
0x0A =
WHILE
DROP
REPEAT
;
\ Loads a level from a description in the input stream.
: LOAD-LEVEL"
CLEAR-BALLS
CLEAR-GOALS
\ Keep track of Y and X
0 0
LEVEL-MAP MAP-TILES-COUNT MAP-TILES EACH DO
GET-NEXT-CHAR
( y x char -- )
CASE
[CHAR] OF EMPTY-TILE ENDOF
[CHAR] # OF WALL-TILE ENDOF
[CHAR] r OF 2DUP RED ADD-GOAL EMPTY-TILE ENDOF
[CHAR] y OF 2DUP YELLOW ADD-GOAL EMPTY-TILE ENDOF
[CHAR] g OF 2DUP GREEN ADD-GOAL EMPTY-TILE ENDOF
[CHAR] c OF 2DUP CYAN ADD-GOAL EMPTY-TILE ENDOF
[CHAR] b OF 2DUP BLUE ADD-GOAL EMPTY-TILE ENDOF
[CHAR] m OF 2DUP MAGENTA ADD-GOAL EMPTY-TILE ENDOF
[CHAR] w OF 2DUP WHITE ADD-GOAL EMPTY-TILE ENDOF
\ Set the ball tile based on the index returned by ADD-BALL.
[CHAR] R OF 2DUP RED ADD-BALL BALL-0-TILE + ENDOF
[CHAR] Y OF 2DUP YELLOW ADD-BALL BALL-0-TILE + ENDOF
[CHAR] G OF 2DUP GREEN ADD-BALL BALL-0-TILE + ENDOF
[CHAR] C OF 2DUP CYAN ADD-BALL BALL-0-TILE + ENDOF
[CHAR] B OF 2DUP BLUE ADD-BALL BALL-0-TILE + ENDOF
[CHAR] M OF 2DUP MAGENTA ADD-BALL BALL-0-TILE + ENDOF
[CHAR] W OF 2DUP WHITE ADD-BALL BALL-0-TILE + ENDOF
[CHAR] @ OF 2DUP SET-PLAYER-COORDS EMPTY-TILE ENDOF
[CHAR] 0 OF TUTORIAL-TILES 0 + ENDOF
[CHAR] 1 OF TUTORIAL-TILES 1 + ENDOF
[CHAR] 2 OF TUTORIAL-TILES 2 + ENDOF
[CHAR] 3 OF TUTORIAL-TILES 3 + ENDOF
[CHAR] 4 OF TUTORIAL-TILES 4 + ENDOF
[CHAR] 5 OF TUTORIAL-TILES 5 + ENDOF
[CHAR] 6 OF TUTORIAL-TILES 6 + ENDOF
[CHAR] 7 OF TUTORIAL-TILES 7 + ENDOF
>R EMPTY-TILE R>
ENDCASE
\ Store the tile.
I C!
\ Increment X, then overflow to Y and reset if necessary.
1+ DUP 32 >= IF
DROP 1+ 0
THEN
1 MAP-TILES +LOOP
KEY [CHAR] " <> ABORT" Level included an incorrect number of characters."
\ Drop the X and Y
DROP DROP
;
\ Compiles the level.
: LEVEL-DATA,
LEVEL-DATA-START LEVEL-DATA-SIZE-BYTES EACH DO
I C@ C,
LOOP
;
( addr page -- )
: LEVEL-DATA@
LEVEL-DATA-SIZE-BYTES LEVEL-DATA-START 0
DMA0-WRAM-LONG-TRANSFER
; LABEL _LEVEL_DATA_FETCH