summaryrefslogtreecommitdiffstats
path: root/src/model/Map.cpp
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-09-25 20:53:17 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-09-25 20:53:17 +0200
commitd82f3b7665435abefe84c8dbc16483acd235b478 (patch)
tree473689f592085a076e45d1cade590ad207cc4840 /src/model/Map.cpp
parenteb40e3de7226d0005ce0a41c67c9355e675a489b (diff)
downloadrpgedit-d82f3b7665435abefe84c8dbc16483acd235b478.tar
rpgedit-d82f3b7665435abefe84c8dbc16483acd235b478.zip
Move external entity state from Entity to Map
Diffstat (limited to 'src/model/Map.cpp')
-rw-r--r--src/model/Map.cpp59
1 files changed, 57 insertions, 2 deletions
diff --git a/src/model/Map.cpp b/src/model/Map.cpp
index faead6d..a5b5ee9 100644
--- a/src/model/Map.cpp
+++ b/src/model/Map.cpp
@@ -31,6 +31,62 @@ namespace RPGEdit {
namespace Model {
+Position Map::getEntityPosition(const Entity *entity, uint64_t time) const {
+ const EntityState &state = entityStates.at(entity);
+
+ if (state.transitionEnd)
+ if (time <= state.transitionStart)
+ return state.position;
+ else if (time >= state.transitionEnd)
+ return state.position + state.direction;
+ else
+ return state.position.translate(state.direction,
+ float(time-state.transitionStart)/
+ float(state.transitionEnd-state.transitionStart));
+ else
+ return state.position;
+}
+
+bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end) {
+ EntityState &state = entityStates.at(entity);
+
+ if (state.transitionEnd)
+ return false;
+
+ entity->setDirection(dir);
+
+ Position pos = state.position + dir;
+
+ if (getCollisionAt(pos.x, pos.y) == BLOCKED)
+ return false;
+
+ setCollisionAt(pos.x, pos.y, BLOCKED);
+
+ state.transitionStart = start;
+ state.transitionEnd = end;
+ state.direction = dir;
+
+ return true;
+}
+
+void Map::moveEntityTo(Entity *entity, Position pos) {
+ EntityState &state = entityStates.at(entity);
+
+ setCollisionAt(state.position.x, state.position.y, EMPTY);
+ setCollisionAt(pos.x, pos.y, BLOCKED);
+
+ state.position = pos;
+ state.transitionStart = state.transitionEnd = 0;
+
+}
+
+void Map::finishEntityTransition(Entity *entity) {
+ EntityState &state = entityStates.at(entity);
+
+ if (state.transitionEnd)
+ moveEntityTo(entity, state.position + state.direction);
+}
+
std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name) {
std::unique_ptr<Map> map(new Map(16, 16, 2));
@@ -54,8 +110,7 @@ std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name)
}
}
- map->entities.emplace_back("square");
- map->entities.back().moveTo(map.get(), Model::Position{6, 6});
+ map->addEntity("square", Model::Position{6, 6});
return map;
}