Skip to content

Commit 0d87ae5

Browse files
authored
Merge pull request #1 from timstrasser/refactor
major refactor #1
2 parents 347b3ed + 9119e76 commit 0d87ae5

14 files changed

+365
-244
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
dist/*
22
.vscode
33
.clang-format
4-
.editorconfig
5-
**/.DS_Store
4+
.editorconfig

includes/barry.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "barry.h"
2+
#include "game_sprites.h"
3+
4+
#include <gui/gui.h>
5+
#include <furi.h>
6+
7+
void barry_tick(BARRY* const barry) {
8+
// Do jetpack things
9+
barry->gravity += GRAVITY_TICK;
10+
barry->point.y += barry->gravity;
11+
12+
// Constrain barry's height within sprite_height and 64 - sprite_height
13+
if(barry->point.y > (64 - BARRY_HEIGHT)) {
14+
barry->point.y = 64 - BARRY_HEIGHT;
15+
barry->gravity = 0; // stop upward momentum
16+
} else if(barry->point.y < 0) {
17+
barry->point.y = 0;
18+
barry->gravity = 0; // stop downward momentum
19+
}
20+
21+
if(barry->isBoosting) {
22+
barry->gravity += GRAVITY_BOOST;
23+
}
24+
}
25+
26+
void draw_barry(const BARRY* barry, Canvas* const canvas, const GameSprites* sprites) {
27+
canvas_draw_icon_animation(canvas, barry->point.x, barry->point.y, sprites->barry);
28+
}

includes/barry.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef BARRY_H
2+
#define BARRY_H
3+
4+
#include <stdbool.h>
5+
6+
#include <gui/gui.h>
7+
#include "point.h"
8+
#include "game_sprites.h"
9+
10+
#define GRAVITY_BOOST -0.3
11+
#define GRAVITY_TICK 0.15
12+
13+
#define BARRY_HEIGHT 15
14+
#define BARRY_WIDTH 11
15+
16+
typedef struct {
17+
float gravity;
18+
POINT point;
19+
bool isBoosting;
20+
} BARRY;
21+
22+
void barry_tick(BARRY* const barry);
23+
void draw_barry(const BARRY* barry, Canvas* const canvas, const GameSprites* sprites);
24+
25+
#endif // BARRY_H

includes/coin.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdlib.h>
2+
#include <stdbool.h>
3+
4+
#include <jetpack_joyride_icons.h>
5+
#include <gui/gui.h>
6+
7+
#include "coin.h"
8+
#include "barry.h"
9+
10+
void coin_tick(COIN* const coins, BARRY* const barry, int* const poins) {
11+
// Move coins towards the player
12+
for(int i = 0; i < COINS_MAX; i++) {
13+
if(coin_colides(&coins[i], barry)) {
14+
coins[i].point.x = 0; // Remove the coin
15+
(*poins)++;
16+
}
17+
if(coins[i].point.x > 0) {
18+
coins[i].point.x -= 1; // move left by 1 unit
19+
if(coins[i].point.x < -16) { // if the coin is out of screen
20+
coins[i].point.x = 0; // set coin x coordinate to 0 to mark it as "inactive"
21+
}
22+
}
23+
}
24+
}
25+
26+
bool coin_colides(COIN* const coin, BARRY* const barry) {
27+
return !(
28+
barry->point.x > coin->point.x + 7 || // Barry is to the right of the coin
29+
barry->point.x + 11 < coin->point.x || // Barry is to the left of the coin
30+
barry->point.y > coin->point.y + 7 || // Barry is below the coin
31+
barry->point.y + 15 < coin->point.y); // Barry is above the coin
32+
}
33+
34+
void spawn_random_coin(COIN* const coins) {
35+
// Check for an available slot for a new coin
36+
for(int i = 0; i < COINS_MAX; ++i) {
37+
if(coins[i].point.x <= 0) {
38+
coins[i].point.x = 127;
39+
coins[i].point.y = rand() % 64;
40+
break;
41+
}
42+
}
43+
}
44+
45+
void draw_coins(const COIN* coins, Canvas* const canvas) {
46+
for(int i = 0; i < COINS_MAX; ++i) {
47+
if(coins[i].point.x > 0) {
48+
canvas_draw_icon(canvas, coins[i].point.x, coins[i].point.y, &I_coin);
49+
}
50+
}
51+
}

