-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.cpp
More file actions
154 lines (131 loc) · 4.54 KB
/
Copy pathball.cpp
File metadata and controls
154 lines (131 loc) · 4.54 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
#include "ball.h"
const sf::Vector2f orgVelocity = sf::Vector2f(1.f, 2.f);
void Ball::initTexture(){
texture = new sf::Texture;
if(!this->texture->loadFromFile("/Users/nina/Desktop/C++practice/Game/texture/pdogs.png"))
{
std::cout << " ERROR: Load BALL image fail.";
}
}
void Ball::initSprite()
{ sprite = new sf::Sprite;
this-> sprite->setTexture(*texture);
//resize
this-> sprite->scale(0.1f, 0.1f);
this-> sprite->setPosition(200.f, -10.f);
}
Ball::Ball()
{
this-> bounceVelocity = orgVelocity;
this-> gravity = .05f;
this-> ballOnGround = false;
this->_timeElapsed = 0;
this-> initTexture();
this-> initSprite();
}
Ball:: ~Ball()
{
}
void Ball:: updateWindowBoundsCollision(const sf::RenderTarget* target)
{
//Left
if(this-> sprite->getGlobalBounds().left < 0.f)
{
this-> sprite->setPosition(0.f, this->sprite->getPosition().y);
this-> bounceVelocity.x = 1.f;
// this-> rotateAngle = 0.5f;
}
//Right
if(this-> sprite->getGlobalBounds().left + this->sprite->getGlobalBounds().width > target->getSize().x)
{
this-> sprite->setPosition(target->getSize().x - this->sprite->getGlobalBounds().width, this->sprite->getPosition().y);
this-> bounceVelocity.x = -1.f;
// this-> rotateAngle = -0.5f;
}
//Top (Ball should fly and fall)
if(this->sprite->getGlobalBounds().top + this->sprite->getGlobalBounds().height * 2 <= 0.f)
{
if(bounceVelocity.x > .0f)
this-> sprite->setPosition(this->sprite->getPosition().x + this->sprite->getGlobalBounds().width/4, -2.f);
else
this-> sprite->setPosition(this->sprite->getPosition().x - this->sprite->getGlobalBounds().width/4, -2.f);
this-> bounceVelocity.y = 1.f;
}
// Bottom (GameOver)!!!
else if(this->sprite->getGlobalBounds().top + this->sprite->getGlobalBounds().height >= target->getSize().y)
{
this-> sprite->setPosition(this->sprite->getPosition().x, target->getSize().y - this->sprite->getGlobalBounds().height);
this-> bounceVelocity = sf:: Vector2f(0.f, 0.f);
this-> ballOnGround = true;
}
//hits the net from right side
if(this-> sprite->getGlobalBounds().left <= target->getSize().x/2 && this->sprite->getGlobalBounds().top + this->sprite->getGlobalBounds().height >= target->getSize().y * 1/2)
{
if(this->sprite->getPosition().x > target->getSize().x/2)
{
this->sprite->setPosition(target->getSize().x/2, this->sprite->getPosition().y);
this->bounceVelocity.x = 1.f;
}
}
//hits the net from the left side
if(this-> sprite->getGlobalBounds().left + this->sprite->getGlobalBounds().width > target->getSize().x/2 && this->sprite->getGlobalBounds().top + this->sprite->getGlobalBounds().height >= target->getSize().y * 1/2)
{
if(this->sprite->getPosition().x < target->getSize().x/2) {
this->sprite->setPosition(target->getSize().x/2 - this->sprite->getGlobalBounds().width, this->sprite->getPosition().y);
this->bounceVelocity.x = -1.f;
}
}
}
void Ball:: velocityChange(const sf::RenderTarget* target)
{
this-> bounceVelocity.y += this-> gravity;
}
void Ball::bounce(const sf::RenderTarget* target)
{
this-> sprite->move(this-> bounceVelocity);
}
void Ball::serve(int server)
{
//set ball to the server
if(server == 1) //server is Jie
this-> sprite->setPosition(192.f, 0.f);
else
this-> sprite->setPosition(1550.f, 0.f);
this-> bounceVelocity = sf::Vector2f(orgVelocity.x, orgVelocity.y * 0.5);
}
void Ball::rotate(const sf::RenderTarget* target)
{
this->sprite->rotate(this-> rotateAngle);
}
void Ball:: update(const sf::RenderTarget* target, const float& dt)
{
// delay reset after ballOnGround
// if(ballOnGround)return;
//
// _timeElapsed += dt;
// if(_timeElapsed < 3)return;
// this-> serve();
if(this-> ballOnGround == true)
{
// this->bounceVelocity = orgVelocity;
if(this-> sprite->getPosition().x < target->getSize().x / 2)
this->serve(2);
else
this->serve(1);
this->ballOnGround = false;
}
this->velocityChange(target);
this->updateWindowBoundsCollision(target);
// this->rotate(target);
this->bounce(target);
}
sf::Rect<float> Ball::getGlobalBounds() {
return this-> sprite->getGlobalBounds();
}
sf:: Vector2f Ball:: getPosition()
{
return this->sprite->getPosition();
}
void Ball::render(sf::RenderTarget* target){
target->draw(*sprite);
}