Przeglądaj źródła

add rolling background + bi-directional scientists

Tim Strasser 2 lat temu
rodzic
commit
0d4a1fc1d3

BIN
assets/bg1.png


BIN
assets/bg2.png


BIN
assets/bg3.png


BIN
assets/scientist_left/frame_01.png


BIN
assets/scientist_left/frame_02.png


BIN
assets/scientist_left/frame_03.png


+ 0 - 0
assets/scientist/frame_rate → assets/scientist_left/frame_rate


+ 0 - 0
assets/scientist/frame_01.png → assets/scientist_right/frame_01.png


+ 0 - 0
assets/scientist/frame_02.png → assets/scientist_right/frame_02.png


+ 0 - 0
assets/scientist/frame_03.png → assets/scientist_right/frame_03.png


+ 1 - 0
assets/scientist_right/frame_rate

@@ -0,0 +1 @@
+3

+ 5 - 1
includes/game_sprites.h

@@ -1,12 +1,16 @@
 #ifndef GAME_SPRITES_H
 #define GAME_SPRITES_H
 
+#include "point.h"
 #include <gui/icon_animation.h>
 
 typedef struct {
     IconAnimation* barry;
-    IconAnimation* scientist;
+    IconAnimation* scientist_left;
+    IconAnimation* scientist_right;
     IconAnimation* missile;
+    const Icon* bg[3];
+    POINT bg_pos[3];
 } GameSprites;
 
 #endif // GAME_SPRITES_H

+ 9 - 5
includes/scientist.c

@@ -7,7 +7,7 @@
 void scientist_tick(SCIENTIST* const scientists) {
     for(int i = 0; i < SCIENTISTS_MAX; i++) {
         if(scientists[i].point.x > 0) {
-            scientists[i].point.x -= scientists[i].velocity_x; // move based on velocity_x
+            scientists[i].point.x -= 1 - scientists[i].velocity_x; // move based on velocity_x
             if(scientists[i].point.x < -16) { // if the scientist is out of screen
                 scientists[i].point.x =
                     0; // set scientist x coordinate to 0 to mark it as "inactive"
@@ -17,6 +17,7 @@ void scientist_tick(SCIENTIST* const scientists) {
 }
 
 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].point.x <= 0 &&
@@ -24,9 +25,7 @@ void spawn_random_scientist(SCIENTIST* const scientists) {
             scientists[i].state = ScientistStateAlive;
             scientists[i].point.x = 127;
             scientists[i].point.y = 49;
-            scientists[i].velocity_x =
-                (rand() % (SCIENTIST_VELOCITY_MAX - SCIENTIST_VELOCITY_MIN + 1)) +
-                SCIENTIST_VELOCITY_MIN; // random velocity between SCIENTIST_VELOCITY_MIN and SCIENTIST_VELOCITY_MAX
+            scientists[i].velocity_x = velocities[rand() % 4];
             break;
         }
     }
@@ -37,7 +36,12 @@ void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const Ga
         if(scientists[i].point.x > 0) {
             if(scientists[i].state == ScientistStateAlive) {
                 canvas_draw_icon_animation(
-                    canvas, scientists[i].point.x, scientists[i].point.y, sprites->scientist);
+                    canvas,
+                    scientists[i].point.x,
+                    scientists[i].point.y,
+                    scientists[i].velocity_x >= 0 ? sprites->scientist_right :
+                                                    sprites->scientist_left);
+
             } else {
                 canvas_draw_icon(
                     canvas, scientists[i].point.x, scientists[i].point.y + 5, &I_dead_scientist);

+ 4 - 4
includes/scientist.h

@@ -5,8 +5,8 @@
 #include "game_sprites.h"
 #include <gui/gui.h>
 
-#define SCIENTIST_VELOCITY_MIN 2
-#define SCIENTIST_VELOCITY_MAX 2
+#define SCIENTIST_VELOCITY_MIN -0.5f
+#define SCIENTIST_VELOCITY_MAX 0.5f
 
 #define SCIENTISTS_MAX 6
 
@@ -17,8 +17,8 @@ typedef enum {
 
 typedef struct {
     float gravity;
-    POINT point;
-    int velocity_x;
+    POINTF point;
+    float velocity_x;
     ScientistState state;
 } SCIENTIST;
 

+ 31 - 3
jetpack.c

@@ -37,10 +37,21 @@ static void jetpack_game_state_init(GameState* const game_state) {
 
     GameSprites sprites;
     sprites.barry = icon_animation_alloc(&A_barry);
-    sprites.scientist = icon_animation_alloc(&A_scientist);
+    sprites.scientist_left = icon_animation_alloc(&A_scientist_left);
+    sprites.scientist_right = icon_animation_alloc(&A_scientist_right);
     sprites.missile = icon_animation_alloc(&A_missile);
 
-    icon_animation_start(sprites.scientist);
+    sprites.bg[0] = &I_bg1;
+    sprites.bg[1] = &I_bg2;
+    sprites.bg[2] = &I_bg3;
+
+    for(int i = 0; i < 3; ++i) {
+        sprites.bg_pos[i].x = i * 128;
+        sprites.bg_pos[i].y = 0;
+    }
+
+    icon_animation_start(sprites.scientist_left);
+    icon_animation_start(sprites.scientist_right);
     icon_animation_start(sprites.barry);
     icon_animation_start(sprites.missile);
 
@@ -57,8 +68,10 @@ static void jetpack_game_state_init(GameState* const game_state) {
 
 static void jetpack_game_state_free(GameState* const game_state) {
     icon_animation_free(game_state->sprites.barry);
-    icon_animation_free(game_state->sprites.scientist);
+    icon_animation_free(game_state->sprites.scientist_left);
+    icon_animation_free(game_state->sprites.scientist_right);
     icon_animation_free(game_state->sprites.missile);
+
     free(game_state);
 }
 
@@ -71,6 +84,13 @@ static void jetpack_game_tick(GameState* const game_state) {
     scientist_tick(game_state->scientists);
     missile_tick(game_state->missiles, &game_state->barry, &game_state->state);
 
+    for(int i = 0; i < 3; ++i) {
+        game_state->sprites.bg_pos[i].x -= 1;
+        if(game_state->sprites.bg_pos[i].x <= -128) {
+            game_state->sprites.bg_pos[i].x = 128 * 2; // 2 other images are 128 px each
+        }
+    }
+
     if((rand() % 100) < 1) {
         spawn_random_coin(game_state->coins);
     }
@@ -95,6 +115,14 @@ static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
         // canvas_draw_box(canvas, 0, 0, 128, 32);
         // canvas_set_color(canvas, ColorXOR);
 
+        for(int i = 0; i < 3; ++i) {
+            // Check if the image is within the screen's boundaries
+            if(game_state->sprites.bg_pos[i].x >= -127 && game_state->sprites.bg_pos[i].x < 128) {
+                canvas_draw_icon(
+                    canvas, game_state->sprites.bg_pos[i].x, 0, game_state->sprites.bg[i]);
+            }
+        }
+
         canvas_set_bitmap_mode(canvas, true);
 
         draw_coins(game_state->coins, canvas);