Add transitions for entity movement
This commit is contained in:
parent
0c3dbabb92
commit
d41e2dae45
6 changed files with 144 additions and 50 deletions
|
@ -1,11 +1,11 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 20amount4, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
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 <string>
|
||||
|
@ -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<float, float> getPosition() const {
|
||||
return std::pair<float, float>(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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
40
src/model/Position.hpp
Normal file
40
src/model/Position.hpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Copyright (c) 20amount4, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue