Use size_t again

This commit is contained in:
Corentin Jabot 2021-09-10 11:02:37 +02:00 committed by cor3ntin
parent 54ec3bcdf9
commit 453dd17d7c
7 changed files with 17 additions and 15 deletions

View File

@ -178,7 +178,7 @@ void Ghost::updateDirection(const GameState & gameState) {
if (opposite_direction)
continue;
const GridPosition grid_position = { int64_t(move.position.x), int64_t(move.position.y) };
const GridPosition grid_position = { size_t(move.position.x), size_t(move.position.y) };
const bool can_walk = isWalkableForGhost(grid_position, current_grid_position, isEyes());
if (!can_walk)
continue;

View File

@ -53,8 +53,8 @@ Position Inky::target(const GameState & gameState) const {
// And selects a point on the line crossing blinky and this position that is at twice that distance
// away from blinky
targetPosition.x += int64_t((targetPosition.x - blinkyPosition.x) / distanceBetweenBlinkyAndTarget) * 2;
targetPosition.y += int64_t((targetPosition.y - blinkyPosition.y) / distanceBetweenBlinkyAndTarget) * 2;
targetPosition.x += size_t((targetPosition.x - blinkyPosition.x) / distanceBetweenBlinkyAndTarget) * 2;
targetPosition.y += size_t((targetPosition.y - blinkyPosition.y) / distanceBetweenBlinkyAndTarget) * 2;
return gridPositionToPosition(targetPosition);
}

View File

@ -64,13 +64,13 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
auto moveToPosition = [position_delta](Position point, Direction move_direction) {
switch (move_direction) {
case Direction::LEFT:
return GridPosition{ int64_t(point.x - position_delta), int64_t(point.y) };
return GridPosition{ size_t(point.x - position_delta), size_t(point.y) };
case Direction::RIGHT:
return GridPosition{ int64_t(point.x + pacman_size), int64_t(point.y) };
return GridPosition{ size_t(point.x + pacman_size), size_t(point.y) };
case Direction::UP:
return GridPosition{ int64_t(point.x), int64_t(point.y - position_delta) };
return GridPosition{ size_t(point.x), size_t(point.y - position_delta) };
case Direction::DOWN:
return GridPosition{ int64_t(point.x), int64_t(point.y + pacman_size) };
return GridPosition{ size_t(point.x), size_t(point.y + pacman_size) };
case Direction::NONE:
default:
return positionToGridPosition(point);

View File

@ -32,7 +32,7 @@ void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_del
return;
animation_position_delta += (0.02) * double(time_delta.count());
animation_position = int64_t(animation_position + animation_position_delta);
animation_position = size_t(animation_position + animation_position_delta);
if (!dead)
animation_position = animation_position % 4;

View File

@ -47,8 +47,8 @@ constexpr GridPosition eyeSprite(Direction direction) {
constexpr GridPosition ghostSprite(Ghost ghost, Direction direction, bool alternative) {
assert(ghost >= Ghost::blinky && ghost <= Ghost::clyde && "Invalid Ghost");
auto y = static_cast<int64_t>(ghost);
int64_t x = 0;
auto y = static_cast<size_t>(ghost);
size_t x = 0;
switch (direction) {
case Direction::RIGHT:
x = 0;

View File

@ -18,7 +18,7 @@ public:
void pause();
private:
int64_t animation_position = 0;
size_t animation_position = 0;
double animation_position_delta = 0.0;
};

View File

@ -1,6 +1,7 @@
#pragma once
#include <cmath>
#include <cassert>
namespace pacman {
@ -10,13 +11,14 @@ struct Position {
};
struct GridPosition {
int64_t x;
int64_t y;
constexpr GridPosition(int64_t x, int64_t y) : x(x), y(y) {}
size_t x;
size_t y;
constexpr GridPosition(size_t x, size_t y) : x(x), y(y) {}
};
inline GridPosition positionToGridPosition(Position pos) {
return { int64_t(std::round(pos.x)), int64_t(std::round(pos.y)) };
assert(pos.x >= 0 && pos.y >= 0 && "Position should have positive values");
return { size_t(std::round(pos.x)), size_t(std::round(pos.y)) };
}
inline Position gridPositionToPosition(GridPosition pos) {