Repository navigation
Expand file tree
/
Copy pathPlayer.cpp
More file actions
226 lines (176 loc) · 5.45 KB
/
Copy pathPlayer.cpp
File metadata and controls
226 lines (176 loc) · 5.45 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "Player.h"
Player::Player(GameMechs* thisGMRef)
{
mainGameMechsRef = thisGMRef;
currentdir = STOP;
playerPosList = new objPosArrayList(); //creates initial snake body list
objPos headPos (mainGameMechsRef->getBoardSizeX()/2, mainGameMechsRef->getBoardSizeY() / 2, '@'); //initializes snake position
playerPosList->insertHead(headPos);
}
Player::~Player()
{
delete playerPosList;
}
objPosArrayList* Player::getPlayerPos() const
{
return playerPosList; //returns the snake array list
}
void Player::updatePlayerDir() //tracks player direction
{
char input = mainGameMechsRef->getInput();
if(input!=0)
{
switch(input)
{
case 'W': //UP
case 'w':
if (currentdir != DOWN && currentdir != UP)
{
currentdir = UP;
}
break;
case 'A': //LEFT
case 'a':
if (currentdir != RIGHT && currentdir != LEFT)
{
currentdir = LEFT;
}
break;
case 'S': //DOWN
case 's':
if (currentdir != UP && currentdir != DOWN)
{
currentdir = DOWN;
}
break;
case 'D': //RIGHT
case 'd':
if (currentdir != LEFT && currentdir != RIGHT)
{
currentdir = RIGHT;
}
break;
case 'E': //INCREASES SPEED
case 'e':
(mainGameMechsRef->setSpeed('+'));
break;
case 'Q': //DECREASES SPEED
case 'q':
(mainGameMechsRef->setSpeed('-'));
break;
case 'f':
mainGameMechsRef->generateFood(getPlayerPos());
break;
case 27:
mainGameMechsRef->setExitTrue();
break;
default:
break;
}
mainGameMechsRef->clearInput();
}
}
void Player::movePlayer()
{
// Finite State Machine logic
if(currentdir != STOP)
{
int height = mainGameMechsRef->getBoardSizeY();
int width = mainGameMechsRef->getBoardSizeX();
//get the current head position
objPos temphead = getPlayerPos()->getHeadElement();
switch (currentdir)
{
case UP:
if (temphead.pos->y > 1) //as soon as the character goes to border it starts again at the bottom
{
temphead.pos->y --;
}
else
{
temphead.pos->y = height- 2;
}
break;
case DOWN:
if (temphead.pos->y < height-2) //as soon as the character goes to border it starts again at the top
{
temphead.pos->y ++;
}
else
{
temphead.pos->y = 1;
}
break;
case LEFT:
if (temphead.pos->x > 1) //as soon as the character goes to border it starts again at the right
{
temphead.pos->x --;
}
else
{
temphead.pos->x = width - 2;
}
break;
case RIGHT:
if (temphead.pos->x < width-2) //as soon as the character goes to border it starts again at the left
{
temphead.pos->x ++;
}
else
{
temphead.pos->x = 1;
}
break;
}
//inserts the new head
getPlayerPos()->insertHead(temphead);
//check for collision
if (checkSelfCollision())
{
mainGameMechsRef->setLoseFlag();
mainGameMechsRef->setExitTrue();
}
else //no self collision
{
//checks for food consumption
if (checkFoodConsumption())
{
increasePlayerLength();
}
else
{
getPlayerPos()->removeTail();
}
}
}
}
bool Player::checkFoodConsumption()
{
//get the player head
objPos head = getPlayerPos()->getHeadElement();
//get food position
objPos food = mainGameMechsRef->getFoodPos();
//checks for collision
if (head.pos->x == food.pos->x && head.pos->y == food.pos->y)
{
return true; //food consumed
}
return false; //if no food consumed
}
void Player::increasePlayerLength()
{
mainGameMechsRef->generateFood(getPlayerPos()); //generates new food
mainGameMechsRef->incrementScore(); //update the score
}
bool Player::checkSelfCollision() //returns true if collision will occur
{
objPos head = getPlayerPos()->getHeadElement();
for (int i = 1; i < getPlayerPos()->getSize(); i++)
{
if (head.pos->x == getPlayerPos()->getElement(i).pos->x && head.pos->y == getPlayerPos()->getElement(i).pos->y)
{
return true;
}
}
return false; //if head element doesnt collide with any body elements, then no collision will occur
}