Add NFC Maker and Jetpack Joyride apps

This commit is contained in:
Willy-JL
2023-07-04 23:57:01 +02:00
parent 02943dd1db
commit 2ada7b3bbb
65 changed files with 2347 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
# For details & more options, see documentation/AppManifests.md in firmware repo
App(
appid="jetpack_joyride",
name="Jetpack Joyride",
apptype=FlipperAppType.EXTERNAL,
entry_point="jetpack_game_app",
cdefines=["APP_JETPACK_GAME"],
requires=["gui"],
stack_size=4 * 1024,
order=100,
fap_icon="icon.png",
fap_category="Games",
fap_icon_assets="assets",
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1 @@
3

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -0,0 +1 @@
3

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 835 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 968 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 886 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -0,0 +1 @@
3

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,81 @@
#include <jetpack_joyride_icons.h>
#include "background_assets.h"
static AssetProperties assetProperties[BG_ASSETS_MAX] = {
{.width = 27, .spawn_chance = 1, .x_offset = 24, .y_offset = 36, .sprite = &I_door},
{.width = 12, .spawn_chance = 6, .x_offset = 33, .y_offset = 14, .sprite = &I_air_vent}};
void background_assets_tick(BackgroundAsset* const assets) {
// Move assets towards the player
for(int i = 0; i < BG_ASSETS_MAX; i++) {
if(assets[i].visible) {
assets[i].point.x -= 1; // move left by 2 units
if(assets[i].point.x <=
-assets[i].properties->width) { // if the asset is out of screen
assets[i].visible = false; // set asset x coordinate to 0 to mark it as "inactive"
}
}
}
}
void spawn_random_background_asset(BackgroundAsset* const assets) {
// Calculate the total spawn chances for all assets
int total_spawn_chance = 0;
for(int i = 0; i < BG_ASSETS_MAX; ++i) {
total_spawn_chance += assetProperties[i].spawn_chance;
}
// Generate a random number between 0 and total_spawn_chance
int random_number = rand() % total_spawn_chance;
// Select the asset based on the random number
int chosen_asset = -1;
int accumulated_chance = 0;
for(int i = 0; i < BG_ASSETS_MAX; ++i) {
accumulated_chance += assetProperties[i].spawn_chance;
if(random_number < accumulated_chance) {
chosen_asset = i;
break;
}
}
// If no asset is chosen, return
if(chosen_asset == -1) {
return;
}
// Look for an available slot for the chosen asset
for(int i = 0; i < BG_ASSETS_MAX; ++i) {
if(assets[i].visible == false) {
// Spawn the asset
assets[i].point.x = 127 + assetProperties[chosen_asset].x_offset;
assets[i].point.y = assetProperties[chosen_asset].y_offset;
assets[i].properties = &assetProperties[chosen_asset];
assets[i].visible = true;
break;
}
}
}
void draw_background_assets(const BackgroundAsset* assets, Canvas* const canvas, int distance) {
canvas_draw_box(canvas, 0, 6, 128, 1);
canvas_draw_box(canvas, 0, 56, 128, 2);
// Calculate the pillar offset based on the traveled distance
int pillar_offset = distance % 64;
// Draw pillars
for(int x = -pillar_offset; x < 128; x += 64) {
canvas_draw_icon(canvas, x, 6, &I_pillar);
}
// Draw assets
for(int i = 0; i < BG_ASSETS_MAX; ++i) {
if(assets[i].visible) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon(
canvas, assets[i].point.x, assets[i].point.y, assets[i].properties->sprite);
}
}
}

View File

@@ -0,0 +1,34 @@
#ifndef BACKGROUND_ASSETS_H
#define BACKGROUND_ASSETS_H
#include <stdlib.h>
#include <stdbool.h>
#include <gui/gui.h>
#include "point.h"
#include "states.h"
#include "game_sprites.h"
#include <jetpack_joyride_icons.h>
#define BG_ASSETS_MAX 3
typedef struct {
int width;
int spawn_chance;
int x_offset;
int y_offset;
const Icon* sprite;
} AssetProperties;
typedef struct {
POINT point;
AssetProperties* properties;
bool visible;
} BackgroundAsset;
void background_assets_tick(BackgroundAsset* const assets);
void spawn_random_background_asset(BackgroundAsset* const assets);
void draw_background_assets(const BackgroundAsset* assets, Canvas* const canvas, int distance);
#endif // BACKGROUND_ASSETS_H

