Skip to content

Instantly share code, notes, and snippets.

@Pharap

Pharap/Tile.h Secret

Created Jul 23, 2018
Embed
What would you like to do?
Tile helper code for HomineLudens
#pragma once
#include <cstdint>
//
// TileRenderFlags
//
enum class TileRenderFlags : std::uint8_t
{
None = 0,
FlipH = (1 << 0),
FlipV = (1 << 1),
Rotate1 = (1 << 2),
Rotate2 = (1 << 3),
};
inline constexpr TileRenderFlags operator ~(TileRenderFlags value)
{
return static_cast<TileRenderFlags>(~static_cast<std::uint8_t>(left));
}
inline constexpr TileRenderFlags operator |(TileRenderFlags left, TileRenderFlags right)
{
return static_cast<TileRenderFlags>(static_cast<std::uint8_t>(left) | static_cast<std::uint8_t>(right));
}
inline TileRenderFlags & operator |=(TileRenderFlags & left, TileRenderFlags right)
{
left = left | right;
return left;
}
inline constexpr TileRenderFlags operator &(TileRenderFlags left, TileRenderFlags right)
{
return static_cast<TileRenderFlags>(static_cast<std::uint8_t>(left) & static_cast<std::uint8_t>(right));
}
inline TileRenderFlags & operator &=(TileRenderFlags & left, TileRenderFlags right)
{
left = left & right;
return left;
}
inline constexpr TileRenderFlags operator ^(TileRenderFlags left, TileRenderFlags right)
{
return static_cast<TileRenderFlags>(static_cast<std::uint8_t>(left) ^ static_cast<std::uint8_t>(right));
}
inline TileRenderFlags & operator ^=(TileRenderFlags & left, TileRenderFlags right)
{
left = left ^ right;
return left;
}
inline constexpr bool hasAllFlags(TileRenderFlags object, TileRenderFlags targetFlags)
{
return (this->object & targetFlags) == targetFlags;
}
inline constexpr bool hasAnyFlags(TileRenderFlags object, TileRenderFlags targetFlags)
{
return (this->object & targetFlags) != TileRenderFlags::None;
}
class TileRenderOptions
{
private:
TileRenderFlags flags = TileRenderFlags::None;
public:
constexpr TileRenderOptions(void) = default;
constexpr TileRenderOptions(TileRenderFlags flags)
: flags(flags) {}
constexpr TileRenderFlags getFlags(void) const
{
return this->flags;
}
constexpr bool hasAllFlags(TileRenderFlags flags) const
{
return (this->flags & flags) == flags;
}
constexpr bool hasAnyFlags(TileRenderFlags flags) const
{
return (this->flags & flags) != TileRenderFlags::None;
}
void setFlags(TileRenderFlags flags)
{
this->flags |= flags;
}
void clearFlags(TileRenderFlags flags)
{
this->flags &= ~flags;
}
void setFlags(TileRenderFlags flags, bool value)
{
if(value)
this->setFlags(flags);
else
this->clearFlags(flags);
}
};
//
// TilePropertyFlags
//
enum class TilePropertyFlags : std::uint8_t
{
None = 0,
Solid = (1 << 0),
Visible = (1 << 1),
Action = (1 << 2),
Destructible = (1 << 3),
Animated = (1 << 4),
Mass = (1 << 5),
Movable = (1 << 6),
Blocking = (1 << 7),
};
inline constexpr TilePropertyFlags operator ~(TilePropertyFlags value)
{
return static_cast<TilePropertyFlags>(~static_cast<std::uint8_t>(left));
}
inline constexpr TilePropertyFlags operator |(TilePropertyFlags left, TilePropertyFlags right)
{
return static_cast<TilePropertyFlags>(static_cast<std::uint8_t>(left) | static_cast<std::uint8_t>(right));
}
inline TilePropertyFlags & operator |=(TilePropertyFlags & left, TilePropertyFlags right)
{
left = left | right;
return left;
}
inline constexpr TilePropertyFlags operator &(TilePropertyFlags left, TilePropertyFlags right)
{
return static_cast<TilePropertyFlags>(static_cast<std::uint8_t>(left) & static_cast<std::uint8_t>(right));
}
inline TilePropertyFlags & operator &=(TilePropertyFlags & left, TilePropertyFlags right)
{
left = left & right;
return left;
}
inline constexpr TilePropertyFlags operator ^(TilePropertyFlags left, TilePropertyFlags right)
{
return static_cast<TilePropertyFlags>(static_cast<std::uint8_t>(left) ^ static_cast<std::uint8_t>(right));
}
inline TilePropertyFlags & operator ^=(TilePropertyFlags & left, TilePropertyFlags right)
{
left = left ^ right;
return left;
}
inline bool hasAllFlags(TilePropertyFlags object, TilePropertyFlags targetFlags)
{
return (this->object & targetFlags) == targetFlags;
}
inline bool hasAnyFlags(TilePropertyFlags object, TilePropertyFlags targetFlags)
{
return (this->object & targetFlags) != TilePropertyFlags::None;
}
class TilePropertyOptions
{
private:
TilePropertyFlags flags = TilePropertyFlags::None;
public:
constexpr TilePropertyOptions(void) = default;
constexpr TilePropertyOptions(TilePropertyFlags flags)
: flags(flags) {}
constexpr TilePropertyFlags getFlags(void) const
{
return this->flags;
}
constexpr bool hasAllFlags(TilePropertyFlags flags) const
{
return (this->flags & flags) == flags;
}
constexpr bool hasAnyFlags(TilePropertyFlags flags) const
{
return (this->flags & flags) != TilePropertyFlags::None;
}
void setFlags(TilePropertyFlags flags)
{
this->flags |= flags;
}
void clearFlags(TilePropertyFlags flags)
{
this->flags &= ~flags;
}
void setFlags(TilePropertyFlags flags, bool value)
{
if(value)
this->setFlags(flags);
else
this->clearFlags(flags);
}
};
//
// Tile
//
class Tile
{
private:
std::uint8_t tileId = 0;
TileRenderOptions renderFlags = TileRenderOptions();
TilePropertyOptions properties = TilePropertyOptions();
public:
constexpr Tile(void) = default;
constexpr Tile(std::uint8_t tileId, TileRenderFlags renderFlags, TilePropertyFlags properties)
: tileId(tileId), renderOptions(renderOptions), properties(properties) {}
constexpr Tile(std::uint8_t tileId, TileRenderOptions renderOptions, TilePropertyOptions properties)
: tileId(tileId), renderOptions(renderOptions), properties(properties) {}
constexpr std::uint8_t getTileId(void) const
{
return this->tileId;
}
constexpr TileRenderOptions getRenderOptions(void) const
{
return this->renderOptions;
}
constexpr TilePropertyOptions getPropertyOptions(void) const
{
return this->properties;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment