forked from simplerr/Banzai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
251 lines (219 loc) · 4.97 KB
/
Player.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "Player.h"
#include <time.h>
#include "common\DirectInput.h"
#include "Input.h"
#include "Player.h"
#include "Board.h"
#include "Piece.h"
#include "Input.h"
#include "LocalState.h"
#include "Sound.h"
//! Constructor.
Player::Player(Color color)
{
mSelectedPiece = NULL;
mColor = color;
mName = "#NOVALUE";
mOpponent = "#NOVALUE";
mBoard = NULL;
srand(time(0));
}
//! Destructor.
Player::~Player()
{
}
//! Updates components.
/**
@note Get overriden.
*/
void Player::update(float dt)
{
}
//! Draws components.
/**
@note Get overriden.
*/
void Player::draw()
{
mBoard->draw();
}
//! Finds out what the result of the players action is.
/**
@return The result of the action the player made. WRONG_COLOR, PIECE_SELECTED, PIECE_MOVED, INVALID_POSITION.
*/
ActionResult Player::performAction()
{
// Translate mouse coordinate to sqaure coordinates
Position pos = mBoard->toGridPos(gInput->mousePosition());
ActionResult action;
bool moved = false;
// No piece is selected
if(gInput->keyPressed(VK_LBUTTON) && mSelectedPiece == NULL)
{
Piece* piece = mBoard->getPieceAt(pos.x, pos.y);
// A piece was pressed
if(piece != NULL)
{
// The piece has another color than the players
if(piece->getColor() != getColor()) {
// Return wrong color msg
action = ActionResult(WRONG_COLOR, pos);
gSound->playEffect(ILLEGAL_SOUND);
}
else {
mSelectedPiece = piece;
mBoard->setSelected(pos.x, pos.y);
// Return piece selected msg
action = ActionResult(PIECE_SELECTED, pos);
}
}
}
// A piece is already selected
else if(gInput->keyPressed(VK_LBUTTON) && mSelectedPiece != NULL)
{
Piece* piece = mBoard->getPieceAt(pos.x, pos.y);
Position oldPos = mSelectedPiece->getPos();
// Was it a piece of the same color that was pressed? - Change the selected piece
if(piece != NULL && piece->getColor() == getColor())
{
mSelectedPiece = piece;
action = ActionResult(PIECE_SELECTED, pos);
}
// Didn't select a piece of the same color as self
else
{
// The piece can move to the pressed location
if(mBoard->validMove(mSelectedPiece, pos.x, pos.y))
{
// A piece was captured
if(piece != NULL) {
handleCapture(piece->getColor(), piece->getType());
mBoard->removePiece(piece);
pieceCapturedSound();
}
// Return piece moved msg
action = ActionResult(PIECE_MOVED, pos, mSelectedPiece->getPos());
// Update the selected piece's position
mSelectedPiece->setPos(pos.x, pos.y);
mSelectedPiece->moved();
// Was it castling?
if(mSelectedPiece->getType() == KING && abs(mSelectedPiece->getPos().x - oldPos.x) > 1) {
handleCastling(mSelectedPiece);
gSound->playEffect(CASTLE_SOUND);
}
else
pieceMovedSound();
mSelectedPiece = NULL;
}
// Can't move to the pressed location
else
{
// Return invalid position or checked msg
if(mBoard->resultsInCheck(mSelectedPiece, pos.x, pos.y))
action = ActionResult(GETS_CHECKED, mBoard->getPiece(KING, mSelectedPiece->getColor())->getPos());
else {
if(pos.x >= 0 && pos.x <= 7 && pos.y >= 0 && pos.y <= 7)
action = ActionResult(INVALID_POSITION, pos);
else
action = ActionResult(PIECE_SELECTED, Position(-10, -10));
}
gSound->playEffect(ILLEGAL_SOUND);
mSelectedPiece = NULL;
}
}
}
// Return the action
return action;
}
//! Get the color.
Color Player::getColor()
{
return mColor;
}
//! Handle castling.
/**
@note Gets overriden.
*/
void Player::handleCastling(Piece* king)
{
}
//! Handle capturing.
/**
@note Gets overriden.
*/
void Player::handleCapture(Color color, PieceType type)
{
}
//! Set the board.
void Player::setBoard(Board* board)
{
mBoard = board;
}
//! Sets the color.
void Player::setColor(Color color)
{
mColor = color;
}
//! Set on which sqaure to draw the green selected piece effect.
void Player::setSelected(int x, int y)
{
mBoard->setSelected(x, y);
}
//! Set on which square to draw the red invalid move effect.
void Player::setInvalid(int x, int y)
{
mBoard->setInvalid(x, y);
}
//! Ask the board if it's check mate.
bool Player::getCheckMate()
{
return mBoard->checkMate(getColor());
}
//! Reset the board and restore all pieces.
void Player::resetBoard()
{
mBoard->reset();
}
//! Sets the player name.
void Player::setName(string name)
{
mName = name;
}
//! Get the player name.
string Player::getName()
{
return mName;
}
//! Set the opponent name.
void Player::setOpponent(string name)
{
mOpponent = name;
}
//! Returns the name of the opponent.
string Player::getOpponent()
{
return mOpponent;
}
//! Returns the board.
Board* Player::getBoard()
{
return mBoard;
}
void Player::pieceMovedSound()
{
int random = rand() % 2;
if(random == 0)
gSound->playEffect(MOVE1_SOUND);
else
gSound->playEffect(MOVE2_SOUND);
}
void Player::pieceCapturedSound()
{
int random = rand() % 3;
if(random == 0)
gSound->playEffect(CAPTURE1_SOUND);
else if(random == 1)
gSound->playEffect(CAPTURE2_SOUND);
else if(random == 2)
gSound->playEffect(CAPTURE3_SOUND);
}