Skip to content

Commit 347b3ed

Browse files
committed
first implementation of scientists
1 parent da41733 commit 347b3ed

File tree

6 files changed

+105
-9
lines changed

6 files changed

+105
-9
lines changed

assets/dead_scientist.png

2.05 KB
Loading

assets/scientist/frame_01.png

1.73 KB
Loading

assets/scientist/frame_02.png

1.74 KB
Loading

assets/scientist/frame_03.png

1.73 KB
Loading

assets/scientist/frame_rate

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3

jetpack.c

+104-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define GRAVITY_BOOST -0.3
1212
#define GRAVITY_TICK 0.15
1313
#define PARTIVLE_VELOCITY 2
14+
#define SCIENTIST_VELOCITY_MIN 2
15+
#define SCIENTIST_VELOCITY_MAX 2
1416

1517
#define SCIENTISTS_MAX 6
1618
#define COINS_MAX 25
@@ -24,7 +26,6 @@ typedef struct {
2426
typedef struct {
2527
float gravity;
2628
POINT point;
27-
IconAnimation* sprite;
2829
bool isBoosting;
2930
} BARRY;
3031

@@ -37,17 +38,29 @@ typedef struct {
3738
POINT point;
3839
} PARTICLE;
3940

41+
typedef enum {
42+
ScientistStateAlive,
43+
ScientistStateDead,
44+
} ScientistState;
4045
typedef struct {
4146
float gravity;
4247
POINT point;
43-
IconAnimation* sprite;
48+
int velocity_x;
49+
ScientistState state;
4450
} SCIENTIST;
4551

4652
typedef enum {
4753
GameStateLife,
4854
GameStateGameOver,
4955
} State;
5056

57+
typedef struct {
58+
IconAnimation* barry;
59+
IconAnimation* scientist;
60+
Icon* coin;
61+
Icon* dead_scientist;
62+
} GameSprites;
63+
5164
typedef struct {
5265
int points;
5366
int distance;
@@ -56,6 +69,7 @@ typedef struct {
5669
COIN coins[COINS_MAX];
5770
PARTICLE particles[PARTICLES_MAX];
5871
State state;
72+
GameSprites sprites;
5973
FuriMutex* mutex;
6074
} GameState;
6175

@@ -80,6 +94,22 @@ static void jetpack_game_random_coins(GameState* const game_state) {
8094
}
8195
}
8296

97+
static void jetpack_game_random_scientists(GameState* const game_state) {
98+
// Check for an available slot for a new scientist
99+
for(int i = 0; i < SCIENTISTS_MAX; ++i) {
100+
if(game_state->scientists[i].point.x <= 0 &&
101+
(rand() % 1000) < 10) { // Spawn rate is less frequent than coins
102+
game_state->scientists[i].state = ScientistStateAlive;
103+
game_state->scientists[i].point.x = 127;
104+
game_state->scientists[i].point.y = 49;
105+
game_state->scientists[i].velocity_x =
106+
(rand() % (SCIENTIST_VELOCITY_MAX - SCIENTIST_VELOCITY_MIN + 1)) +
107+
SCIENTIST_VELOCITY_MIN; // random velocity between SCIENTIST_VELOCITY_MIN and SCIENTIST_VELOCITY_MAX
108+
break;
109+
}
110+
}
111+
}
112+
83113
static void jetpack_game_spawn_particles(GameState* const game_state) {
84114
for(int i = 0; i < PARTICLES_MAX; i++) {
85115
if(game_state->particles[i].point.y <= 0) {
@@ -97,14 +127,19 @@ static void jetpack_game_state_init(GameState* const game_state) {
97127
barry.gravity = 0;
98128
barry.point.x = 64;
99129
barry.point.y = 32;
100-
barry.sprite = icon_animation_alloc(&A_barry);
101130
barry.isBoosting = false;
102131

103-
icon_animation_start(barry.sprite);
132+
GameSprites sprites;
133+
sprites.barry = icon_animation_alloc(&A_barry);
134+
sprites.scientist = icon_animation_alloc(&A_scientist);
135+
136+
icon_animation_start(sprites.scientist);
137+
icon_animation_start(sprites.barry);
104138

105139
game_state->barry = barry;
106140
game_state->points = 0;
107141
game_state->distance = 0;
142+
game_state->sprites = sprites;
108143
game_state->state = GameStateLife;
109144

110145
memset(game_state->scientists, 0, sizeof(game_state->scientists));
@@ -113,7 +148,8 @@ static void jetpack_game_state_init(GameState* const game_state) {
113148
}
114149

115150
static void jetpack_game_state_free(GameState* const game_state) {
116-
icon_animation_free(game_state->barry.sprite);
151+
icon_animation_free(game_state->sprites.barry);
152+
icon_animation_free(game_state->sprites.scientist);
117153
free(game_state);
118154
}
119155

@@ -152,13 +188,49 @@ static void jetpack_game_tick(GameState* const game_state) {
152188
for(int i = 0; i < PARTICLES_MAX; i++) {
153189
if(game_state->particles[i].point.y > 0) {
154190
game_state->particles[i].point.y += PARTIVLE_VELOCITY;
191+
192+
// Check collision with scientists
193+
for(int j = 0; j < SCIENTISTS_MAX; j++) {
194+
if(game_state->scientists[j].state == ScientistStateAlive &&
195+
game_state->scientists[j].point.x > 0) {
196+
// Added half the width and height of the scientist sprite to the scientist's x and y respectively
197+
float scientist_center_x = game_state->scientists[j].point.x + 5.5;
198+
float scientist_center_y = game_state->scientists[j].point.y + 7.5;
199+
if(!(game_state->particles[i].point.x >
200+
scientist_center_x +
201+
5.5 || // particle is to the right of the scientist
202+
game_state->particles[i].point.x + 11 <
203+
scientist_center_x -
204+
5.5 || // particle is to the left of the scientist
205+
game_state->particles[i].point.y >
206+
scientist_center_y + 7.5 || // particle is below the scientist
207+
game_state->particles[i].point.y + 15 <
208+
scientist_center_y - 7.5)) { // particle is above the scientist
209+
game_state->scientists[j].state = ScientistStateDead;
210+
game_state->points += 2; // Increase the score by 2
211+
}
212+
}
213+
}
214+
155215
if(game_state->particles[i].point.x < 0 || game_state->particles[i].point.x > 128 ||
156216
game_state->particles[i].point.y < 0 || game_state->particles[i].point.y > 64) {
157217
game_state->particles[i].point.y = 0;
158218
}
159219
}
160220
}
161221

222+
// Move scientists
223+
for(int i = 0; i < SCIENTISTS_MAX; i++) {
224+
if(game_state->scientists[i].point.x > 0) {
225+
game_state->scientists[i].point.x -=
226+
game_state->scientists[i].velocity_x; // move based on velocity_x
227+
if(game_state->scientists[i].point.x < -16) { // if the scientist is out of screen
228+
game_state->scientists[i].point.x =
229+
0; // set scientist x coordinate to 0 to mark it as "inactive"
230+
}
231+
}
232+
}
233+
162234
// Spawn scientists and coins...
163235
jetpack_game_random_coins(game_state);
164236
// Sprite height of Barry
@@ -175,6 +247,7 @@ static void jetpack_game_tick(GameState* const game_state) {
175247

176248
// spawn scientists and coins...
177249
jetpack_game_random_coins(game_state);
250+
jetpack_game_random_scientists(game_state);
178251

179252
if(game_state->barry.isBoosting) {
180253
game_state->barry.gravity += GRAVITY_BOOST;
@@ -187,8 +260,6 @@ static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
187260
const GameState* game_state = ctx;
188261
furi_mutex_acquire(game_state->mutex, FuriWaitForever);
189262

190-
canvas_draw_frame(canvas, 0, 0, 128, 64);
191-
192263
if(game_state->state == GameStateLife) {
193264
// Draw scene
194265

@@ -200,9 +271,28 @@ static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
200271
}
201272
}
202273

274+
// Draw scientists
275+
for(int i = 0; i < SCIENTISTS_MAX; ++i) {
276+
if(game_state->scientists[i].point.x > 0) {
277+
if(game_state->scientists[i].state == ScientistStateAlive) {
278+
canvas_draw_icon_animation(
279+
canvas,
280+
game_state->scientists[i].point.x,
281+
game_state->scientists[i].point.y,
282+
game_state->sprites.scientist);
283+
} else {
284+
canvas_draw_icon(
285+
canvas,
286+
game_state->scientists[i].point.x,
287+
game_state->scientists[i].point.y,
288+
&I_dead_scientist);
289+
}
290+
}
291+
}
292+
203293
// Draw particles
204294
for(int i = 0; i < PARTICLES_MAX; i++) {
205-
if(game_state->particles[i].point.x > 0) {
295+
if(game_state->particles[i].point.y > 0) {
206296
canvas_draw_line(
207297
canvas,
208298
game_state->particles[i].point.x,
@@ -214,7 +304,10 @@ static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
214304

215305
// Draw barry
216306
canvas_draw_icon_animation(
217-
canvas, game_state->barry.point.x, game_state->barry.point.y, game_state->barry.sprite);
307+
canvas,
308+
game_state->barry.point.x,
309+
game_state->barry.point.y,
310+
game_state->sprites.barry);
218311

219312
canvas_set_font(canvas, FontSecondary);
220313
char buffer[12];
@@ -229,6 +322,8 @@ static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
229322
// Show highscore
230323
}
231324

325+
canvas_draw_frame(canvas, 0, 0, 128, 64);
326+
232327
furi_mutex_release(game_state->mutex);
233328
}
234329

0 commit comments

Comments
 (0)