diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a31ef8e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 + +[*.{cpp,h,c,py,sh}] +indent_style = space +indent_size = 4 + +[{Makefile,*.mk}] +indent_size = tab diff --git a/.github/workflows/build-fap.yml b/.github/workflows/build-fap.yml new file mode 100644 index 0000000..c13956e --- /dev/null +++ b/.github/workflows/build-fap.yml @@ -0,0 +1,202 @@ +name: Build Flipper Application Package (FAP) + +on: + push: + branches: [ main ] + tags: [ 'v*' ] + pull_request: + branches: [ main ] + schedule: + # Check for new Flipper Zero firmware releases daily at 12:00 UTC + - cron: '0 12 * * *' + workflow_dispatch: + # Allow manual triggering for testing + +jobs: + check-firmware: + runs-on: ubuntu-latest + name: Check for New Flipper Zero Firmware + outputs: + new-release: ${{ steps.check.outputs.new-release }} + firmware-version: ${{ steps.check.outputs.firmware-version }} + release-tag: ${{ steps.check.outputs.release-tag }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Check for new Flipper Zero firmware release + id: check + run: | + # Get the latest Flipper Zero firmware release + LATEST_FW=$(curl -s https://api.github.com/repos/flipperdevices/flipperzero-firmware/releases/latest | jq -r '.tag_name') + echo "Latest firmware version: $LATEST_FW" + + # Check if we already have a release for this firmware version + EXISTING_RELEASE=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases | jq -r --arg fw "$LATEST_FW" '.[] | select(.tag_name | contains($fw)) | .tag_name' | head -1) + + if [ -z "$EXISTING_RELEASE" ]; then + echo "New firmware version detected: $LATEST_FW" + echo "new-release=true" >> $GITHUB_OUTPUT + echo "firmware-version=$LATEST_FW" >> $GITHUB_OUTPUT + echo "release-tag=asteroids-$LATEST_FW" >> $GITHUB_OUTPUT + else + echo "Release already exists for firmware $LATEST_FW: $EXISTING_RELEASE" + echo "new-release=false" >> $GITHUB_OUTPUT + fi + + build: + runs-on: ubuntu-latest + name: Build Asteroids FAP + needs: check-firmware + if: always() && (github.event_name != 'schedule' || needs.check-firmware.outputs.new-release == 'true') + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: true + + - name: Build Flipper Application Package + uses: flipperdevices/flipperzero-ufbt-action@v0.1.3 + id: build-fap + with: + app-dir: . + + - name: Upload FAP artifact + uses: actions/upload-artifact@v4 + with: + name: asteroids-fap-${{ needs.check-firmware.outputs.firmware-version || 'manual' }} + path: | + dist/*.fap + dist/*.fap.debug + retention-days: 30 + + - name: Create Release for Manual Tags + if: startsWith(github.ref, 'refs/tags/') && github.event_name != 'schedule' + id: manual-release + uses: softprops/action-gh-release@v1 + with: + files: | + dist/*.fap + generate_release_notes: true + draft: false + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Create Automatic Release for New Firmware + if: needs.check-firmware.outputs.new-release == 'true' && github.event_name == 'schedule' + id: auto-release + uses: softprops/action-gh-release@v1 + with: + tag_name: ${{ needs.check-firmware.outputs.release-tag }} + name: "Asteroids for Flipper Zero ${{ needs.check-firmware.outputs.firmware-version }}" + body: | + 🎮 **Asteroids Game for Flipper Zero** + + This release is automatically built and tested for compatibility with **Flipper Zero firmware ${{ needs.check-firmware.outputs.firmware-version }}**. + + ## 📦 Installation + 1. Download the `asteroids.fap` file from the assets below + 2. Copy it to your Flipper Zero's `apps/Games/` folder via qFlipper or SD card + 3. Launch the game from Apps → Games → Asteroids + + ## 🔧 Compatibility + - **Firmware Version**: ${{ needs.check-firmware.outputs.firmware-version }} + - **Build Date**: ${{ github.run_id }} + - **App Version**: Built from commit ${{ github.sha }} + + ## 🎯 Game Features + - Classic Asteroids gameplay + - Power-ups and special weapons + - High score tracking + - Optimized for Flipper Zero controls + + --- + *This release was automatically generated when Flipper Zero firmware ${{ needs.check-firmware.outputs.firmware-version }} was detected.* + files: | + dist/*.fap + draft: false + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Update README with Latest Release Info + if: steps.manual-release.conclusion == 'success' || steps.auto-release.conclusion == 'success' + run: | + # Set release information based on which type of release was created + if [ "${{ steps.auto-release.conclusion }}" == "success" ]; then + RELEASE_TAG="${{ needs.check-firmware.outputs.release-tag }}" + RELEASE_NAME="Asteroids for Flipper Zero ${{ needs.check-firmware.outputs.firmware-version }}" + FIRMWARE_VERSION="${{ needs.check-firmware.outputs.firmware-version }}" + else + RELEASE_TAG="${{ github.ref_name }}" + RELEASE_NAME="Asteroids ${{ github.ref_name }}" + FIRMWARE_VERSION="Latest" + fi + + # Get current date + CURRENT_DATE=$(date -u +"%Y-%m-%d %H:%M UTC") + + # Create the latest release section + cat > latest_release_section.md << EOF + + ## 🚀 Latest Release + + [![Latest Release](https://img.shields.io/github/v/release/SimplyMinimal/FlipperZero-Asteroids?style=for-the-badge&logo=github&color=success)](https://github.com/SimplyMinimal/FlipperZero-Asteroids/releases/latest) + [![Download FAP](https://img.shields.io/badge/Download-asteroids.fap-blue?style=for-the-badge&logo=download)](https://github.com/SimplyMinimal/FlipperZero-Asteroids/releases/latest/download/asteroids.fap) + [![Firmware Compatibility](https://img.shields.io/badge/Firmware-${FIRMWARE_VERSION}-orange?style=for-the-badge&logo=flipboard)](https://github.com/flipperdevices/flipperzero-firmware) + + **${RELEASE_NAME}** | *Last updated: ${CURRENT_DATE}* + + ### 📥 Quick Install + 1. **[Download asteroids.fap](https://github.com/SimplyMinimal/FlipperZero-Asteroids/releases/latest/download/asteroids.fap)** ⬅️ *Click here for direct download* + 2. Copy to your Flipper Zero: `/ext/apps/Games/asteroids.fap` + 3. Launch from: Apps → Games → Asteroids + + --- + + + EOF + + # Check if the latest release section already exists + if grep -q "" README.md; then + # Replace existing section + sed -i '//,//c\' README.md + sed -i '1r latest_release_section.md' README.md + else + # Add new section at the top + cat latest_release_section.md README.md > temp_readme.md + mv temp_readme.md README.md + fi + + # Clean up + rm latest_release_section.md + continue-on-error: true + + - name: Commit README Updates + if: steps.manual-release.conclusion == 'success' || steps.auto-release.conclusion == 'success' + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + + if git diff --quiet README.md; then + echo "No changes to README.md" + else + git add README.md + git commit -m "docs: Update README with latest release information + + - Add latest release badges and download links + - Update firmware compatibility information + - Add quick install instructions + - Auto-generated from workflow run ${{ github.run_id }}" + + git push origin main + fi + continue-on-error: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7225e82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode/ +dist/ +.clang-format +docs/ diff --git a/README.md b/README.md index 58cc4dd..4994fa4 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,58 @@ -This is an implementation of the classic Asteroids game for the [Flipper Zero](https://flipperzero.one/). Inside you will find a simple 2D engine that can be reused to implement other games. +This is an implementation of the classic Asteroids game for the [Flipper Zero](https://flipperzero.one/). Inside you will find a simple 2D engine that can be reused to implement other games. Note: This one is SimplyMinimal's fork of Antirez's version with several modifications. -Controls: -* Left/Right: rotate ship in the two directions. -* Ok, short press: fire bullets. -* Ok, long press: accelerate towards ship head. -* Down: decelerates. +# What's New +* Auto rapid fire (less wear on the buttons this way too) +* Up button applies thrusters +* Haptic feedback and LED effects +* Power up system +* High Score system +* Automatic save and load of high score +* Ability to Pause +* Some modifications to certain game play elements + +## What's coming next +* Settings screen +* Enabling sound effects (configurable on/off option) +* ~~Power Ups~~ Improved power up management + +--- This is a screenshot, but the game looks a lot better in the device itself: -![Asteroids for Flipper Zero screenshot](/images/Asteroids.jpg) +![Asteroids for Flipper Zero screenshot](/images/Asteroids-PowerUps.png "In game screenshot") + +![Pause Screen](images/PauseScreen.png "Pause screen") + +# Controls: +* Left/Right: rotate ship in the two directions. +* Ok, short press: Short burst bullets +* Ok, long press: Auto-fire bullets +* Up: Accelerate +* Down: Decelerate +* Back (Short Press): Pause game +* Back (Long Press): Exit game. It will automatically save the high scoore too. + +Your high scores will automatically be saved. Go forth and compete! + +--- +# Power Ups +* ![](assets/firepower_shifted_9x10.png "Ammunition") - Machine gun fire. Press and hold OK button to fire more than the default 5 bullets +* ![](assets/heart_10x10.png "Lives") - Et tu, Brute? Gives a bonus life up to a maximum of 5 lives +* ![](assets/nuke_10x10.png "Nuke") - Nuke (work in progress). Destroys everything in sight (but keeps the power ups) +* ![](assets/split_shield_10x10.png "Shield") - Use the force! Spins up a shield that can be used as a battering ram. Take zero damage while in use. +* D - A mini drone assist that has your back! -P.S. Don't miss the game over screen. +## More power ups coming soon... +--- ## Installing the binary file (no build needed) +### Option 1 (Recommended) +Download from the Flipper Zero App catalog store +https://lab.flipper.net/apps/asteroids -Drop the `asteroids.fap` file you can find in the `binaries` folder into the + +### Option 2) +Go to the releases and drop the `asteroids.fap` file into the following Flipper Zero location: /ext/apps/Games @@ -25,17 +63,19 @@ you can just take out the SD card, insert it in your computer, copy the fine into `apps/Games`, and that's it. ## Installing the app from source +Step 1) Install `ufbt` cli tool +- **Linux & macOS**: `python3 -m pip install --upgrade ufbt` +- **Windows**: `py -m pip install --upgrade ufbt` -* Download the Flipper Zero dev kit and build it: +Step 2) Clone this repo with `git` ``` -mkdir -p ~/flipperZero/official/ -cd ~/flipperZero/official/ -git clone --recursive https://github.com/flipperdevices/flipperzero-firmware.git ./ -./fbt +git clone https://github.com/SimplyMinimal/FlipperZero-Asteroids/ ``` -* Copy this application folder in `official/application_user`. + +Step 3) Launch Asteroids +* `cd FlipperZero-Asteroids` * Connect your Flipper via USB. -* Build and install with: `./fbt launch_app APPSRC=asteroids`. +* Build and install with: `ufbt launch`. ## License diff --git a/app.c b/app.c index 39639f0..16db83b 100644 --- a/app.c +++ b/app.c @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -10,83 +11,241 @@ #include #include #include +#include +#include +#include #define TAG "Asteroids" // Used for logging -#define DEBUG_MSG 1 +#define DEBUG_MSG 0 #define SCREEN_XRES 128 #define SCREEN_YRES 64 #define GAME_START_LIVES 3 +#define MAXLIVES 5 /* Max bonus lives allowed. */ +#define TTLBUL 30 /* Bullet time to live, in ticks. */ +#define MAXBUL 50 /* Max bullets on the screen. */ +//@todo MAX Asteroids +#define MAXAST 32 /* Max asteroids on the screen. */ +#define MAXPOWERUPS 3 /* Max powerups allowed on screen */ +#define POWERUPSTTL 400 /* Max powerup time to live, in ticks. */ +#define MAXDRONES 1 /* Max drone buddies allowed */ +#define DRONE_TTL 300 /* Drone time to live, in ticks. */ +#define DRONE_FIRE_RATE 20 /* Drone fire rate in ticks. */ +#define SHIP_HIT_ANIMATION_LEN 15 +#define SAVING_DIRECTORY "/ext/apps/Games" +#define SAVING_FILENAME SAVING_DIRECTORY "/game_asteroids.save" +#define SPLASH_SCREEN_DURATION 3000 /* Splash screen duration in milliseconds */ #ifndef PI #define PI 3.14159265358979f #endif /* ============================ Data structures ============================= */ +typedef enum PowerUpType { + PowerUpTypeShield, // Shield + PowerUpTypeLife, // Extra life + PowerUpTypeFirePower, // Burst Fire power + // PowerUpTypeRadialFire, // Radial Fire power + PowerUpTypeNuke, // Nuke power + PowerUpTypeDroneBuddy, // Drone assistant + // PowerUpTypeClone, // Clone ship + // PowerUpTypeAssist, // Secondary ship + Number_of_PowerUps // Used to count the number of powerups +} PowerUpType; + +// struct PowerUp +typedef struct PowerUp { + float x, y, vx, vy; /* Fields like in ship. */ + // rot, /* Fields like ship. */ + // rot_speed, /* Angular velocity (rot speed and sense). */ + float size; /* Power Up size */ + + uint32_t ttl; /* Time to live, in ticks. */ + uint32_t display_ttl; /* How long to display the powerup before it disappears */ + enum PowerUpType powerUpType; /* PowerUp type */ + bool isPowerUpActive; /* Is the powerup active? */ +} PowerUp; typedef struct Ship { - float x, /* Ship x position. */ - y, /* Ship y position. */ - vx, /* x velocity. */ - vy, /* y velocity. */ - rot; /* Current rotation. 2*PI full ortation. */ + float x, /* Ship x position. */ + y, /* Ship y position. */ + vx, /* x velocity. */ + vy, /* y velocity. */ + rot; /* Current rotation. 2*PI full ortation. */ } Ship; typedef struct Bullet { - float x, y, vx, vy; /* Fields like in ship. */ - uint32_t ttl; /* Time to live, in ticks. */ + float x, y, vx, vy; /* Fields like in ship. */ + uint32_t ttl; /* Time to live, in ticks. */ } Bullet; typedef struct Asteroid { - float x, y, vx, vy, rot, /* Fields like ship. */ - rot_speed, /* Angular velocity (rot speed and sense). */ - size; /* Asteroid size. */ - uint8_t shape_seed; /* Seed to give random shape. */ + float x, y, vx, vy, rot, /* Fields like ship. */ + rot_speed, /* Angular velocity (rot speed and sense). */ + size; /* Asteroid size. */ + uint8_t shape_seed; /* Seed to give random shape. */ } Asteroid; -#define MAXBUL 10 /* Max bullets on the screen. */ -#define MAXAST 32 /* Max asteroids on the screen. */ -#define SHIP_HIT_ANIMATION_LEN 15 +typedef struct Drone { + float x, y, vx, vy, rot; /* Fields like ship. */ + uint32_t ttl; /* Time to live, in ticks. */ + uint32_t last_fire_tick; /* Last time drone fired. */ + bool active; /* Is the drone active? */ +} Drone; + +// @todo AsteroidsApp typedef struct AsteroidsApp { /* GUI */ - Gui *gui; - ViewPort *view_port; /* We just use a raw viewport and we render + Gui* gui; + ViewPort* view_port; /* We just use a raw viewport and we render everything into the low level canvas. */ - FuriMessageQueue *event_queue; /* Keypress events go here. */ + FuriMessageQueue* event_queue; /* Keypress events go here. */ /* Game state. */ - int running; /* Once false exists the app. */ - bool gameover; /* Gameover status. */ - uint32_t ticks; /* Game ticks. Increments at each refresh. */ - uint32_t score; /* Game score. */ - uint32_t lives; /* Number of lives in the current game. */ - uint32_t ship_hit; /* When non zero, the ship was hit by an asteroid + int running; /* Once false exists the app. */ + bool gameover; /* Gameover status. */ + bool paused; /* Game paused status. */ + bool splash_screen; /* Splash screen status. */ + uint32_t splash_start_time; /* When splash screen started. */ + uint32_t ticks; /* Game ticks. Increments at each refresh. */ + uint32_t score; /* Game score. */ + uint32_t highscore; /* Highscore. Shown on Game Over Screen */ + bool is_new_highscore; /* Is the last score a new highscore? */ + uint32_t lives; /* Number of lives in the current game. */ + uint32_t ship_hit; /* When non zero, the ship was hit by an asteroid and we need to show an animation as long as its value is non-zero (and decrease it's value at each tick of animation). */ /* Ship state. */ struct Ship ship; + struct PowerUp powerUps[MAXPOWERUPS]; /* Each powerup state. */ + int powerUps_num; /* Active powerups. */ /* Bullets state. */ - struct Bullet bullets[MAXBUL]; /* Each bullet state. */ - int bullets_num; /* Active bullets. */ - uint32_t last_bullet_tick; /* Tick the last bullet was fired. */ + struct Bullet bullets[MAXBUL]; /* Each bullet state. */ + int bullets_num; /* Active bullets. */ + uint32_t last_bullet_tick; /* Tick the last bullet was fired. */ + uint32_t bullet_min_period; /* Minimum time between bullets in ms. */ /* Asteroids state. */ - Asteroid asteroids[MAXAST]; /* Each asteroid state. */ - int asteroids_num; /* Active asteroids. */ + Asteroid asteroids[MAXAST]; /* Each asteroid state. */ + int asteroids_num; /* Active asteroids. */ + + /* Drone state. */ + Drone drones[MAXDRONES]; /* Each drone state. */ + int drones_num; /* Active drones. */ uint32_t pressed[InputKeyMAX]; /* pressed[id] is true if pressed. Each array item contains the time in milliseconds the key was pressed. */ - bool fire; /* Short press detected: fire a bullet. */ + bool fire; /* Short press detected: fire a bullet. */ } AsteroidsApp; +const NotificationSequence sequence_thrusters = { + &message_vibro_on, + &message_delay_10, + &message_vibro_off, + NULL, +}; + +const NotificationSequence sequence_brake = { + &message_vibro_on, + &message_delay_10, + &message_delay_1, + &message_delay_1, + &message_vibro_off, + NULL, +}; + +const NotificationSequence sequence_crash = { + &message_red_255, + + &message_vibro_on, + // &message_note_g5, // Play sound but currently disabled + &message_delay_25, + // &message_note_e5, + &message_vibro_off, + &message_sound_off, + NULL, +}; + +const NotificationSequence sequence_bullet_fired = { + &message_vibro_on, + // &message_note_g5, // Play sound but currently disabled. Need On/Off menu setting + &message_delay_10, + &message_delay_1, + &message_delay_1, + &message_delay_1, + &message_delay_1, + &message_delay_1, + + // &message_note_e5, + &message_vibro_off, + &message_sound_off, + NULL, +}; + +const NotificationSequence sequence_powerup_collected = { + &message_vibro_on, + &message_delay_1, + &message_delay_1, + &message_delay_1, + &message_delay_1, + &message_delay_1, + &message_vibro_off, + NULL, +}; + +const NotificationSequence sequence_nuke = { + &message_blink_set_color_red, + &message_blink_start_100, + + &message_vibro_on, + &message_delay_10, + &message_vibro_off, + + &message_vibro_on, + &message_delay_10, + &message_vibro_off, + + &message_vibro_on, + &message_delay_10, + &message_vibro_off, + &message_red_0, + + &message_vibro_on, + &message_delay_10, + &message_delay_1, + &message_delay_1, + &message_vibro_off, + + &message_vibro_on, + &message_delay_10, + &message_delay_1, + &message_delay_1, + &message_vibro_off, + + &message_vibro_on, + &message_delay_10, + &message_delay_1, + &message_delay_1, + &message_vibro_off, + + &message_blink_stop, + &message_vibro_off, + &message_sound_off, + NULL, +}; + /* ============================== Prototyeps ================================ */ // Only functions called before their definition are here. - -void restart_game_after_gameover(AsteroidsApp *app); -uint32_t key_pressed_time(AsteroidsApp *app, InputKey key); +bool isPowerUpActive(AsteroidsApp* app, enum PowerUpType powerUpType); +bool isPowerUpAlreadyExists(AsteroidsApp* app, enum PowerUpType powerUpType); +bool load_game(AsteroidsApp* app); +void save_game(AsteroidsApp* app); +void restart_game_after_gameover(AsteroidsApp* app); +uint32_t key_pressed_time(AsteroidsApp* app, InputKey key); +void spawn_drone_buddy(AsteroidsApp* app); /* ============================ 2D drawing ================================== */ @@ -101,23 +260,24 @@ typedef struct Poly { } Poly; /* Define the polygons we use. */ -Poly ShipPoly = { - {-3, 0, 3}, - {-3, 6, -3}, - 3 -}; +Poly ShipPoly = {{-3, 0, 3}, {-3, 6, -3}, 3}; + +Poly ShipFirePoly = {{-1.5, 0, 1.5}, {-3, -6, -3}, 3}; + +/* Smaller drone polygon - triangular shape */ +Poly DronePoly = {{-2, 0, 2}, {-2, 4, -2}, 3}; /* Rotate the point of the poligon 'poly' and store the new rotated * polygon in 'rot'. The polygon is rotated by an angle 'a', with * center at 0,0. */ -void rotate_poly(Poly *rot, Poly *poly, float a) { +void rotate_poly(Poly* rot, Poly* poly, float a) { /* We want to compute sin(a) and cos(a) only one time * for every point to rotate. It's a slow operation. */ float sin_a = (float)sin(a); float cos_a = (float)cos(a); - for (uint32_t j = 0; j < poly->points; j++) { - rot->x[j] = poly->x[j]*cos_a - poly->y[j]*sin_a; - rot->y[j] = poly->y[j]*cos_a + poly->x[j]*sin_a; + for(uint32_t j = 0; j < poly->points; j++) { + rot->x[j] = poly->x[j] * cos_a - poly->y[j] * sin_a; + rot->y[j] = poly->y[j] * cos_a + poly->x[j] * sin_a; } rot->points = poly->points; } @@ -125,36 +285,68 @@ void rotate_poly(Poly *rot, Poly *poly, float a) { /* This is an 8 bit LFSR we use to generate a predictable and fast * pseudorandom sequence of numbers, to give a different shape to * each asteroid. */ -void lfsr_next(unsigned char *prev) { +void lfsr_next(unsigned char* prev) { unsigned char lsb = *prev & 1; *prev = *prev >> 1; - if (lsb == 1) *prev ^= 0b11000111; - *prev ^= *prev<<7; /* Mix things a bit more. */ + if(lsb == 1) *prev ^= 0b11000111; + *prev ^= *prev << 7; /* Mix things a bit more. */ +} + +/* ================================ Render ================================ */ + +/* Safely clamp coordinate to valid canvas bounds */ +static inline int clamp_coordinate(float coord, int min_val, int max_val) { + if(coord < min_val) return min_val; + if(coord > max_val) return max_val; + return (int)coord; } /* Render the polygon 'poly' at x,y, rotated by the specified angle. */ -void draw_poly(Canvas *const canvas, Poly *poly, uint8_t x, uint8_t y, float a) -{ +void draw_poly(Canvas* const canvas, Poly* poly, int x, int y, float a) { Poly rot; - rotate_poly(&rot,poly,a); + rotate_poly(&rot, poly, a); canvas_set_color(canvas, ColorBlack); - for (uint32_t j = 0; j < rot.points; j++) { + for(uint32_t j = 0; j < rot.points; j++) { uint32_t a = j; - uint32_t b = j+1; - if (b == rot.points) b = 0; - canvas_draw_line(canvas,x+rot.x[a],y+rot.y[a], - x+rot.x[b],y+rot.y[b]); + uint32_t b = j + 1; + if(b == rot.points) b = 0; + + /* Calculate final coordinates with bounds checking */ + int x1 = clamp_coordinate(x + rot.x[a], 0, SCREEN_XRES - 1); + int y1 = clamp_coordinate(y + rot.y[a], 0, SCREEN_YRES - 1); + int x2 = clamp_coordinate(x + rot.x[b], 0, SCREEN_XRES - 1); + int y2 = clamp_coordinate(y + rot.y[b], 0, SCREEN_YRES - 1); + + /* Only draw if coordinates are reasonable (prevent extreme off-screen rendering) */ + if(abs(x - SCREEN_XRES/2) < SCREEN_XRES * 2 && abs(y - SCREEN_YRES/2) < SCREEN_YRES * 2) { + canvas_draw_line(canvas, x1, y1, x2, y2); + } } } +/* Validate that coordinates are safe for drawing */ +static inline bool is_coordinate_safe(float x, float y) { + return (x >= -10 && x <= SCREEN_XRES + 10 && y >= -10 && y <= SCREEN_YRES + 10); +} + /* A bullet is just a + pixels pattern. A single pixel is not * visible enough. */ -void draw_bullet(Canvas *const canvas, Bullet *b) { - canvas_draw_dot(canvas,b->x-1,b->y); - canvas_draw_dot(canvas,b->x+1,b->y); - canvas_draw_dot(canvas,b->x,b->y); - canvas_draw_dot(canvas,b->x,b->y-1); - canvas_draw_dot(canvas,b->x,b->y+1); +void draw_bullet(Canvas* const canvas, Bullet* b) { + /* Safety check to prevent drawing at invalid coordinates */ + if(!is_coordinate_safe(b->x, b->y)) { + FURI_LOG_W(TAG, "Unsafe bullet coordinates: x=%.2f, y=%.2f", (double)b->x, (double)b->y); + return; + } + + int x = clamp_coordinate(b->x, 0, SCREEN_XRES - 1); + int y = clamp_coordinate(b->y, 0, SCREEN_YRES - 1); + + /* Draw bullet with bounds checking for each dot */ + if(x > 0) canvas_draw_dot(canvas, x - 1, y); + if(x < SCREEN_XRES - 1) canvas_draw_dot(canvas, x + 1, y); + canvas_draw_dot(canvas, x, y); + if(y > 0) canvas_draw_dot(canvas, x, y - 1); + if(y < SCREEN_YRES - 1) canvas_draw_dot(canvas, x, y + 1); } /* Draw an asteroid. The asteroid shapes is computed on the fly and @@ -162,15 +354,27 @@ void draw_bullet(Canvas *const canvas, Bullet *b) { * the shape, we use an initial fixed shape that we resize according * to the asteroid size, perturbate according to the asteroid shape * seed, and finally draw it rotated of the right amount. */ -void draw_asteroid(Canvas *const canvas, Asteroid *ast) { +void draw_asteroid(Canvas* const canvas, Asteroid* ast) { + /* Safety check for asteroid coordinates */ + if(!is_coordinate_safe(ast->x, ast->y)) { + FURI_LOG_W(TAG, "Unsafe asteroid coordinates: x=%.2f, y=%.2f", (double)ast->x, (double)ast->y); + return; + } + + /* Validate asteroid size to prevent extreme shapes */ + if(ast->size < 1 || ast->size > 50) { + FURI_LOG_W(TAG, "Invalid asteroid size: %.2f", (double)ast->size); + return; + } + Poly ap; /* Start with what is kinda of a circle. Note that this could be * stored into a template and copied here, to avoid computing * sin() / cos(). But the Flipper can handle it without problems. */ uint8_t r = ast->shape_seed; - for (int j = 0; j < 8; j++) { - float a = (PI*2)/8*j; + for(int j = 0; j < 8; j++) { + float a = (PI * 2) / 8 * j; /* Before generating the point, to make the shape unique generate * a random factor between .7 and 1.3 to scale the distance from @@ -178,75 +382,307 @@ void draw_asteroid(Canvas *const canvas, Asteroid *ast) { * that remains always the same, so we use a predictable PRNG * implemented by an 8 bit shift register. */ lfsr_next(&r); - float scaling = .7+((float)r/255*.6); + float scaling = .7 + ((float)r / 255 * .6); ap.x[j] = (float)sin(a) * ast->size * scaling; ap.y[j] = (float)cos(a) * ast->size * scaling; } ap.points = 8; - draw_poly(canvas,&ap,ast->x,ast->y,ast->rot); + draw_poly(canvas, &ap, ast->x, ast->y, ast->rot); } /* Draw small ships in the top-right part of the screen, one for * each left live. */ -void draw_left_lives(Canvas *const canvas, AsteroidsApp *app) { +void draw_left_lives(Canvas* const canvas, AsteroidsApp* app) { int lives = app->lives; - int x = SCREEN_XRES-5; + int x = SCREEN_XRES - 5; - Poly mini_ship = { - {-2, 0, 2}, - {-2, 4, -2}, - 3 - }; + Poly mini_ship = {{-2, 0, 2}, {-2, 4, -2}, 3}; while(lives--) { - draw_poly(canvas,&mini_ship,x,6,PI); + draw_poly(canvas, &mini_ship, x, 6, PI); x -= 6; } } -/* Given the current position, update it according to the velocity and - * wrap it back to the other side if the object went over the screen. */ -void update_pos_by_velocity(float *x, float *y, float vx, float vy) { - /* Return back from one side to the other of the screen. */ - *x += vx; - *y += vy; - if (*x >= SCREEN_XRES) *x = 0; - else if (*x < 0) *x = SCREEN_XRES-1; - if (*y >= SCREEN_YRES) *y = 0; - else if (*y < 0) *y = SCREEN_YRES-1; +bool should_draw_powerUp(PowerUp* const p) { + // Just return if power up has already been picked up + if(p->display_ttl == 0) return false; + + // Begin flashing power up when it is about to expire + if(p->display_ttl < 100) { + return p->display_ttl % 8 > 0; + } + + return true; +} + +void draw_powerUp_RemainingLife(Canvas* canvas, PowerUp* const p, int y_offset) { + if(!p->isPowerUpActive) return; + + /* + Here we generate a reverse progress bar. The bar is 24 pixels wide and 1 pixel tall. + Calculate the percentage of hitpoints left: hitpoints / total hitpoints + Multiply the percentage by the width of the bar (in pixels): percentage * bar width + Subtract the result from the width of the bar to get the filled portion of the bar: bar width - (percentage * bar width) + Round the result to the nearest integer to get the final result. + + 400 / 400 = 1.0 + 1.0 * 24 = 24 + 24 - 24 = 0 + Round(0) = 0 + */ + int progress_bar_width = SCREEN_XRES / 4; + + if(p->ttl > 0) { + canvas_set_color(canvas, ColorBlack); + int remaining = ceil(((float)p->ttl / (float)POWERUPSTTL) * (float)progress_bar_width); + + if(remaining > 0) { + canvas_draw_line( + canvas, + (SCREEN_XRES / 2) - remaining, // x1 + 3 + y_offset, //y1 + (SCREEN_XRES / 2) + remaining, //x2 + 3 + y_offset); // y2 + } + } +} + +void draw_powerUps(Canvas* const canvas, PowerUp* const p) { + /* + + * * * * * * * * * * + * * + * * + * * + * F * + * * + * * + * * + * * + * * * * * * * * * * + + BOX_SIZE = 10 + Box_Width = BOX_SIZE + BOX_HEIGHT = BOX_SIZE + BOX_X_POS = x - BOX_WIDTH/2 + BOX_Y_POS = y - BOX_HEIGHT/2 + POS_F_X = WIDTH/2 + POS_F_Y = HEIGHT/2 + + */ + + //@todo render_callback + + // Just return if power up has already been picked up + // FURI_LOG_I(TAG, "[draw_powerUps] Display TTL: %lu", p->display_ttl); + if(p->display_ttl == 0) return; + if(!should_draw_powerUp(p)) return; + + canvas_set_color(canvas, ColorXOR); + + // Display power up to be picked up + switch(p->powerUpType) { + case PowerUpTypeFirePower: + canvas_draw_icon(canvas, p->x, p->y, &I_firepower_shifted_9x10); + break; + case PowerUpTypeShield: + canvas_draw_icon(canvas, p->x, p->y, &I_shield_frame); + break; + case PowerUpTypeLife: + // Draw a heart + canvas_draw_icon(canvas, p->x, p->y, &I_heart_10x10); + break; + case PowerUpTypeNuke: + // canvas_draw_disc(canvas, p->x, p->y, p->size); + // canvas_draw_str(canvas, p->x, p->y, "N"); + canvas_draw_icon(canvas, p->x, p->y, &I_nuke_10x10); + break; + case PowerUpTypeDroneBuddy: + // Draw box with letter D inside for drone buddy + canvas_draw_str(canvas, p->x, p->y, "D"); + break; + // case PowerUpTypeRadialFire: + // // Draw box with letter R inside + // canvas_draw_str(canvas, p->x, p->y, "R"); + // break; + // case PowerUpTypeAssist: + // // Draw box with letter A inside + // canvas_draw_str(canvas, p->x, p->y, "A"); + // break; + // case PowerUpTypeClone: + // // Draw box with letter C inside + // canvas_draw_str(canvas, p->x, p->y, "C"); + // break; + default: + //@todo Uknown Power Up Type Detected + // Draw box with letter U inside + canvas_draw_str(canvas, p->x, p->y, "?"); + FURI_LOG_E(TAG, "Unexpected Power Up Type Detected: %i", p->powerUpType); + break; + } +} + +void draw_shield(Canvas* const canvas, AsteroidsApp* app) { + if(isPowerUpActive(app, PowerUpTypeShield) == false) return; + + /* Safety check for shield coordinates */ + if(!is_coordinate_safe(app->ship.x, app->ship.y)) { + return; /* Skip drawing shield if ship coordinates are invalid */ + } + + canvas_set_color(canvas, ColorXOR); + int x = clamp_coordinate(app->ship.x, 8, SCREEN_XRES - 8); + int y = clamp_coordinate(app->ship.y, 8, SCREEN_YRES - 8); + // canvas_draw_disc(canvas, x, y, 4); + canvas_draw_circle(canvas, x, y, 8); +} + +/* Draw a drone buddy */ +void draw_drone(Canvas* const canvas, Drone* drone) { + /* Safety check for drone coordinates */ + if(!is_coordinate_safe(drone->x, drone->y)) { + FURI_LOG_W(TAG, "Unsafe drone coordinates: x=%.2f, y=%.2f", (double)drone->x, (double)drone->y); + return; + } + + /* Draw drone with a different color/style to distinguish from main ship */ + canvas_set_color(canvas, ColorBlack); + draw_poly(canvas, &DronePoly, drone->x, drone->y, drone->rot); + + /* Draw a small dot in the center to make it more visible */ + int x = clamp_coordinate(drone->x, 0, SCREEN_XRES - 1); + int y = clamp_coordinate(drone->y, 0, SCREEN_YRES - 1); + canvas_draw_dot(canvas, x, y); +} + +/* Render the splash screen with credits */ +void render_splash_screen(Canvas* const canvas, AsteroidsApp* app) { + UNUSED(app); + + /* Clear screen */ + canvas_set_color(canvas, ColorWhite); + canvas_draw_box(canvas, 0, 0, SCREEN_XRES - 1, SCREEN_YRES - 1); + + /* Set black color for text */ + canvas_set_color(canvas, ColorBlack); + + /* Draw main title */ + canvas_set_font(canvas, FontPrimary); + canvas_draw_str_aligned(canvas, SCREEN_XRES / 2, 12, AlignCenter, AlignCenter, "ASTEROIDS++"); + + /* Draw credits section */ + canvas_set_font(canvas, FontSecondary); + canvas_draw_str_aligned(canvas, SCREEN_XRES / 2, 26, AlignCenter, AlignCenter, "Designed and Developed by"); + + /* Draw developer credits */ + canvas_set_font(canvas, FontSecondary); + canvas_draw_str_aligned(canvas, SCREEN_XRES / 2, 38, AlignCenter, AlignCenter, "SimplyMinimal"); + canvas_draw_str_aligned(canvas, SCREEN_XRES / 2, 48, AlignCenter, AlignCenter, "& AntiRez"); + + /* Draw skip instruction */ + canvas_set_font(canvas, FontSecondary); + canvas_draw_str_aligned(canvas, SCREEN_XRES / 2, 60, AlignCenter, AlignCenter, "Press any key to skip"); } /* Render the current game screen. */ -void render_callback(Canvas *const canvas, void *ctx) { - AsteroidsApp *app = ctx; +void render_callback(Canvas* const canvas, void* ctx) { + AsteroidsApp* app = ctx; + + /* Handle splash screen rendering */ + if(app->splash_screen) { + render_splash_screen(canvas, app); + return; + } /* Clear screen. */ canvas_set_color(canvas, ColorWhite); - canvas_draw_box(canvas, 0, 0, SCREEN_XRES-1, SCREEN_YRES-1); + canvas_draw_box(canvas, 0, 0, SCREEN_XRES - 1, SCREEN_YRES - 1); /* Draw score. */ canvas_set_color(canvas, ColorBlack); canvas_set_font(canvas, FontSecondary); char score[32]; - snprintf(score,sizeof(score),"%lu",app->score); + snprintf(score, sizeof(score), "%lu", app->score); canvas_draw_str(canvas, 0, 8, score); /* Draw left ships. */ - draw_left_lives(canvas,app); + draw_left_lives(canvas, app); /* Draw ship, asteroids, bullets. */ - draw_poly(canvas,&ShipPoly,app->ship.x,app->ship.y,app->ship.rot); + /* Safety check for ship coordinates before rendering */ + if(!is_coordinate_safe(app->ship.x, app->ship.y)) { + FURI_LOG_E(TAG, "Ship coordinate corruption detected: x=%.2f, y=%.2f", (double)app->ship.x, (double)app->ship.y); + /* Emergency reset ship to center */ + app->ship.x = SCREEN_XRES / 2; + app->ship.y = SCREEN_YRES / 2; + app->ship.vx = 0; + app->ship.vy = 0; + } + + draw_poly(canvas, &ShipPoly, app->ship.x, app->ship.y, app->ship.rot); + + /* Draw shield if active. */ + draw_shield(canvas, app); + + if(key_pressed_time(app, InputKeyUp) > 0) { + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_thrusters); + draw_poly(canvas, &ShipFirePoly, app->ship.x, app->ship.y, app->ship.rot); + } + + for(int j = 0; j < app->bullets_num; j++) draw_bullet(canvas, &app->bullets[j]); + + for(int j = 0; j < app->asteroids_num; j++) draw_asteroid(canvas, &app->asteroids[j]); + + /* Draw active drones */ + for(int j = 0; j < app->drones_num; j++) { + if(app->drones[j].active) { + draw_drone(canvas, &app->drones[j]); + } + } - for (int j = 0; j < app->bullets_num; j++) - draw_bullet(canvas,&app->bullets[j]); + for(int j = 0; j < app->powerUps_num; j++) { + draw_powerUps(canvas, &app->powerUps[j]); + draw_powerUp_RemainingLife(canvas, &app->powerUps[j], j); + } - for (int j = 0; j < app->asteroids_num; j++) - draw_asteroid(canvas,&app->asteroids[j]); + if(app->paused) { + canvas_set_color(canvas, ColorXOR); + canvas_set_font(canvas, FontPrimary); + canvas_draw_rbox(canvas, 0, 0, SCREEN_XRES, SCREEN_YRES, 4); + canvas_draw_str_aligned( + canvas, SCREEN_XRES / 2, SCREEN_YRES / 2, AlignCenter, AlignCenter, "Paused"); + return; + } /* Game over text. */ - if (app->gameover) { + if(app->gameover) { canvas_set_color(canvas, ColorBlack); canvas_set_font(canvas, FontPrimary); + + // TODO: if new highscore, display blinking "New High Score" + // Display High Score + if(app->is_new_highscore) { + canvas_draw_str(canvas, 22, 9, "New High Score!"); + } else { + canvas_draw_str(canvas, 36, 9, "High Score"); + } + + // Convert highscore to string + int length = snprintf(NULL, 0, "%lu", app->highscore); + char* str_high_score = malloc(length + 1); + snprintf(str_high_score, length + 1, "%lu", app->highscore); + + // Get length to center on screen + int nDigits = 0; + if(app->highscore > 0) { + nDigits = floor(log10(app->highscore)) + 1; + } + + // Draw highscore centered + canvas_draw_str(canvas, (SCREEN_XRES / 2) - (nDigits * 2), 20, str_high_score); + free(str_high_score); + canvas_draw_str(canvas, 28, 35, "GAME OVER"); canvas_set_font(canvas, FontSecondary); canvas_draw_str(canvas, 25, 50, "Press OK to restart"); @@ -255,10 +691,49 @@ void render_callback(Canvas *const canvas, void *ctx) { /* ============================ Game logic ================================== */ +/* Safely wrap coordinate to screen bounds with proper modulo arithmetic */ +static inline float safe_wrap_coordinate(float coord, float max_val) { + /* Handle extreme negative values */ + while(coord < 0) { + coord += max_val; + } + /* Handle extreme positive values */ + while(coord >= max_val) { + coord -= max_val; + } + return coord; +} + +/* Given the current position, update it according to the velocity and + * wrap it back to the other side if the object went over the screen. */ +void update_pos_by_velocity(float* x, float* y, float vx, float vy) { + /* Update position */ + *x += vx; + *y += vy; + + /* Clamp velocity to prevent extreme jumps that could cause coordinate corruption */ + const float MAX_VELOCITY = 10.0f; + if(vx > MAX_VELOCITY || vx < -MAX_VELOCITY || vy > MAX_VELOCITY || vy < -MAX_VELOCITY) { + FURI_LOG_W(TAG, "Extreme velocity detected: vx=%.2f, vy=%.2f", (double)vx, (double)vy); + } + + /* Safely wrap coordinates using proper modulo arithmetic */ + *x = safe_wrap_coordinate(*x, SCREEN_XRES); + *y = safe_wrap_coordinate(*y, SCREEN_YRES); + + /* Additional safety check - ensure coordinates are within reasonable bounds */ + if(*x < 0 || *x >= SCREEN_XRES || *y < 0 || *y >= SCREEN_YRES) { + FURI_LOG_E(TAG, "Coordinate corruption detected: x=%.2f, y=%.2f", (double)*x, (double)*y); + /* Emergency reset to center of screen */ + *x = SCREEN_XRES / 2; + *y = SCREEN_YRES / 2; + } +} + float distance(float x1, float y1, float x2, float y2) { - float dx = x1-x2; - float dy = y1-y2; - return sqrt(dx*dx+dy*dy); + float dx = x1 - x2; + float dy = y1 - y2; + return sqrt(dx * dx + dy * dy); } /* Detect a collision between the object at x1,y1 of radius r1 and @@ -272,10 +747,7 @@ float distance(float x1, float y1, float x2, float y2) { * spheres (this is why this function only takes the radius). This * is, after all, kinda accurate for asteroids, for bullets, and * even for the ship "core" itself. */ -bool objects_are_colliding(float x1, float y1, float r1, - float x2, float y2, float r2, - float factor) -{ +bool objects_are_colliding(float x1, float y1, float r1, float x2, float y2, float r2, float factor) { /* The objects are colliding if the distance between object 1 and 2 * is smaller than the sum of the two radiuses r1 and r2. * So it would be like: sqrt((x1-x2)^2+(y1-y2)^2) < r1+r2. @@ -284,24 +756,32 @@ bool objects_are_colliding(float x1, float y1, float r1, * the comparison like this: * * (x1-x2)^2+(y1-y2)^2 < (r1+r2)^2. */ - float dx = (x1-x2)*factor; - float dy = (y1-y2)*factor; - float rsum = r1+r2; - return dx*dx+dy*dy < rsum*rsum; + float dx = (x1 - x2) * factor; + float dy = (y1 - y2) * factor; + float rsum = r1 + r2; + return dx * dx + dy * dy < rsum * rsum; } +/* ================================ Bullets ================================ */ +//@todo ship_fire_bullet /* Create a new bullet headed in the same direction of the ship. */ -void ship_fire_bullet(AsteroidsApp *app) { - if (app->bullets_num == MAXBUL) return; - Bullet *b = &app->bullets[app->bullets_num]; +void ship_fire_bullet(AsteroidsApp* app) { + // No power ups, only 5 bullets allowed + if(isPowerUpActive(app, PowerUpTypeFirePower) == false && app->bullets_num >= 5) return; + + // Double the Fire Power + if(isPowerUpActive(app, PowerUpTypeFirePower) && (app->bullets_num >= (MAXBUL))) return; + + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_bullet_fired); + Bullet* b = &app->bullets[app->bullets_num]; b->x = app->ship.x; b->y = app->ship.y; b->vx = -sin(app->ship.rot); b->vy = cos(app->ship.rot); /* Ship should fire from its head, not in the middle. */ - b->x += b->vx*5; - b->y += b->vy*5; + b->x += b->vx * 5; + b->y += b->vy * 5; /* Give the bullet some velocity (for now the vector is just * normalized to 1). */ @@ -314,94 +794,399 @@ void ship_fire_bullet(AsteroidsApp *app) { b->vx += app->ship.vx; b->vy += app->ship.vy; - b->ttl = 50; /* The bullet will disappear after N ticks. */ + b->ttl = TTLBUL; /* The bullet will disappear after N ticks. */ app->bullets_num++; } /* Remove the specified bullet by id (index in the array). */ -void remove_bullet(AsteroidsApp *app, int bid) { +void remove_bullet(AsteroidsApp* app, int bid) { /* Replace the top bullet with the empty space left * by the removal of this bullet. This way we always take the * array dense, which is an advantage when looping. */ int n = --app->bullets_num; - if (n && bid != n) app->bullets[bid] = app->bullets[n]; + if(n && bid != n) app->bullets[bid] = app->bullets[n]; } +/* ================================ Asteroids ================================ */ /* Create a new asteroid, away from the ship. Return the * pointer to the asteroid object, so that the caller can change * certain things of the asteroid if needed. */ -Asteroid *add_asteroid(AsteroidsApp *app) { - if (app->asteroids_num == MAXAST) return NULL; - float size = 4+rand()%15; +Asteroid* add_asteroid(AsteroidsApp* app) { + if(app->asteroids_num == MAXAST) return NULL; + float size = 4 + rand() % 15; float min_distance = 20; - float x,y; + float x, y; do { x = rand() % SCREEN_XRES; y = rand() % SCREEN_YRES; - } while(distance(app->ship.x,app->ship.y,x,y) < min_distance+size); - Asteroid *a = &app->asteroids[app->asteroids_num++]; + } while(distance(app->ship.x, app->ship.y, x, y) < min_distance + size); + Asteroid* a = &app->asteroids[app->asteroids_num++]; a->x = x; a->y = y; - a->vx = 2*(-.5 + ((float)rand()/RAND_MAX)); - a->vy = 2*(-.5 + ((float)rand()/RAND_MAX)); + a->vx = 2 * (-.5 + ((float)rand() / RAND_MAX)); + a->vy = 2 * (-.5 + ((float)rand() / RAND_MAX)); a->size = size; a->rot = 0; - a->rot_speed = ((float)rand()/RAND_MAX)/10; - if (app->ticks & 1) a->rot_speed = -(a->rot_speed); + a->rot_speed = ((float)rand() / RAND_MAX) / 10; + if(app->ticks & 1) a->rot_speed = -(a->rot_speed); a->shape_seed = rand() & 255; return a; } /* Remove the specified asteroid by id (index in the array). */ -void remove_asteroid(AsteroidsApp *app, int id) { +void remove_asteroid(AsteroidsApp* app, int id) { /* Replace the top asteroid with the empty space left * by the removal of this one. This way we always take the * array dense, which is an advantage when looping. */ int n = --app->asteroids_num; - if (n && id != n) app->asteroids[id] = app->asteroids[n]; + if(n && id != n) app->asteroids[id] = app->asteroids[n]; } /* Called when an asteroid was reached by a bullet. The asteroid * hit is the one with the specified 'id'. */ -void asteroid_was_hit(AsteroidsApp *app, int id) { +void asteroid_was_hit(AsteroidsApp* app, int id) { float sizelimit = 6; // Smaller than that polverize in one shot. - Asteroid *a = &app->asteroids[id]; + Asteroid* a = &app->asteroids[id]; /* Asteroid is large enough to break into fragments. */ float size = a->size; float x = a->x, y = a->y; - remove_asteroid(app,id); - if (size > sizelimit) { + remove_asteroid(app, id); + if(size > sizelimit) { int max_fragments = size / sizelimit; - int fragments = 2+rand()%max_fragments; - float newsize = size/fragments; - if (newsize < 2) newsize = 2; - for (int j = 0; j < fragments; j++) { + int fragments = 2 + rand() % max_fragments; + float newsize = size / fragments; + if(newsize < 2) newsize = 2; + for(int j = 0; j < fragments; j++) { a = add_asteroid(app); - if (a == NULL) break; // Too many asteroids on screen. - a->x = x + -(size/2) + rand() % (int)newsize; - a->y = y + -(size/2) + rand() % (int)newsize; + if(a == NULL) break; // Too many asteroids on screen. + a->x = x + -(size / 2) + rand() % (int)newsize; + a->y = y + -(size / 2) + rand() % (int)newsize; a->size = newsize; } } else { app->score++; + if(app->score > app->highscore) { + app->is_new_highscore = true; + app->highscore = app->score; // Show on Game Over Screen and future main menu + } + } +} + +/* ================================ Power Up ================================ */ +bool isPowerUpCollidingWithEachOther(AsteroidsApp* app, float x, float y, float size) { + for(int i = 0; i < app->powerUps_num; i++) { + PowerUp* p2 = &app->powerUps[i]; + if(objects_are_colliding(x, y, size, p2->x, p2->y, p2->size, 1)) return true; + } + return false; +} + +bool should_trigger_rare_powerUp(PowerUpType selected_powerUpType) { + switch(selected_powerUpType) { + case PowerUpTypeLife: // Make extra life power up more rare + return rand() % 10 != 0; + case PowerUpTypeShield: // Make shield power up more rare + return rand() % 4 != 0; + default: + return true; + } +} + +//@todo Add PowerUp +PowerUp* add_powerUp(AsteroidsApp* app) { + FURI_LOG_I(TAG, "add_powerUp: %i", app->powerUps_num); + if(app->powerUps_num == MAXPOWERUPS) return NULL; // Max Power Ups reached + if(app->lives == MAXLIVES) return NULL; // Max Lives reached + + // Randomly select power up for display + PowerUpType selected_powerUpType = rand() % Number_of_PowerUps; + FURI_LOG_I(TAG, "[add_powerUp] Power Up Selected: %i", selected_powerUpType); + + // Don't add already existing power ups + if(isPowerUpAlreadyExists(app, selected_powerUpType)) { + FURI_LOG_D(TAG, "[add_powerUp] Power Up %i already active", selected_powerUpType); + return NULL; + } + + // Make some power ups more rare + if(!should_trigger_rare_powerUp(selected_powerUpType)) { + FURI_LOG_D(TAG, "[add_powerUp] Power Up %i not triggered", selected_powerUpType); + return NULL; + } + + float size = 10; + float min_distance = 20; + float x, y; + do { + //Make sure power up is not spawned on the edge of the screen + x = rand() % (SCREEN_XRES - (int)size); + y = rand() % (SCREEN_YRES - (int)size); + + //Also keep it away from the lives and score at the top of screen + if(y < size) y = size; + } while( + ((distance(app->ship.x, app->ship.y, x, y) < min_distance + size) || + isPowerUpCollidingWithEachOther(app, x, y, size))); + + PowerUp* p = &app->powerUps[app->powerUps_num++]; + p->x = x; + p->y = y; + //@todo Disable Velocity + p->vx = 0; //2 * (-.5 + ((float)rand() / RAND_MAX)); + p->vy = 0; //2 * (-.5 + ((float)rand() / RAND_MAX)); + p->display_ttl = 200; + p->ttl = POWERUPSTTL; + p->size = size; + // p->size = size; + // p->rot = 0; + // p->rot_speed = ((float)rand() / RAND_MAX) / 10; + // if(app->ticks & 1) p->rot_speed = -(p->rot_speed); + + //@todo add powerup type, for now hardcoding to firepower + p->powerUpType = selected_powerUpType; + p->isPowerUpActive = false; + FURI_LOG_I(TAG, "[add_powerUp] Power Up Added: %i", p->powerUpType); + return p; +} + +bool isPowerUpActive(AsteroidsApp* const app, PowerUpType const powerUpType) { + for(int i = 0; i < app->powerUps_num; i++) { + // PowerUp* p = &app->powerUps[i]; + // if(p->powerUpType == powerUpType && p->ttl > 0 && p->display_ttl == 0) return true; + if(app->powerUps[i].isPowerUpActive && app->powerUps[i].powerUpType == powerUpType) { + return true; + } + } + return false; +} + +bool isPowerUpAlreadyExists(AsteroidsApp* const app, PowerUpType const powerUpType) { + for(int i = 0; i < app->powerUps_num; i++) { + if(app->powerUps[i].powerUpType == powerUpType) return true; + } + return false; +} + +//@todo remove_powerUp +void remove_powerUp(AsteroidsApp* app, int id) { + FURI_LOG_I(TAG, "remove_powerUp: %i", id); + // Invalid ID, ignore + if(id < 0) { + FURI_LOG_E(TAG, "remove_powerUp: Invalid ID: %i", id); + return; + } + // TODO: Break this out into object types that set the game state + // Return the bullet period to normal + if(app->powerUps[id].powerUpType == PowerUpTypeFirePower) { + app->bullet_min_period = 200; + } + + /* Replace the top powerUp with the empty space left + * by the removal of this one. This way we always take the + * array dense, which is an advantage when looping. */ + int n = --app->powerUps_num; + if(n && id != n) app->powerUps[id] = app->powerUps[n]; +} + +void remove_all_astroids_and_bullets(AsteroidsApp* app) { + app->score += app->asteroids_num; + app->asteroids_num = 0; + app->bullets_num = 0; +} + +//@todo powerUp_was_hit +void powerUp_was_hit(AsteroidsApp* app, int id) { + PowerUp* p = &app->powerUps[id]; + if(p->display_ttl == 0) return; // Don't collect if already collected or expired + + // Vibrate to indicate power up was collected + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_powerup_collected); + + switch(p->powerUpType) { + case PowerUpTypeLife: + if(app->lives < MAXLIVES) app->lives++; + remove_powerUp(app, id); + break; + case PowerUpTypeFirePower: + p->ttl = POWERUPSTTL / 2; + app->bullet_min_period = 100; + break; + case PowerUpTypeNuke: + //TODO: Animate explosion + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_nuke); + // Simulate nuke explosion + remove_all_astroids_and_bullets(app); + break; + case PowerUpTypeDroneBuddy: + // Spawn a drone buddy + spawn_drone_buddy(app); + remove_powerUp(app, id); + break; + default: + break; + } + p->display_ttl = 0; + p->isPowerUpActive = true; +} + +/* ================================ Drone Buddy ================================ */ +/* Spawn a drone buddy near the ship */ +void spawn_drone_buddy(AsteroidsApp* app) { + if(app->drones_num >= MAXDRONES) return; // Max drones reached + + Drone* drone = &app->drones[app->drones_num++]; + + /* Position drone near the ship but not too close */ + drone->x = app->ship.x + 15; + drone->y = app->ship.y + 10; + drone->vx = 0; + drone->vy = 0; + drone->rot = app->ship.rot; + drone->ttl = DRONE_TTL; + drone->last_fire_tick = 0; + drone->active = true; + + FURI_LOG_I(TAG, "Drone buddy spawned at x=%.2f, y=%.2f", (double)drone->x, (double)drone->y); +} + +/* Remove a drone */ +void remove_drone(AsteroidsApp* app, int id) { + if(id < 0 || id >= app->drones_num) return; + + FURI_LOG_I(TAG, "Removing drone %d", id); + + /* Replace with last drone to keep array dense */ + int n = --app->drones_num; + if(n && id != n) app->drones[id] = app->drones[n]; +} + +/* Find the nearest asteroid to a drone */ +int find_nearest_asteroid(AsteroidsApp* app, Drone* drone) { + int nearest = -1; + float min_distance = 50.0f; /* Maximum targeting range */ + + for(int i = 0; i < app->asteroids_num; i++) { + float dist = distance(drone->x, drone->y, app->asteroids[i].x, app->asteroids[i].y); + if(dist < min_distance) { + min_distance = dist; + nearest = i; + } } + + return nearest; } +/* Make drone fire at nearest asteroid */ +void drone_fire_bullet(AsteroidsApp* app, Drone* drone) { + if(app->bullets_num >= MAXBUL) return; /* No space for more bullets */ + + int target = find_nearest_asteroid(app, drone); + if(target == -1) return; /* No target in range */ + + Asteroid* ast = &app->asteroids[target]; + + /* Calculate direction to target */ + float dx = ast->x - drone->x; + float dy = ast->y - drone->y; + float dist = sqrt(dx * dx + dy * dy); + + if(dist > 0) { + Bullet* b = &app->bullets[app->bullets_num++]; + b->x = drone->x; + b->y = drone->y; + b->vx = (dx / dist) * 3.0f; /* Bullet speed */ + b->vy = (dy / dist) * 3.0f; + b->ttl = TTLBUL; + + /* Update drone rotation to face target */ + drone->rot = atan2(-dx, dy); + + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_bullet_fired); + } +} + +/* Update drone positions and behavior */ +void update_drones(AsteroidsApp* app) { + for(int i = 0; i < app->drones_num; i++) { + Drone* drone = &app->drones[i]; + + if(!drone->active) continue; + + /* Decrease TTL */ + if(drone->ttl > 0) { + drone->ttl--; + } else { + /* Drone expired */ + remove_drone(app, i); + i--; /* Process this index again */ + continue; + } + + /* Follow the ship at a distance */ + float target_x = app->ship.x - 20; + float target_y = app->ship.y - 15; + + /* Simple following behavior */ + float dx = target_x - drone->x; + float dy = target_y - drone->y; + float dist = sqrt(dx * dx + dy * dy); + + if(dist > 5.0f) { + /* Move towards target position */ + drone->vx = (dx / dist) * 1.5f; + drone->vy = (dy / dist) * 1.5f; + } else { + /* Close enough, reduce velocity */ + drone->vx *= 0.8f; + drone->vy *= 0.8f; + } + + /* Update position */ + update_pos_by_velocity(&drone->x, &drone->y, drone->vx, drone->vy); + + /* Fire at asteroids */ + uint32_t now = furi_get_tick(); + if(now - drone->last_fire_tick >= DRONE_FIRE_RATE) { + drone_fire_bullet(app, drone); + drone->last_fire_tick = now; + } + } +} + +/* Check drone collisions with asteroids */ +void check_drone_collisions(AsteroidsApp* app) { + for(int i = 0; i < app->drones_num; i++) { + Drone* drone = &app->drones[i]; + if(!drone->active) continue; + + for(int j = 0; j < app->asteroids_num; j++) { + Asteroid* ast = &app->asteroids[j]; + if(objects_are_colliding(drone->x, drone->y, 3, ast->x, ast->y, ast->size, 1)) { + /* Drone was hit by asteroid */ + FURI_LOG_I(TAG, "Drone destroyed by asteroid"); + remove_drone(app, i); + i--; /* Process this index again */ + break; + } + } + } +} + +/* ================================ Game States ================================ */ /* Set gameover state. When in game-over mode, the game displays a gameover * text with a background of many asteroids floating around. */ -void game_over(AsteroidsApp *app) { - restart_game_after_gameover(app); +void game_over(AsteroidsApp* app) { + if(app->is_new_highscore) save_game(app); // Save highscore but only on change app->gameover = true; - int asteroids = 8; - while(asteroids-- && add_asteroid(app) != NULL); + app->lives = GAME_START_LIVES; // Show 3 lives in game over screen to match new game start } /* Function called when a collision between the asteroid and the * ship is detected. */ -void ship_was_hit(AsteroidsApp *app) { +void ship_was_hit(AsteroidsApp* app) { app->ship_hit = SHIP_HIT_ANIMATION_LEN; - if (app->lives) { + if(app->lives) { app->lives--; } else { game_over(app); @@ -410,35 +1195,40 @@ void ship_was_hit(AsteroidsApp *app) { /* Restart game after the ship is hit. Will reset the ship position, bullets * and asteroids to restart the game. */ -void restart_game(AsteroidsApp *app) { +void restart_game(AsteroidsApp* app) { app->ship.x = SCREEN_XRES / 2; app->ship.y = SCREEN_YRES / 2; - app->ship.rot = PI; /* Start headed towards top. */ + app->ship.rot = PI; /* Start headed towards top. */ app->ship.vx = 0; app->ship.vy = 0; app->bullets_num = 0; + app->powerUps_num = 0; app->last_bullet_tick = 0; + app->bullet_min_period = 200; app->asteroids_num = 0; + app->drones_num = 0; + app->ship_hit = 0; } /* Called after gameover to restart the game. This function * also calls restart_game(). */ -void restart_game_after_gameover(AsteroidsApp *app) { +void restart_game_after_gameover(AsteroidsApp* app) { app->gameover = false; app->ticks = 0; app->score = 0; - app->ship_hit = 0; - app->lives = GAME_START_LIVES; + app->is_new_highscore = false; + app->lives = GAME_START_LIVES - 1; restart_game(app); } +/* ================================ Position & Status ================================ */ /* Move bullets. */ -void update_bullets_position(AsteroidsApp *app) { - for (int j = 0; j < app->bullets_num; j++) { - update_pos_by_velocity(&app->bullets[j].x,&app->bullets[j].y, - app->bullets[j].vx,app->bullets[j].vy); - if (--app->bullets[j].ttl == 0) { - remove_bullet(app,j); +void update_bullets_position(AsteroidsApp* app) { + for(int j = 0; j < app->bullets_num; j++) { + update_pos_by_velocity( + &app->bullets[j].x, &app->bullets[j].y, app->bullets[j].vx, app->bullets[j].vy); + if(--app->bullets[j].ttl == 0) { + remove_bullet(app, j); j--; /* Process this bullet index again: the removal will fill it with the top bullet to take the array dense. */ } @@ -446,28 +1236,90 @@ void update_bullets_position(AsteroidsApp *app) { } /* Move asteroids. */ -void update_asteroids_position(AsteroidsApp *app) { - for (int j = 0; j < app->asteroids_num; j++) { - update_pos_by_velocity(&app->asteroids[j].x,&app->asteroids[j].y, - app->asteroids[j].vx,app->asteroids[j].vy); +void update_asteroids_position(AsteroidsApp* app) { + for(int j = 0; j < app->asteroids_num; j++) { + update_pos_by_velocity( + &app->asteroids[j].x, &app->asteroids[j].y, app->asteroids[j].vx, app->asteroids[j].vy); app->asteroids[j].rot += app->asteroids[j].rot_speed; - if (app->asteroids[j].rot < 0) app->asteroids[j].rot = 2*PI; - else if (app->asteroids[j].rot > 2*PI) app->asteroids[j].rot = 0; + if(app->asteroids[j].rot < 0) + app->asteroids[j].rot = 2 * PI; + else if(app->asteroids[j].rot > 2 * PI) + app->asteroids[j].rot = 0; + } +} + +bool should_add_powerUp(AsteroidsApp* app) { + srand(furi_get_tick()); + int random_number = rand() % 100 + 1; + + // The chance of spawning a power-up decreases as the game goes on + int threshold = 100 - (app->score * 5); + + // Make sure the threshold doesn't go below 10 + threshold = (threshold < 10) ? 10 : threshold; + // FURI_LOG_I( + // TAG, + // "Random number: %d, threshold: %d Bool: %d", + // random_number, + // threshold, + // random_number <= threshold); + return random_number <= threshold; +} + +void update_powerUps_position(AsteroidsApp* app) { + for(int j = 0; j < app->powerUps_num; j++) { + // @todo update_powerUps_position + if(app->powerUps[j].display_ttl > 0) { + update_pos_by_velocity( + &app->powerUps[j].x, &app->powerUps[j].y, app->powerUps[j].vx, app->powerUps[j].vy); + } + } +} + +// @todo update_powerUp_status +/* This updates the state of each power up collected and removes them if they have expired. */ +void update_powerUp_status(AsteroidsApp* app) { + for(int j = 0; j < app->powerUps_num; j++) { + if(app->powerUps[j].ttl > 0 && app->powerUps[j].isPowerUpActive) { + // Only decrement ttl if we actually picked up power up + app->powerUps[j].ttl--; + } else if(app->powerUps[j].display_ttl > 0) { + app->powerUps[j].display_ttl--; + } else if(app->powerUps[j].ttl == 0 || app->powerUps[j].display_ttl == 0) { + FURI_LOG_I( + TAG, + "[update_powerUp_status] Power up expired!, ttl: %lu, display_ttl: %lu id: %d", + app->powerUps[j].ttl, + app->powerUps[j].display_ttl, + j); + // we've reached the end of life of the power up + // Time to remove it + app->powerUps[j].isPowerUpActive = false; + remove_powerUp(app, j); + j--; /* Process this power up index again: the removal will + fill it with the top power up to take the array dense. */ + } else { + FURI_LOG_E( + TAG, + "[update_powerUp_status] Power up error! Invalid Index: %d ttl: %lu display_ttl: %lu PowerUp_Num: %d", + j, + app->powerUps[j].ttl, + app->powerUps[j].display_ttl, + app->powerUps_num); + } } } /* Collision detection and game state update based on collisions. */ -void detect_collisions(AsteroidsApp *app) { +void detect_collisions(AsteroidsApp* app) { /* Detect collision between bullet and asteroid. */ - for (int j = 0; j < app->bullets_num; j++) { - Bullet *b = &app->bullets[j]; - for (int i = 0; i < app->asteroids_num; i++) { - Asteroid *a = &app->asteroids[i]; - if (objects_are_colliding(a->x, a->y, a->size, - b->x, b->y, 1.5, 1)) - { - asteroid_was_hit(app,i); - remove_bullet(app,j); + for(int j = 0; j < app->bullets_num; j++) { + Bullet* b = &app->bullets[j]; + for(int i = 0; i < app->asteroids_num; i++) { + Asteroid* a = &app->asteroids[i]; + if(objects_are_colliding(a->x, a->y, a->size, b->x, b->y, 1.5, 1)) { + asteroid_was_hit(app, i); + remove_bullet(app, j); /* The bullet no longer exist. Break the loop. * However we want to start processing from the * same bullet index, since now it is used by @@ -479,13 +1331,29 @@ void detect_collisions(AsteroidsApp *app) { } /* Detect collision between ship and asteroid. */ - for (int j = 0; j < app->asteroids_num; j++) { - Asteroid *a = &app->asteroids[j]; - if (objects_are_colliding(a->x, a->y, a->size, - app->ship.x, app->ship.y, 4, 1)) - { - ship_was_hit(app); - break; + for(int j = 0; j < app->asteroids_num; j++) { + Asteroid* a = &app->asteroids[j]; + if(objects_are_colliding(a->x, a->y, a->size, app->ship.x, app->ship.y, 4, 1)) { + if(isPowerUpActive(app, PowerUpTypeShield)) { + // Asteroid was hit with shield + notification_message( + furi_record_open(RECORD_NOTIFICATION), &sequence_bullet_fired); + asteroid_was_hit(app, j); + j--; /* Scan this j value again. */ + } else { + // No sheild active, take damage + ship_was_hit(app); + break; + } + } + } + + /* Detect collision between ship and powerUp. */ + for(int j = 0; j < app->powerUps_num; j++) { + PowerUp* p = &app->powerUps[j]; + if(objects_are_colliding(p->x, p->y, p->size, app->ship.x, app->ship.y, 4, 1)) { + powerUp_was_hit(app, j); + // break; } } } @@ -496,40 +1364,59 @@ void detect_collisions(AsteroidsApp *app) { * on velocity. Detect collisions. Update the score and so forth. * * Each time this function is called, app->tick is incremented. */ -void game_tick(void *ctx) { - AsteroidsApp *app = ctx; +void game_tick(void* ctx) { + AsteroidsApp* app = ctx; + + /* Handle splash screen timing */ + if(app->splash_screen) { + uint32_t elapsed = furi_get_tick() - app->splash_start_time; + if(elapsed >= SPLASH_SCREEN_DURATION) { + app->splash_screen = false; + } + view_port_update(app->view_port); + return; + } - /* There are two special screens: + /* There are three special screens: * * 1. Ship was hit, we frozen the game as long as ship_hit isn't zero * again, and show an animation of a rotating ship. */ - if (app->ship_hit) { + if(app->ship_hit) { + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_crash); app->ship.rot += 0.5; app->ship_hit--; view_port_update(app->view_port); - if (app->ship_hit == 0) { + if(app->ship_hit == 0) { restart_game(app); } return; - } else if (app->gameover) { - /* 2. Game over. We need to update only background asteroids. In this + } else if(app->gameover) { + /* 2. Game over. We need to update only background asteroids. In this * state the game just displays a GAME OVER text with the floating * asteroids in backgroud. */ - if (key_pressed_time(app,InputKeyOk) > 100) { + + if(key_pressed_time(app, InputKeyOk) > 100) { restart_game_after_gameover(app); } update_asteroids_position(app); view_port_update(app->view_port); return; + } else if(app->paused) { + if(key_pressed_time(app, InputKeyBack) > 100 || key_pressed_time(app, InputKeyOk) > 100) { + app->paused = false; + } + view_port_update(app->view_port); + return; } /* Handle keypresses. */ - if (app->pressed[InputKeyLeft]) app->ship.rot -= .35; - if (app->pressed[InputKeyRight]) app->ship.rot += .35; - if (key_pressed_time(app,InputKeyOk) > 70) { - app->ship.vx -= 0.5*(float)sin(app->ship.rot); - app->ship.vy += 0.5*(float)cos(app->ship.rot); - } else if (app->pressed[InputKeyDown]) { + if(app->pressed[InputKeyLeft]) app->ship.rot -= .35; + if(app->pressed[InputKeyRight]) app->ship.rot += .35; + if(app->pressed[InputKeyUp]) { + app->ship.vx -= 0.5 * (float)sin(app->ship.rot); + app->ship.vy += 0.5 * (float)cos(app->ship.rot); + } else if(app->pressed[InputKeyDown]) { + notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_brake); app->ship.vx *= 0.75; app->ship.vy *= 0.75; } @@ -537,49 +1424,105 @@ void game_tick(void *ctx) { /* Fire a bullet if needed. app->fire is set in * asteroids_update_keypress_state() since depends on exact * pressure timing. */ - if (app->fire) { - uint32_t bullet_min_period = 200; // In milliseconds + if(app->fire) { uint32_t now = furi_get_tick(); - if (now - app->last_bullet_tick >= bullet_min_period) { + if(now - app->last_bullet_tick >= app->bullet_min_period) { ship_fire_bullet(app); app->last_bullet_tick = now; } app->fire = false; } + // DEBUG: Show Power Up Status + // for(int j = 0; j < app->powerUps_num; j++) { + // PowerUp* p = &app->powerUps[j]; + // FURI_LOG_I( + // TAG, + // "Power Up Type: %d TTL: %lu Display_TTL: %lu PowerUpNum: %i", + // p->powerUpType, + // p->ttl, + // p->display_ttl, + // app->powerUps_num); + // } + /* Update positions and detect collisions. */ - update_pos_by_velocity(&app->ship.x,&app->ship.y,app->ship.vx,app->ship.vy); + update_pos_by_velocity(&app->ship.x, &app->ship.y, app->ship.vx, app->ship.vy); update_bullets_position(app); update_asteroids_position(app); + update_powerUp_status(app); //@todo update_powerUp_status + update_powerUps_position(app); + update_drones(app); detect_collisions(app); + check_drone_collisions(app); /* From time to time, create a new asteroid. The more asteroids * already on the screen, the smaller probability of creating * a new one. */ - if (app->asteroids_num == 0 || - (random() % 5000) < (30/(1+app->asteroids_num))) - { + if(app->asteroids_num == 0 || (random() % 5000) < (30 / (1 + app->asteroids_num))) { add_asteroid(app); } + /* From time to time add a random power up */ + //@todo game tick + // if(app->powerUps_num == 0 || random() % (500 + (100 * (int)app->score)) <= app->powerUps_num) { + if(should_add_powerUp(app)) { + add_powerUp(app); + } + app->ticks++; view_port_update(app->view_port); } /* ======================== Flipper specific code =========================== */ +bool load_game(AsteroidsApp* app) { + 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, app, sizeof(AsteroidsApp)); + } + storage_file_close(file); + storage_file_free(file); + + furi_record_close(RECORD_STORAGE); + + return bytes_readed == sizeof(AsteroidsApp); +} + +void save_game(AsteroidsApp* app) { + 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, app, sizeof(AsteroidsApp)); + } + storage_file_close(file); + storage_file_free(file); + + furi_record_close(RECORD_STORAGE); +} + /* Here all we do is putting the events into the queue that will be handled * in the while() loop of the app entry point function. */ -void input_callback(InputEvent* input_event, void* ctx) -{ - AsteroidsApp *app = ctx; - furi_message_queue_put(app->event_queue,input_event,FuriWaitForever); +void input_callback(InputEvent* input_event, void* ctx) { + AsteroidsApp* app = ctx; + furi_message_queue_put(app->event_queue, input_event, FuriWaitForever); } /* Allocate the application state and initialize a number of stuff. * This is called in the entry point to create the application state. */ AsteroidsApp* asteroids_app_alloc() { - AsteroidsApp *app = malloc(sizeof(AsteroidsApp)); + AsteroidsApp* app = malloc(sizeof(AsteroidsApp)); + + load_game(app); app->gui = furi_record_open(RECORD_GUI); app->view_port = view_port_alloc(); @@ -588,16 +1531,21 @@ AsteroidsApp* asteroids_app_alloc() { gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen); app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent)); - app->running = 1; /* Turns 0 when back is pressed. */ + app->running = 1; /* Turns 0 when back is pressed. */ + + /* Initialize splash screen */ + app->splash_screen = true; + app->splash_start_time = furi_get_tick(); + restart_game_after_gameover(app); - memset(app->pressed,0,sizeof(app->pressed)); + memset(app->pressed, 0, sizeof(app->pressed)); return app; } /* Free what the application allocated. It is not clear to me if the * Flipper OS, once the application exits, will be able to reclaim space * even if we forget to free something here. */ -void asteroids_app_free(AsteroidsApp *app) { +void asteroids_app_free(AsteroidsApp* app) { furi_assert(app); // View related. @@ -613,28 +1561,30 @@ void asteroids_app_free(AsteroidsApp *app) { /* Return the time in milliseconds the specified key is continuously * pressed. Or 0 if it is not pressed. */ -uint32_t key_pressed_time(AsteroidsApp *app, InputKey key) { - return app->pressed[key] == 0 ? 0 : - furi_get_tick() - app->pressed[key]; +uint32_t key_pressed_time(AsteroidsApp* app, InputKey key) { + return app->pressed[key] == 0 ? 0 : furi_get_tick() - app->pressed[key]; } /* Handle keys interaction. */ -void asteroids_update_keypress_state(AsteroidsApp *app, InputEvent input) { - if (input.type == InputTypePress) { +void asteroids_update_keypress_state(AsteroidsApp* app, InputEvent input) { + // Allow Rapid fire + if(input.key == InputKeyOk) { + app->fire = true; + } + + if(input.type == InputTypePress) { app->pressed[input.key] = furi_get_tick(); - } else if (input.type == InputTypeRelease) { - uint32_t dur = key_pressed_time(app,input.key); + } else if(input.type == InputTypeRelease) { app->pressed[input.key] = 0; - if (dur < 200 && input.key == InputKeyOk) app->fire = true; } } int32_t asteroids_app_entry(void* p) { UNUSED(p); - AsteroidsApp *app = asteroids_app_alloc(); + AsteroidsApp* app = asteroids_app_alloc(); /* Create a timer. We do data analysis in the callback. */ - FuriTimer *timer = furi_timer_alloc(game_tick, FuriTimerTypePeriodic, app); + FuriTimer* timer = furi_timer_alloc(game_tick, FuriTimerTypePeriodic, app); furi_timer_start(timer, furi_kernel_get_tick_frequency() / 10); /* This is the main event loop: here we get the events that are pushed @@ -643,26 +1593,43 @@ int32_t asteroids_app_entry(void* p) { InputEvent input; while(app->running) { FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100); - if (qstat == FuriStatusOk) { - if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u", - input.type, input.key); + if(qstat == FuriStatusOk) { + // if(DEBUG_MSG) + // FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u", input.type, input.key); + + /* Handle splash screen skip */ + if(app->splash_screen && input.type == InputTypePress) { + app->splash_screen = false; + continue; + } + + /* Handle Pause */ + if(input.type == InputTypeShort && input.key == InputKeyBack) { + app->paused = !app->paused; + if(app->paused) { + furi_timer_stop(timer); + } else { + furi_timer_start(timer, furi_kernel_get_tick_frequency() / 10); + } + } /* Handle navigation here. Then handle view-specific inputs * in the view specific handling function. */ - if (input.type == InputTypeShort && - input.key == InputKeyBack) - { + if(input.type == InputTypeLong && input.key == InputKeyBack) { + // Save High Score even if player didn't finish game + if(app->is_new_highscore) save_game(app); // Save highscore but only on change app->running = 0; } else { - asteroids_update_keypress_state(app,input); + asteroids_update_keypress_state(app, input); } } else { /* Useful to understand if the app is still alive when it * does not respond because of bugs. */ - if (DEBUG_MSG) { - static int c = 0; c++; - if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout"); - } + // if(DEBUG_MSG) { + // static int c = 0; + // c++; + // if(!(c % 20)) FURI_LOG_E(TAG, "Loop timeout"); + // } } } diff --git a/application.fam b/application.fam index 0a56122..c62de55 100644 --- a/application.fam +++ b/application.fam @@ -5,8 +5,12 @@ App( entry_point="asteroids_app_entry", cdefines=["APP_PROTOVIEW"], requires=["gui"], - stack_size=8*1024, + stack_size=8 * 1024, order=50, fap_icon="appicon.png", fap_category="Games", + fap_icon_assets="assets", # Image assets to compile for this application + fap_description="An implementation of the classic arcade game Asteroids", + fap_author="antirez, SimplyMinimal", + fap_weburl="https://github.com/SimplyMinimal/FlipperZero-Asteroids", ) diff --git a/assets/ammo_10x10.png b/assets/ammo_10x10.png new file mode 100644 index 0000000..b112a1a Binary files /dev/null and b/assets/ammo_10x10.png differ diff --git a/assets/ammo_11x11.png b/assets/ammo_11x11.png new file mode 100644 index 0000000..55e59f8 Binary files /dev/null and b/assets/ammo_11x11.png differ diff --git a/assets/firepower_12x12.png b/assets/firepower_12x12.png new file mode 100644 index 0000000..711a292 Binary files /dev/null and b/assets/firepower_12x12.png differ diff --git a/assets/firepower_9x10.png b/assets/firepower_9x10.png new file mode 100644 index 0000000..4070b6c Binary files /dev/null and b/assets/firepower_9x10.png differ diff --git a/assets/firepower_shifted_9x10.png b/assets/firepower_shifted_9x10.png new file mode 100644 index 0000000..9c8506d Binary files /dev/null and b/assets/firepower_shifted_9x10.png differ diff --git a/assets/heart_10x10.png b/assets/heart_10x10.png new file mode 100644 index 0000000..0d66b49 Binary files /dev/null and b/assets/heart_10x10.png differ diff --git a/assets/heart_12x12.png b/assets/heart_12x12.png new file mode 100644 index 0000000..b1cfdcd Binary files /dev/null and b/assets/heart_12x12.png differ diff --git a/assets/nuke_10x10.png b/assets/nuke_10x10.png new file mode 100644 index 0000000..8b49fc9 Binary files /dev/null and b/assets/nuke_10x10.png differ diff --git a/assets/shield-frame.png b/assets/shield-frame.png new file mode 100644 index 0000000..60a670f Binary files /dev/null and b/assets/shield-frame.png differ diff --git a/assets/shield_clean.png b/assets/shield_clean.png new file mode 100644 index 0000000..59eefb7 Binary files /dev/null and b/assets/shield_clean.png differ diff --git a/assets/split_shield_10x10.png b/assets/split_shield_10x10.png new file mode 100644 index 0000000..bff879c Binary files /dev/null and b/assets/split_shield_10x10.png differ diff --git a/binaries/README.md b/binaries/README.md deleted file mode 100644 index f9dbaac..0000000 --- a/binaries/README.md +++ /dev/null @@ -1,10 +0,0 @@ -This is the binary distribution of the application. If you don't want -to build it from source, just take `asteroids.fap` file and drop it into the -following Flipper Zero location: - - /ext/apps/Games - -The `ext` part means that we are in the SD card. So if you don't want -to use the Android (or other) application to upload the file, -you can just take out the SD card, insert it in your computer, -copy the fine into `apps/Games`, and that's it. diff --git a/binaries/asteroids.fap b/binaries/asteroids.fap deleted file mode 100644 index dd58ad4..0000000 Binary files a/binaries/asteroids.fap and /dev/null differ diff --git a/binaries/update.sh b/binaries/update.sh deleted file mode 100755 index a474894..0000000 --- a/binaries/update.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -BINPATH="/Users/antirez/hack/flipper/official/build/f7-firmware-D/.extapps/asteroids.fap" -cp $BINPATH . -git commit -a -m 'Binary file updated.' diff --git a/images/Asteroids-PowerUps.png b/images/Asteroids-PowerUps.png new file mode 100644 index 0000000..66b17b1 Binary files /dev/null and b/images/Asteroids-PowerUps.png differ diff --git a/images/PauseScreen.png b/images/PauseScreen.png new file mode 100644 index 0000000..3f035c5 Binary files /dev/null and b/images/PauseScreen.png differ