117 lines
3.4 KiB
C++
117 lines
3.4 KiB
C++
/*
|
|
Copyright (c) 2014, 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,
|
|
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.
|
|
*/
|
|
|
|
|
|
#include "Map.hpp"
|
|
|
|
|
|
namespace RPGEdit {
|
|
|
|
namespace Model {
|
|
|
|
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 p;
|
|
else if (time >= state.transitionEnd)
|
|
return p + state.direction;
|
|
else
|
|
return p.translate(state.direction,
|
|
float(time-state.transitionStart)/
|
|
float(state.transitionEnd-state.transitionStart));
|
|
else
|
|
return p;
|
|
}
|
|
|
|
bool Map::moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end) {
|
|
EntityState &state = entityStates.at(entity);
|
|
|
|
if (state.transitionEnd)
|
|
return false;
|
|
|
|
entity->setDirection(dir);
|
|
|
|
if (isBlocked(state.position + dir))
|
|
return false;
|
|
|
|
state.transitionStart = start;
|
|
state.transitionEnd = end;
|
|
state.direction = dir;
|
|
|
|
return true;
|
|
}
|
|
|
|
void Map::moveEntityTo(Entity *entity, Position<int> pos) {
|
|
EntityState &state = entityStates.at(entity);
|
|
|
|
removeEntityPosition(state.position, entity);
|
|
addEntityPosition(pos, entity);
|
|
|
|
state.position = pos;
|
|
state.transitionStart = state.transitionEnd = 0;
|
|
}
|
|
|
|
void Map::finishEntityTransition(Entity *entity) {
|
|
EntityState &state = entityStates.at(entity);
|
|
|
|
if (state.transitionEnd)
|
|
moveEntityTo(entity, state.position + state.direction);
|
|
}
|
|
|
|
std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name) {
|
|
std::unique_ptr<Map> map(new Map(16, 16, 2));
|
|
|
|
map->tileset.push_back("dirt");
|
|
map->tileset.push_back("horizontal_bar");
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
for (int j = 0; j < 16; j++) {
|
|
if (4 <= i && i < 12 && 4 <= j && j < 12) {
|
|
map->setTileAt(0, Position<int>{i, j}, 1);
|
|
map->setCollisionAt(Position<int>{i, j}, CollisionType::EMPTY);
|
|
}
|
|
else {
|
|
map->setTileAt(0, Position<int>{i, j}, 0);
|
|
}
|
|
|
|
if (4 <= i && i < 12 && j == 8)
|
|
map->setTileAt(1, Position<int>{i, j}, 2);
|
|
else
|
|
map->setTileAt(1, Position<int>{i, j}, 0);
|
|
}
|
|
}
|
|
|
|
map->addEntity("square", Model::Position<int>{6, 6});
|
|
|
|
return map;
|
|
}
|
|
|
|
}
|
|
|
|
}
|