diff options
Diffstat (limited to 'src/model/Map.cpp')
-rw-r--r-- | src/model/Map.cpp | 59 |
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; } |