This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
356 lines (313 loc) · 8.4 KB
/
main.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
#include <math.h>
#include <stdlib.h>
#include "SMSlib.h"
#include "states.h"
#include "main.h"
#include "assets/assets.h"
SMS_EMBED_SEGA_ROM_HEADER(9999,0);
SMS_EMBED_SDSC_HEADER_AUTO_DATE(0, 2, "chains\\2017", "Bad Aliens GG", "a little concept of a old school shump\nBuilt using devkitSMS & SMSlib");
#define BG_TILES 0
#define PS_TILES 40
#define SCORE_TILES 24
#define SPRITE_TILES 256
#define SHIP_TILE SPRITE_TILES
#define BULLET_TILE (SPRITE_TILES+1)
#define ENEMY_TILE (SPRITE_TILES+2)
#define GG_X 6
#define GG_Y 3
#define GG_AREA 20
#define START_LIVES 3
#define INIT_P_POSX (GG_X * 8) + 80
#define INIT_P_POSY (GG_Y * 8) + 136
#define INIT_E_POSY (GG_Y * 8) + 20
#define MIN_X (GG_X * 8)
#define MAX_XY (GG_X * 8) + 152
#define NUMBER_OF_DIGITS 16
unsigned char gameState;
unsigned char playerLives;
unsigned int score;
unsigned char level;
unsigned int playerPosition;
// 0: x, 1: y, 2: state?
unsigned int bulletBehavior[3];
// 0: X, 1: Y, 2: state?
unsigned int enemyBehavior[3];
//====================================
// Utils
//====================================
// ref.:http://stackoverflow.com/questions/18691677/how-to-put-an-integer-to-an-array-of-digits
// implementação super simples (mas que funciona)
// retorna o array ao contrário. necessário exibir em ordem reversa
void numberToArray(char *buf, unsigned int number) {
unsigned int i = 0;
while(number != 0) {
buf[i] = number % 10;
number /= 10;
i++;
}
}
//====================================
// Load Assets
//====================================
// GG display 160 x 144
void loadTitleAssets(void) {
SMS_loadPSGaidencompressedTiles(title_scene__tiles__psgcompr, BG_TILES);
SMS_loadSTMcompressedTileMapArea(GG_X, GG_Y, title_scene__tilemap__stmcompr, GG_AREA);
GG_loadBGPalette(default__palette__bin);
SMS_loadPSGaidencompressedTiles(press_start__tiles__psgcompr, PS_TILES);
GG_setBGPaletteColor(4, RGB(3,3,3));
}
void loadMainAssets(void) {
SMS_loadPSGaidencompressedTiles(main_scene__tiles__psgcompr, BG_TILES);
SMS_loadSTMcompressedTileMapArea(GG_X, GG_Y, main_scene__tilemap__stmcompr, GG_AREA);
GG_loadBGPalette(default__palette__bin);
SMS_loadPSGaidencompressedTiles(numbers__tiles__psgcompr, SCORE_TILES);
SMS_loadPSGaidencompressedTiles(sprites__tiles__psgcompr, SPRITE_TILES);
GG_loadSpritePalette(default__palette__bin);
}
//====================================
// Variables update
//====================================
void setScore(unsigned long points) {
unsigned char x;
unsigned char pos = 0;
unsigned char num[5] = {0,0,0,0,0};
const unsigned int *pnt;
if(points > 99999) {
return;
}
numberToArray(num, points);
for(x = 5; x-- > 0 ;){
SMS_setNextTileatXY(GG_X+7+pos,GG_Y+1);
pnt = &numbers__tilemap__bin[num[x]*2];
SMS_setTile(*pnt+++SCORE_TILES);
pos++;
}
}
void setLevel(unsigned char level) {
const unsigned int *pnt;
if(level > 10) {
return;
}
SMS_setNextTileatXY(GG_X+14,GG_Y+1);
pnt = &numbers__tilemap__bin[level*2];
SMS_setTile(*pnt+++SCORE_TILES);
}
void setLives(unsigned char lives) {
const unsigned int *pnt;
unsigned char x;
if(lives > 3) {
return;
}
for(x = 0; x < 3; x++) {
SMS_setNextTileatXY(GG_X+16+x,GG_Y+1);
if(lives >= x + 1) {
pnt = &numbers__tilemap__bin[20];
} else {
pnt = &numbers__tilemap__bin[18];
}
SMS_setTile(*pnt+++SCORE_TILES);
}
}
//====================================
// Moving
//====================================
unsigned int randEnemyXPos(void) {
unsigned int pos = 0;
if(playerPosition >= MIN_X && playerPosition <= MIN_X + 16) {
pos = rand() % MAX_XY;
} else {
pos = rand() % playerPosition;
}
if(pos < MIN_X) {
pos = playerPosition;
}
if(pos > MAX_XY) {
pos = MAX_XY;
}
return pos;
}
void moveEnemy(void) {
if(enemyBehavior[2] == STATE_DEAD) {
enemyBehavior[2] = STATE_ATACKING;
enemyBehavior[1] = INIT_E_POSY;
enemyBehavior[0] = randEnemyXPos();
} else {
++enemyBehavior[1];
}
}
void movePlayer(void) {
unsigned int ks = SMS_getKeysStatus();
if((ks & PORT_A_KEY_LEFT) && (playerPosition > MIN_X)) {
playerPosition -= 2;
} else if((ks & PORT_A_KEY_RIGHT) && (playerPosition < MAX_XY)) {
playerPosition += 2;
}
}
void moveBullet(void) {
unsigned int ks;
// ok para atirar
if(bulletBehavior[2] == STATE_DEAD) {
ks = SMS_getKeysStatus();
if(ks & PORT_A_KEY_1) {
bulletBehavior[0] = playerPosition;
bulletBehavior[1] = INIT_P_POSY + 8;
bulletBehavior[2] = STATE_FIRED;
}
} else {
--bulletBehavior[1];
}
}
//====================================
// Behavior
//====================================
unsigned char checkCollision(unsigned int va[2][2], unsigned int vb[2][2]) {
if(va[1][0] < vb[0][0] || va[0][0] > vb[1][0]) return 0;
if(va[1][1] < vb[0][1] || va[0][1] > vb[1][1]) return 0;
return 1;
}
unsigned char checkBulletEnemyCollision() {
unsigned int e[2][2];
unsigned int b[2][2];
e[0][0] = bulletBehavior[0]+5; //min x
e[0][1] = bulletBehavior[1]+4; //min y
e[1][0] = bulletBehavior[0]+6; //max x
e[1][1] = bulletBehavior[1]+8; //max y
b[0][0] = enemyBehavior[0]+2; //min x // +2 pixels para diminuir o box
b[0][1] = enemyBehavior[1]; // min y
b[1][0] = enemyBehavior[0]+8; // max x // -1 pixel para diminuir o box
b[1][1] = enemyBehavior[1]+8; //max y
return checkCollision(e, b);
}
unsigned char checkEnemyShipCollision() {
unsigned int e[2][2];
unsigned int s[2][2];
s[0][0] = playerPosition; //min x
s[0][1] = INIT_P_POSY; //min y (y é fixo)
s[1][0] = playerPosition+8; //max x
s[1][1] = INIT_P_POSY+8; //max y
e[0][0] = enemyBehavior[0]+2; //min x // +2 pixels para diminuir o box
e[0][1] = enemyBehavior[1]; // min y
e[1][0] = enemyBehavior[0]+8; // max x
e[1][1] = enemyBehavior[1]+8; //max y
return checkCollision(e, s);
}
void checkEnemyOverlapsScreeen(void) {
if(enemyBehavior[1] >= MAX_XY) {
enemyBehavior[2] = STATE_DEAD;
}
}
void checkBulletOverlapsScreen(void) {
if(bulletBehavior[1] <= (GG_Y * 8) + 16) {
bulletBehavior[2] = STATE_DEAD;
}
}
void updateBullet(void) {
checkBulletOverlapsScreen();
if(bulletBehavior[2] == STATE_FIRED) {
if(checkBulletEnemyCollision() == 1) {
bulletBehavior[2] = STATE_DEAD;
enemyBehavior[2] = STATE_DEAD;
score += 10;
setScore(score);
}
}
}
void updateEnemy() {
checkEnemyOverlapsScreeen();
if(enemyBehavior[2] == STATE_ATACKING) {
if(checkEnemyShipCollision() == 1) {
setLives(--playerLives);
enemyBehavior[2] = STATE_DEAD;
playerPosition = INIT_P_POSX;
}
}
}
void update(void) {
movePlayer();
moveEnemy();
moveBullet();
updateEnemy();
updateBullet();
// mó...rreu
if(playerLives == 0) {
gameState = ST_GAMEOVER;
}
}
//====================================
// Drawing
//====================================
void drawBullet(void) {
if(bulletBehavior[2] == STATE_FIRED) {
SMS_addSprite(bulletBehavior[0], bulletBehavior[1], BULLET_TILE);
}
}
void drawPlayer(void) {
SMS_addSprite(playerPosition, INIT_P_POSY, SHIP_TILE);
drawBullet();
}
void drawEnemy(void) {
if(enemyBehavior[2] == STATE_ATACKING) {
SMS_addSprite(enemyBehavior[0], enemyBehavior[1], ENEMY_TILE);
}
}
void draw() {
SMS_initSprites();
drawPlayer();
drawEnemy();
SMS_finalizeSprites();
}
//====================================
// Scenes
//====================================
void gameOverScene(void) {
gameState = ST_TITLE;
titleScene();
}
void mainScene(void) {
playerPosition = INIT_P_POSX;
enemyBehavior[2] = STATE_DEAD;
bulletBehavior[2] = STATE_DEAD;
playerLives = START_LIVES;
score = 0;
level = 1;
loadMainAssets();
setScore(score);
setLevel(level);
setLives(playerLives);
SMS_displayOn();
while(gameState == ST_MAIN) {
update();
draw();
SMS_waitForVBlank();
SMS_copySpritestoSAT();
}
SMS_displayOff();
gameOverScene();
}
void titleScene(void) {
unsigned int ks = 0;
unsigned int x = 0;
const unsigned int *pnt;
loadTitleAssets();
SMS_displayOn();
while(gameState == ST_TITLE) {
for (x = 0; x < 13; x++) {
pnt = &press_start__tilemap__bin[x*2];
SMS_setNextTileatXY(GG_X+3+x,GG_Y+13);
SMS_setTile(*pnt+++PS_TILES);
}
ks = SMS_getKeysStatus();
if(ks & GG_KEY_START) {
gameState = ST_MAIN;
break;
}
SMS_waitForVBlank();
}
SMS_displayOff();
mainScene();
}
void main(void) {
gameState = ST_TITLE;
titleScene();
}