View File

@@ -0,0 +1,33 @@
#include "barry.h"
#include "game_sprites.h"
#include <gui/gui.h>
#include <furi.h>
void barry_tick(BARRY* const barry) {
// Do jetpack things
if(barry->isBoosting) {
barry->gravity += GRAVITY_BOOST; // Increase upward momentum
} else {
barry->gravity += GRAVITY_FALL; // Increase downward momentum faster
}
barry->point.y += barry->gravity;
// Constrain barry's height within sprite_height and 64 - sprite_height
if(barry->point.y > (64 - BARRY_HEIGHT)) {
barry->point.y = 64 - BARRY_HEIGHT;
barry->gravity = 0; // stop upward momentum
} else if(barry->point.y < 0) {
barry->point.y = 0;
barry->gravity = 0; // stop downward momentum
}
}
void draw_barry(const BARRY* barry, Canvas* const canvas, const GameSprites* sprites) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon_animation(canvas, barry->point.x, barry->point.y, sprites->barry);
canvas_set_color(canvas, ColorWhite);
canvas_draw_icon(canvas, barry->point.x, barry->point.y, sprites->barry_infill);
}

View File

@@ -0,0 +1,23 @@
#ifndef BARRY_H
#define BARRY_H
#include <stdbool.h>
#include <gui/gui.h>
#include "point.h"
#include "game_sprites.h"
#define GRAVITY_TICK 0.2
#define GRAVITY_BOOST -0.4
#define GRAVITY_FALL 0.3
typedef struct {
float gravity;
POINT point;
bool isBoosting;
} BARRY;
void barry_tick(BARRY* const barry);
void draw_barry(const BARRY* barry, Canvas* const canvas, const GameSprites* sprites);
#endif // BARRY_H

View File

@@ -0,0 +1,98 @@
#include <stdlib.h>
#include <stdbool.h>
#include <jetpack_joyride_icons.h>
#include <gui/gui.h>
#include "coin.h"
#include "barry.h"
#define PATTERN_MAX_HEIGHT 40
// Patterns
const COIN_PATTERN coin_patterns[] = {
{// Square pattern
.count = 9,
.coins = {{0, 0}, {8, 0}, {16, 0}, {0, 8}, {8, 8}, {16, 8}, {0, 16}, {8, 16}, {16, 16}}},
{// Wavy pattern (approximate sine wave)
.count = 8,
.coins = {{0, 8}, {8, 16}, {16, 24}, {24, 16}, {32, 8}, {40, 0}, {48, 8}, {56, 16}}},
{// Diagonal pattern
.count = 5,
.coins = {{0, 0}, {8, 8}, {16, 16}, {24, 24}, {32, 32}}},
// Add more patterns here
};
void coin_tick(COIN* const coins, BARRY* const barry, int* const total_coins) {
// Move coins towards the player
for(int i = 0; i < COINS_MAX; i++) {
if(coin_colides(&coins[i], barry)) {
coins[i].point.x = 0; // Remove the coin
(*total_coins)++;
}
if(coins[i].point.x > 0) {
coins[i].point.x -= 1; // move left by 1 unit
if(coins[i].point.x < -COIN_WIDTH) { // if the coin is out of screen
coins[i].point.x = 0; // set coin x coordinate to 0 to mark it as "inactive"
}
}
}
}
bool coin_colides(COIN* const coin, BARRY* const barry) {
return !(
barry->point.x > coin->point.x + COIN_WIDTH || // Barry is to the right of the coin
barry->point.x + BARRY_WIDTH < coin->point.x || // Barry is to the left of the coin
barry->point.y > coin->point.y + COIN_WIDTH || // Barry is below the coin
barry->point.y + BARRY_HEIGHT < coin->point.y); // Barry is above the coin
}
void spawn_random_coin(COIN* const coins) {
// Select a random pattern
int pattern_index = rand() % (sizeof(coin_patterns) / sizeof(coin_patterns[0]));
const COIN_PATTERN* pattern = &coin_patterns[pattern_index];
// Count available slots for new coins
int available_slots = 0;
for(int i = 0; i < COINS_MAX; ++i) {
if(coins[i].point.x <= 0) {
++available_slots;
}
}
// If there aren't enough slots, return without spawning coins
if(available_slots < pattern->count) return;
// Spawn coins according to the selected pattern
int coin_index = 0;
int random_offset = rand() % (SCREEN_HEIGHT - PATTERN_MAX_HEIGHT);
int random_offset_x = rand() % 16;
for(int i = 0; i < pattern->count; ++i) {
// Find an available slot for a new coin
while(coins[coin_index].point.x > 0 && coin_index < COINS_MAX) {
++coin_index;
}
// If no slot is available, stop spawning coins
if(coin_index == COINS_MAX) break;
// Spawn the coin
coins[coin_index].point.x = SCREEN_WIDTH - 1 + pattern->coins[i].x + random_offset_x;
coins[coin_index].point.y =
random_offset +
pattern->coins[i]
.y; // The pattern is spawned at a random y position, but not too close to the screen edge
}
}
void draw_coins(const COIN* coins, Canvas* const canvas, const GameSprites* sprites) {
canvas_set_color(canvas, ColorBlack);
for(int i = 0; i < COINS_MAX; ++i) {
if(coins[i].point.x > 0) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon(canvas, coins[i].point.x, coins[i].point.y, sprites->coin);
canvas_set_color(canvas, ColorWhite);
canvas_draw_icon(canvas, coins[i].point.x, coins[i].point.y, sprites->coin_infill);
}
}
}

