From 8b4ca336ce68cc17573115dd07eb1bfc6521b298 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 25 Sep 2014 23:33:50 +0200 Subject: Differentiate between int and float positions --- src/model/Map.cpp | 22 ++++++++++++---------- src/model/Map.hpp | 10 +++++----- src/model/Position.hpp | 12 +++++++++--- 3 files changed, 26 insertions(+), 18 deletions(-) (limited to 'src/model') diff --git a/src/model/Map.cpp b/src/model/Map.cpp index a5b5ee9..b7b40f5 100644 --- a/src/model/Map.cpp +++ b/src/model/Map.cpp @@ -31,20 +31,22 @@ namespace RPGEdit { namespace Model { -Position Map::getEntityPosition(const Entity *entity, uint64_t time) const { +Position Map::getEntityPosition(const Entity *entity, uint64_t time) const { const EntityState &state = entityStates.at(entity); + Position p(state.position); + if (state.transitionEnd) if (time <= state.transitionStart) - return state.position; + return p; else if (time >= state.transitionEnd) - return state.position + state.direction; + return p + state.direction; else - return state.position.translate(state.direction, - float(time-state.transitionStart)/ - float(state.transitionEnd-state.transitionStart)); + return p.translate(state.direction, + float(time-state.transitionStart)/ + float(state.transitionEnd-state.transitionStart)); else - return state.position; + return p; } bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end) { @@ -55,7 +57,7 @@ bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end entity->setDirection(dir); - Position pos = state.position + dir; + Position pos = state.position + dir; if (getCollisionAt(pos.x, pos.y) == BLOCKED) return false; @@ -69,7 +71,7 @@ bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end return true; } -void Map::moveEntityTo(Entity *entity, Position pos) { +void Map::moveEntityTo(Entity *entity, Position pos) { EntityState &state = entityStates.at(entity); setCollisionAt(state.position.x, state.position.y, EMPTY); @@ -110,7 +112,7 @@ std::unique_ptr Map::load(__attribute__((unused)) const std::string &name) } } - map->addEntity("square", Model::Position{6, 6}); + map->addEntity("square", Model::Position{6, 6}); return map; } diff --git a/src/model/Map.hpp b/src/model/Map.hpp index c7b9783..4e434b1 100644 --- a/src/model/Map.hpp +++ b/src/model/Map.hpp @@ -58,13 +58,13 @@ protected: class Map : private _Map { private: struct EntityState { - Position position; + Position position; Direction direction; uint64_t transitionStart = 0; uint64_t transitionEnd = 0; - EntityState(const Position &position0) : position(position0) {} + EntityState(const Position &position0) : position(position0) {} }; std::vector> entities; @@ -123,7 +123,7 @@ public: return entities; } - Entity * addEntity(const std::string &name, const Position &pos) { + Entity * addEntity(const std::string &name, const Position &pos) { Entity *e = new Entity(name); pushEntity(e, EntityState(pos)); setCollisionAt(pos.x, pos.y, BLOCKED); @@ -171,9 +171,9 @@ public: return tiles[layer][y*width + x]; } - Position getEntityPosition(const Entity *entity, uint64_t time) const; + Position getEntityPosition(const Entity *entity, uint64_t time) const; bool moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end); - void moveEntityTo(Entity *entity, Position pos); + void moveEntityTo(Entity *entity, Position pos); void finishEntityTransition(Entity *entity); static std::unique_ptr load(const std::string &name); diff --git a/src/model/Position.hpp b/src/model/Position.hpp index 84044e6..a88c151 100644 --- a/src/model/Position.hpp +++ b/src/model/Position.hpp @@ -33,10 +33,11 @@ namespace RPGEdit { namespace Model { +template struct Position { - float x, y; + T x, y; - Position translate(Direction dir, float amount) const { + Position translate(Direction dir, T amount) const { Position p = *this; switch (dir) { @@ -59,9 +60,14 @@ struct Position { return p; } - Position operator+(Direction dir) const { + Position operator+(Direction dir) const { return translate(dir, 1); } + + template + explicit operator Position() const { + return Position{T2(x), T2(y)}; + } }; } -- cgit v1.2.3