From d82f3b7665435abefe84c8dbc16483acd235b478 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 25 Sep 2014 20:53:17 +0200 Subject: Move external entity state from Entity to Map --- src/model/Map.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'src/model/Map.cpp') 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::load(__attribute__((unused)) const std::string &name) { std::unique_ptr map(new Map(16, 16, 2)); @@ -54,8 +110,7 @@ std::unique_ptr 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; } -- cgit v1.2.3