View File

@@ -0,0 +1,26 @@
#ifndef COIN_H
#define COIN_H
#include <gui/gui.h>
#include "point.h"
#include "barry.h"
#define COINS_MAX 15
typedef struct {
float gravity;
POINT point;
} COIN;
typedef struct {
int count;
POINT coins[COINS_MAX];
} COIN_PATTERN;
void coin_tick(COIN* const coins, BARRY* const barry, int* const poins);
void spawn_random_coin(COIN* const coins);
bool coin_colides(COIN* const coin, BARRY* const barry);
void draw_coins(const COIN* coins, Canvas* const canvas, const GameSprites* sprites);
#endif // COIN_H

View File

@@ -0,0 +1,35 @@
#ifndef GAME_SPRITES_H
#define GAME_SPRITES_H
#include "point.h"
#include <gui/icon_animation.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BARRY_WIDTH 11
#define BARRY_HEIGHT 15
#define MISSILE_WIDTH 26
#define MISSILE_HEIGHT 12
#define SCIENTIST_WIDTH 9
#define SCIENTIST_HEIGHT 14
#define COIN_WIDTH 7
typedef struct {
IconAnimation* barry;
const Icon* barry_infill;
const Icon* scientist_left;
const Icon* scientist_left_infill;
const Icon* scientist_right;
const Icon* scientist_right_infill;
const Icon* coin;
const Icon* coin_infill;
IconAnimation* missile;
IconAnimation* alert;
const Icon* missile_infill;
} GameSprites;
#endif // GAME_SPRITES_H

View File

@@ -0,0 +1,5 @@
#include "game_state.h"
void game_state_tick(GameState* const game_state) {
game_state->distance++;
}

View File

@@ -0,0 +1,34 @@
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include <gui/icon_animation.h>
#include <furi.h>
#include "barry.h"
#include "scientist.h"
#include "coin.h"
#include "particle.h"
#include "game_sprites.h"
#include "states.h"
#include "missile.h"
#include "background_assets.h"
typedef struct {
int total_coins;
int distance;
bool new_highscore;
BARRY barry;
COIN coins[COINS_MAX];
PARTICLE particles[PARTICLES_MAX];
SCIENTIST scientists[SCIENTISTS_MAX];
MISSILE missiles[MISSILES_MAX];
BackgroundAsset bg_assets[BG_ASSETS_MAX];
State state;
GameSprites sprites;
FuriMutex* mutex;
FuriTimer* timer;
void (*death_handler)();
} GameState;
void game_state_tick(GameState* const game_state);
#endif // GAMESTATE_H

