-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
138 lines (121 loc) · 2.2 KB
/
Copy pathBall.cpp
File metadata and controls
138 lines (121 loc) · 2.2 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
#include "Ball.h"
#include <stdio.h>
Ball::Ball()
{
mTimer = Timer::Instance();
mBall.x = 1024 / 2;
mBall.y = 768 / 2;
mBall.w = 15;
mBall.h = 15;
mSpeedX = 5.0f;
mSpeedY = 5.0f;
Upbounds = 133;
Downbounds = 753;
leftBounds = -15;
rightBounds = 1039;
}
Ball::~Ball()
{
Timer::Release();
mTimer = NULL;
}
void Ball::HandleMovement(Player& player1, Player& player2)
{
mBall.x -= mSpeedX;
mBall.y += mSpeedY;
//Checks for collision Twice, If top side collision, change mSpeedY
if (CheckCollisionSide(player1) || CheckCollisionSide(player2))
{
if (CheckCollisionTop(player1) || CheckCollisionTop(player2))
{
mSpeedY = mSpeedY * -1.0f;
mSpeedY *= 1.1f;
mSpeedX *= 1.1f;
}
mSpeedX = mSpeedX * -1.0f;
mSpeedX *= 1.1f;
mSpeedY *= 1.1f;
}
else if (mBall.y < Upbounds)
{
mBall.y = Upbounds;
mSpeedY = mSpeedY * -1.0f;
}
else if (mBall.y > Downbounds)
{
mBall.y = Downbounds;
mSpeedY = mSpeedY * -1.0f;
}
if (mBall.x > rightBounds)
{
player1.AddScore();
mSpeedX = 5.0f;
mSpeedY = 5.0f;
mBall.x = 1024 / 2;
}
if (mBall.x < leftBounds)
{
player2.AddScore();
mSpeedX = 5.0f;
mSpeedY = 5.0f;
mBall.x = 1024 / 2;
}
}
//Box Box Collision
bool Ball::CheckCollisionSide(Player& player)
{
//Sides of player1 and ball
int leftA, leftC;
int rightA, rightC;
int topA, topC;
int bottomA, bottomC;
//Sides for player1
leftA = player.mPlayer.x;
rightA = player.mPlayer.x + player.mPlayer.w;
topA = player.mPlayer.y;
bottomA = player.mPlayer.y + player.mPlayer.h;
//Sides for Ball
leftC = mBall.x;
rightC = mBall.x + mBall.w;
topC = mBall.y;
bottomC = mBall.y + mBall.h;
if (bottomA <= topC)
{
return false;
}
if (topA >= bottomC)
{
return false;
}
if (rightA <= leftC)
{
return false;
}
if (leftA >= rightC)
{
return false;
}
return true;
}
bool Ball::CheckCollisionTop(Player& player)
{
int topA, topC;
int bottomA, bottomC;
topA = player.mPlayer.y;
bottomA = player.mPlayer.y + player.mPlayer.h;
topC = mBall.y;
bottomC = mBall.y + mBall.h;
if (bottomA <= topC)
{
return false;
}
if (topA >= bottomC)
{
return false;
}
return true;
}
void Ball::Update(Player& player1, Player& player2)
{
HandleMovement(player1, player2);
}