-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.h
168 lines (151 loc) · 4.32 KB
/
Snake.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef SNAKE_H_INCLUDED
#define SNAKE_H_INCLUDED
#include "Random.h"
#include "SnakeBrain.h"
#include "Sprite.h"
#include "AnimatedSprite.h"
#include "Node.h"
//#include <iostream>
//using namespace std;
class Snake
{
public:
Snake();
Snake(Node node);
Snake(Vector2df vec);
void eat(Node* segment);
void mutation(); // Mutate the nodes of this snake.
void setHeadColour(CRuint colour);
uint getHeadColour(){return nodes[0].getColour();};
void setVelocityX(CRint x){velocity.x = x;};
void setVelocityX(const float& x){velocity.x = x;};
void setVelocityY(CRint y){velocity.y = y;};
void setVelocityY(const float& y){velocity.y = y;};
void setVelocityXY(CRint x, CRint y)
{
velocity.x = x;
velocity.y = y;
}
void setVelocityXY(const Vector2df& vel){velocity = vel;};
Vector2df getXY(){return nodes[0].getPos();};
Fixed getX(){return nodes[0].getX();};
Fixed getY(){return nodes[0].getY();};
void eraseNode(CRuint i)
{
if(i >= nodes.size())
return;
nodes.erase(nodes.begin() + i);
oldPos.erase(oldPos.begin() + i);
}
Node& getNode(CRuint i){return nodes[i];};
void render(Sprite &red, Sprite &green, Sprite &blue);
void render(AnimatedSprite &red, AnimatedSprite &green, AnimatedSprite &blue);
void update()
{
if(nodes.size() == 0)
return;
// Update oldpositions
uint iMax = (uint)oldPos.size();
for (int i = iMax-1; i >= 0; i--)
oldPos[i] = nodes[i].getPos();
iMax = (uint)nodes.size();
// Adjust the positions of the nodes
// Use powers to adjust speed
if(bluePower > 1)
{
// Check x direction
float vel = velocity.x;
if(vel == -1 || vel == 1)
velocity.x = vel * bluePower;
// Check y direction
vel = velocity.y;
if(vel == -1 || vel == 1)
velocity.y = vel * bluePower;
velocity *= 0.8f;
}
for(int i = iMax-1; i >= 1; i--)
nodes[i].updateXY(velocity);
nodes[0].updateXY(velocity);
// Then shuffle the positions along
for(uint i=1; i < iMax; i++)
{
nodes[i].setX(oldPos[i-1].x );
nodes[i].setY(oldPos[i-1].y );
}
// Check AI stuff
if(brain.getMode() != NONE)
{
brain.update(waypoint, nodes[0].getPos(), oldPos[0]);
calcNewVelocity();
}
}
void clear()
{
nodes.clear();
oldPos.clear();
redPower = 0;
greenPower = 0;
bluePower = 0;
Vector2df v(0,0);
nodes.push_back(Node(v));
oldPos.push_back(v);
}
size_t size()const{return nodes.size();};
uint getChildCreateLevel(){return childCreateLevel;};
void setChildCreateLevel(uint lvl){childCreateLevel = lvl;};
Vector2df getVelocity(){return velocity;};
uint findStrongestType();
uint getPower(CRuint colour);
void setWaypoint(const Vector2df& p){waypoint = p;};
void setWaypoint(Vector2di p){waypoint = p;};
void setMode(CRuint m){brain.setMode(m);};
void disableBrain();
uint getAIMode(){return brain.getMode();};
void setPosition(Vector2df v){nodes.at(0).setPos(v);}
private:
// Disabled to revent double freed pointers.
Snake(const Snake& BlingRef);
Snake& operator=(const Snake& BlingRef);
void calcNewVelocity()
{
uint dir = brain.getDirection();
if(dir == UP_LEFT)
{
velocity.x = -1;
velocity.y = -1;
}
else if(dir == UP_RIGHT)
{
velocity.x = 1;
velocity.y = -1;
}
else if(dir == DOWN_LEFT)
{
velocity.x = -1;
velocity.y = 1;
}
else if(dir == DOWN_RIGHT)
{
velocity.x = 1;
velocity.y = 1;
}
else if(dir == UP)
velocity.y = -1;
else if(dir == DOWN)
velocity.y = 1;
else if(dir == LEFT)
velocity.x =-1;
else if(dir == RIGHT)
velocity.x = 1;
}
SnakeBrain brain;
Vector2df velocity;
Vector2df waypoint;
vector<Node> nodes;
vector<Vector2df> oldPos;
uint redPower;
uint greenPower;
uint bluePower;
uint childCreateLevel; // This is the level needed to create a baby snake
};
#endif // SNAKE_H_INCLUDED