includes/coin.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef COIN_H
2+
#define COIN_H
3+
4+
#include <gui/gui.h>
5+
6+
#include "point.h"
7+
#include "barry.h"
8+
9+
#define COINS_MAX 25
10+
11+
typedef struct {
12+
float gravity;
13+
POINT point;
14+
} COIN;
15+
16+
void coin_tick(COIN* const coins, BARRY* const barry, int* const poins);
17+
void spawn_random_coin(COIN* const coins);
18+
bool coin_colides(COIN* const coin, BARRY* const barry);
19+
void draw_coins(const COIN* coins, Canvas* const canvas);
20+
21+
#endif // COIN_H

includes/game_sprites.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef GAME_SPRITES_H
2+
#define GAME_SPRITES_H
3+
4+
#include <gui/icon_animation.h>
5+
6+
typedef struct {
7+
IconAnimation* barry;
8+
IconAnimation* scientist;
9+
} GameSprites;
10+
11+
#endif // GAME_SPRITES_H

includes/game_state.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "game_state.h"
2+
3+
void game_state_tick(GameState* const game_state) {
4+
game_state->distance++;
5+
}

includes/game_state.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef GAMESTATE_H
2+
#define GAMESTATE_H
3+
4+
#include <gui/icon_animation.h>
5+
#include <furi.h>
6+
7+
#include "barry.h"
8+
#include "scientist.h"
9+
#include "coin.h"
10+
#include "particle.h"
11+
#include "game_sprites.h"
12+
13+
typedef enum {
14+
GameStateLife,
15+
GameStateGameOver,
16+
} State;
17+
18+
typedef struct {
19+
int points;
20+
int distance;
21+
BARRY barry;
22+
COIN coins[COINS_MAX];
23+
PARTICLE particles[PARTICLES_MAX];
24+
SCIENTIST scientists[SCIENTISTS_MAX];
25+
State state;
26+
GameSprites sprites;
27+
FuriMutex* mutex;
28+
} GameState;
29+
30+
void game_state_tick(GameState* const game_state);
31+
32+
#endif // GAMESTATE_H

includes/particle.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdlib.h>
2+
3+
#include "particle.h"
4+
#include "scientist.h"
5+
#include "barry.h"
6+
7+
void particle_tick(PARTICLE* const particles, SCIENTIST* const scientists, int* const points) {
8+
// Move particles
9+
for(int i = 0; i < PARTICLES_MAX; i++) {
10+
if(particles[i].point.y > 0) {
11+
particles[i].point.y += PARTICLE_VELOCITY;
12+
13+
// Check collision with scientists
14+
for(int j = 0; j < SCIENTISTS_MAX; j++) {
15+
if(scientists[j].state == ScientistStateAlive && scientists[j].point.x > 0) {
16+
// Added half the width and height of the scientist sprite to the scientist's x and y respectively
17+
float scientist_center_x = scientists[j].point.x + 5.5;
18+
float scientist_center_y = scientists[j].point.y + 7.5;
19+
if(!(particles[i].point.x >
20+
scientist_center_x +
21+
5.5 || // particle is to the right of the scientist
22+
particles[i].point.x + 11 <
23+
scientist_center_x -
24+
5.5 || // particle is to the left of the scientist
25+
particles[i].point.y >
26+
scientist_center_y + 7.5 || // particle is below the scientist
27+
particles[i].point.y + 15 <
28+
scientist_center_y - 7.5)) { // particle is above the scientist
29+
scientists[j].state = ScientistStateDead;
30+
(*points) += 2; // Increase the score by 2
31+
}
32+
}
33+
}
34+
35+
if(particles[i].point.x < 0 || particles[i].point.x > 128 ||
36+
particles[i].point.y < 0 || particles[i].point.y > 64) {
37+
particles[i].point.y = 0;
38+
}
39+
}
40+
}
41+
}
42+
43+
void spawn_random_particles(PARTICLE* const particles, BARRY* const barry) {
44+
for(int i = 0; i < PARTICLES_MAX; i++) {
45+
if(particles[i].point.y <= 0) {
46+
particles[i].point.x = barry->point.x + (rand() % 7) - 3;
47+
particles[i].point.y = barry->point.y;
48+
break;
49+
}
50+
}
51+
}
52+
53+
void draw_particles(const PARTICLE* particles, Canvas* const canvas) {
54+
for(int i = 0; i < PARTICLES_MAX; i++) {
55+
if(particles[i].point.y > 0) {
56+
canvas_draw_line(
57+
canvas,
58+
particles[i].point.x,
59+
particles[i].point.y,
60+
particles[i].point.x,
61+
particles[i].point.y + 3);
62+
}
63+
}
64+
}

includes/particle.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
#ifndef PARTICLE_H
4+
#define PARTICLE_H
5+
6+
#include "point.h"
7+
#include "scientist.h"
8+
#include "barry.h"
9+
10+
#define PARTICLES_MAX 50
11+
#define PARTICLE_VELOCITY 2
12+
13+
typedef struct {
14+
POINT point;
15+
} PARTICLE;
16+
17+
void particle_tick(PARTICLE* const particles, SCIENTIST* const scientists, int* const points);
18+
void spawn_random_particles(PARTICLE* const particles, BARRY* const barry);
19+
void draw_particles(const PARTICLE* particles, Canvas* const canvas);
20+
21+
#endif // PARTICLE_H

includes/point.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef POINT_H
2+
#define POINT_H
3+
4+
typedef struct {
5+
int x;
6+
int y;
7+
} POINT;
8+
9+
#endif // POINT_H

includes/scientist.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "scientist.h"
2+
#include "game_sprites.h"
3+
4+
#include <jetpack_joyride_icons.h>
5+
#include <gui/gui.h>
6+
7+
void scientist_tick(SCIENTIST* const scientists) {
8+
for(int i = 0; i < SCIENTISTS_MAX; i++) {
9+
if(scientists[i].point.x > 0) {
10+
scientists[i].point.x -= scientists[i].velocity_x; // move based on velocity_x
11+
if(scientists[i].point.x < -16) { // if the scientist is out of screen
12+
scientists[i].point.x =
13+
0; // set scientist x coordinate to 0 to mark it as "inactive"
14+
}
15+
}
16+
}
17+
}
18+
19+
void spawn_random_scientist(SCIENTIST* const scientists) {
20+
// Check for an available slot for a new scientist
21+
for(int i = 0; i < SCIENTISTS_MAX; ++i) {
22+
if(scientists[i].point.x <= 0 &&
23+
(rand() % 1000) < 10) { // Spawn rate is less frequent than coins
24+
scientists[i].state = ScientistStateAlive;
25+
scientists[i].point.x = 127;
26+
scientists[i].point.y = 49;
27+
scientists[i].velocity_x =
28+
(rand() % (SCIENTIST_VELOCITY_MAX - SCIENTIST_VELOCITY_MIN + 1)) +
29+
SCIENTIST_VELOCITY_MIN; // random velocity between SCIENTIST_VELOCITY_MIN and SCIENTIST_VELOCITY_MAX
30+
break;
31+
}
32+
}
33+
}
34+
35+
void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const GameSprites* sprites) {
36+
for(int i = 0; i < SCIENTISTS_MAX; ++i) {
37+
if(scientists[i].point.x > 0) {
38+
if(scientists[i].state == ScientistStateAlive) {
39+
canvas_draw_icon_animation(
40+
canvas, scientists[i].point.x, scientists[i].point.y, sprites->scientist);
41+
} else {
42+
canvas_draw_icon(
43+
canvas, scientists[i].point.x, scientists[i].point.y, &I_dead_scientist);
44+
}
45+
}
46+
}
47+
}

includes/scientist.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef SCIENTIST_H
2+
#define SCIENTIST_H
3+
4+
#include "point.h"
5+
#include "game_sprites.h"
6+
#include <gui/gui.h>
7+
8+
#define SCIENTIST_VELOCITY_MIN 2
9+
#define SCIENTIST_VELOCITY_MAX 2
10+
11+
#define SCIENTISTS_MAX 6
12+
13+
typedef enum {
14+
ScientistStateAlive,
15+
ScientistStateDead,
16+
} ScientistState;
17+
18+
typedef struct {
19+
float gravity;
20+
POINT point;
21+
int velocity_x;
22+
ScientistState state;
23+
} SCIENTIST;
24+
25+
void scientist_tick(SCIENTIST* const scientist);
26+
void spawn_random_scientist(SCIENTIST* const scientists);
27+
void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const GameSprites* sprites);
28+
29+
#endif // SCIENTIST_H

0 commit comments

Comments
 (0)