summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/character.cpp7
-rw-r--r--src/character.hpp4
-rw-r--r--src/entity.cpp17
-rw-r--r--src/entity.hpp10
-rw-r--r--src/scenery.cpp7
-rw-r--r--src/scenery.hpp6
-rw-r--r--src/world.cpp2
-rw-r--r--src/world.hpp10
8 files changed, 38 insertions, 25 deletions
diff --git a/src/character.cpp b/src/character.cpp
index 31376925..51c243c2 100644
--- a/src/character.cpp
+++ b/src/character.cpp
@@ -54,7 +54,6 @@ constexpr auto arrows_to_dir(bool L, bool R, bool U, bool D)
character_proto::character_proto(const character_proto&) = default;
character_proto::~character_proto() noexcept = default;
character_proto& character_proto::operator=(const character_proto&) = default;
-character::~character() = default;
character_proto::character_proto()
{
@@ -132,6 +131,8 @@ bool character::update(size_t i, float dt)
return true;
}
+entity_type character::type() const noexcept { return entity_type::character; }
+
character::operator character_proto() const
{
character_proto ret;
@@ -141,8 +142,8 @@ character::operator character_proto() const
return ret;
}
-character::character(object_id id, struct chunk& c, entity_type type, const character_proto& proto) :
- entity{id, c, type, proto},
+character::character(object_id id, struct chunk& c, const character_proto& proto) :
+ entity{id, c, proto},
name{proto.name},
playable{proto.playable}
{
diff --git a/src/character.hpp b/src/character.hpp
index f4c934fa..24e0e173 100644
--- a/src/character.hpp
+++ b/src/character.hpp
@@ -23,7 +23,7 @@ struct character_proto : entity_proto
struct character final : entity
{
- ~character() override;
+ entity_type type() const noexcept override;
explicit operator character_proto() const;
void set_keys(bool L, bool R, bool U, bool D);
@@ -39,7 +39,7 @@ private:
static Vector2 move_vec(int left_right, int top_bottom);
friend struct world;
- character(object_id id, struct chunk& c, entity_type type, const character_proto& proto);
+ character(object_id id, struct chunk& c, const character_proto& proto);
};
template<> struct entity_type_<struct character> : std::integral_constant<entity_type, entity_type::character> {};
diff --git a/src/entity.cpp b/src/entity.cpp
index cc7bc00d..8822326e 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -13,14 +13,14 @@ entity_proto& entity_proto::operator=(const entity_proto&) = default;
entity_proto::~entity_proto() noexcept = default;
entity_proto::entity_proto() = default;
entity_proto::entity_proto(const entity_proto&) = default;
+entity_type entity_proto::type_of() const noexcept { return type; }
-entity::entity(object_id id, struct chunk& c, entity_type type, const entity_proto& proto) :
+entity::entity(object_id id, struct chunk& c, const entity_proto& proto) :
id{id}, c{&c}, atlas{proto.atlas},
offset{proto.offset}, bbox_offset{proto.bbox_offset},
bbox_size{proto.bbox_size}, delta{proto.delta},
- frame{proto.frame}, type{type}, r{proto.r}, pass{proto.pass}
+ frame{proto.frame}, r{proto.r}, pass{proto.pass}
{
- fm_assert(type == proto.type);
if (atlas)
{
fm_soft_assert(atlas->check_rotation(r));
@@ -60,7 +60,7 @@ float entity_proto::ordinal(local_coords local) const
float entity::ordinal() const
{
- return ordinal(coord.local(), offset, type);
+ return ordinal(coord.local(), offset, type());
}
float entity::ordinal(local_coords xy, Vector2b offset, entity_type type)
@@ -181,7 +181,7 @@ size_t entity::move_to(size_t i, Vector2i delta, rotation new_r)
const auto bb_size = rotate_size(bbox_size, r, new_r);
bool b0 = c->_bbox_for_scenery(*this, bb0),
b1 = c->_bbox_for_scenery(*this, coord_.local(), offset_, bb_offset, bb_size, bb1);
- const auto ord = ordinal(coord_.local(), offset_, type);
+ const auto ord = ordinal(coord_.local(), offset_, type());
if (coord_.chunk() == coord.chunk())
{
@@ -239,7 +239,7 @@ entity::operator entity_proto() const
ret.bbox_size = bbox_size;
ret.delta = delta;
ret.frame = frame;
- ret.type = type;
+ ret.type = type();
ret.r = r;
ret.pass = pass;
return ret;
@@ -265,4 +265,9 @@ bool entity::is_dynamic() const
return atlas->info().fps > 0;
}
+entity_type entity::type_of() const noexcept
+{
+ return type();
+}
+
} // namespace floormat
diff --git a/src/entity.hpp b/src/entity.hpp
index 490f0b7d..d759cbd1 100644
--- a/src/entity.hpp
+++ b/src/entity.hpp
@@ -32,6 +32,8 @@ struct entity_proto
virtual bool operator==(const entity_proto&) const;
virtual ~entity_proto() noexcept;
+
+ entity_type type_of() const noexcept;
};
struct entity
@@ -45,7 +47,6 @@ struct entity
const Vector2b offset, bbox_offset;
const Vector2ub bbox_size;
uint16_t delta = 0, frame = 0;
- const entity_type type;
const rotation r = rotation::N;
const pass_mode pass = pass_mode::see_through;
@@ -59,6 +60,7 @@ struct entity
explicit operator entity_proto() const;
+ virtual entity_type type() const noexcept = 0;
virtual bool can_activate(size_t i) const;
virtual bool activate(size_t i);
virtual bool update(size_t i, float dt) = 0;
@@ -67,18 +69,18 @@ struct entity
virtual bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize);
virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass);
+ entity_type type_of() const noexcept;
static Pair<global_coords, Vector2b> normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta);
+ bool is_dynamic() const;
bool can_rotate(rotation new_r);
bool can_move_to(Vector2i delta);
size_t move_to(size_t i, Vector2i delta, rotation new_r);
- bool is_dynamic() const;
-
friend struct world;
protected:
- entity(object_id id, struct chunk& c, entity_type type, const entity_proto& proto);
+ entity(object_id id, struct chunk& c, const entity_proto& proto);
void set_bbox_(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass);
};
diff --git a/src/scenery.cpp b/src/scenery.cpp
index a424351f..bfa69142 100644
--- a/src/scenery.cpp
+++ b/src/scenery.cpp
@@ -100,6 +100,8 @@ bool scenery_proto::operator==(const entity_proto& e0) const
closing == s0.closing && interactive == s0.interactive;
}
+entity_type scenery::type() const noexcept { return entity_type::scenery; }
+
scenery::operator scenery_proto() const
{
scenery_proto ret;
@@ -111,11 +113,10 @@ scenery::operator scenery_proto() const
return ret;
}
-scenery::scenery(object_id id, struct chunk& c, entity_type type_, const scenery_proto& proto) :
- entity{id, c, type_, proto}, sc_type{proto.sc_type}, active{proto.active},
+scenery::scenery(object_id id, struct chunk& c, const scenery_proto& proto) :
+ entity{id, c, proto}, sc_type{proto.sc_type}, active{proto.active},
closing{proto.closing}, interactive{proto.interactive}
{
- fm_assert(type == proto.type);
}
} // namespace floormat
diff --git a/src/scenery.hpp b/src/scenery.hpp
index a985d7d7..b52ddded 100644
--- a/src/scenery.hpp
+++ b/src/scenery.hpp
@@ -41,14 +41,16 @@ struct scenery final : entity
unsigned char closing : 1 = false;
unsigned char interactive : 1 = false;
+ bool update(size_t i, float dt) override;
bool can_activate(size_t i) const override;
bool activate(size_t i) override;
- bool update(size_t i, float dt) override;
+
+ entity_type type() const noexcept override;
explicit operator scenery_proto() const;
private:
friend struct world;
- scenery(object_id id, struct chunk& c, entity_type type, const scenery_proto& proto);
+ scenery(object_id id, struct chunk& c, const scenery_proto& proto);
};
template<> struct entity_type_<scenery> : std::integral_constant<entity_type, entity_type::scenery> {};
diff --git a/src/world.cpp b/src/world.cpp
index 07cfe196..8c564007 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -117,7 +117,7 @@ void world::do_make_entity(const std::shared_ptr<entity>& e, global_coords pos,
fm_debug_assert(_unique_id && e->c->world()._unique_id == _unique_id);
fm_assert(!_entities.contains(e->id));
fm_assert(Vector2ui(e->bbox_size).product() > 0);
- fm_assert(e->type != entity_type::none);
+ fm_assert(e->type() != entity_type::none);
const_cast<global_coords&>(e->coord) = pos;
_entities[e->id] = e;
if (sorted)
diff --git a/src/world.hpp b/src/world.hpp
index 5d9b8eb6..d269acf6 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -68,11 +68,13 @@ public:
size_t collect_threshold() const noexcept { return _collect_every; }
template<typename T, bool sorted = true, typename... Xs>
- requires requires(chunk& c) { T{object_id(), c, entity_type(), std::declval<Xs>()...}; }
+ requires requires(chunk& c) {
+ T{object_id(), c, std::declval<Xs>()...};
+ std::is_base_of_v<entity, T>;
+ }
std::shared_ptr<T> make_entity(object_id id, global_coords pos, Xs&&... xs)
{
- static_assert(std::is_base_of_v<entity, T>);
- auto ret = std::shared_ptr<T>(new T{id, operator[](pos.chunk()), entity_type_<T>::value, std::forward<Xs>(xs)...});
+ auto ret = std::shared_ptr<T>(new T{id, operator[](pos.chunk()), std::forward<Xs>(xs)...});
do_make_entity(std::static_pointer_cast<entity>(ret), pos, sorted);
return ret;
}
@@ -110,7 +112,7 @@ std::shared_ptr<T> world::find_entity(object_id id)
return ptr;
else
{
- fm_soft_assert(ptr->type == entity_type_<T>::value);
+ fm_soft_assert(ptr->type() == entity_type_<T>::value);
return std::static_pointer_cast<T>(std::move(ptr));
}
}