part of Bot class

This commit is contained in:
programsnail 2024-07-25 22:42:54 +03:00
parent 1e9c70f7d8
commit 360c3b064a
4 changed files with 61 additions and 7 deletions

View file

@ -1,6 +1,60 @@
#pragma once
class Bot {
#include <optional>
#include "Map.hpp"
#include "Snake.hpp"
class Bot : public Snake, public GameObject {
public:
private:
struct Config {
float time_between_targets;
double speed;
float move_interval;
};
Bot(canvas::SnakeObject obj, Config config, Map &map)
: Snake(obj), config_(config), map_(map), real_pos_(obj.pos) {}
void act(float dt) override {
time_from_target_set_ += dt;
int eaten = map_.eat(utils::to_world_coord(pos), 20);
inc_length(eaten);
if (not target_.has_value() or
time_from_target_set_ >= config_.time_between_targets or
(target_.value() - real_pos_).len_sq() < radius * radius) {
time_from_target_set_ = 0;
target_ = map_.find_nearest_food(real_pos_); // TODO
}
direction_ = (Vecf(target_.value()) - real_pos_).norm();
// TODO: manually change direction
move(dt);
}
void draw(Veci offset) override { Snake::draw(offset); }
protected:
void move(float dt) {
real_pos_ += direction_ * dt * config_.speed;
move_time_delta_ += dt;
if (move_time_delta_ > config_.move_interval) {
move_time_delta_ -= config_.move_interval;
add(Veci(real_pos_));
}
}
private:
const Config config_;
Map &map_;
Vecf real_pos_;
Vecf direction_ = {};
std::optional<Vecf> target_ = {};
float time_from_target_set_ = 0;
float move_time_delta_ = 0;
};

View file

@ -90,7 +90,7 @@ private:
}
private:
Config config_;
const Config config_;
double time_from_last_gen_ = 0;
size_t current_gen_ = 0;
std::deque<Food> food_ = {};

View file

@ -6,16 +6,16 @@
namespace canvas {
struct WormObject : public Object {
struct SnakeObject : public Object {
size_t length;
int radius;
};
} // namespace canvas
class Worm : protected canvas::WormObject {
class Snake : protected canvas::SnakeObject {
public:
Worm(canvas::WormObject obj) : WormObject(obj), track_{pos} {}
Snake(canvas::SnakeObject obj) : SnakeObject(obj), track_{pos} {}
void add(Veci pos);
@ -41,7 +41,7 @@ public:
//
bool touches(const Worm &other);
bool touches(const Snake &other);
protected:
std::deque<Veci> track_;

View file

@ -53,7 +53,7 @@ Map map{{
// std::vector<Worm> bots; // TODO
auto snake = Worm(canvas::WormObject{
auto snake = Snake(canvas::SnakeObject{
{.pos = Veci(player.pos), .color = {color::GREEN}},
10,
10,