mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2026-01-09 05:04:57 +00:00
part of Bot class
This commit is contained in:
parent
1e9c70f7d8
commit
360c3b064a
4 changed files with 61 additions and 7 deletions
|
|
@ -1,6 +1,60 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class Bot {
|
#include <optional>
|
||||||
|
|
||||||
|
#include "Map.hpp"
|
||||||
|
#include "Snake.hpp"
|
||||||
|
|
||||||
|
class Bot : public Snake, public GameObject {
|
||||||
public:
|
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;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Config config_;
|
const Config config_;
|
||||||
double time_from_last_gen_ = 0;
|
double time_from_last_gen_ = 0;
|
||||||
size_t current_gen_ = 0;
|
size_t current_gen_ = 0;
|
||||||
std::deque<Food> food_ = {};
|
std::deque<Food> food_ = {};
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,16 @@
|
||||||
|
|
||||||
namespace canvas {
|
namespace canvas {
|
||||||
|
|
||||||
struct WormObject : public Object {
|
struct SnakeObject : public Object {
|
||||||
size_t length;
|
size_t length;
|
||||||
int radius;
|
int radius;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace canvas
|
} // namespace canvas
|
||||||
|
|
||||||
class Worm : protected canvas::WormObject {
|
class Snake : protected canvas::SnakeObject {
|
||||||
public:
|
public:
|
||||||
Worm(canvas::WormObject obj) : WormObject(obj), track_{pos} {}
|
Snake(canvas::SnakeObject obj) : SnakeObject(obj), track_{pos} {}
|
||||||
|
|
||||||
void add(Veci pos);
|
void add(Veci pos);
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ public:
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
bool touches(const Worm &other);
|
bool touches(const Snake &other);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::deque<Veci> track_;
|
std::deque<Veci> track_;
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ Map map{{
|
||||||
|
|
||||||
// std::vector<Worm> bots; // TODO
|
// std::vector<Worm> bots; // TODO
|
||||||
|
|
||||||
auto snake = Worm(canvas::WormObject{
|
auto snake = Snake(canvas::SnakeObject{
|
||||||
{.pos = Veci(player.pos), .color = {color::GREEN}},
|
{.pos = Veci(player.pos), .color = {color::GREEN}},
|
||||||
10,
|
10,
|
||||||
10,
|
10,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue