mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-06 06:28:43 +00:00
Bot class: first iteration, refactoring fix (Worm -> Snake)
This commit is contained in:
parent
360c3b064a
commit
a92c96f91e
3 changed files with 27 additions and 8 deletions
|
|
@ -24,14 +24,17 @@ public:
|
|||
|
||||
if (not target_.has_value() or
|
||||
time_from_target_set_ >= config_.time_between_targets or
|
||||
(target_.value() - real_pos_).len_sq() < radius * radius) {
|
||||
(target_.value() - pos).len_sq() < radius * radius) {
|
||||
time_from_target_set_ = 0;
|
||||
|
||||
target_ = map_.find_nearest_food(real_pos_); // TODO
|
||||
target_ = map_.find_nearest_food(pos);
|
||||
}
|
||||
|
||||
direction_ = (Vecf(target_.value()) - real_pos_).norm();
|
||||
// TODO: manually change direction
|
||||
// check for case when no food found
|
||||
if (target_.has_value()) {
|
||||
direction_ = (Vecf(target_.value()) - real_pos_).norm();
|
||||
// TODO: manually change direction
|
||||
}
|
||||
|
||||
move(dt);
|
||||
}
|
||||
|
|
@ -54,7 +57,7 @@ private:
|
|||
|
||||
Vecf real_pos_;
|
||||
Vecf direction_ = {};
|
||||
std::optional<Vecf> target_ = {};
|
||||
std::optional<Veci> target_ = {};
|
||||
float time_from_target_set_ = 0;
|
||||
float move_time_delta_ = 0;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
#include "Canvas.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
|
@ -61,6 +62,21 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<Veci> find_nearest_food(Veci pos) {
|
||||
if (food_.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Veci target = food_.front().pos;
|
||||
for (const auto &food : food_) {
|
||||
if ((pos - food.pos).len_sq() < (pos - target).len_sq()) {
|
||||
target = food.pos;
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
private:
|
||||
void generate() {
|
||||
++current_gen_;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "Snake.hpp"
|
||||
|
||||
void Worm::add(Veci pos) {
|
||||
void Snake::add(Veci pos) {
|
||||
this->pos = pos;
|
||||
track_.push_back(pos);
|
||||
if (track_.size() > length) {
|
||||
|
|
@ -8,13 +8,13 @@ void Worm::add(Veci pos) {
|
|||
}
|
||||
}
|
||||
|
||||
void Worm::draw(Veci offset) const {
|
||||
void Snake::draw(Veci offset) const {
|
||||
for (const auto &pos : track_) {
|
||||
paint::circle({{.pos = pos - offset, .color = color}, radius});
|
||||
}
|
||||
}
|
||||
|
||||
bool Worm::touches(const Worm &other) {
|
||||
bool Snake::touches(const Snake &other) {
|
||||
int dist = radius + other.radius;
|
||||
for (const auto &elem : other.track_) {
|
||||
if ((pos - elem).len_sq() < dist * dist) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue