FeatherMC/src/block/state/BlockState.h

60 lines
1.3 KiB
C++

#pragma once
#include "Common.h"
#include <string>
#include <string_view>
#include <functional>
#include <map>
namespace Feather
{
// Represents the state data of a particular instance of a Block
class BlockState
{
friend struct std::hash<BlockState>;
// PLACEHOLDER
std::string_view m_name;
std::map<std::string_view, std::string_view> m_properties;
public:
BlockState(std::string_view name) : m_name(name) {}
inline const std::string_view GetName() const { return m_name; }
inline void AddProperty(std::string_view key, std::string_view value) { m_properties.insert({ key, value }); }
inline bool operator ==(const BlockState that) const
{
if (m_name != that.m_name) return false;
for (auto& [key, value] : that.m_properties)
{
if (!m_properties.contains(key) || m_properties.at(key) != value)
return false;
}
return true;
}
};
}
template <>
struct std::hash<Feather::BlockState>
{
size_t operator()(const Feather::BlockState& state) const
{
using std::hash;
using std::string_view;
size_t accum = 0;
accum += hash<string_view>()(state.GetName());
// pretty basic hash, sum of all keys and values + block name
for (auto& [key, value] : state.m_properties)
accum += hash<string_view>()(key) + hash<string_view>()(value);
return accum;
}
};