Try to make animation less dependant on framerate
This commit is contained in:
parent
a69e66d114
commit
ecf2ab8b6c
4 changed files with 29 additions and 24 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
Canvas::Canvas()
|
Canvas::Canvas()
|
||||||
: window(sf::VideoMode(windowDimensions().width, windowDimensions().height), "Pacman", sf::Style::Titlebar | sf::Style::Close) {
|
: window(sf::VideoMode(windowDimensions().width, windowDimensions().height), "Pacman", sf::Style::Titlebar | sf::Style::Close) {
|
||||||
|
window.setFramerateLimit(60);
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
maze_texture = loadTexture("maze.png");
|
maze_texture = loadTexture("maze.png");
|
||||||
|
|
26
lib/Game.cpp
26
lib/Game.cpp
|
@ -12,15 +12,29 @@ auto Game::now() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::run() {
|
void Game::run() {
|
||||||
InputState inputState;
|
|
||||||
|
const std::chrono::milliseconds delta_time (1000/60);
|
||||||
|
|
||||||
|
std::chrono::milliseconds t(0);
|
||||||
|
std::chrono::milliseconds accumulator(0);
|
||||||
auto current_time = now();
|
auto current_time = now();
|
||||||
while (!inputState.close) {
|
|
||||||
|
InputState inputState;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
auto newTime = now();
|
||||||
|
auto frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(newTime - current_time);
|
||||||
|
current_time = newTime;
|
||||||
|
accumulator += frameTime;
|
||||||
processEvents(inputState);
|
processEvents(inputState);
|
||||||
auto time_delta = now() - current_time;
|
if(inputState.close)
|
||||||
auto milli_delta = std::chrono::duration_cast<std::chrono::milliseconds>(time_delta);
|
return;
|
||||||
pacMan.update(milli_delta, inputState, board);
|
while ( accumulator >= delta_time ) {
|
||||||
|
pacMan.update(delta_time, inputState, board);
|
||||||
eatPellets();
|
eatPellets();
|
||||||
current_time += time_delta;
|
accumulator -= delta_time;
|
||||||
|
t += delta_time;
|
||||||
|
}
|
||||||
canvas.update(pacMan, pellets, superPellets);
|
canvas.update(pacMan, pellets, superPellets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,17 +13,7 @@ Position PacMan::position() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
Position PacMan::positionInGrid() const {
|
Position PacMan::positionInGrid() const {
|
||||||
switch (direction) {
|
return { std::round(pos.x), std::round(pos.y) };
|
||||||
case Direction::LEFT:
|
|
||||||
case Direction::RIGHT:
|
|
||||||
return { std::floor(pos.x), std::round(pos.y) };
|
|
||||||
case Direction::UP:
|
|
||||||
case Direction::DOWN:
|
|
||||||
return { std::floor(pos.x), std::round(pos.y) };
|
|
||||||
default:
|
|
||||||
return pos;
|
|
||||||
}
|
|
||||||
return pos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) {
|
void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) {
|
||||||
|
@ -50,7 +40,7 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) {
|
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) {
|
||||||
float position_delta = time_delta.count() / 150.0;
|
float position_delta = 0.004 * time_delta.count();
|
||||||
|
|
||||||
// Handle teleport
|
// Handle teleport
|
||||||
if (pos.x >= COLUMNS - 1 && direction == Direction::RIGHT) {
|
if (pos.x >= COLUMNS - 1 && direction == Direction::RIGHT) {
|
||||||
|
|
|
@ -17,7 +17,7 @@ PositionInt PacManAnimation::animationFrame(Direction direction) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta) {
|
void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta) {
|
||||||
animation_position_delta += (time_delta.count() / 100.0);
|
animation_position_delta += (0.02) * float(time_delta.count());
|
||||||
animation_position = int(animation_position + animation_position_delta) % 4;
|
animation_position = int(animation_position + animation_position_delta) % 4;
|
||||||
animation_position_delta = (animation_position_delta < 1) ? animation_position_delta : (animation_position_delta - 1);
|
animation_position_delta = (animation_position_delta < 1) ? animation_position_delta : (animation_position_delta - 1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue