FeatherMC/src/block/state/BlockState.h

60 lines
1.3 KiB
C
Raw Normal View History

2020-08-13 04:05:25 +01:00
#pragma once
#include "Common.h"
2020-11-07 01:51:08 +00:00
2020-08-13 04:05:25 +01:00
#include <string>
#include <string_view>
#include <functional>
2020-08-14 02:47:19 +01:00
#include <map>
2020-08-13 04:05:25 +01:00
namespace Feather
{
2020-11-07 01:51:08 +00:00
// Represents the state data of a particular instance of a Block
2020-08-13 04:05:25 +01:00
class BlockState
{
2020-08-14 02:47:19 +01:00
friend struct std::hash<BlockState>;
2020-08-13 04:05:25 +01:00
// PLACEHOLDER
std::string_view m_name;
2020-08-14 02:47:19 +01:00
std::map<std::string_view, std::string_view> m_properties;
2020-08-13 04:05:25 +01:00
public:
BlockState(std::string_view name) : m_name(name) {}
inline const std::string_view GetName() const { return m_name; }
2020-08-14 02:47:19 +01:00
inline void AddProperty(std::string_view key, std::string_view value) { m_properties.insert({ key, value }); }
2020-08-13 04:05:25 +01:00
inline bool operator ==(const BlockState that) const
{
2020-08-14 02:47:19 +01:00
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;
2020-08-13 04:05:25 +01:00
}
};
}
template <>
struct std::hash<Feather::BlockState>
{
size_t operator()(const Feather::BlockState& state) const
{
using std::hash;
using std::string_view;
2020-08-14 02:47:19 +01:00
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;
2020-08-13 04:05:25 +01:00
}
2020-11-07 01:51:08 +00:00
};