Tim Strasser 2 лет назад
Родитель
Сommit
c9f4083fb6
2 измененных файлов с 18 добавлено и 11 удалено
  1. 17 10
      includes/scientist.c
  2. 1 1
      includes/scientist.h

+ 17 - 10
includes/scientist.c

@@ -6,13 +6,16 @@
 
 void scientist_tick(SCIENTIST* const scientists) {
     for(int i = 0; i < SCIENTISTS_MAX; i++) {
-        if(scientists[i].point.x > 0) {
+        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
-            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"
+            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;
             }
         }
     }
@@ -22,12 +25,13 @@ 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 &&
+        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;
         }
     }
@@ -35,12 +39,12 @@ void spawn_random_scientist(SCIENTIST* const scientists) {
 
 void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const GameSprites* sprites) {
     for(int i = 0; i < SCIENTISTS_MAX; ++i) {
-        if(scientists[i].point.x > 0) {
+        if(scientists[i].visible) {
             canvas_set_color(canvas, ColorBlack);
             if(scientists[i].state == ScientistStateAlive) {
                 canvas_draw_icon(
                     canvas,
-                    scientists[i].point.x,
+                    (int)scientists[i].point.x,
                     scientists[i].point.y,
                     scientists[i].velocity_x >= 0 ? sprites->scientist_right :
                                                     sprites->scientist_left);
@@ -48,7 +52,7 @@ void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const Ga
                 canvas_set_color(canvas, ColorWhite);
                 canvas_draw_icon(
                     canvas,
-                    scientists[i].point.x,
+                    (int)scientists[i].point.x,
                     scientists[i].point.y,
                     scientists[i].velocity_x >= 0 ? sprites->scientist_right_infill :
                                                     sprites->scientist_left_infill);
@@ -56,12 +60,15 @@ void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const Ga
             } else {
                 canvas_set_color(canvas, ColorBlack);
                 canvas_draw_icon(
-                    canvas, scientists[i].point.x, scientists[i].point.y + 5, &I_dead_scientist);
+                    canvas,
+                    (int)scientists[i].point.x,
+                    scientists[i].point.y + 5,
+                    &I_dead_scientist);
 
                 canvas_set_color(canvas, ColorWhite);
                 canvas_draw_icon(
                     canvas,
-                    scientists[i].point.x,
+                    (int)scientists[i].point.x,
                     scientists[i].point.y + 5,
                     &I_dead_scientist_infill);
             }

+ 1 - 1
includes/scientist.h

@@ -16,7 +16,7 @@ typedef enum {
 } ScientistState;
 
 typedef struct {
-    float gravity;
+    bool visible;
     POINTF point;
     float velocity_x;
     ScientistState state;