MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
walk::MazeWorld Class Reference

Public Member Functions

int activeCollectibles () const
int activeCollectibles () const
bool checkCollectibleCollision (const glm::vec3 &point, size_t &indexOut) const
bool checkCollectibleCollision (const glm::vec3 &point, size_t &indexOut) const
bool checkPillarCollision (const glm::vec3 &position, float playerRadius) const
bool checkPillarCollision (const glm::vec3 &position, float playerRadius) const
bool checkWallCollision (const glm::vec3 &position, float radius) const
bool checkWallCollision (const glm::vec3 &position, float radius) const
const std::vector< Collectible > & collectibles () const noexcept
const std::vector< Collectible > & collectibles () const noexcept
std::vector< Collectible > & collectibles () noexcept
std::vector< Collectible > & collectibles () noexcept
void generate (uint32_t seed)
void generate (uint32_t seed)
const std::vector< PillarInstance > & pillars () const noexcept
const std::vector< PillarInstance > & pillars () const noexcept
glm::vec3 randomPointInCell (int cellX, int cellZ, float objectRadius, float y, std::mt19937 &rng, float margin) const
glm::vec3 randomPointInCell (int cellX, int cellZ, float objectRadius, float y, std::mt19937 &rng, float margin) const
glm::vec3 startPosition () const noexcept
glm::vec3 startPosition () const noexcept
const std::vector< WallSegment > & walls () const noexcept
const std::vector< WallSegment > & walls () const noexcept
float wallThickness () const noexcept
float wallThickness () const noexcept

Detailed Description

Definition at line 94 of file room.cpp.

Member Function Documentation

◆ activeCollectibles() [1/2]

int walk::MazeWorld::activeCollectibles ( ) const
inlinenodiscard

Definition at line 106 of file room.cpp.

106 {
107 int count = 0;
108 for (const Collectible &obj : collectibleItems) {
109 if (obj.active) {
110 ++count;
111 }
112 }
113 return count;
114 }

◆ activeCollectibles() [2/2]

int walk::MazeWorld::activeCollectibles ( ) const
inlinenodiscard

Definition at line 108 of file room.cpp.

108 {
109 int count = 0;
110 for (const Collectible &obj : collectibleItems) {
111 if (obj.active) {
112 ++count;
113 }
114 }
115 return count;
116 }

◆ checkCollectibleCollision() [1/2]

bool walk::MazeWorld::checkCollectibleCollision ( const glm::vec3 & point,
size_t & indexOut ) const
inlinenodiscard

Definition at line 167 of file room.cpp.

167 {
168 for (size_t i = 0; i < collectibleItems.size(); ++i) {
169 if (!collectibleItems[i].active) {
170 continue;
171 }
172 const Collectible &collectible = collectibleItems[i];
173 const glm::vec3 center = collectible.position + collectible.hitCenterOffset;
174 if (collectible.type == Collectible::Type::Bird) {
175 const glm::vec3 delta = point - center;
176 const float halfSide = collectible.radius;
177 if (std::abs(delta.x) <= halfSide &&
178 std::abs(delta.y) <= halfSide &&
179 std::abs(delta.z) <= halfSide) {
180 indexOut = i;
181 return true;
182 }
183 continue;
184 }
185
186 if (glm::length(center - point) < collectible.radius) {
187 indexOut = i;
188 return true;
189 }
190 }
191 return false;
192 }
std::atomic< bool > active
Definition relay.cpp:12

◆ checkCollectibleCollision() [2/2]

bool walk::MazeWorld::checkCollectibleCollision ( const glm::vec3 & point,
size_t & indexOut ) const
inlinenodiscard

Definition at line 169 of file room.cpp.

169 {
170 for (size_t i = 0; i < collectibleItems.size(); ++i) {
171 if (!collectibleItems[i].active) {
172 continue;
173 }
174 const Collectible &collectible = collectibleItems[i];
175 const glm::vec3 center = collectible.position + collectible.hitCenterOffset;
176 if (collectible.type == Collectible::Type::Bird) {
177 const glm::vec3 delta = point - center;
178 const float halfSide = collectible.radius;
179 if (std::abs(delta.x) <= halfSide &&
180 std::abs(delta.y) <= halfSide &&
181 std::abs(delta.z) <= halfSide) {
182 indexOut = i;
183 return true;
184 }
185 continue;
186 }
187
188 if (glm::length(center - point) < collectible.radius) {
189 indexOut = i;
190 return true;
191 }
192 }
193 return false;
194 }

◆ checkPillarCollision() [1/2]

bool walk::MazeWorld::checkPillarCollision ( const glm::vec3 & position,
float playerRadius ) const
inlinenodiscard

Definition at line 156 of file room.cpp.

156 {
157 for (const PillarInstance &pillar : pillarInstances) {
158 const glm::vec2 player2d(position.x, position.z);
159 const glm::vec2 pillar2d(pillar.position.x, pillar.position.z);
160 if (glm::length(player2d - pillar2d) < (playerRadius + pillar.radius)) {
161 return true;
162 }
163 }
164 return false;
165 }

◆ checkPillarCollision() [2/2]

bool walk::MazeWorld::checkPillarCollision ( const glm::vec3 & position,
float playerRadius ) const
inlinenodiscard

Definition at line 158 of file room.cpp.

158 {
159 for (const PillarInstance &pillar : pillarInstances) {
160 const glm::vec2 player2d(position.x, position.z);
161 const glm::vec2 pillar2d(pillar.position.x, pillar.position.z);
162 if (glm::length(player2d - pillar2d) < (playerRadius + pillar.radius)) {
163 return true;
164 }
165 }
166 return false;
167 }

◆ checkWallCollision() [1/2]

bool walk::MazeWorld::checkWallCollision ( const glm::vec3 & position,
float radius ) const
inlinenodiscard

Definition at line 134 of file room.cpp.

134 {
135 const float halfThickness = wallThicknessValue * 0.5f;
136 const float hitRadius = radius + halfThickness;
137 for (const WallSegment &wall : wallSegments) {
138 const glm::vec3 segment = wall.end - wall.start;
139 const float segmentLength = glm::length(segment);
140 if (segmentLength < 0.0001f) {
141 continue;
142 }
143
144 const glm::vec3 segmentDir = segment / segmentLength;
145 const glm::vec3 toPoint = position - wall.start;
146 const float projection = glm::clamp(glm::dot(toPoint, segmentDir), 0.0f, segmentLength);
147 glm::vec3 closest = wall.start + (segmentDir * projection);
148 closest.y = position.y;
149 if (glm::length(position - closest) < hitRadius) {
150 return true;
151 }
152 }
153 return false;
154 }

◆ checkWallCollision() [2/2]

bool walk::MazeWorld::checkWallCollision ( const glm::vec3 & position,
float radius ) const
inlinenodiscard

Definition at line 136 of file room.cpp.

136 {
137 const float halfThickness = wallThicknessValue * 0.5f;
138 const float hitRadius = radius + halfThickness;
139 for (const WallSegment &wall : wallSegments) {
140 const glm::vec3 segment = wall.end - wall.start;
141 const float segmentLength = glm::length(segment);
142 if (segmentLength < 0.0001f) {
143 continue;
144 }
145
146 const glm::vec3 segmentDir = segment / segmentLength;
147 const glm::vec3 toPoint = position - wall.start;
148 const float projection = glm::clamp(glm::dot(toPoint, segmentDir), 0.0f, segmentLength);
149 glm::vec3 closest = wall.start + (segmentDir * projection);
150 closest.y = position.y;
151 if (glm::length(position - closest) < hitRadius) {
152 return true;
153 }
154 }
155 return false;
156 }

◆ collectibles() [1/4]

const std::vector< Collectible > & walk::MazeWorld::collectibles ( ) const
inlinenodiscardnoexcept

Definition at line 104 of file room.cpp.

104{ return collectibleItems; }

◆ collectibles() [2/4]

const std::vector< Collectible > & walk::MazeWorld::collectibles ( ) const
inlinenodiscardnoexcept

Definition at line 106 of file room.cpp.

106{ return collectibleItems; }

◆ collectibles() [3/4]

std::vector< Collectible > & walk::MazeWorld::collectibles ( )
inlinenodiscardnoexcept

Definition at line 102 of file room.cpp.

102{ return collectibleItems; }

◆ collectibles() [4/4]

std::vector< Collectible > & walk::MazeWorld::collectibles ( )
inlinenodiscardnoexcept

Definition at line 104 of file room.cpp.

104{ return collectibleItems; }

◆ generate() [1/2]

void walk::MazeWorld::generate ( uint32_t seed)
inline

Definition at line 116 of file room.cpp.

116 {
117 std::mt19937 rng(seed);
118 generateMaze(rng);
119 generatePillars(rng);
120 // Rescue: if the player start happens to land inside a pillar, push the
121 // start point to the safest unoccupied spot in the same cell.
122 if (checkPillarCollision(startPositionValue, 0.6f)) {
123 for (int attempt = 0; attempt < 64; ++attempt) {
124 const glm::vec3 candidate = randomPointInCell(startCellX, startCellZ, 0.6f, eyeHeight, rng, 0.5f);
125 if (!checkWallCollision(candidate, 0.5f) && !checkPillarCollision(candidate, 0.6f)) {
126 startPositionValue = candidate;
127 break;
128 }
129 }
130 }
131 generateCollectibles(rng);
132 }
bool checkWallCollision(const glm::vec3 &position, float radius) const
Definition room.cpp:134
bool checkPillarCollision(const glm::vec3 &position, float playerRadius) const
Definition room.cpp:156
glm::vec3 randomPointInCell(int cellX, int cellZ, float objectRadius, float y, std::mt19937 &rng, float margin) const
Definition room.cpp:194
std::default_random_engine & rng()
Returns the thread-local random number engine used by simulation helpers.

◆ generate() [2/2]

void walk::MazeWorld::generate ( uint32_t seed)
inline

Definition at line 118 of file room.cpp.

118 {
119 std::mt19937 rng(seed);
120 generateMaze(rng);
121 generatePillars(rng);
122 // Rescue: if the player start happens to land inside a pillar, push the
123 // start point to the safest unoccupied spot in the same cell.
124 if (checkPillarCollision(startPositionValue, 0.6f)) {
125 for (int attempt = 0; attempt < 64; ++attempt) {
126 const glm::vec3 candidate = randomPointInCell(startCellX, startCellZ, 0.6f, eyeHeight, rng, 0.5f);
127 if (!checkWallCollision(candidate, 0.5f) && !checkPillarCollision(candidate, 0.6f)) {
128 startPositionValue = candidate;
129 break;
130 }
131 }
132 }
133 generateCollectibles(rng);
134 }

◆ pillars() [1/2]

const std::vector< PillarInstance > & walk::MazeWorld::pillars ( ) const
inlinenodiscardnoexcept

Definition at line 100 of file room.cpp.

100{ return pillarInstances; }

◆ pillars() [2/2]

const std::vector< PillarInstance > & walk::MazeWorld::pillars ( ) const
inlinenodiscardnoexcept

Definition at line 102 of file room.cpp.

102{ return pillarInstances; }

◆ randomPointInCell() [1/2]

glm::vec3 walk::MazeWorld::randomPointInCell ( int cellX,
int cellZ,
float objectRadius,
float y,
std::mt19937 & rng,
float margin ) const
inlinenodiscard

Definition at line 194 of file room.cpp.

194 {
195 const float pad = objectRadius + wallThicknessValue * 0.5f + margin;
196 const float x0 = -size + static_cast<float>(cellX) * cellSize;
197 const float z0 = -size + static_cast<float>(cellZ) * cellSize;
198 const float x1 = x0 + cellSize;
199 const float z1 = z0 + cellSize;
200
201 float minX = x0 + pad;
202 float maxX = x1 - pad;
203 float minZ = z0 + pad;
204 float maxZ = z1 - pad;
205 if (minX > maxX) {
206 minX = maxX = (x0 + x1) * 0.5f;
207 }
208 if (minZ > maxZ) {
209 minZ = maxZ = (z0 + z1) * 0.5f;
210 }
211
212 std::uniform_real_distribution<float> distX(minX, maxX);
213 std::uniform_real_distribution<float> distZ(minZ, maxZ);
214 return glm::vec3(distX(rng), y, distZ(rng));
215 }

◆ randomPointInCell() [2/2]

glm::vec3 walk::MazeWorld::randomPointInCell ( int cellX,
int cellZ,
float objectRadius,
float y,
std::mt19937 & rng,
float margin ) const
inlinenodiscard

Definition at line 196 of file room.cpp.

196 {
197 const float pad = objectRadius + wallThicknessValue * 0.5f + margin;
198 const float x0 = -size + static_cast<float>(cellX) * cellSize;
199 const float z0 = -size + static_cast<float>(cellZ) * cellSize;
200 const float x1 = x0 + cellSize;
201 const float z1 = z0 + cellSize;
202
203 float minX = x0 + pad;
204 float maxX = x1 - pad;
205 float minZ = z0 + pad;
206 float maxZ = z1 - pad;
207 if (minX > maxX) {
208 minX = maxX = (x0 + x1) * 0.5f;
209 }
210 if (minZ > maxZ) {
211 minZ = maxZ = (z0 + z1) * 0.5f;
212 }
213
214 std::uniform_real_distribution<float> distX(minX, maxX);
215 std::uniform_real_distribution<float> distZ(minZ, maxZ);
216 return glm::vec3(distX(rng), y, distZ(rng));
217 }

◆ startPosition() [1/2]

glm::vec3 walk::MazeWorld::startPosition ( ) const
inlinenodiscardnoexcept

Definition at line 96 of file room.cpp.

96{ return startPositionValue; }

◆ startPosition() [2/2]

glm::vec3 walk::MazeWorld::startPosition ( ) const
inlinenodiscardnoexcept

Definition at line 98 of file room.cpp.

98{ return startPositionValue; }

◆ walls() [1/2]

const std::vector< WallSegment > & walk::MazeWorld::walls ( ) const
inlinenodiscardnoexcept

Definition at line 98 of file room.cpp.

98{ return wallSegments; }

◆ walls() [2/2]

const std::vector< WallSegment > & walk::MazeWorld::walls ( ) const
inlinenodiscardnoexcept

Definition at line 100 of file room.cpp.

100{ return wallSegments; }

◆ wallThickness() [1/2]

float walk::MazeWorld::wallThickness ( ) const
inlinenodiscardnoexcept

Definition at line 217 of file room.cpp.

217{ return wallThicknessValue; }

◆ wallThickness() [2/2]

float walk::MazeWorld::wallThickness ( ) const
inlinenodiscardnoexcept

Definition at line 219 of file room.cpp.

219{ return wallThicknessValue; }

The documentation for this class was generated from the following files: