File CollisionLayers.hpp¶
File List > Components > CollisionLayers.hpp
Go to the documentation of this file
#ifndef ENGINE_COMPONENTS_COLLISIONLAYERS_HPP_
#define ENGINE_COMPONENTS_COLLISIONLAYERS_HPP_
#include <array>
#include <cstdint>
#include <string>
#include <string_view>
namespace Engine {
namespace CollisionLayers {
inline constexpr int kLayerCount = 32;
inline constexpr std::uint32_t kAllLayers = 0xFFFFFFFFu;
inline constexpr std::uint32_t kNoLayers = 0u;
inline constexpr int kDefaultLayer = 0;
inline constexpr std::array<std::string_view, 8> kLayerNames = {
"Default", "Player", "Enemy", "PlayerBullet", "EnemyBullet", "Wall", "Pickup", "Trigger"};
inline constexpr int kNamedLayerCount = static_cast<int>(kLayerNames.size());
inline std::uint32_t Bit(int layer) {
if (layer < 0 || layer >= kLayerCount) {
return 0u;
}
return 1u << layer;
}
inline std::string Name(int layer) {
if (layer >= 0 && layer < kNamedLayerCount) {
return std::string(kLayerNames[static_cast<std::size_t>(layer)]);
}
return "Layer " + std::to_string(layer);
}
inline int Index(std::string_view name) {
for (int layer = 0; layer < kNamedLayerCount; ++layer) {
if (kLayerNames[static_cast<std::size_t>(layer)] == name) {
return layer;
}
}
// Numeric fallback: "3" or "Layer 3".
const std::size_t first = name.find_first_of("0123456789");
if (first == std::string_view::npos) {
return -1;
}
int value = 0;
for (std::size_t i = first; i < name.size(); ++i) {
if (name[i] < '0' || name[i] > '9') {
break;
}
value = value * 10 + (name[i] - '0');
if (value >= kLayerCount) {
return -1;
}
}
return value;
}
} // namespace CollisionLayers
} // namespace Engine
#endif // ENGINE_COMPONENTS_COLLISIONLAYERS_HPP_