-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
134 lines (122 loc) · 2.82 KB
/
Ball.cpp
File metadata and controls
134 lines (122 loc) · 2.82 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
#include "Ball.h"
#include "Datastore.h"
void Ball::Draw(ID2D1HwndRenderTarget* renderTarget) {
// create brush
ID2D1SolidColorBrush* b;
HRESULT res = renderTarget->CreateSolidColorBrush(col, &b);
// draw ellipse at x, y with radius r
D2D1_ELLIPSE ellipse = D2D1::Ellipse(D2D1::Point2F(x, y), r, r);
renderTarget->FillEllipse(ellipse, b);
// release brush
b->Release();
b = NULL;
}
void Ball::Update() {
if (doneCounter < 100) doneCounter--;
if (held) {
y = ds->bat->y - r;
x = ds->bat->x + ds->bat->width / 2;
return;
}
// drift to right?
//xSpeed += 0.1f;
// die
if (top() > ds->carea.bottom + 250.0f) {
ds->control->Die();
}
// bounce of right wall
if (right() > ds->carea.right) {
x = ds->carea.right - r;
xSpeed = -xSpeed;
}
// bounce of left wall
else if (left() < 0) {
x = r;
xSpeed = -xSpeed;
}
// bounce of top wall
if (top() < 0) {
y = r;
ySpeed = -ySpeed;
}
// bounce of bat
else if (
bottom() > ds->bat->y
&& top() < ds->bat->y + ds->bat->height
&& right() > ds->bat->x
&& left() < ds->bat->x + ds->bat->width
) {
y = ds->bat->y - r;
ySpeed = -ySpeed;
// set x speed based off where the ball hit
const float force = 8.0f;
const float dist = ds->bat->width / 2;
const float center = ds->bat->x + dist;
const float pt = x - center;
xSpeed = (pt / dist) * force;
const float absXSpeed = xSpeed > 0 ? xSpeed : -xSpeed;
const float newYSpeed = -(force - absXSpeed);
//ySpeed = newYSpeed > -1.5f ? -1.5f : newYSpeed;
}
else HitBricks();
// update position
x += xSpeed;
y += ySpeed;
}
void Ball::HitBricks() {
Brick* cb = ds->bricks[0];
while (cb != NULL) {
if (
!cb->broken
&& top() < cb->bottom()
&& bottom() > cb->top()
&& left() < cb->right()
&& right() > cb->left()
) {
float spdRatio = 1.0f;
float posRatio = 1.0f;
// d r
if (ySpeed > 0 && xSpeed > 0) {
spdRatio = ySpeed / xSpeed;
posRatio = (right() - cb->left()) / (bottom() - cb->top());
}
// d l
else if (ySpeed > 0 && xSpeed < 0) {
spdRatio = ySpeed / -xSpeed;
posRatio = (cb->right() - left()) / (bottom() - cb->top());
}
// u r
else if (ySpeed < 0 && xSpeed > 0) {
spdRatio = -ySpeed / xSpeed;
posRatio = (right() - cb->left()) / (cb->bottom() - top());
}
// u l
else if (ySpeed < 0 && xSpeed < 0) {
spdRatio = -ySpeed / -xSpeed;
posRatio = (cb->right() - left()) / (cb->bottom() - top());
}
if (spdRatio * posRatio > 1) ySpeed = -ySpeed;
else xSpeed = -xSpeed;
// destroy brick
//cb->hidden = true;
cb->Break();
ds->numBricks--;
if (ds->numBricks == 0) doneCounter--;
break;
}
else cb = cb->nextb;
}
}
void Ball::Reposition() {
x = ds->carea.right / 2;
y = 440.0f;
xSpeed = 0;
ySpeed = 0;
held = true;
doneCounter = 100;
}
void Ball::Go() {
held = false;
xSpeed = 4.0f;
ySpeed = -8.0f;
}