-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshooter.cpp
292 lines (253 loc) · 7.59 KB
/
shooter.cpp
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
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "libs\w_mlib.h" // Mouse-library
#include "libs\gfxlib.h" // Gfx-header
#include "libs\imageio.h"
const short TRUE = 1;
const short FALSE = 0;
const short SHOT_DELAY = 10;
const short SHIP_WIDTH = 16;
const short SHIP_HEIGHT = 32;
const short SHIP_ANIMATION_TRESHOLD = 2;
const short BULLET_WIDTH = 4;
const short BULLET_HEIGHT = 3;
const short EXPLOSION_WIDTH = 16;
const short EXPLOSION_HEIGHT = 32;
const short ENEMY_WIDTH = 16;
const short ENEMY_HEIGHT = 25;
short smallestNumber(short a, short b);
short largestNumber(short a, short b);
void loadAssets();
void initBullets();
void initVgaGraphicsMode();
void initMouse();
short checkShotFired(short shotDelay, short curMouseX, short curMouseY);
short updateBullets(short shotDelay, short shotVelocity);
void clearShip(short curMouseX, short curMouseY, signed short deltaX, signed short deltaY);
void initEnemy();
void spawnEnemy();
void clearAndMoveEnemy();
void handlePlayerShipAnimation(short& freezeFrames, short& targetState, short& curState, signed short deltaX);
struct Bullet {
short x;
short y;
};
struct Enemy {
short x;
short y;
short width;
short height;
short yVel;
short numHits;
short state;
short frameDelay;
short doFinalClear;
};
byte ship[5][512];
byte explosionFrames[4][512];
byte enemyShip[400];
byte bullet[12];
signed char palette[768];
Bullet bullets[20];
Enemy enemy;
int main(int argc, char *argv[])
{
initBullets();
loadAssets();
initVgaGraphicsMode();
initMouse();
initEnemy();
short curMouseX = MOUSE_X, curMouseY = MOUSE_Y;
signed short deltaX = 0, deltaY = 0;
short targetState = 2;
short curState = 2;
short freezeFrames = 0;
short shotDelay = 0;
short shotVelocity = 4;
short xOffset = 0;
short yOffset = 0;
short quit = FALSE;
short spawnNewEnemy = FALSE;
while (quit == FALSE) {
short key;
shotDelay = checkShotFired(shotDelay, curMouseX, curMouseY);
shotDelay = updateBullets(shotDelay, shotVelocity);
// draw enemy ship
if (enemy.y < 200 && enemy.state == 1) {
clip_put_block(enemy.x, enemy.y, enemy.width, enemy.height, enemyShip);
} else {
if (enemy.state > 1 && enemy.state <= 5) {
clip_put_block(enemy.x, enemy.y, enemy.width, enemy.height, explosionFrames[enemy.state - 2]);
enemy.frameDelay--;
}
if (enemy.frameDelay == 0) {
if (enemy.state == 5) {
enemy.state = 0;
enemy.doFinalClear = TRUE;
}
if (enemy.state > 0 && enemy.state < 5) {
enemy.state++;
enemy.frameDelay = 10;
}
}
}
// draw player ship
put_block(curMouseX, curMouseY, SHIP_WIDTH, SHIP_HEIGHT, ship[curState]);
wait_vbl();
get_mouse_status();
deltaX = MOUSE_X - curMouseX;
deltaY = MOUSE_Y - curMouseY;
clearShip(curMouseX, curMouseY, deltaX, deltaY);
clearAndMoveEnemy();
if (spawnNewEnemy) {
spawnEnemy();
spawnNewEnemy = FALSE;
}
curMouseX = MOUSE_X;
curMouseY = MOUSE_Y;
handlePlayerShipAnimation(freezeFrames, targetState, curState, deltaX);
if (kbhit()) {
key = getch();
if (key == 27) // ESC
quit = TRUE;
if (key == 32) // SPACE
spawnNewEnemy = TRUE;
if (key == 97) { // a
enemy.state = 2;
enemy.frameDelay = 10;
}
}
}
text_mode();
reset_mouse();
}
short smallestNumber(short a, short b) {
return a < b ? a : b;
}
short largestNumber(short a, short b) {
return a > b ? a : b;
}
void loadAssets()
{
pcxRead("bullets.pcx", bullet, palette, BULLET_WIDTH, BULLET_HEIGHT);
pcxRead("eship.pcx", enemyShip, palette, ENEMY_WIDTH, ENEMY_HEIGHT);
pcxRead("ship_ll.pcx", ship[0], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("ship_l.pcx", ship[1], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("ship.pcx", ship[2], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("ship_r.pcx", ship[3], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("ship_rr.pcx", ship[4], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("exp01.pcx", explosionFrames[0], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("exp02.pcx", explosionFrames[1], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("exp03.pcx", explosionFrames[2], palette, SHIP_WIDTH, SHIP_HEIGHT);
pcxRead("exp04.pcx", explosionFrames[3], palette, SHIP_WIDTH, SHIP_HEIGHT);
}
void initBullets()
{
for (short i = 0; i < 20; i++) {
bullets[i].x = 0;
bullets[i].y = 0;
}
}
void initVgaGraphicsMode()
{
mode_13h();
set_pal(palette);
}
void initMouse()
{
set_mouse_sens(20,16);
xlimit_mouse(8,300);
ylimit_mouse(167,167);
get_mouse_status();
}
short checkShotFired(short shotDelay, short curMouseX, short curMouseY)
{
if (shotDelay == 0 && BUTTON_STATE == 1)
{
short freeBulletSlot = -1;
for (short i = 0; i < 20; i++) {
if (bullets[i].y <= 0) {
freeBulletSlot = i;
break;
}
}
if (freeBulletSlot >= 0) {
bullets[freeBulletSlot].x = curMouseX + 6;
bullets[freeBulletSlot].y = curMouseY - 4;
shotDelay = SHOT_DELAY;
}
}
return shotDelay;
}
short updateBullets(short shotDelay, short shotVelocity)
{
if (shotDelay > 0) shotDelay--;
for (short i = 0; i < 20; i++) {
if (bullets[i].y > 0) {
draw_box(bullets[i].x, bullets[i].y, BULLET_WIDTH, BULLET_HEIGHT, 0);
bullets[i].y -= shotVelocity;
}
if (bullets[i].y > 0) {
put_block(bullets[i].x, bullets[i].y, BULLET_WIDTH, BULLET_HEIGHT, bullet);
}
}
return shotDelay;
}
void clearShip(short curMouseX, short curMouseY, signed short deltaX, signed short deltaY)
{
short xOffset = 0,
yOffset = 0;
if (deltaX < 0) {
xOffset = SHIP_WIDTH;
}
if (deltaY < 0) {
yOffset = SHIP_HEIGHT;
}
draw_box(smallestNumber(curMouseX, MOUSE_X) + xOffset, curMouseY, abs(deltaX) + 1, SHIP_HEIGHT, 0);
draw_box(curMouseX, smallestNumber(curMouseY, MOUSE_Y) + yOffset, SHIP_WIDTH, abs(deltaY) + 1, 0);
}
void initEnemy()
{
enemy.x = 0;
enemy.y = 200;
enemy.width = ENEMY_WIDTH;
enemy.height = ENEMY_HEIGHT;
enemy.yVel = 2;
enemy.numHits = 1;
}
void spawnEnemy()
{
enemy.x = 100;
enemy.y = -1 - ENEMY_HEIGHT;
enemy.state = 1;
enemy.frameDelay = 4;
enemy.doFinalClear = FALSE;
}
void clearAndMoveEnemy()
{
if (enemy.y >= 0 && enemy.y < 200 && enemy.state > 0) {
draw_box(enemy.x, enemy.y, enemy.width, enemy.yVel, 0);
}
if (enemy.doFinalClear == TRUE) {
draw_box(enemy.x, enemy.y, EXPLOSION_WIDTH, EXPLOSION_HEIGHT, 0);
}
if (enemy.y < 200) enemy.y += enemy.yVel;
}
void handlePlayerShipAnimation(short& freezeFrames, short& targetState, short& curState, signed short deltaX)
{
if (freezeFrames == 0) {
if (abs(deltaX) >= SHIP_ANIMATION_TRESHOLD) {
targetState = deltaX > 0 ? 0 : 4;
} else {
targetState = 2;
}
if (targetState != curState) {
if (targetState < curState) curState--;
if (targetState > curState) curState++;
freezeFrames = 10;
}
}
if (freezeFrames > 0) freezeFrames--;
}