-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
214 lines (161 loc) · 4.86 KB
/
Copy pathPlayer.cpp
File metadata and controls
214 lines (161 loc) · 4.86 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
#include "Player.h"
#include "objPosArrayList.h"
Player::Player(GameMechs* thisGMRef, Food *foodRef)
{
mainGameMechsRef = thisGMRef;
myDir = STOP;
food = foodRef;
// more actions to be included
playerPosList = new objPosArrayList();
playerPosList->insertHead(objPos(20, 10, '*')); // Starting Position in the middle of the board
}
Player::~Player()
{
// delete any heap members here
delete playerPosList;
playerPosList = nullptr;
}
objPos Player::getPlayerPos() const
{
// return the reference to the playerPos arrray list
return playerPosList->getHeadElement();
}
void Player::updatePlayerDir()
{
// PPA3 input processing logic
char input = mainGameMechsRef->getInput(); // Get input from game mechanisms
if (input == 27) { // ASCII code for Escape key
mainGameMechsRef->setExitTrue(); // Ends the game
}
switch (input) //FSM for the snake movement
{
case 'w': case 'W':
if (myDir != DOWN) myDir = UP;
break;
case 's': case 'S':
if (myDir != UP) myDir = DOWN;
break;
case 'a': case 'A':
if (myDir != RIGHT) myDir = LEFT;
break;
case 'd': case 'D':
if (myDir != LEFT) myDir = RIGHT;
break;
default:
break; // Ignore invalid inputs
}
mainGameMechsRef->clearInput(); // Clear input after processing
}
void Player::movePlayer()
{
if (myDir == STOP) return; // Do nothing if direction is STOP
objPos newHead = playerPosList->getHeadElement(); // Get current head position
// Update head position based on direction
switch (myDir)
{
case UP: newHead.pos->y--; break;
case DOWN: newHead.pos->y++; break;
case LEFT: newHead.pos->x--; break;
case RIGHT: newHead.pos->x++; break;
default: break;
}
// Handles wraparound
if (newHead.pos->x < 1)
{
newHead.pos->x = BOARD_X - 2;
}
else if (newHead.pos->x > BOARD_X - 2)
{
newHead.pos->x = 1;
}
if (newHead.pos->y < 1)
{
newHead.pos->y = BOARD_Y - 2;
}
else if (newHead.pos->y > BOARD_Y - 2)
{
newHead.pos->y = 1;
}
// Add the new head to the front of the snake
playerPosList->insertHead(newHead);
// Remove the tail only if the snake is not growing
if (!isGrowing)
{
playerPosList->removeTail();
}
else
{
isGrowing = false; // Reset the growth flag
}
moveCount++; // Increment move count
// Check for food and self-collision
foodCollisionCheck(*food);
selfCollisionCheck();
}
// More methods to be added
// Check for collision with food
void Player::foodCollisionCheck(const Food& foodRef)
{
objPosArrayList* foodList = food->getFood();
objPos head = playerPosList->getHeadElement();
for (int i = 0; i < foodList->getSize(); ++i)
{
objPos foodPos = foodList->getElement(i);
if (head.isPosEqual(&foodPos))
{
if (foodPos.getSymbol() == 'S') // Special food detected
{
mainGameMechsRef->incrementScore(5); // Bonus points (5)
// Reset the snake to only the head
while (playerPosList->getSize() > 1)
{
playerPosList->removeTail();
}
}
else // Normal food
{
mainGameMechsRef->incrementScore(1); // Normal food score (1)
isGrowing = true; // Allow the snake to grow
}
food->generateFood(playerPosList); // Generate new food
return;
}
}
}
// Check for self-collision
void Player::selfCollisionCheck()
{
objPos head = playerPosList->getHeadElement(); // Get the head of the snake
// Start checking from the second element because the head is already at index 0
for (int i = 1; i < playerPosList->getSize(); ++i)
{
objPos bodyElement = playerPosList->getElement(i); // Get each body segment
if (head.isPosEqual(&bodyElement))
{
// If the head collides with any body segment, game over
mainGameMechsRef->setLoseFlag(); // Set the lose flag in GameMechs
return;
}
}
}
// Update player's speed
void Player::updatePlayerSpeed()
{
int currentSpeed = mainGameMechsRef->getSpeed();
mainGameMechsRef->setSpeed(currentSpeed + 1);
}
// Update player's delay
void Player::updatePlayerDelay()
{
int currentDelay = mainGameMechsRef->getDelay();
mainGameMechsRef->setDelay(currentDelay - 5000); //reduces delay, increasing game speed
}
objPosArrayList* Player::getPlayerPosList() const {
return playerPosList;
}
int Player::getMoveCount() const {
return moveCount;
}
Player::Dir Player::getDirection() const {
return myDir;
}