-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
184 lines (146 loc) · 4.59 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Snake.h"
#include <iostream>
Snake::Snake():initialized(false),linksUsed(0),dead(false),appendNum(SNAKE_HEAD_SIZE){
}
Snake::~Snake(){
}
void Snake::initialize(Game *game, int x, int y, const char *headTexture, const char *linkTexture){
// checks for initialization
if(initialized)
throw GameError::exception("Snake was already initialized");
// makes sure we can make the textures
if (!this->linkTexture.initialize(game->getGraphics(), linkTexture))
throw GameError::exception("SnakeLink texture could not be initialized");
if (!this->headTexture.initialize(game->getGraphics(), headTexture))
throw GameError::exception("Snake Head texture could not be initialized");
defaultX = x;
defaultY = y;
// initializes all of the links we will use
for ( int i = 0; i < SNAKE_MAX_LENGTH; i++){
//Image* sprite = &links[i];
if(i < SNAKE_HEAD_SIZE && !links[i].initialize(game, this->headTexture.getWidth(),this->headTexture.getHeight(), 1, &this->headTexture))
throw GameError::exception("Snake link not able to initialize");
if(i >= SNAKE_HEAD_SIZE && !links[i].initialize(game, this->linkTexture.getWidth(),this->linkTexture.getHeight(), 1, &this->linkTexture))
throw GameError::exception("Snake link not able to initialize");
links[i].setScale( BOARD_CELL_HEIGHT / (float)(i < SNAKE_HEAD_SIZE ? this->headTexture : this->linkTexture).getHeight());
updateLink(links[i], defaultX, defaultY);
}
links[linksUsed].setVisible(true);
links[linksUsed].setActive(true);
updateLink(links[linksUsed++], defaultX, defaultY);
appendNum--;
this->initialized = true;
}
Snake::Link::Link(): Entity(), boardX(0),boardY(0){
edge = SNAKE_LINK_RECT;
spriteData.rect = SNAKE_LINK_RECT;
radius = SNAKE_LINK_RADIUS;
mass = SNAKE_LINK_MASS;
collisionType = entityNS::CIRCLE;
}
void Snake::wipe(){
isInitialized();
for ( int i = 1; i < linksUsed; i++){
links[i].setVisible(false);
links[i].setActive(false);
}
appendNum = SNAKE_HEAD_SIZE - 1;
linksUsed = 1;
updateLink(links[0], defaultX, defaultY);
dead = false;
}
void Snake::move(){
isInitialized();
VECTOR2 movement(0,0);//int Xmovement = 0, Ymovement = 0;
int degrees=0;
int currentLink = 0;
// demo function showing how append would be
if(appendNum > 0 && linksUsed < SNAKE_MAX_LENGTH){
if(linksUsed < SNAKE_HEAD_SIZE)
links[linksUsed].setActive(true);
links[linksUsed++].setVisible(true);
appendNum--;
}
switch(movementDir){
case(Right):
movement.x++;
degrees = 90;
break;
case(Left):
movement.x--;
degrees = 270;
break;
case(Up):
movement.y--;
degrees = 0;
break;
case(Down):
movement.y++;
degrees = 180;
break;
}
VECTOR2 next(movement.x + links[0].boardX, movement.y + links[0].boardY);
if(next.x < 0 || next.x >= BOARD_WIDTH || next.y < 0 || next.y >= BOARD_HEIGHT)
dead = true;
for(int currentLink = linksUsed - 1; currentLink > 0; --currentLink){
// update x/y in accordance with the next link in the chain
if(links[currentLink - 1].boardX == next.x && links[currentLink - 1].boardY == next.y)
dead = true;
links[currentLink].setVelocity(VECTOR2(links[currentLink].boardX - links[currentLink - 1].boardX,
links[currentLink].boardY - links[currentLink - 1].boardY));
updateLink(links[currentLink], links[currentLink-1].boardX, links[currentLink-1].boardY);
links[currentLink].setDegrees(links[currentLink-1].getDegrees());
}
updateLink(links[0], next.x, next.y);
links[0].setDegrees(degrees);
}
void Snake::append(UINT toAdd){
appendNum += toAdd;
}
Direction Snake::getMovementDirection(){
isInitialized();
return movementDir;
}
void Snake::setMovementDirection(Direction newDir){
isInitialized();
movementDir = newDir;
}
void Snake::draw(){
isInitialized();
for( int i = 0; i < linksUsed && links[i].getVisible(); ++i){
links[i].draw();
}
}
Entity** Snake::getEntities(){
Entity **ret = new Entity *[SNAKE_HEAD_SIZE];
for(int currentHead = 0; currentHead < SNAKE_HEAD_SIZE; currentHead++){
ret[currentHead] = &links[currentHead];
}
return ret;
}
void Snake::setDead(bool state){
dead = state;
}
bool Snake::isDead() const{
return dead;
}
void Snake::onLostDevice(){
isInitialized();
headTexture.onLostDevice();
linkTexture.onLostDevice();
}
void Snake::onResetDevice(){
isInitialized();
headTexture.onResetDevice();
linkTexture.onResetDevice();
}
inline void Snake::isInitialized(){
if(!this->initialized)
throw GameError::exception("Snake is not initialized");
}
void Snake::updateLink(Link &input, int newX, int newY){
input.boardX = newX;
input.boardY = newY;
input.setX(newX * BOARD_CELL_WIDTH);
input.setY(newY * BOARD_CELL_HEIGHT);
}