Toast Engine
Loading...
Searching...
No Matches
hardness.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <array>
11#include <cstdint>
12#include <optional>
13#include <string_view>
14
15namespace physics {
16
17enum class Hardness : uint8_t {
18 soft,
19 medium,
20 hard,
21};
22
23inline constexpr std::array<std::string_view, 3> k_hardness_names {"soft", "medium", "hard"};
24
25inline constexpr Hardness k_hardest = Hardness::hard;
26
27[[nodiscard]]
28constexpr auto hardnessName(Hardness hardness) -> std::string_view {
29 const auto index = static_cast<size_t>(hardness);
30 return index < k_hardness_names.size() ? k_hardness_names[index] : std::string_view {};
31}
32
33[[nodiscard]]
34constexpr auto hardnessFromName(std::string_view name) -> std::optional<Hardness> {
35 for (size_t i = 0; i < k_hardness_names.size(); ++i) {
36 if (k_hardness_names[i] == name) {
37 return static_cast<Hardness>(i);
38 }
39 }
40 return std::nullopt;
41}
42
43[[nodiscard]]
44constexpr auto canBreak(Hardness max_hardness, Hardness material) -> bool {
45 return static_cast<uint8_t>(material) <= static_cast<uint8_t>(max_hardness);
46}
47
48}