View File

@@ -0,0 +1,86 @@
#include <stdlib.h>
#include <stdbool.h>
#include <jetpack_joyride_icons.h>
#include <gui/gui.h>
#include "states.h"
#include "game_sprites.h"
#include "missile.h"
#include "barry.h"
void missile_tick(MISSILE* const missiles, BARRY* const barry, void (*death_handler)()) {
// Move missiles towards the player
for(int i = 0; i < MISSILES_MAX; i++) {
if(missiles[i].visible && missile_colides(&missiles[i], barry)) {
death_handler();
}
if(missiles[i].visible) {
missiles[i].point.x -= 2; // move left by 2 units
if(missiles[i].point.x < -MISSILE_WIDTH) { // if the missile is out of screen
missiles[i].visible = false; // set missile as "inactive"
}
}
}
}
void spawn_random_missile(MISSILE* const missiles) {
// Check for an available slot for a new missile
for(int i = 0; i < MISSILES_MAX; ++i) {
if(!missiles[i].visible) {
missiles[i].point.x = 2 * SCREEN_WIDTH;
missiles[i].point.y = rand() % (SCREEN_HEIGHT - MISSILE_HEIGHT);
missiles[i].visible = true;
break;
}
}
}
void draw_missiles(const MISSILE* missiles, Canvas* const canvas, const GameSprites* sprites) {
for(int i = 0; i < MISSILES_MAX; ++i) {
if(missiles[i].visible) {
canvas_set_color(canvas, ColorBlack);
if(missiles[i].point.x > 128) {
canvas_draw_icon_animation(
canvas, SCREEN_WIDTH - 7, missiles[i].point.y, sprites->alert);
} else {
canvas_draw_icon_animation(
canvas, missiles[i].point.x, missiles[i].point.y, sprites->missile);
canvas_set_color(canvas, ColorWhite);
canvas_draw_icon(
canvas, missiles[i].point.x, missiles[i].point.y, sprites->missile_infill);
}
}
}
}
bool missile_colides(MISSILE* const missile, BARRY* const barry) {
return !(
barry->point.x >
missile->point.x + MISSILE_WIDTH - 14 || // Barry is to the right of the missile
barry->point.x + BARRY_WIDTH - 3 <
missile->point.x || // Barry is to the left of the missile
barry->point.y > missile->point.y + MISSILE_HEIGHT || // Barry is below the missile
barry->point.y + BARRY_HEIGHT < missile->point.y); // Barry is above the missile
}
int get_rocket_spawn_distance(int player_distance) {
// Define the start and end points for rocket spawn distance
int start_distance = 256;
int end_distance = 24;
// Define the maximum player distance at which the spawn distance should be at its minimum
int max_player_distance = 5000; // Adjust this value based on your game's difficulty curve
if(player_distance >= max_player_distance) {
return end_distance;
}
// Calculate the linear interpolation factor
float t = (float)player_distance / max_player_distance;
// Interpolate the rocket spawn distance
return start_distance + t * (end_distance - start_distance);
}

View File

@@ -0,0 +1,24 @@
#ifndef MISSILE_H
#define MISSILE_H
#include <gui/gui.h>
#include "game_sprites.h"
#include "states.h"
#include "point.h"
#include "barry.h"
#define MISSILES_MAX 5
typedef struct {
POINT point;
bool visible;
} MISSILE;
void missile_tick(MISSILE* const missiles, BARRY* const barry, void (*death_handler)());
void spawn_random_missile(MISSILE* const MISSILEs);
bool missile_colides(MISSILE* const MISSILE, BARRY* const barry);
int get_rocket_spawn_distance(int player_distance);
void draw_missiles(const MISSILE* missiles, Canvas* const canvas, const GameSprites* sprites);
#endif // MISSILE_H

View File

