-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
134 lines (111 loc) · 3.2 KB
/
Ball.cpp
File metadata and controls
134 lines (111 loc) · 3.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
#include "Ball.h"
#include "Constants.h"
#include "ResourceManager.h"
#include <iostream>
std::random_device Ball::mSrd;
Ball::Ball() :
mBallShape(BALL_RADIUS),
mPosition({ SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f }),
mVelocity({ BALL_SPEED, BALL_SPEED }),
mScore( {0, 0} ),
mCircleTexture(ResourceManager::get().getTexture("ball")),
mSoundOn(true),
mRecorder(),
mMt(mSrd())
{
mBallShape.setPointCount(100);
mBallShape.setTexture(&mCircleTexture);
mBallShape.setTextureRect(sf::IntRect({5, 5}, {90, 90}));
mBallShape.setPosition(mPosition);
}
void Ball::update(sf::Time deltaTime, bool sound)
{
mSoundOn = sound;
// --- 1. Îáðàáîòêà Àêòèâíîé Çàäåðæêè ---
if (isDelayActive)
{
// Íàêàïëèâàåì âðåìÿ, ïðîøåäøåå ñ ïîñëåäíåãî êàäðà
timeAccumulator += deltaTime;
if (timeAccumulator >= moveDelay) {
// Çàäåðæêà çàêîí÷èëàñü:
isDelayActive = false;
timeAccumulator = sf::Time::Zero;
resetPosition(); // Ñáðîñèòü ìÿ÷ íà ïîëå
}
// ÂÀÆÍÎ: Âûõîäèì èç ôóíêöèè, íå îáíîâëÿÿ ïîçèöèþ è íå îòðèñîâûâàÿ!
// Ýòî è áóäåò îçíà÷àòü, ÷òî øàð "ïåðåñòàë îòðèñîâûâàòüñÿ" (îí ïðîñòî íå îáíîâèò ïîçèöèþ Shape)
return;
}
// --- 2. Îáíîâëåíèå Ïîçèöèè (ïðîèñõîäèò òîëüêî åñëè isDelayActive == false) ---
mPosition += mVelocity;
// --- 3. Ïðîâåðêà Îòñêîêîâ (âåðõ/íèç) ---
if (mPosition.y <= 0 || mPosition.y + mBallShape.getRadius() * 2 >= SCREEN_HEIGHT)
{
mVelocity.y = -mVelocity.y;
if (mSoundOn)
mRecorder.playSound(SoundType::Beat);
}
// 4. Ïðîâåðêà Âûõîäà çà Ãðàíèöû è Ïîäñ÷åò Î÷êîâ
bool goalScored = false;
// Ëåâàÿ ãðàíèöà
if (mPosition.x <= 0)
{
mScore.y++;
goalScored = true;
}
// Ïðàâàÿ ãðàíèöà
else if (mPosition.x + mBallShape.getRadius() * 2 >= SCREEN_WIDTH)
{
mScore.x++;
goalScored = true;
}
// 5. Îáùàÿ Ëîãèêà Çàïóñêà Çàäåðæêè
if (goalScored)
{
isDelayActive = true;
timeAccumulator = sf::Time::Zero;
if (mSoundOn)
mRecorder.playSound(SoundType::End);
mVelocity = { BALL_SPEED, BALL_SPEED }; // reset speed to default
}
// 6. Îáíîâëåíèå Îòðèñîâêè
mBallShape.setPosition(mPosition);
}
void Ball::render(sf::RenderWindow& window)
{
if (!isDelayActive)
{
window.draw(mBallShape);
}
}
void Ball::resetPosition()
{
mPosition = { SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f };
mBallShape.setPosition(mPosition);
// direction
int direction_choices[2] = { -1, 1 };
mVelocity.x *= direction_choices[getRandomValue()];
mVelocity.y *= direction_choices[getRandomValue()];
}
int Ball::getRandomValue() // 0 or 1
{
std::uniform_int_distribution<int> dist(0, 1);
return dist(mMt);
}
void Ball::changeSpeed()
{
if (mSoundOn)
mRecorder.playSound(SoundType::Ball);
mVelocity.x = -mVelocity.x;
// random increase speed
int speed_choices[2] = { 0, 1 };
if (mVelocity.x > 0)
mVelocity.x += speed_choices[getRandomValue()];
else
mVelocity.x -= speed_choices[getRandomValue()];
}
void Ball::changePosition(float posX) // Prevent sticking
{
mPosition.x = posX;
mBallShape.setPosition(mPosition);
}