mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-07 06:58:43 +00:00
moving circle prototype, circle and square drawing, part of food map implementation, utils
This commit is contained in:
parent
b4a4ffc08a
commit
4570d6e593
9 changed files with 378 additions and 18 deletions
62
include/Map.hpp
Normal file
62
include/Map.hpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#pragma once
|
||||
|
||||
#include <deque>
|
||||
|
||||
#include "Utils.hpp"
|
||||
#include "Vec.hpp"
|
||||
|
||||
class Map : public GameObject {
|
||||
struct Food {
|
||||
size_t gen;
|
||||
Veci pos;
|
||||
int weight;
|
||||
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:
|
||||
void act(float dt) override {
|
||||
time_from_last_gen_ += dt;
|
||||
if (time_from_last_gen_ > GEN_INTERVAL) {
|
||||
time_from_last_gen_ -= GEN_INTERVAL;
|
||||
generate();
|
||||
}
|
||||
}
|
||||
|
||||
int eat(Veci pos, int dist) { // TODO: faster variant ?
|
||||
size_t eaten_weight = 0;
|
||||
for (auto &food : food_) {
|
||||
if ((pos - food.pos).len_sq() < dist * dist and not food.eaten) {
|
||||
food.eaten = true;
|
||||
eaten_weight += food.weight;
|
||||
}
|
||||
}
|
||||
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});
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
double time_from_last_gen_ = 0;
|
||||
size_t current_gen_ = 0;
|
||||
std::deque<Food> food_ = {};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue