-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
119 lines (107 loc) · 3.76 KB
/
Copy pathPlayer.cpp
File metadata and controls
119 lines (107 loc) · 3.76 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
#include "Player.h"
Player::Player(){
//
}
Player::Player(string name, bool side){
this->name = name;
this->side = side;
Rectangle temp;
temp.width = 20;
temp.height = SCREEN_HEIGHT/8;
temp.y = SCREEN_HEIGHT/2 - temp.height/2;
temp.x = (this->side == LEFT_SIDE) ? 5 : SCREEN_WIDTH - 5 - temp.width;
this->rec = temp;
this->recColor = (this->side == LEFT_SIDE) ? BLUE : RED;
this->score = 0;
this->recSpeed = DEFAULT_REC_SPEED;
this->isControlInverted = false;
}
void Player::inputHandler(Player *leftPlayer, Player *rightPlayer){
// if(this->side == LEFT_SIDE){
// if(IsKeyDown(KEY_W) && this->rec.y>0){
// this->rec.y-=this->recSpeed;
// if(this->rec.y < 0) this->rec.y = 0;
// }
// if(IsKeyDown(KEY_S) && this->rec.y<768){
// this->rec.y+=this->recSpeed;
// if(this->rec.y > 768) this->rec.y = 768;
// }
// }
// else{
// if(IsKeyDown(KEY_UP) && this->rec.y>0){
// this->rec.y-=this->recSpeed;
// if(this->rec.y < 0) this->rec.y = 0;
// }
// if(IsKeyDown(KEY_DOWN) && this->rec.y<768){
// this->rec.y+=this->recSpeed;
// if(this->rec.y > 768) this->rec.y = 768;
// }
// }
// leftPlayer input handler
if(!leftPlayer->isControlInverted) {
if(IsKeyDown(KEY_W) && leftPlayer->rec.y>0){
leftPlayer->rec.y-=leftPlayer->recSpeed;
if(leftPlayer->rec.y < 0) leftPlayer->rec.y = 0;
}
if(IsKeyDown(KEY_S) && leftPlayer->rec.y<768){
leftPlayer->rec.y+=leftPlayer->recSpeed;
if(leftPlayer->rec.y > 768 - leftPlayer->rec.height) leftPlayer->rec.y = 768 - leftPlayer->rec.height;
}
}
else {
if(IsKeyDown(KEY_S) && leftPlayer->rec.y>0){
leftPlayer->rec.y-=leftPlayer->recSpeed;
if(leftPlayer->rec.y < 0) leftPlayer->rec.y = 0;
}
if(IsKeyDown(KEY_W) && leftPlayer->rec.y<768){
leftPlayer->rec.y+=leftPlayer->recSpeed;
if(leftPlayer->rec.y > 768 - leftPlayer->rec.height) leftPlayer->rec.y = 768 - leftPlayer->rec.height;
}
}
//right player
if(!rightPlayer->isControlInverted) {
if(IsKeyDown(KEY_UP) && rightPlayer->rec.y>0){
rightPlayer->rec.y-=rightPlayer->recSpeed;
if(rightPlayer->rec.y < 0) rightPlayer->rec.y = 0;
}
if(IsKeyDown(KEY_DOWN) && rightPlayer->rec.y<768){
rightPlayer->rec.y+=rightPlayer->recSpeed;
if(rightPlayer->rec.y > 768 - rightPlayer->rec.height) rightPlayer->rec.y = 768 - rightPlayer->rec.height;
}
}
else {
if(IsKeyDown(KEY_DOWN) && rightPlayer->rec.y>0){
rightPlayer->rec.y-=rightPlayer->recSpeed;
if(rightPlayer->rec.y < 0) rightPlayer->rec.y = 0;
}
if(IsKeyDown(KEY_UP) && rightPlayer->rec.y<768){
rightPlayer->rec.y+=rightPlayer->recSpeed;
if(rightPlayer->rec.y > 768 - rightPlayer->rec.height) rightPlayer->rec.y = 768 - rightPlayer->rec.height;
}
}
}
bool Player::playerWon(Ball ball){
if(this->side == RIGHT_SIDE){
if(ball.position.x - ball.radius <= 0){
return true;
}
}
else{
if(ball.position.x + ball.radius >= SCREEN_WIDTH){
return true;
}
}
return false;
}
void Player::resetWin(Player *p1, Player *p2) {
p1->name.clear();
p2->name.clear();
p1-> score = 0;
p2-> score = 0;
p1->rec.y = SCREEN_HEIGHT/2 - SCREEN_HEIGHT/16;
p2->rec.y = SCREEN_HEIGHT/2 - SCREEN_HEIGHT/16;
p1->rec.height = SCREEN_HEIGHT/8;
p2->rec.height = SCREEN_HEIGHT/8;
p1->recSpeed = DEFAULT_REC_SPEED;
p2->recSpeed = DEFAULT_REC_SPEED;
}