From 94e7f7d90015d95745484690ba6634198c4f30df Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Mon, 19 Dec 2022 22:47:12 +0000 Subject: [PATCH] entity/token system from hexboard --- include/fggl/grid/hexgrid.hpp | 60 ++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/include/fggl/grid/hexgrid.hpp b/include/fggl/grid/hexgrid.hpp index 374641c..cc8aa06 100644 --- a/include/fggl/grid/hexgrid.hpp +++ b/include/fggl/grid/hexgrid.hpp @@ -22,12 +22,67 @@ #include <map> #include <set> #include <optional> +#include <utility> #include "fggl/grid/hexagon.hpp" #include "fggl/math/types.hpp" namespace fggl::grid { + class TokenType { + public: + using PropType = int64_t; + + inline void setProperty(util::GUID name, PropType newVal) { + m_properties[name] = newVal; + } + + [[nodiscard]] + inline PropType getProperty(util::GUID name, PropType unsetVal=0) const { + try { + return m_properties.at(name); + } catch ( std::out_of_range& e ) { + return unsetVal; + } + } + private: + std::map<util::GUID, PropType> m_properties; + std::map<util::GUID, uint64_t> m_cost; + }; + + class Token { + public: + Token() = default; + explicit Token(std::shared_ptr<TokenType> type) : m_type(std::move(type)) {} + + inline void setProperty(util::GUID name, TokenType::PropType newVal) { + m_properties[name] = newVal; + } + + [[nodiscard]] + inline TokenType::PropType getProperty(util::GUID name, TokenType::PropType unsetVal=0) const { + try { + auto prop = m_properties.at(name); + return prop; + } catch ( std::out_of_range& ex) { + return m_type->getProperty(name, unsetVal); + } + } + + [[nodiscard]] + inline TokenType::PropType getPropertyDirect(util::GUID name, TokenType::PropType unsetVal=0) const { + try { + auto prop = m_properties.at(name); + return prop; + } catch ( std::out_of_range& ex) { + return unsetVal; + } + } + private: + std::shared_ptr<TokenType> m_type; + std::map<util::GUID, TokenType::PropType> m_properties; + }; + struct MaterialData { std::string name; math::vec3 colour; @@ -39,6 +94,7 @@ namespace fggl::grid { struct HexTile { std::shared_ptr<MaterialData> terrain; + std::vector< std::shared_ptr<Token> > m_tokens; [[nodiscard]] inline std::optional<const MaterialData> data() const { @@ -46,10 +102,11 @@ namespace fggl::grid { {} } - return *terrain.get(); + return *terrain; } }; + class HexGrid { public: inline bool isValidPos(const IntHex& pos) const { @@ -82,6 +139,7 @@ namespace fggl::grid { private: std::map<IntHex, HexTile> m_tiles; + std::map<uint64_t, std::shared_ptr<Token>> m_tokens; }; } // namespace fggl::grid -- GitLab