#pragma once
#include "raylib.h"
#include <vector>

struct Fly {
  Vector2 position;
  Vector2 velocity;
  bool isAlive = true;
};

class World {
 public:
  std::vector<Fly> flies;
  World(int count, int w, int h) {
    for (int i = 0; i < count; i++) {
      flies.push_back({{(float)GetRandomValue(0, w), (float)GetRandomValue(0, h)},
                       {(float)GetRandomValue(-100, 100) / 40.0f, (float)GetRandomValue(-100, 100) / 40.0f}});
    }
  }

  void update() {
    for (auto& f : flies) {
      if (!f.isAlive) continue;
      f.position = Vector2Add(f.position, f.velocity);
      if (f.position.x < 0 || f.position.x > 1200) f.velocity.x *= -1;
      if (f.position.y < 0 || f.position.y > 800) f.velocity.y *= -1;
    }
  }

  void draw() {
    for (const auto& f : flies) {
      if (f.isAlive) DrawCircleV(f.position, 3, LIME);
    }
  }

  void respawn(int targetCount) {
    int currentAlive = 0;
    for (const auto& f : flies) {
      if (f.isAlive) currentAlive++;
    }

    if (currentAlive < targetCount) {
      if (rand() % 100 == 0)
        flies.push_back({{(float)GetRandomValue(0, 1200), (float)GetRandomValue(0, 800)},
                         {(float)GetRandomValue(-100, 100) / 40.0f, (float)GetRandomValue(-100, 100) / 40.0f},
                         true});
    }
  }
};