playtime and totalSeconds are now stored as std::chrono objects, totalSeconds is now a member of the Game class
This commit is contained in:
parent
fe353f2913
commit
1da27bd621
|
|
@ -27,6 +27,7 @@ struct Cpu
|
||||||
sf::Vector2f pos{};
|
sf::Vector2f pos{};
|
||||||
sf::Vector2f prevPos{};
|
sf::Vector2f prevPos{};
|
||||||
float velocity{};
|
float velocity{};
|
||||||
|
float speed{5};
|
||||||
unsigned int score{};
|
unsigned int score{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -103,7 +104,7 @@ private:
|
||||||
|
|
||||||
double accumulator{};
|
double accumulator{};
|
||||||
|
|
||||||
double playTime{};
|
TimeDuration playTime{};
|
||||||
|
std::chrono::seconds totalSeconds{};
|
||||||
|
|
||||||
};
|
};
|
||||||
17
src/Game.cpp
17
src/Game.cpp
|
|
@ -95,7 +95,8 @@ void Game::run()
|
||||||
TimeDuration duration = currentFrameStartPoint - lastFrameStartPoint;
|
TimeDuration duration = currentFrameStartPoint - lastFrameStartPoint;
|
||||||
const double frameTime = duration.count();
|
const double frameTime = duration.count();
|
||||||
|
|
||||||
playTime += frameTime;
|
playTime += duration;
|
||||||
|
totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(playTime).count();
|
||||||
|
|
||||||
lastFrameStartPoint = currentFrameStartPoint;
|
lastFrameStartPoint = currentFrameStartPoint;
|
||||||
|
|
||||||
|
|
@ -228,14 +229,19 @@ void Game::render()
|
||||||
|
|
||||||
void Game::updateCpu()
|
void Game::updateCpu()
|
||||||
{
|
{
|
||||||
|
if (totalSeconds > 0 and totalSeconds % 10 == 0)
|
||||||
|
{
|
||||||
|
cpu.speed += .5f;
|
||||||
|
}
|
||||||
|
|
||||||
if (ball.pos.y > cpu.pos.y)
|
if (ball.pos.y > cpu.pos.y)
|
||||||
{
|
{
|
||||||
cpu.velocity = 5;
|
cpu.velocity = cpu.speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ball.pos.y < cpu.pos.y)
|
if (ball.pos.y < cpu.pos.y)
|
||||||
{
|
{
|
||||||
cpu.velocity = -5;
|
cpu.velocity = cpu.speed * -1.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ball.pos.y == cpu.pos.y)
|
if (ball.pos.y == cpu.pos.y)
|
||||||
|
|
@ -262,9 +268,8 @@ void Game::collision() {}
|
||||||
|
|
||||||
void Game::updateTexts()
|
void Game::updateTexts()
|
||||||
{
|
{
|
||||||
const int totalSeconds = static_cast<int>(playTime);
|
const auto minutes = totalSeconds / 60;
|
||||||
const int minutes = totalSeconds / 60;
|
const auto seconds = totalSeconds % 60;
|
||||||
const int seconds = totalSeconds % 60;
|
|
||||||
|
|
||||||
const std::string secondsStr = (seconds < 10) ? "0" + std::to_string(seconds) : std::to_string(seconds);
|
const std::string secondsStr = (seconds < 10) ? "0" + std::to_string(seconds) : std::to_string(seconds);
|
||||||
const std::string minutesStr = (minutes < 10) ? "0" + std::to_string(minutes) : std::to_string(minutes);
|
const std::string minutesStr = (minutes < 10) ? "0" + std::to_string(minutes) : std::to_string(minutes);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue