Differentiate between int and float positions
This commit is contained in:
parent
d82f3b7665
commit
8b4ca336ce
7 changed files with 31 additions and 23 deletions
|
@ -35,7 +35,7 @@ MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const s
|
|||
: eventBus(eventBus0), inputHandler(inputHandler0), map(map0) {
|
||||
view = std::unique_ptr<View::MapView>(new View::MapView(window, map.getTileset()));
|
||||
|
||||
playerEntity = map.addEntity("square", Model::Position{8, 8});
|
||||
playerEntity = map.addEntity("square", Model::Position<int>{8, 8});
|
||||
|
||||
view->updateEntities(map.getEntities());
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ private:
|
|||
void movePlayerContinue(uint64_t time);
|
||||
void keyPressed(uint16_t key, uint64_t time);
|
||||
|
||||
Model::Position getViewPosition(uint64_t time) {
|
||||
Model::Position<float> getViewPosition(uint64_t time) {
|
||||
return map.getEntityPosition(playerEntity, time);
|
||||
}
|
||||
|
||||
|
|
|
@ -31,20 +31,22 @@ namespace RPGEdit {
|
|||
|
||||
namespace Model {
|
||||
|
||||
Position Map::getEntityPosition(const Entity *entity, uint64_t time) const {
|
||||
Position<float> Map::getEntityPosition(const Entity *entity, uint64_t time) const {
|
||||
const EntityState &state = entityStates.at(entity);
|
||||
|
||||
Position<float> p(state.position);
|
||||
|
||||
if (state.transitionEnd)
|
||||
if (time <= state.transitionStart)
|
||||
return state.position;
|
||||
return p;
|
||||
else if (time >= state.transitionEnd)
|
||||
return state.position + state.direction;
|
||||
return p + state.direction;
|
||||
else
|
||||
return state.position.translate(state.direction,
|
||||
float(time-state.transitionStart)/
|
||||
float(state.transitionEnd-state.transitionStart));
|
||||
return p.translate(state.direction,
|
||||
float(time-state.transitionStart)/
|
||||
float(state.transitionEnd-state.transitionStart));
|
||||
else
|
||||
return state.position;
|
||||
return p;
|
||||
}
|
||||
|
||||
bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end) {
|
||||
|
@ -55,7 +57,7 @@ bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end
|
|||
|
||||
entity->setDirection(dir);
|
||||
|
||||
Position pos = state.position + dir;
|
||||
Position<int> pos = state.position + dir;
|
||||
|
||||
if (getCollisionAt(pos.x, pos.y) == BLOCKED)
|
||||
return false;
|
||||
|
@ -69,7 +71,7 @@ bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end
|
|||
return true;
|
||||
}
|
||||
|
||||
void Map::moveEntityTo(Entity *entity, Position pos) {
|
||||
void Map::moveEntityTo(Entity *entity, Position<int> pos) {
|
||||
EntityState &state = entityStates.at(entity);
|
||||
|
||||
setCollisionAt(state.position.x, state.position.y, EMPTY);
|
||||
|
@ -110,7 +112,7 @@ std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name)
|
|||
}
|
||||
}
|
||||
|
||||
map->addEntity("square", Model::Position{6, 6});
|
||||
map->addEntity("square", Model::Position<int>{6, 6});
|
||||
|
||||
return map;
|
||||
}
|
||||
|
|
|
@ -58,13 +58,13 @@ protected:
|
|||
class Map : private _Map {
|
||||
private:
|
||||
struct EntityState {
|
||||
Position position;
|
||||
Position<int> position;
|
||||
|
||||
Direction direction;
|
||||
uint64_t transitionStart = 0;
|
||||
uint64_t transitionEnd = 0;
|
||||
|
||||
EntityState(const Position &position0) : position(position0) {}
|
||||
EntityState(const Position<int> &position0) : position(position0) {}
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
|
@ -123,7 +123,7 @@ public:
|
|||
return entities;
|
||||
}
|
||||
|
||||
Entity * addEntity(const std::string &name, const Position &pos) {
|
||||
Entity * addEntity(const std::string &name, const Position<int> &pos) {
|
||||
Entity *e = new Entity(name);
|
||||
pushEntity(e, EntityState(pos));
|
||||
setCollisionAt(pos.x, pos.y, BLOCKED);
|
||||
|
@ -171,9 +171,9 @@ public:
|
|||
return tiles[layer][y*width + x];
|
||||
}
|
||||
|
||||
Position getEntityPosition(const Entity *entity, uint64_t time) const;
|
||||
Position<float> getEntityPosition(const Entity *entity, uint64_t time) const;
|
||||
bool moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end);
|
||||
void moveEntityTo(Entity *entity, Position pos);
|
||||
void moveEntityTo(Entity *entity, Position<int> pos);
|
||||
void finishEntityTransition(Entity *entity);
|
||||
|
||||
static std::unique_ptr<Map> load(const std::string &name);
|
||||
|
|
|
@ -33,10 +33,11 @@ namespace RPGEdit {
|
|||
|
||||
namespace Model {
|
||||
|
||||
template<typename T>
|
||||
struct Position {
|
||||
float x, y;
|
||||
T x, y;
|
||||
|
||||
Position translate(Direction dir, float amount) const {
|
||||
Position translate(Direction dir, T amount) const {
|
||||
Position p = *this;
|
||||
|
||||
switch (dir) {
|
||||
|
@ -59,9 +60,14 @@ struct Position {
|
|||
return p;
|
||||
}
|
||||
|
||||
Position operator+(Direction dir) const {
|
||||
Position<T> operator+(Direction dir) const {
|
||||
return translate(dir, 1);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
explicit operator Position<T2>() const {
|
||||
return Position<T2>{T2(x), T2(y)};
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ void MapView::clearEntities() {
|
|||
entitySprites.clear();
|
||||
}
|
||||
|
||||
void MapView::render(const Model::Map *map, Model::Position center, uint64_t time) {
|
||||
void MapView::render(const Model::Map *map, Model::Position<float> center, uint64_t time) {
|
||||
SDL_RenderClear(window->getRenderer());
|
||||
|
||||
std::pair<int, int> viewport = window->getViewport();
|
||||
|
@ -137,7 +137,7 @@ void MapView::render(const Model::Map *map, Model::Position center, uint64_t tim
|
|||
}
|
||||
|
||||
for (auto &entity : map->getEntities()) {
|
||||
Model::Position pos = map->getEntityPosition(entity.get(), time);
|
||||
Model::Position<float> pos = map->getEntityPosition(entity.get(), time);
|
||||
Model::Direction dir = entity->getDirection();
|
||||
|
||||
SDL_Rect src = {
|
||||
|
|
|
@ -58,7 +58,7 @@ public:
|
|||
|
||||
void clear();
|
||||
|
||||
void render(const Model::Map *map, Model::Position center, uint64_t time);
|
||||
void render(const Model::Map *map, Model::Position<float> center, uint64_t time);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue