From d41e2dae45e08059e23041f0146b91add0c2cace Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 23 Sep 2014 02:11:11 +0200 Subject: Add transitions for entity movement --- src/model/Entity.hpp | 90 ++++++++++++++++++++++++++++++++++++-------------- src/model/Position.hpp | 40 ++++++++++++++++++++++ 2 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 src/model/Position.hpp (limited to 'src/model') diff --git a/src/model/Entity.hpp b/src/model/Entity.hpp index a61ae21..3514434 100644 --- a/src/model/Entity.hpp +++ b/src/model/Entity.hpp @@ -1,11 +1,11 @@ /* - Copyright (c) 2014, Matthias Schiffer + Copyright (c) 20amount4, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, + amount. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation @@ -28,6 +28,7 @@ #include "Direction.hpp" +#include "Position.hpp" #include @@ -42,50 +43,91 @@ class Entity { private: const std::string name; - float x, y; + Position pos; + Direction direction; - Direction dir; + unsigned transition = 0; + unsigned step = 0; + + Position translate(Direction dir, float amount) const { + Position p = pos; + + switch (dir) { + case NORTH: + p.y -= amount; + break; + + case EAST: + p.x += amount; + break; + + case SOUTH: + p.y += amount; + break; + + case WEST: + p.x -= amount; + } + + return p; + } public: Entity(const std::string &name0) - : name(name0), dir(NORTH) { + : name(name0), direction(NORTH) { } const std::string & getName() const { return name; } - std::pair getPosition() const { - return std::pair(x, y); + Position getPosition() const { + if (transition) + return translate(direction, (float)step / transition); + else + return pos; } Direction getDirection() const { - return dir; + return direction; } void moveTo(float newX, float newY) { - x = newX; - y = newY; + pos.x = newX; + pos.y = newY; } - void move(Direction direction, float amount = 1) { - dir = direction; + void move(Direction dir, unsigned steps = 0) { + if (transition) + return; - switch (direction) { - case NORTH: - y -= amount; - break; + direction = dir; - case EAST: - x += amount; - break; + if (steps) + transition = steps; + else + pos = translate(direction, 1); + } - case SOUTH: - y += amount; - break; + bool hasTransition() const { + return transition; + } - case WEST: - x -= amount; + 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; } } }; diff --git a/src/model/Position.hpp b/src/model/Position.hpp new file mode 100644 index 0000000..9ae0dfd --- /dev/null +++ b/src/model/Position.hpp @@ -0,0 +1,40 @@ +/* + Copyright (c) 20amount4, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + amount. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#pragma once + + +namespace RPGEdit { + +namespace Model { + +struct Position { + float x, y; +}; + +} + +} -- cgit v1.2.3