-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
164 lines (141 loc) · 4.13 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
#include "graphics.h"
#include "dino.h"
#include "myLib.h"
#include "colors.h"
#include "main.h"
#include "text.h"
#include "game.h"
#include "enemy.h"
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define GROUND 100
#define MIN_Y 50
#define SCORE_DIVIDER 100
#define UNUSED(var) ((void) (var))
bool jumped = false;
int mult = 1;
volatile unsigned int score;
volatile int num_enemies = 0;
AppState initialAppState() {
AppState appState;
Dino dino = (Dino) {
STATE_LEFT,
(Point) {0, 75}, // Current location
(Point) {0, 0}, // New location
(Vector) {0, 0}, // Current velocity
(Vector) {0, 0}, // New velocity
0,
};
appState.dino = dino;
for (unsigned int i = 0; i < MAX_ENEMIES; i++) {
appState.enemies[i] = (Enemy) {
.alive = false,
(Point) {0, 0},
(Point) {0, 0},
(Vector) {0, 0},
(Vector) {0, 0},
(Size) {0, 0},
NULL,
};
}
appState.score = 0;
appState.tick = 0;
return appState;
}
/*
* Reset app state at beginning of game
*/
void resetAppState(AppState *appState) {
Dino dino = (Dino) {
STATE_LEFT,
(Point) {0, 75}, // Current location
(Point) {0, 0}, // New location
(Vector) {0, 0}, // Current velocity
(Vector) {0, 0}, // New velocity
0,
};
appState->dino = dino;
for (unsigned int i = 0; i < MAX_ENEMIES; i++) {
appState->enemies[i] = (Enemy) {
.alive = false,
(Point) {0, 0},
(Point) {0, 0},
(Vector) {0, 0},
(Vector) {0, 0},
(Size) {0, 0},
NULL,
};
}
appState->score = 0;
}
/**
* Update the app state
*/
void updateAppState(AppState *appState, uint_t previousButtons, uint_t currentButtons) {
// Update score every 16th tick
if ((appState->tick & 127) == 0) {
// TODO In the future the score should scale with speed
appState->score += 1;
}
// Update feet every 8th tick
if ((appState->tick & 63) == 0) {
dino__update_state(&appState->dino);
}
if (KEY_JUST_PRESSED(BUTTON_UP, previousButtons, currentButtons)) {
appState->dino.state = STATE_STILL;
dino__jump(&appState->dino);
}
if (didLose(&appState->dino, appState->enemies)) {
appState->dino.state = STATE_STILL;
}
if (num_enemies < MAX_ENEMIES) {
// Decide if we should place another enemy
int p = rand() % (1000 + 1 - 0) + 0;
if (p < 10) {
Enemy new_enemy = enemy__random();
for (unsigned int i = 0; i < MAX_ENEMIES; i++) {
if (!appState->enemies[i].alive) {
appState->enemies[i] = new_enemy;
}
}
}
}
dino__update(&appState->dino);
}
/*
* Checks if player is contacting an enemy
*/
bool didLose(Dino *dino, Enemy *enemies) {
for (int i = 0; i < MAX_ENEMIES; i++) {
Enemy enemy = enemies[i];
if (enemy.alive) {
// If left corner is in
int x = enemy.p.x;
int y = enemy.p.y;
if (x > dino->p.x && (x < (dino->p.x + DINO_WIDTH)) && y > dino->p.y && (y < (dino->p.y + DINO_HEIGHT))) {
return true;
}
// Bottom left
x = enemy.p.x;
y = enemy.p.y + enemy.size.height;
if (x > dino->p.x && (x < (dino->p.x + DINO_WIDTH)) && y > dino->p.y && (y < (dino->p.y + DINO_HEIGHT))) {
return true;
}
// Bottom right
x = enemy.p.x + enemy.size.width;
y = enemy.p.y + enemy.size.height;
if (x > dino->p.x && (x < (dino->p.x + DINO_WIDTH)) && y > dino->p.y && (y < (dino->p.y + DINO_HEIGHT))) {
return true;
}
// Top right
x = enemy.p.x + enemy.size.width;
y = enemy.p.y;
if (x > dino->p.x && (x < (dino->p.x + DINO_WIDTH)) && y > dino->p.y && (y < (dino->p.y + DINO_HEIGHT))) {
return true;
}
}
}
return false;
}