-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cpp
98 lines (78 loc) · 1.79 KB
/
Snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <cassert>
#include <cstdlib>
#include "Snake.hpp"
#include "Functions.hpp"
#include "Aimer.hpp"
#include "Router.hpp"
#include "State.hpp"
Snake::Snake()
{
objects.push_back(
{
Ball(
WIDTH >> 1,
HEIGHT >> 1
)
}
);
IMG_TARGET.load(FOLDER + "textures/TARGET.tga");
for(int i = 0; i < NO_ICONSETS; ++i) {
skins[i].head.load(FOLDER + "textures/" + std::to_string(i) + "_HEAD.tga");
skins[i].body.load(FOLDER + "textures/" + std::to_string(i) + "_BODY.tga");
skins[i].tail.load(FOLDER + "textures/" + std::to_string(i) + "_TAIL.tga");
}
aimer = new Aimer(this);
router = new Router(aimer);
}
Snake::~Snake() {
delete aimer;
delete router;
}
const Ball &Snake::head() const {
return objects.front();
}
const Ball &Snake::tail() const {
return objects.back();
}
Ball Snake::GetAutoStep() {
aimer->Reset();
Ball step = router->GetStep();
if(safe_walk) {
Ball overhead = objects.front() + step;
if(Ball::InSegment(overhead, objects)
|| Ball::InSegment(overhead, WALLS->GetObjects()))
{
int
bu_aim = aimer->aim,
bu_route = router->route;
aimer->aim = 1;
router->route = 4;
step = router->GetStep();
aimer->aim = bu_aim;
router->route = bu_route;
}
}
targetLast = aimer->GetTarget();
return step;
}
void Snake::SetStep(Ball step) {
if(step == -previousDirection && objects.size() != 1)
return;
assert(step.is_valid_step());
assert((head() + step).is_valid_position());
currentDirection = step;
}
void Snake::DoStep() {
for(size_t i = objects.size() - 1; i != 0; --i)
objects[i] = objects[i - 1];
objects[0] += currentDirection;
if(growNextMove) {
objects.push_back(snakeLast);
growNextMove = false;
}
previousDirection = currentDirection;
}
void Snake::PushBack() {
snakeLast = objects.back();
growNextMove = true;
}