@@ -0,0 +1,57 @@
#include <stdlib.h>
#include "particle.h"
#include "scientist.h"
#include "barry.h"
void particle_tick(PARTICLE* const particles, SCIENTIST* const scientists) {
// Move particles
for(int i = 0; i < PARTICLES_MAX; i++) {
if(particles[i].point.y > 0) {
particles[i].point.y += PARTICLE_VELOCITY;
// Check collision with scientists
for(int j = 0; j < SCIENTISTS_MAX; j++) {
if(scientists[j].state == ScientistStateAlive && scientists[j].point.x > 0) {
// Check whether the particle lies within the scientist's bounding box
if(!(particles[i].point.x > scientists[j].point.x + SCIENTIST_WIDTH ||
particles[i].point.x < scientists[j].point.x ||
particles[i].point.y > scientists[j].point.y + SCIENTIST_HEIGHT ||
particles[i].point.y < scientists[j].point.y)) {
scientists[j].state = ScientistStateDead;
// (*points) += 2; // Increase the score by 2
}
}
}
if(particles[i].point.x < 0 || particles[i].point.x > SCREEN_WIDTH ||
particles[i].point.y < 0 || particles[i].point.y > SCREEN_HEIGHT) {
particles[i].point.y = 0;
}
}
}
}
void spawn_random_particles(PARTICLE* const particles, BARRY* const barry) {
for(int i = 0; i < PARTICLES_MAX; i++) {
if(particles[i].point.y <= 0) {
particles[i].point.x = barry->point.x + (rand() % 4);
particles[i].point.y = barry->point.y + 14;
break;
}
}
}
void draw_particles(const PARTICLE* particles, Canvas* const canvas) {
canvas_set_color(canvas, ColorBlack);
for(int i = 0; i < PARTICLES_MAX; i++) {
if(particles[i].point.y > 0) {
canvas_draw_line(
canvas,
particles[i].point.x,
particles[i].point.y,
particles[i].point.x,
particles[i].point.y + 3);
}
}
}

View File

@@ -0,0 +1,21 @@
#ifndef PARTICLE_H
#define PARTICLE_H
#include "point.h"
#include "scientist.h"
#include "barry.h"
#define PARTICLES_MAX 50
#define PARTICLE_VELOCITY 2
typedef struct {
POINT point;
} PARTICLE;
void particle_tick(PARTICLE* const particles, SCIENTIST* const scientists);
void spawn_random_particles(PARTICLE* const particles, BARRY* const barry);
void draw_particles(const PARTICLE* particles, Canvas* const canvas);
#endif // PARTICLE_H

View File

@@ -0,0 +1,14 @@
#ifndef POINT_H
#define POINT_H
typedef struct {
int x;
int y;
} POINT;
typedef struct {
float x;
float y;
} POINTF;
#endif // POINT_H

View File

@@ -0,0 +1,77 @@
#include "scientist.h"
#include "game_sprites.h"
#include <jetpack_joyride_icons.h>
#include <gui/gui.h>
void scientist_tick(SCIENTIST* const scientists) {
for(int i = 0; i < SCIENTISTS_MAX; i++) {
if(scientists[i].visible) {
if(scientists[i].point.x < 64) scientists[i].velocity_x = 0.5f;
scientists[i].point.x -= scientists[i].state == ScientistStateAlive ?
1 - scientists[i].velocity_x :
1; // move based on velocity_x
int width = (scientists[i].state == ScientistStateAlive) ? SCIENTIST_WIDTH :
SCIENTIST_HEIGHT;
if(scientists[i].point.x <= -width) { // if the scientist is out of screen
scientists[i].visible = false;
}
}
}
}
void spawn_random_scientist(SCIENTIST* const scientists) {
float velocities[] = {-0.5f, 0.0f, 0.5f, -1.0f};
// Check for an available slot for a new scientist
for(int i = 0; i < SCIENTISTS_MAX; ++i) {
if(!scientists[i].visible &&
(rand() % 1000) < 10) { // Spawn rate is less frequent than coins
scientists[i].state = ScientistStateAlive;
scientists[i].point.x = 127;
scientists[i].point.y = 49;
scientists[i].velocity_x = velocities[rand() % 4];
scientists[i].visible = true;
break;
}
}
}
void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const GameSprites* sprites) {
for(int i = 0; i < SCIENTISTS_MAX; ++i) {
if(scientists[i].visible) {
canvas_set_color(canvas, ColorBlack);
if(scientists[i].state == ScientistStateAlive) {
canvas_draw_icon(
canvas,
(int)scientists[i].point.x,
scientists[i].point.y,
scientists[i].velocity_x >= 0 ? sprites->scientist_right :
sprites->scientist_left);
canvas_set_color(canvas, ColorWhite);
canvas_draw_icon(
canvas,
(int)scientists[i].point.x,
scientists[i].point.y,
scientists[i].velocity_x >= 0 ? sprites->scientist_right_infill :
sprites->scientist_left_infill);
} else {
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon(
canvas,
(int)scientists[i].point.x,
scientists[i].point.y + 5,
&I_dead_scientist);
canvas_set_color(canvas, ColorWhite);
canvas_draw_icon(
canvas,
(int)scientists[i].point.x,
scientists[i].point.y + 5,
&I_dead_scientist_infill);
}
}
}
}

View File

@@ -0,0 +1,29 @@
#ifndef SCIENTIST_H
#define SCIENTIST_H
#include "point.h"
#include "game_sprites.h"
#include <gui/gui.h>
#define SCIENTIST_VELOCITY_MIN -0.5f
#define SCIENTIST_VELOCITY_MAX 0.5f
#define SCIENTISTS_MAX 6
typedef enum {
ScientistStateAlive,
ScientistStateDead,
} ScientistState;
typedef struct {
bool visible;
POINTF point;
float velocity_x;
ScientistState state;
} SCIENTIST;
void scientist_tick(SCIENTIST* const scientist);
void spawn_random_scientist(SCIENTIST* const scientists);
void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const GameSprites* sprites);
#endif // SCIENTIST_H

View File

@@ -0,0 +1,9 @@
#ifndef STATE_H
#define STATE_H
typedef enum {
GameStateLife,
GameStateGameOver,
} State;
#endif // STATE_H

View File

@@ -0,0 +1,379 @@
#include <stdlib.h>
#include <jetpack_joyride_icons.h>
#include <furi.h>
#include <gui/gui.h>
#include <gui/icon_animation.h>
#include <input/input.h>
#include <storage/storage.h>
#include "includes/point.h"
#include "includes/barry.h"
#include "includes/scientist.h"
#include "includes/particle.h"
#include "includes/coin.h"
#include "includes/missile.h"
#include "includes/background_assets.h"
#include "includes/game_state.h"
#define TAG "Jetpack Joyride"
#define SAVING_DIRECTORY "/ext/apps/Games"
#define SAVING_FILENAME SAVING_DIRECTORY "/jetpack.save"
static GameState* global_state;
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} GameEvent;
typedef struct {
int max_distance;
int total_coins;
} SaveGame;
static SaveGame save_game;
static bool storage_game_state_load() {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
uint16_t bytes_readed = 0;
if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING))
bytes_readed = storage_file_read(file, &save_game, sizeof(SaveGame));
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return bytes_readed == sizeof(SaveGame);
}
static void storage_game_state_save() {
Storage* storage = furi_record_open(RECORD_STORAGE);
if(storage_common_stat(storage, SAVING_DIRECTORY, NULL) == FSE_NOT_EXIST) {
if(!storage_simply_mkdir(storage, SAVING_DIRECTORY)) {
return;
}
}
File* file = storage_file_alloc(storage);
if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
storage_file_write(file, &save_game, sizeof(SaveGame));
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
void handle_death() {
global_state->state = GameStateGameOver;
global_state->new_highscore = global_state->distance > save_game.max_distance;
if(global_state->distance > save_game.max_distance) {
save_game.max_distance = global_state->distance;
}
save_game.total_coins += global_state->total_coins;
storage_game_state_save();
}
static void jetpack_game_state_init(GameState* const game_state) {
UNUSED(game_state);
UNUSED(storage_game_state_save);
BARRY barry;
barry.gravity = 0;
barry.point.x = 32 + 5;
barry.point.y = 32;
barry.isBoosting = false;
GameSprites sprites;
sprites.barry = icon_animation_alloc(&A_barry);
sprites.barry_infill = &I_barry_infill;
sprites.scientist_left = (&I_scientist_left);
sprites.scientist_left_infill = (&I_scientist_left_infill);
sprites.scientist_right = (&I_scientist_right);
sprites.scientist_right_infill = (&I_scientist_right_infill);
sprites.coin = (&I_coin);
sprites.coin_infill = (&I_coin_infill);
sprites.missile = icon_animation_alloc(&A_missile);
sprites.missile_infill = &I_missile_infill;
sprites.alert = icon_animation_alloc(&A_alert);
icon_animation_start(sprites.barry);
icon_animation_start(sprites.missile);
icon_animation_start(sprites.alert);
game_state->barry = barry;
game_state->total_coins = 0;
game_state->distance = 0;
game_state->new_highscore = false;
game_state->sprites = sprites;
game_state->state = GameStateLife;
game_state->death_handler = handle_death;
memset(game_state->bg_assets, 0, sizeof(game_state->bg_assets));
memset(game_state->scientists, 0, sizeof(game_state->scientists));
memset(game_state->coins, 0, sizeof(game_state->coins));
memset(game_state->particles, 0, sizeof(game_state->particles));
memset(game_state->missiles, 0, sizeof(game_state->missiles));
}
static void jetpack_game_state_free(GameState* const game_state) {
icon_animation_free(game_state->sprites.barry);
icon_animation_free(game_state->sprites.missile);
icon_animation_free(game_state->sprites.alert);
free(game_state);
}
static void jetpack_game_tick(GameState* const game_state) {
if(game_state->state == GameStateGameOver) return;
barry_tick(&game_state->barry);
game_state_tick(game_state);
coin_tick(game_state->coins, &game_state->barry, &game_state->total_coins);
particle_tick(game_state->particles, game_state->scientists);
scientist_tick(game_state->scientists);
missile_tick(game_state->missiles, &game_state->barry, game_state->death_handler);
background_assets_tick(game_state->bg_assets);
// generate background every 64px aka. ticks
if(game_state->distance % 64 == 0 && rand() % 3 == 0) {
spawn_random_background_asset(game_state->bg_assets);
}
if(game_state->distance % 48 == 0 && rand() % 2 == 0) {
spawn_random_coin(game_state->coins);
}
if(game_state->distance % get_rocket_spawn_distance(game_state->distance) == 0 &&
rand() % 2 == 0) {
spawn_random_missile(game_state->missiles);
}
spawn_random_scientist(game_state->scientists);
if(game_state->barry.isBoosting) {
spawn_random_particles(game_state->particles, &game_state->barry);
}
}
static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
furi_assert(ctx);
const GameState* game_state = ctx;
furi_mutex_acquire(game_state->mutex, FuriWaitForever);
if(game_state->state == GameStateLife) {
canvas_set_bitmap_mode(canvas, false);
draw_background_assets(game_state->bg_assets, canvas, game_state->distance);
canvas_set_bitmap_mode(canvas, true);
draw_coins(game_state->coins, canvas, &game_state->sprites);
draw_scientists(game_state->scientists, canvas, &game_state->sprites);
draw_particles(game_state->particles, canvas);
draw_missiles(game_state->missiles, canvas, &game_state->sprites);
draw_barry(&game_state->barry, canvas, &game_state->sprites);
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontSecondary);
char buffer[12];
snprintf(buffer, sizeof(buffer), "%u m", game_state->distance / 10);
canvas_draw_str_aligned(canvas, 123, 15, AlignRight, AlignBottom, buffer);
snprintf(buffer, sizeof(buffer), "$%u", game_state->total_coins);
canvas_draw_str_aligned(canvas, 5, 15, AlignLeft, AlignBottom, buffer);
}
if(game_state->state == GameStateGameOver) {
// Show highscore
char buffer[64];
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignTop, "You flew");
snprintf(
buffer,
sizeof(buffer),
game_state->new_highscore ? "%u m (new best)" : "%u m",
game_state->distance / 10);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 16, AlignCenter, AlignTop, buffer);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, "and collected");
snprintf(buffer, sizeof(buffer), "$%u", game_state->total_coins);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignTop, buffer);
snprintf(
buffer,
sizeof(buffer),
"Best: %u m, Tot: $%u",
save_game.max_distance / 10,
save_game.total_coins);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 63, AlignCenter, AlignBottom, buffer);
canvas_draw_rframe(canvas, 0, 3, 128, 49, 5);
// char buffer[12];
// snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
// canvas_draw_str_aligned(canvas, 123, 12, AlignRight, AlignBottom, buffer);
// snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
// canvas_draw_str_aligned(canvas, 5, 12, AlignLeft, AlignBottom, buffer);
// canvas_draw_str_aligned(canvas, 64, 34, AlignCenter, AlignCenter, "Highscore:");
// snprintf(buffer, sizeof(buffer), "Dist: %u", save_game.max_distance);
// canvas_draw_str_aligned(canvas, 123, 50, AlignRight, AlignBottom, buffer);
// snprintf(buffer, sizeof(buffer), "Score: %u", save_game.max_score);
// canvas_draw_str_aligned(canvas, 5, 50, AlignLeft, AlignBottom, buffer);
// canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, "boom.");
// if(furi_timer_is_running(game_state->timer)) {
// furi_timer_start(game_state->timer, 0);
// }
}
// canvas_draw_frame(canvas, 0, 0, 128, 64);
furi_mutex_release(game_state->mutex);
}
static void jetpack_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
GameEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
static void jetpack_game_update_timer_callback(FuriMessageQueue* event_queue) {
furi_assert(event_queue);
GameEvent event = {.type = EventTypeTick};
furi_message_queue_put(event_queue, &event, 0);
}
int32_t jetpack_game_app(void* p) {
UNUSED(p);
int32_t return_code = 0;
if(!storage_game_state_load()) {
memset(&save_game, 0, sizeof(save_game));
}
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
GameState* game_state = malloc(sizeof(GameState));
global_state = game_state;
jetpack_game_state_init(game_state);
game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!game_state->mutex) {
FURI_LOG_E(TAG, "cannot create mutex\r\n");
return_code = 255;
goto free_and_exit;
}
// Set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, jetpack_game_render_callback, game_state);
view_port_input_callback_set(view_port, jetpack_game_input_callback, event_queue);
FuriTimer* timer =
furi_timer_alloc(jetpack_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
game_state->timer = timer;
// Open GUI and register view_port
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
GameEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
furi_mutex_acquire(game_state->mutex, FuriWaitForever);
if(event_status == FuriStatusOk) {
// press events
if(event.type == EventTypeKey) {
if(event.input.type == InputTypeRelease && event.input.key == InputKeyOk) {
game_state->barry.isBoosting = false;
}
// Reset highscore, for debug purposes
if(event.input.type == InputTypeLong && event.input.key == InputKeyLeft) {
save_game.max_distance = 0;
save_game.total_coins = 0;
storage_game_state_save();
}
if(event.input.type == InputTypePress) {
switch(event.input.key) {
case InputKeyUp:
break;
case InputKeyDown:
break;
case InputKeyRight:
break;
case InputKeyLeft:
break;
case InputKeyOk:
if(game_state->state == GameStateGameOver) {
jetpack_game_state_init(game_state);
}
if(game_state->state == GameStateLife) {
// Do something
game_state->barry.isBoosting = true;
}
break;
case InputKeyBack:
processing = false;
break;
default:
break;
}
}
} else if(event.type == EventTypeTick) {
jetpack_game_tick(game_state);
}
}
view_port_update(view_port);
furi_mutex_release(game_state->mutex);
}
furi_timer_free(timer);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close(RECORD_GUI);
view_port_free(view_port);
furi_mutex_free(game_state->mutex);
free_and_exit:
jetpack_game_state_free(game_state);
furi_message_queue_free(event_queue);
return return_code;
}