-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.c
457 lines (412 loc) · 13.6 KB
/
sprite.c
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* StickGBC by Matt Comben is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit
* http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
#pragma bank=4
#include <types.h>
#include "game_constants.h"
#include "sprite.h"
#include "main_map_sprite_tileset.h"
#include "main_map_sprite_palette.h"
#include "main.h"
UINT8 sprite_prop_data;
/*
* location data for road car to randomise
* start location.
* 4 outer array elements for each of the starting
* locations.
*/
typedef struct {
UINT16 x;
UINT16 current_y;
UINT8 dir_y;
} road_car_location_t;
/*
* location data for road car to randomise
* start location.
* Holds current Y location and direction
* and min/max/current X location
*/
const road_car_location_t road_car_locations[4] = {
// Start at top on left side
{0x110U, 0x0U, 1U},
// Start at bottom on left side
{0x110U, 0x1C0U, 0U},
// Start at top on right side
{0x128U, 0x0U, 1U},
// Start at bottom on right side
{0x128U, 0x1C0U, 0U},
};
#define SPRITE_PLAYER_PALETTE_NORMAL 0U
#define SPRITE_PLAYER_PALETTE_HURT 1U
/*
* Main player palettes
*/
const UINT16 main_player_sprite_palettes[2U * PALETTE_COLOURS_PER_PALETTE] = {
// Normal
RGB(31, 31, 31),
RGB(0, 0, 0),
RGB(8, 11, 31),
RGB(14, 31, 0),
// Hurt
RGB(31, 31, 31),
RGB(0, 0, 0),
RGB(8, 11, 31),
RGB(26, 2, 0),
};
void set_main_player_hurt(ai_sprite *player)
{
player->current_pause = player->pause_period;
set_sprite_palette(
player->color_palette * PALETTE_COLOURS_PER_PALETTE,
1U,
&(main_player_sprite_palettes[SPRITE_PLAYER_PALETTE_HURT * PALETTE_COLOURS_PER_PALETTE])
);
set_sprite_tile(player->sprite_index, SPRITE_TILESET_HURT);
}
void set_main_player_normal(ai_sprite *player)
{
set_sprite_palette(
player->color_palette * PALETTE_COLOURS_PER_PALETTE,
1U,
&(main_player_sprite_palettes[SPRITE_PLAYER_PALETTE_NORMAL * PALETTE_COLOURS_PER_PALETTE])
);
set_sprite_direction(player);
}
/*
* Selection of colours palette colors to be used
* for road car
*/
#define ROAD_CAR_MAX_COLORS 4
#define ROAD_CAR_COLOR_MASK 3U
const UINT16 road_car_colors[PALETTE_COLOURS_PER_PALETTE * ROAD_CAR_MAX_COLORS] = {
// Green palette
RGB(31, 31, 31),
RGB(0, 2, 1),
RGB(14, 31, 0),
RGB(15, 22, 0),
// Orange palette
RGB(31, 31, 31),
RGB(0, 2, 1),
RGB(31, 19, 8),
RGB(25, 15, 0),
// Yellow palette
RGB(31, 31, 31),
RGB(0, 2, 1),
RGB(31, 24, 5),
RGB(22, 19, 0),
// Blue palette
RGB(31, 31, 31),
RGB(0, 2, 1),
RGB(6, 10, 31),
RGB(0, 5, 25),
};
/*
* setup_sprites
*
* Setup palettes and tiles requires for sprites.
* Setup initial direction for sprites
*/
void setup_sprites(
ai_sprite *player_sprite,
ai_sprite *skater_sprite,
ai_sprite *dealer_sprite,
ai_sprite *house_car_sprite,
ai_sprite *road_car_sprite)
{
// Load single sprite tile
HIDE_SPRITES;
VBK_REG = 0;
// Load spirte tile data into VRAM
set_sprite_data(0U, 13U, mainmapspritetiles);
// Load sprite palette into VRAM
set_sprite_palette(0U, 4U, main_map_sprite_palette);
VBK_REG = 0;
// Configure sprite to sprite tile
// Main player
set_sprite_tile(player_sprite->sprite_index, player_sprite->sprite_tile);
// Skater
set_sprite_tile(skater_sprite->sprite_index, 0U);
set_sprite_direction(skater_sprite);
// Dealer
set_sprite_tile(dealer_sprite->sprite_index, 0U);
// Set sprite directions
set_sprite_direction(dealer_sprite);
set_sprite_direction(house_car_sprite);
set_sprite_direction(road_car_sprite);
SHOW_SPRITES;
}
/*
* set_sprite_direction
*
* Set sprite tiles based on current direction of travel.
*/
void set_sprite_direction(ai_sprite *sprite)
{
UINT8 tile_index_offset;
UINT8 itx_x;
UINT8 itx_y;
UINT8 itx;
itx = sprite->sprite_index;
for (itx_x = 0; itx_x != sprite->sprite_count_x; itx_x ++)
{
for (itx_y = 0; itx_y != sprite->sprite_count_y; itx_y ++)
{
// Update flip of sprite tile
sprite_prop_data = sprite->color_palette & 0x07U;
tile_index_offset = sprite->sprite_tile;
// Check for just vertical movement
if (sprite->travel_direction_y != 0)
{
if (sprite->travel_direction_x == 0)
{
// If travelling up, flip Y
if (sprite->travel_direction_y == 1)
sprite_prop_data |= S_FLIPY;
if (sprite->sprite_count_y == 2U)
{
// If there are multiple tiles in Y,
// for the first row to use 'back' tiles (second set of 3)
if (sprite->travel_direction_y == 1)
tile_index_offset += ((1 - itx_y) * 3);
else
tile_index_offset += (itx_y * 3);
// If on second row of tiles, flip in X to
// make up second half of sprite
if (itx_x == 1U)
sprite_prop_data |= S_FLIPX;
}
} else {
// Handle diagonal movement
if (sprite->travel_direction_y == 1)
sprite_prop_data |= S_FLIPY;
if (sprite->travel_direction_x == -1)
sprite_prop_data |= S_FLIPX;
tile_index_offset += 2U;
}
}
else if (sprite->travel_direction_x != 0)
{
tile_index_offset += 1U;
if (sprite->travel_direction_x == -1)
sprite_prop_data |= S_FLIPX;
// If there are multiple tiles in X,
// for the first column to use 'back' tiles (second set of 3)
if (sprite->sprite_count_x == 2U)
{
if (sprite->travel_direction_x == 1)
tile_index_offset += ((1 - itx_x) * 3);
else
tile_index_offset += (itx_x * 3);
// If on second row of tiles, flip in Y to
// make up second half of sprite
if (itx_y == 1U)
sprite_prop_data |= S_FLIPY;
}
}
// Only update flipping if actually moving
if (sprite->travel_direction_x != 0 || sprite->travel_direction_y != 0)
set_sprite_prop(sprite->sprite_index, sprite_prop_data);
set_sprite_tile(itx, tile_index_offset);
itx += 1U;
}
}
}
/*
* move_ai_sprites
*
* Move AI sprites along path of travel
* Wait at end of travel and switches direction of travel.
*/
void move_ai_sprite(screen_state_t* screen_state, ai_sprite* sprite_to_move)
{
UINT8 itx;
UINT8 itx_x;
UINT8 itx_y;
// Check if sprite should be disabled
if (! (
screen_state->displayed_sprites_y[sprite_to_move->sprite_display_bit] &&
screen_state->displayed_sprites_x[sprite_to_move->sprite_display_bit]
))
{
if (sprite_to_move->already_offscreen)
return;
// Move sprite off-screen
itx = sprite_to_move->sprite_index;
for (itx_x = 0; itx_x != sprite_to_move->sprite_count_x; itx_x ++)
{
for (itx_y = 0; itx_y != sprite_to_move->sprite_count_y; itx_y ++)
{
move_sprite(itx, 0, 0);
itx += 1U;
}
}
sprite_to_move->already_offscreen = 1U;
return;
}
sprite_to_move->already_offscreen = 0U;
if (sprite_to_move->move_speed != 0U && (sys_time % sprite_to_move->move_speed) == 0U)
{
if (sprite_to_move->current_pause)
{
sprite_to_move->current_pause -= 1U;
if (sprite_to_move->current_pause == 0U)
// Check if now at 0 current pause
// and change direction ready for travel
set_sprite_direction(sprite_to_move);
}
// Check if moving right
else if (sprite_to_move->travel_direction_x == 1)
{
// Check if hit max
if (sprite_to_move->current_location_x == sprite_to_move->max_location_x)
{
// Switch direction and set pause period
sprite_to_move->travel_direction_x = -1;
sprite_to_move->current_pause = sprite_to_move->pause_period;
// Update direction of sprite movement
set_sprite_direction(sprite_to_move);
}
else
sprite_to_move->current_location_x += 1;
}
else if (sprite_to_move->travel_direction_x == -1)
{
if (sprite_to_move->current_location_x == sprite_to_move->min_location_x)
{
// Switch direction and set pause period
sprite_to_move->travel_direction_x = 1;
sprite_to_move->current_pause = sprite_to_move->pause_period;
// Update direction of sprite
set_sprite_direction(sprite_to_move);
}
else
sprite_to_move->current_location_x -= 1;
}
else if (sprite_to_move->travel_direction_y == 1)
{
// Check if hit max
if (sprite_to_move->current_location_y == sprite_to_move->max_location_y)
{
// Switch direction and set pause period
sprite_to_move->travel_direction_y = -1;
sprite_to_move->current_pause = sprite_to_move->pause_period;
// Update direction of sprite movement
set_sprite_direction(sprite_to_move);
}
else
sprite_to_move->current_location_y += 1;
}
else if (sprite_to_move->travel_direction_y == -1)
{
if (sprite_to_move->current_location_y == sprite_to_move->min_location_y)
{
// Switch direction and set pause period
sprite_to_move->travel_direction_y = 1;
sprite_to_move->current_pause = sprite_to_move->pause_period;
// Update direction of sprite
set_sprite_direction(sprite_to_move);
}
else
sprite_to_move->current_location_y -= 1;
}
set_ai_sprt_scrn_loc(screen_state, sprite_to_move);
}
else if (screen_state->screen_has_moved == 1U)
{
set_ai_sprt_scrn_loc(screen_state, sprite_to_move);
}
}
/*
* set_ai_sprt_scrn_loc
*
* Sets AI sprite on-screen location and moves off-screen if not visible.
*/
void set_ai_sprt_scrn_loc(screen_state_t* screen_state, ai_sprite* sprite_to_move)
{
UINT8 itx;
UINT8 itx_x;
UINT8 itx_y;
// Move AI sprites
// This must always be done, as it is required when the screen moves
itx = sprite_to_move->sprite_index;
for (itx_x = 0; itx_x != sprite_to_move->sprite_count_x; itx_x ++)
{
for (itx_y = 0; itx_y != sprite_to_move->sprite_count_y; itx_y ++)
{
move_sprite(
itx,
(sprite_to_move->current_location_x - screen_state->screen_location_x) + SPRITE_OFFSET_X + (itx_x * 8),
(sprite_to_move->current_location_y - screen_state->screen_location_y) + SPRITE_OFFSET_Y + (itx_y * 8)
);
itx += 1U;
}
}
}
/*
* rndise_rd_car_loc
*
* Randomise road car start location to top/bottom of road
* on left or right side
*/
void rndise_rd_car_loc(ai_sprite *road_car_sprite)
{
UINT8 random_number;
// Get random number between 0 and 3
random_number = (UINT8)(sys_time) & 0x3U;
road_car_sprite->min_location_x = road_car_locations[random_number].x;
road_car_sprite->max_location_x = road_car_locations[random_number].x;
road_car_sprite->current_location_x = road_car_locations[random_number].x;
road_car_sprite->current_location_y = road_car_locations[random_number].current_y;
if (road_car_locations[random_number].dir_y)
{
road_car_sprite->travel_direction_y = 1;
}
else
{
road_car_sprite->travel_direction_y = -1;
}
// Randomise car color palette
set_sprite_palette(
CAR_SPRITE_PALETTE_INDEX,
1U,
&(road_car_colors[
(sys_time & ROAD_CAR_COLOR_MASK) * PALETTE_COLOURS_PER_PALETTE
])
);
}
/*
* check_road_car_onscreen
*
* Check that road car should be displayed on screen.
* If not, move off-screen.
* This is better than disabling it in display_sprites_y, as
* this would then stop the AI from being moved.
*/
void check_road_car_onscreen(screen_state_t *screen_state, ai_sprite *road_car_sprite)
{
UINT8 itx;
UINT8 itx_x;
UINT8 itx_y;
// Check if road_car_sprite current pause has just started and randomise location
if (road_car_sprite->current_pause == road_car_sprite->pause_period)
{
rndise_rd_car_loc(road_car_sprite);
}
if ((screen_state->screen_location_y + SCREEN_HEIGHT) < road_car_sprite->current_location_y ||
screen_state->screen_location_y > road_car_sprite->current_location_y ||
road_car_sprite->current_pause != 0)
{
// Move sprite off-screen
itx = road_car_sprite->sprite_index;
for (itx_x = 0; itx_x != road_car_sprite->sprite_count_x; itx_x ++)
{
for (itx_y = 0; itx_y != road_car_sprite->sprite_count_y; itx_y ++)
{
move_sprite(itx, 0, 0);
itx += 1U;
}
}
}
}