summaryrefslogtreecommitdiffstats
path: root/src/model/Entity.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/Entity.hpp')
-rw-r--r--src/model/Entity.hpp90
1 files changed, 66 insertions, 24 deletions
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 <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;
}
}
};