-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameloop.c
224 lines (198 loc) · 4.36 KB
/
gameloop.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
#ifndef GAMELOOPC
#define GAMELOOPC
#include "sprite_pool.c"
#include "engine.c"
#include <evo.h>
#include "dialogs.c"
#include "bytecode.h"
#include "level1.c"
#include "level3.c"
#include "level4.c"
#include "interpreter.c"
#define LEFT KEY_5
#define RIGHT KEY_8
#define DOWN KEY_6
#define UP KEY_7
#define FIRE KEY_SPACE
static u8 i, j;
static void init_level(u8 image);
static void start_level(u8 image, u8 *name)
{
u8 c;
music_stop();
player.y = 321;
flush_sprites();
init_text();
swap_screen();
text_y = 29;
text_x = 20;
put_str("Girls are praying...");
text_y = 10;
text_x = 3;
put_str("Lives: ");
put_num(hp);
text_y = 11;
text_x = 3;
put_str("Score: ");
put_num(score);
text_y = 2;
text_x = 20 - strlen(name) / 2;
put_slow_str(name);
text_x = 10;
text_y = 5;
draw_face(REIMU_FACE);
text_y--;
text_x += 5;
for (c = 0; c < 50; c++)
swap_screen();
draw_face(MARISA_FACE);
text_y--;
text_x += 5;
for (c = 0; c < 50; c++)
swap_screen();
draw_face(CIRNO_FACE);
for (c = 0; c < 100; c++)
swap_screen();
init_level(image);
}
static void init_level(u8 image)
{
pal_bright(BRIGHT_MIN);
if (image == IMG256_BG8)
unpack_pal256(PAL256_BG8, 0);
else
unpack_pal256(PAL256_BG1, 0);
unpack_pal16(PAL16_SPRITES, 15, 0);
draw_image_g256(0, 0, image);
pal_bright(BRIGHT_MID);
player.x = 120;
player.y = 168;
flush_sprites();
swap_screen();
scroll_pos = 0;
hor_scroll_pos = 0;
hor_scroll_speed = 0;
level_loop = 0;
schedule = 0;
add_cnt = 0;
}
void start_game_loop()
{
int i;
hp = 3;
score = 0;
process_level = process_level1;
scroll_pos = 0;
bullet_sprite = DEFAULT_BULLET;
start_level(IMG256_BG1, "Day before festival");
init_vm(level1_bytecode);
music_play(MUS_TH0EA1);
sprites_start();
immortality = 20;
game_loop();
}
static void player_control()
{
if (keys[LEFT])
player.x -= 4;
if (keys[RIGHT])
player.x += 4;
if (keys[UP])
player.y -= 4;
if (keys[DOWN])
player.y += 4;
if (keys[FIRE])
user_fire();
if (player.x < 0)
player.x = 0;
if (player.x > 300)
player.x = 300;
if (player.y < 0)
player.y = 0;
if (player.y > MAX_Y_RES - 32)
player.y = MAX_Y_RES - 32;
}
static void bg_scroll()
{
scroll_pos -= scroll_speed;
scroll_pos &= 511;
hor_scroll_pos += hor_scroll_speed;
hor_scroll_pos &= 511;
scroll(hor_scroll_pos, scroll_pos);
}
static void process_events()
{
if ((schedule & 7) == 0)
{
level_loop++;
process_level();
if (immortality)
{
immortality--;
}
}
}
static void player_collisions()
{
static MovableSprite *b;
if (!immortality && is_player_collide())
{
immortality = 20;
player.x = 160;
player.y = 168;
cards = 3;
hp--;
sfx_play(1, 0);
for (push_counter = 0; push_counter < BULLETS_count; push_counter++)
bullets[push_counter].dy = 10;
}
b = is_player_collide_with_pups();
if (b)
{
switch (b->tile)
{
case BONUS_CARD:
if (cards < MY_BULLETS_max)
cards++;
break;
case HEALTH_CARD:
hp++;
break;
default:
break;
}
}
}
void game_loop()
{
scroll_speed = 1;
immortality = 10;
while (state == STATE_PLAY)
{
schedule++;
if ((schedule & 3) == 0)
anim_frame++;
anim_frame &= 3;
player.tile = (immortality & 1) ? 2560 : 0;
if (schedule & 1)
{
keyboard(keys);
player_control();
player_collisions();
}
else
{
enemy_collision();
process_events();
}
if (hp < 1)
{
state = STATE_GAMEOVER;
}
update_sprites(schedule & 1);
if ((schedule & 3) == 0)
bg_scroll();
swap_screen();
}
}
#endif