Skip to content

Commit

Permalink
split scenery-proto into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Apr 13, 2024
1 parent ff2a121 commit b4f0ae9
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 106 deletions.
2 changes: 1 addition & 1 deletion editor/scenery-editor.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "src/scenery.hpp"
#include "src/scenery-proto.hpp"
#include <map>
#include <memory>
#include <Corrade/Containers/String.h>
Expand Down
3 changes: 1 addition & 2 deletions loader/scenery-cell.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "compat/safe-ptr.hpp"
#include "src/scenery.hpp"
#include <memory>
#include "src/scenery-proto.hpp"
#include <cr/String.h>
#include <cr/Optional.h>

Expand Down
1 change: 1 addition & 0 deletions serialize/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "src/wall-atlas.hpp"
#include "src/anim-atlas.hpp"
#include "src/scenery.hpp"
#include "src/scenery-proto.hpp"
#include "src/critter.hpp"
#include "src/light.hpp"
#include "src/world.hpp"
Expand Down
60 changes: 60 additions & 0 deletions src/scenery-proto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "scenery-proto.hpp"
#include "compat/overloaded.hpp"

namespace floormat {

// ---------- generic_scenery_proto ----------

bool generic_scenery_proto::operator==(const generic_scenery_proto& p) const = default;
enum scenery_type generic_scenery_proto::scenery_type() { return scenery_type::generic; }

// ---------- door_scenery_proto ----------

bool door_scenery_proto::operator==(const door_scenery_proto& p) const = default;
enum scenery_type door_scenery_proto::scenery_type() { return scenery_type::door; }

// --- scenery_proto ---

scenery_proto::scenery_proto() noexcept { type = object_type::scenery; }
scenery_proto::~scenery_proto() noexcept = default;
scenery_proto::operator bool() const { return atlas != nullptr; }

scenery_proto& scenery_proto::operator=(const scenery_proto&) noexcept = default;
scenery_proto::scenery_proto(const scenery_proto&) noexcept = default;
scenery_proto& scenery_proto::operator=(scenery_proto&&) noexcept = default;
scenery_proto::scenery_proto(scenery_proto&&) noexcept = default;

enum scenery_type scenery_proto::scenery_type() const
{
return std::visit(overloaded {
[](std::monostate) { return scenery_type::none; },
[]<typename T>(const T&) { return T::scenery_type(); },
}, subtype
);
}

bool scenery_proto::operator==(const object_proto& e0) const
{
if (type != e0.type)
return false;

if (!object_proto::operator==(e0))
return false;

const auto& sc = static_cast<const scenery_proto&>(e0);

if (subtype.index() != sc.subtype.index())
return false;

return std::visit(
[](const auto& a, const auto& b) -> bool {
if constexpr(std::is_same_v<std::decay_t<decltype(a)>, std::decay_t<decltype(b)>>)
return a == b;
else
fm_assert(false);
},
subtype, sc.subtype
);
}

} // namespace floormat
44 changes: 44 additions & 0 deletions src/scenery-proto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include "scenery-type.hpp"
#include "object.hpp"
#include <variant>

namespace floormat {

struct generic_scenery_proto
{
bool active : 1 = false;
bool interactive : 1 = false;

bool operator==(const generic_scenery_proto& p) const;
static enum scenery_type scenery_type();
};

struct door_scenery_proto
{
bool active : 1 = false;
bool interactive : 1 = true;
bool closing : 1 = false;

bool operator==(const door_scenery_proto& p) const;
static enum scenery_type scenery_type();
};

using scenery_proto_variants = std::variant<std::monostate, generic_scenery_proto, door_scenery_proto>;

struct scenery_proto : object_proto
{
scenery_proto_variants subtype; // todo! add std::monostate

scenery_proto() noexcept;
~scenery_proto() noexcept override;
explicit operator bool() const;
bool operator==(const object_proto& proto) const override;
enum scenery_type scenery_type() const;
scenery_proto(const scenery_proto&) noexcept;
scenery_proto& operator=(const scenery_proto&) noexcept;
scenery_proto(scenery_proto&&) noexcept;
scenery_proto& operator=(scenery_proto&&) noexcept;
};

} // namespace floormat
9 changes: 9 additions & 0 deletions src/scenery-type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

namespace floormat {

enum class scenery_type : unsigned char {
none, generic, door, COUNT,
};

} // namespace floormat
55 changes: 1 addition & 54 deletions src/scenery.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,17 @@
#include "scenery.hpp"
#include "scenery-proto.hpp"
#include "compat/assert.hpp"
#include "compat/exception.hpp"
#include "compat/overloaded.hpp"
#include "tile-constants.hpp"
#include "anim-atlas.hpp"
#include "rotation.inl"
#include "nanosecond.hpp"
#include "world.hpp"
#include "shaders/shader.hpp"
#include <mg/Functions.h>

