initial game

This commit is contained in:
programsnail 2024-07-22 21:22:52 +03:00
parent 4570d6e593
commit adabb50a9e
10 changed files with 200 additions and 132 deletions

View file

@ -2,6 +2,7 @@
#include <deque>
#include "Canvas.hpp"
#include "Utils.hpp"
#include "Vec.hpp"
@ -13,17 +14,23 @@ class Map : public GameObject {
bool eaten = false;
};
static constexpr double GEN_INTERVAL = 1.0;
static constexpr size_t FOOD_EXISTS_GENS = 10;
static constexpr size_t GEN_FOOD_COUNT = 1000;
static constexpr int SIZE_X = 20000;
static constexpr int SIZE_Y = 20000;
public:
struct Config {
double gen_interval;
size_t food_exists_gens;
size_t gen_food_count;
Veci size;
int min_food_weight;
int max_food_weight;
Color food_color;
};
Map(Config config) : config_(config) {}
void act(float dt) override {
time_from_last_gen_ += dt;
if (time_from_last_gen_ > GEN_INTERVAL) {
time_from_last_gen_ -= GEN_INTERVAL;
if (time_from_last_gen_ > config_.gen_interval) {
time_from_last_gen_ -= config_.gen_interval;
generate();
}
}
@ -39,23 +46,36 @@ public:
return eaten_weight;
}
void draw() override {} // TODO
private:
void generate() {
++current_gen_;
while (food_.front().gen + FOOD_EXISTS_GENS < current_gen_) {
food_.pop_front();
}
for (size_t i = 0; i < GEN_FOOD_COUNT; ++i) {
food_.push_back({.gen = current_gen_,
.pos = {.x = rand() % SIZE_X, .y = rand() % SIZE_Y},
.weight = 1});
void draw(Veci offset) override {
for (const auto &food : food_) {
Veci food_pos = food.pos - offset;
if (utils::is_valid_pos(food_pos) and not food.eaten) {
paint::circle(
{{.pos = food_pos, .color = config_.food_color}, food.weight * 3});
}
}
}
private:
void generate() {
++current_gen_;
while (food_.front().gen + config_.food_exists_gens < current_gen_) {
food_.pop_front();
}
for (size_t i = 0; i < config_.gen_food_count; ++i) {
food_.push_back({
.gen = current_gen_,
.pos = {.x = rand() % config_.size.x, .y = rand() % config_.size.y},
.weight = config_.min_food_weight +
rand() % std::abs(config_.max_food_weight -
config_.min_food_weight),
});
}
}
private:
Config config_;
double time_from_last_gen_ = 0;
size_t current_gen_ = 0;
std::deque<Food> food_ = {};