You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.8 KiB

9 years ago
#include "Explorer.h"
#include <iostream>
#include "Pather.h"
9 years ago
Explorer::Explorer(Map* map_) {
9 years ago
color = sf::Color::Blue;
position = sf::Vector2i(6, 10);
9 years ago
map = map_;
}
Explorer::~Explorer() {
}
sf::Vector2i Explorer::getPosition() {
return position;
}
sf::Color Explorer::getColor() {
return color;
}
void Explorer::setDestination(sf::Vector2i destination_) {
plan(destination_);
}
9 years ago
bool Explorer::move() {
// While there are moves for us to take
if (!movement_stack.empty()) {
bool valid = false; // If the next move is valid, collision
int x = movement_stack.back();
9 years ago
switch (x) {
case 0:
break;
9 years ago
case 1: // North
9 years ago
if (!map->getTile(position.x, position.y - 1)->isSolid()) {
valid = true;
position = sf::Vector2i(position.x, position.y - 1);
}
break;
case 2: // East
9 years ago
if (!map->getTile(position.x + 1, position.y)->isSolid()) {
valid = true;
position = sf::Vector2i(position.x + 1, position.y);
}
break;
case 3: // South
9 years ago
if (!map->getTile(position.x, position.y + 1)->isSolid()) {
valid = true;
position = sf::Vector2i(position.x, position.y + 1);
}
break;
case 4: // West
9 years ago
if (!map->getTile(position.x - 1, position.y)->isSolid()) {
valid = true;
position = sf::Vector2i(position.x - 1, position.y);
}
break;
}
// If the path was blocked
if (!valid) {
std::cout << "Path blocked" << std::endl;
// Flush the moves list
while (!movement_stack.empty()) {
movement_stack.pop_back();
9 years ago
}
return false;
}
// If everything went well, pop and return true
movement_stack.pop_back();
9 years ago
return true;
}
else
return false;
}
// A*
bool Explorer::plan(sf::Vector2i destination_) {
Pather pather(map);
movement_stack = pather.pathTo(position, destination_);
9 years ago
9 years ago
return true;
}