mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-15 19:08:42 +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
|
||||
|
||||
class Bot {
|
||||
#include <optional>
|
||||
|
||||
#include "Map.hpp"
|
||||
#include "Snake.hpp"
|
||||
|
||||
class Bot : public Snake, public GameObject {
|
||||
public:
|
||||
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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue