-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
284 lines (198 loc) · 6.31 KB
/
game.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
/*
* Dragon - Game screen logic
* Copyright (C) 2016 Sandor Zsuga (Jubatian)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "story.h"
#include "seq.h"
#include "display.h"
#include "global.h"
#include "gstat.h"
#include "dragon.h"
#include "levelscr.h"
#include "physics.h"
#include "mapact.h"
#include "mapobjp.h"
#include "status.h"
#include "map.h"
#include "res.h"
#include "torch.h"
#include "music.h"
#include "sound.h"
#include <avr/pgmspace.h>
#include <uzebox.h>
/* Performance check on score. Enable this to make score display the kernel's
** "sync_pulse" variable:
** 1: Logic frames
** 2: Render frames (score adjustments make it jitter a bit) */
#define PERF_DBG 0
#define GAME_BTNC global_shared_word[0 >> 1]
/* Level End: Also see aceol.c which writes 1 here when collecting level end.
** Death writes 2 in this, but that only happens in this module. */
#define GAME_LEVEND global_shared[7]
/* Temp for health & time to run end level score animations */
#define GAME_THEALTH global_shared[2]
#define GAME_TTIME global_shared[3]
/* Pause function */
#define GAME_PAUSED global_shared[4]
/* "Any movement button" mask */
#define GAME_BTN_ANYM (BTN_RIGHT | BTN_LEFT | BTN_UP | BTN_DOWN | BTN_A | BTN_B | BTN_X | BTN_Y | BTN_SR | BTN_SL)
#if (PERF_DBG != 0)
extern uint8 sync_pulse;
#endif
/*
** Frame routine of the game screen
*/
static void game_frame(void)
{
auint cmd;
uint16 btn = GAME_BTNC;
global_process();
btn |= ReadJoypad(0);
if ((global_framectr & 0x1U) == 0U){ /* Logic frame */
if ((GAME_PAUSED & 1U) != 0U){ /* Game is paused */
global_fadecolor = 0xADU;
if (global_fadectr == 0x00U){
global_palctr = GLOBAL_FADE_TOP | GLOBAL_FADE_INC;
}
if (global_fadectr == 0xFFU){
global_palctr = GLOBAL_FADE_TOP;
}
if ( ((btn & (BTN_START | BTN_SELECT)) == 0U) &&
((btn & (GAME_BTN_ANYM)) != 0U) ){
GAME_PAUSED = 0U;
global_palctr = GLOBAL_FADE_TOP;
}
GAME_BTNC = 0U;
M74_Halt(); /* If paused, logic frame ends here */
}
if ((GAME_LEVEND & 1U) != 0U){ /* End of level reached */
if (gstat_time != 0U){
gstat_time --;
gstat_score_add(2U); /* Each remaining tick of time adds 2 points (normally it is 1 point) */
if ((global_framectr & 0x7U) == 0U){ sound_effect(SOUND_TICK, 0x30U); }
}else if (dragon_stat.hp != 0U){
dragon_stat.hp --;
gstat_score_add(1U); /* Each remaining unit of health adds 1 point */
if ((global_framectr & 0x7U) == 0U){ sound_effect(SOUND_TICK, 0x30U); }
global_fadecolor = 0x00U;
}else if (global_fadectr != 0xFFU){
global_palctr = GLOBAL_FADE_ALLV | GLOBAL_FADE_INC;
}else{
gstat_time = GAME_TTIME; /* Restore stuff added to score */
dragon_stat.hp = GAME_THEALTH;
seq_next(); /* Level completed */
}
dragon_follow(0);
}else{ /* End of level not reached: normal processing */
GAME_THEALTH = dragon_stat.hp;
GAME_TTIME = gstat_time;
/* Global time & death logic */
if ((global_framectr & 0x3FU) == 0U){
if (gstat_time != 0U){ /* Remaining time decrements (~1 sec) */
gstat_time --;
}else{
GAME_LEVEND = 2U; /* Die */
}
}
if ((global_framectr & 0x3FU) == 0U){
gstat_score_add(1U); /* Score increments as time passes (~1 sec)*/
}
if (dragon_stat.hp == 0U){
GAME_LEVEND = 2U; /* Die */
}
}
/* Common processing (even at end level, so player may keep moving the dragon around) */
if ((GAME_LEVEND & 2U) != 0U){
/* Player died either by depleted health or time. Fade. */
global_fadecolor = 0x00U;
global_palctr = GLOBAL_FADE_ALLV | GLOBAL_FADE_INC;
if (global_fadectr == 0xFFU){
seq_over();
}
}else{
/* No death, normal processing */
/* Controls */
cmd = DRAGON_RUN;
if ((btn & BTN_RIGHT) != 0U){ cmd |= DRAGON_RIGHT | DRAGON_MOVE; }
if ((btn & BTN_LEFT) != 0U){ cmd |= DRAGON_LEFT | DRAGON_MOVE; }
if ((btn & BTN_SR) != 0U){ cmd &= ~DRAGON_RUN; }
if ((btn & BTN_Y) != 0U){ cmd |= DRAGON_FIRE; }
if ((btn & BTN_B) != 0U){ cmd |= DRAGON_FIRE; }
if ((btn & BTN_X) != 0U){ cmd |= DRAGON_JUMP; }
if ((btn & BTN_A) != 0U){ cmd |= DRAGON_JUMP; }
if ((btn & BTN_DOWN) != 0U){ cmd |= DRAGON_DOWN; }
if ((btn & BTN_UP) != 0U){ cmd |= DRAGON_UP; }
if ((btn & BTN_SL) != 0U){ cmd |= DRAGON_UP; }
if ( ((btn & (BTN_START | BTN_SELECT)) == (BTN_START | BTN_SELECT)) ){
gstat_score_sub(100U); /* Pausing removes score */
GAME_PAUSED = 1U;
}
GAME_BTNC = 0U;
physics_frame();
dragon_logic(cmd);
mapobjp_process();
map_pollobjs();
}
#if (PERF_DBG == 1)
gstat_score = sync_pulse;
#endif
/* All done */
M74_Halt();
}
/* Render frame */
GAME_BTNC = btn;
M74_VramRestore();
torch_prep();
if ((GAME_PAUSED & 1U) == 0U){
levelscr_scroll();
}else{
levelscr_screen();
}
global_genpal();
dragon_render();
mapobjp_render();
status_render();
torch_render();
#if (PERF_DBG == 2)
gstat_score = sync_pulse;
#endif
M74_Halt();
}
/*
** Initializes and starts a game screen. Calls seq_next() or seq_over() when
** it is over.
*/
void game_enter(auint map)
{
/* Make sure nothing would be visible */
global_hide();
/* Inits control variables */
GAME_BTNC = 0U;
GAME_LEVEND = 0x00U;
GAME_PAUSED = 0x00U;
/* Clear VRAM */
display_clear(DISPLAY_GAME);
/* Reset VRAM restore list (for sprite blitting) */
M74_ResReset();
/* Initialize map */
map_init(pgm_read_word(&res_maplist[map << 1]));
/* Control fading: Fade in from whatever was set before */
global_palctr = GLOBAL_FADE_ALLV;
/* Start music */
music_start_id(2U);
/* Sets display for game mode */
display_set(DISPLAY_GAME, (uint16)(&game_frame));
}