mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2026-01-02 11:38:16 +00:00
Better code structure: snake game object, player game object, bot game object
This commit is contained in:
parent
a92c96f91e
commit
421574ab40
6 changed files with 155 additions and 100 deletions
|
|
@ -1,11 +1,48 @@
|
|||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
#include "Snake.hpp"
|
||||
|
||||
struct Player {
|
||||
Vecf pos;
|
||||
Vecf direction;
|
||||
double speed;
|
||||
double move_interval;
|
||||
double move_time_delta;
|
||||
// struct Player {
|
||||
// Vecf pos;
|
||||
// Vecf direction;
|
||||
// double speed;
|
||||
// double move_interval;
|
||||
// double move_time_delta;
|
||||
// };
|
||||
|
||||
class Player : public Snake {
|
||||
public:
|
||||
using Snake::Snake;
|
||||
|
||||
void act(float dt) override {
|
||||
int eaten = map_.eat(
|
||||
utils::to_world_coord(get_pos() + utils::get_screen_center()), 20);
|
||||
inc_length(eaten);
|
||||
|
||||
change_direction(dt);
|
||||
|
||||
move(dt);
|
||||
}
|
||||
|
||||
void draw(Veci offset) override {
|
||||
Snake::draw(offset - utils::get_screen_center());
|
||||
}
|
||||
|
||||
protected:
|
||||
void change_direction(float) override {
|
||||
Veci cursor = utils::get_cursor();
|
||||
|
||||
if (cursor != prev_cursor_ and utils::is_on_screen(cursor)) {
|
||||
Vecf diff(cursor - utils::get_screen_center()); // - pos;
|
||||
|
||||
if (diff.len() > MIN_CONTROL_DISTANCE) {
|
||||
direction_ = diff.norm();
|
||||
}
|
||||
}
|
||||
|
||||
prev_cursor_ = cursor;
|
||||
}
|
||||
|
||||
protected:
|
||||
Veci prev_cursor_ = {};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue