Bomberman Multiplayer
Authoritative multiplayer networking layer for Bomberman.
Loading...
Searching...
No Matches
Movement.h
Go to the documentation of this file.
1#ifndef BOMBERMAN_SIM_MOVEMENT_H
2#define BOMBERMAN_SIM_MOVEMENT_H
3
4#include "Const.h"
5#include "Sim/SimConfig.h"
6
21{
23 constexpr int32_t kPlayerSpeedQ = static_cast<int32_t>(kPlayerSpeedTilesPerSecond * 256.0 / kTickRate + 0.5);
24
26 constexpr int32_t kHitboxHalfQ = static_cast<int32_t>(kPlayerHitboxScale * 256.0f / 2.0f);
27
29 struct TilePos
30 {
31 int32_t xQ = 0;
32 int32_t yQ = 0;
33 };
34
37
44 [[nodiscard]]
45 inline bool isWalkable(const TileMap& map, int col, int row)
46 {
47 // Out-of-bounds check.
48 if (col < 0 || row < 0 ||
49 col >= static_cast<int>(tileArrayWidth) ||
50 row >= static_cast<int>(tileArrayHeight))
51 {
52 return false;
53 }
54
55 const Tile t = map[row][col];
56 return t != Tile::Stone && t != Tile::Brick;
57 }
58
62 [[nodiscard]]
63 inline bool overlapsWall(const TileMap& map, int32_t xQ, int32_t yQ)
64 {
65 // Convert the hitbox corners from tile-Q8 center coordinates to tile indices.
66 const int left = (xQ - kHitboxHalfQ) / 256;
67 const int right = (xQ + kHitboxHalfQ - 1) / 256; // -1 so touching edge is not solid
68 const int top = (yQ - kHitboxHalfQ) / 256;
69 const int bottom = (yQ + kHitboxHalfQ - 1) / 256;
70
71 return !isWalkable(map, left, top) ||
72 !isWalkable(map, right, top) ||
73 !isWalkable(map, left, bottom) ||
74 !isWalkable(map, right, bottom);
75 }
76
84 [[nodiscard]]
85 inline TilePos stepMovement(TilePos pos, int8_t moveX, int8_t moveY, int32_t speedQ = kPlayerSpeedQ)
86 {
87 pos.xQ += moveX * speedQ;
88 pos.yQ += moveY * speedQ;
89 return pos;
90 }
91
104 [[nodiscard]]
106 int8_t moveX,
107 int8_t moveY,
108 const TileMap& map,
109 int32_t speedQ = kPlayerSpeedQ)
110 {
111 // Try X axis.
112 if (moveX != 0)
113 {
114 const int32_t newXQ = pos.xQ + moveX * speedQ;
115 if (!overlapsWall(map, newXQ, pos.yQ))
116 pos.xQ = newXQ;
117 }
118
119 // Try Y axis.
120 if (moveY != 0)
121 {
122 const int32_t newYQ = pos.yQ + moveY * speedQ;
123 if (!overlapsWall(map, pos.xQ, newYQ))
124 pos.yQ = newYQ;
125 }
126
127 return pos;
128 }
129
141 [[nodiscard]]
142 inline int tileQToScreen(int32_t tileQ, int fieldPx, int scaledTile, int cameraPx)
143 {
144 return fieldPx + (tileQ * scaledTile / 256) - cameraPx;
145 }
146
159 [[nodiscard]]
160 inline int tileQToScreenTopLeft(int32_t tileQ, int fieldPx, int scaledTile, int cameraPx)
161 {
162 return fieldPx + ((tileQ - 128) * scaledTile / 256) - cameraPx;
163 }
164
165} // namespace bomberman::sim
166
167#endif // BOMBERMAN_SIM_MOVEMENT_H
Game-world configuration constants.
constexpr unsigned int tileArrayWidth
Tile map width in tiles.
Definition Const.h:43
constexpr unsigned int tileArrayHeight
Tile map height in tiles.
Definition Const.h:44
Tile
Tile type identifiers used in the tile map.
Definition Const.h:13
Shared simulation tuning constants used by client and server gameplay code.
Shared simulation primitives and constants used by both the client and the authoritative server.
Definition Movement.h:21
constexpr double kPlayerSpeedTilesPerSecond
Player movement speed in tiles per second.
Definition SimConfig.h:26
TilePos stepMovementWithCollision(TilePos pos, int8_t moveX, int8_t moveY, const TileMap &map, int32_t speedQ=kPlayerSpeedQ)
Advances a position by one tick with tile-map collision.
Definition Movement.h:105
bool overlapsWall(const TileMap &map, int32_t xQ, int32_t yQ)
Returns true if the AABB centered at (xQ, yQ) with half-extent kHitboxHalfQ overlaps any solid tile.
Definition Movement.h:63
bool isWalkable(const TileMap &map, int col, int row)
Returns true if the given tile coordinate is passable.
Definition Movement.h:45
constexpr int32_t kHitboxHalfQ
Player hitbox half-extent in tile-Q8 units.
Definition Movement.h:26
int tileQToScreenTopLeft(int32_t tileQ, int fieldPx, int scaledTile, int cameraPx)
Converts a center tile-Q8 coordinate to the screen top-left pixel for sprite rendering.
Definition Movement.h:160
constexpr float kPlayerHitboxScale
Player hitbox size as a fraction of the tile size.
Definition SimConfig.h:33
constexpr int32_t kPlayerSpeedQ
Player movement speed in tile-Q8 units per simulation tick.
Definition Movement.h:23
Tile[tileArrayHeight][tileArrayWidth] TileMap
Read-only view of a runtime tile map (same layout as LevelScene::tiles).
Definition Movement.h:36
int tileQToScreen(int32_t tileQ, int fieldPx, int scaledTile, int cameraPx)
Converts a center tile-Q8 coordinate to the screen pixel at that center.
Definition Movement.h:142
constexpr int16_t kTickRate
Simulation tick rate in Hz.
Definition SimConfig.h:14
TilePos stepMovement(TilePos pos, int8_t moveX, int8_t moveY, int32_t speedQ=kPlayerSpeedQ)
Advances a position by one simulation tick given a directional input.
Definition Movement.h:85
Tile-space Q8 position representing the CENTER of the entity.
Definition Movement.h:30