From ea8840291cdf66784c6a2cb465b63ccfb5483c38 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Sep 2014 01:38:30 +0200 Subject: New event-driven goodness --- src/model/Entity.hpp | 54 +++++++++++++++++++++------------------------------- src/model/Map.cpp | 2 +- src/model/Map.hpp | 4 ++-- 3 files changed, 25 insertions(+), 35 deletions(-) (limited to 'src/model') diff --git a/src/model/Entity.hpp b/src/model/Entity.hpp index fa4477d..1ff444a 100644 --- a/src/model/Entity.hpp +++ b/src/model/Entity.hpp @@ -46,8 +46,8 @@ private: Position pos; Direction direction; - unsigned transition = 0; - unsigned step = 0; + uint64_t transitionStart = 0; + uint64_t transitionEnd = 0; Position translate(Direction dir, float amount) const { Position p = pos; @@ -81,9 +81,14 @@ public: return name; } - Position getPosition() const { - if (transition) - return translate(direction, (float)step / transition); + Position getPosition(uint64_t time) const { + if (hasTransition()) + if (time <= transitionStart) + return pos; + else if (time >= transitionEnd) + return translate(direction, 1); + else + return translate(direction, float(time-transitionStart)/float(transitionEnd-transitionStart)); else return pos; } @@ -92,43 +97,28 @@ public: return direction; } - void moveTo(float newX, float newY) { - pos.x = newX; - pos.y = newY; + void moveTo(Position newPos) { + pos = newPos; + transitionStart = transitionEnd = 0; } - void move(Direction dir, unsigned steps = 0) { - if (transition) + void move(Direction dir, uint64_t start, uint64_t end) { + if (hasTransition()) return; direction = dir; - if (steps) - transition = steps; - else - pos = translate(direction, 1); + transitionStart = start; + transitionEnd = end; } - bool hasTransition() const { - return transition; + void finishTransition() { + if (hasTransition()) + moveTo(translate(direction, 1)); } - unsigned advance(unsigned ticks) { - if (!transition) - return ticks; - - step += ticks; - - if (step >= transition) { - ticks = step - transition; - transition = step = 0; - pos = translate(direction, 1); - - return ticks; - } - else { - return 0; - } + bool hasTransition() const { + return transitionEnd; } }; diff --git a/src/model/Map.cpp b/src/model/Map.cpp index 8081255..db18baf 100644 --- a/src/model/Map.cpp +++ b/src/model/Map.cpp @@ -46,7 +46,7 @@ std::shared_ptr Map::load(__attribute__((unused)) const std::string &name) } map->playerEntity = std::make_shared("square"); - map->playerEntity->moveTo(6, 6); + map->playerEntity->moveTo(Model::Position{6, 6}); map->entities.push_back(map->playerEntity); diff --git a/src/model/Map.hpp b/src/model/Map.hpp index 5d6ba01..9d4eded 100644 --- a/src/model/Map.hpp +++ b/src/model/Map.hpp @@ -67,8 +67,8 @@ public: return entities; } - Entity & getPlayerEntity() const { - return *playerEntity; + const std::shared_ptr & getPlayerEntity() const { + return playerEntity; } size_t getWidth() const { -- cgit v1.2.3