namespace floormat {

// ---------- generic_scenery_proto ----------

bool generic_scenery_proto::operator==(const generic_scenery_proto& p) const = default;
enum scenery_type generic_scenery_proto::scenery_type() { return scenery_type::generic; }

// ---------- door_scenery_proto ----------

bool door_scenery_proto::operator==(const door_scenery_proto& p) const = default;
enum scenery_type door_scenery_proto::scenery_type() { return scenery_type::door; }

// --- scenery_proto ---

scenery_proto::scenery_proto() noexcept { type = object_type::scenery; }
scenery_proto::~scenery_proto() noexcept = default;
scenery_proto::operator bool() const { return atlas != nullptr; }

scenery_proto& scenery_proto::operator=(const scenery_proto&) noexcept = default;
scenery_proto::scenery_proto(const scenery_proto&) noexcept = default;
scenery_proto& scenery_proto::operator=(scenery_proto&&) noexcept = default;
scenery_proto::scenery_proto(scenery_proto&&) noexcept = default;

enum scenery_type scenery_proto::scenery_type() const
{
return std::visit(overloaded {
[](std::monostate) { return scenery_type::none; },
[]<typename T>(const T&) { return T::scenery_type(); },
}, subtype
);
}

bool scenery_proto::operator==(const object_proto& e0) const
{
if (type != e0.type)
return false;

if (!object_proto::operator==(e0))
return false;

const auto& sc = static_cast<const scenery_proto&>(e0);

if (subtype.index() != sc.subtype.index())
return false;

return std::visit(
[](const auto& a, const auto& b) -> bool {
if constexpr(std::is_same_v<std::decay_t<decltype(a)>, std::decay_t<decltype(b)>>)
return a == b;
else
fm_assert(false);
},
subtype, sc.subtype
);
}

// --- scenery ---

Expand Down
49 changes: 4 additions & 45 deletions src/scenery.hpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,15 @@
#pragma once
#include "object.hpp"
#include <variant>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Magnum.h>
#include "scenery-type.hpp"

namespace floormat {

class chunk;
class anim_atlas;
class world;

enum class scenery_type : unsigned char {
none, generic, door, COUNT,
};

struct generic_scenery_proto
{
bool active : 1 = false;
bool interactive : 1 = false;

bool operator==(const generic_scenery_proto& p) const;
static enum scenery_type scenery_type();
};

struct door_scenery_proto
{
bool active : 1 = false;
bool interactive : 1 = true;
bool closing : 1 = false;

bool operator==(const door_scenery_proto& p) const;
static enum scenery_type scenery_type();
};

using scenery_proto_variants = std::variant<std::monostate, generic_scenery_proto, door_scenery_proto>;

struct scenery_proto : object_proto
{
scenery_proto_variants subtype; // todo! add std::monostate

scenery_proto() noexcept;
~scenery_proto() noexcept override;
explicit operator bool() const;
bool operator==(const object_proto& proto) const override;
enum scenery_type scenery_type() const;
scenery_proto(const scenery_proto&) noexcept;
scenery_proto& operator=(const scenery_proto&) noexcept;
scenery_proto(scenery_proto&&) noexcept;
scenery_proto& operator=(scenery_proto&&) noexcept;
};

struct scenery;
struct scenery_proto;
struct generic_scenery_proto;
struct door_scenery_proto;

struct scenery : object
{
Expand Down
1 change: 1 addition & 0 deletions src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "object.hpp"
#include "critter.hpp"
#include "scenery.hpp"
#include "scenery-proto.hpp"
#include "light.hpp"
#include "compat/shared-ptr-wrapper.hpp"
#include "compat/int-hash.hpp"
Expand Down
2 changes: 1 addition & 1 deletion test/critter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include "compat/shared-ptr-wrapper.hpp"
#include "compat/function2.hpp"
#include "src/critter.hpp"
#include "src/scenery-proto.hpp"
#include "src/world.hpp"
#include "src/wall-atlas.hpp"
#include "src/nanosecond.inl"
#include "src/log.hpp"
#include "src/point.inl"
#include "loader/loader.hpp"
#include "src/scenery.hpp"

#include <cinttypes>
#include <cstdio>
Expand Down
3 changes: 1 addition & 2 deletions test/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#include "src/ground-atlas.hpp"
#include "src/wall-atlas.hpp"
#include "src/anim-atlas.hpp"
#include "src/scenery.hpp"
#include <mg/Texture.h>
#include "src/scenery-proto.hpp"

namespace floormat {

Expand Down
2 changes: 1 addition & 1 deletion test/path-search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "loader/loader.hpp"
#include "loader/wall-cell.hpp"
#include "src/world.hpp"
#include "src/scenery.hpp"
#include "src/scenery-proto.hpp"
#include "src/search-bbox.hpp"
#include "src/search-constants.hpp"
#include <Magnum/Math/Functions.h>
Expand Down
1 change: 1 addition & 0 deletions test/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "src/world.hpp"
#include "loader/loader.hpp"
#include "src/scenery.hpp"
#include "src/scenery-proto.hpp"
#include "src/critter.hpp"
#include "src/light.hpp"
#include "src/ground-atlas.hpp"
Expand Down

0 comments on commit b4f0ae9

Please sign in to comment.