MainMap.h OnUpdate:
void OnUpdate()
{
// On efface l'écran
clear(sf::Color::White);
if (errCode == 0) {
draw(map);
perso.updateMoving();
draw(perso);
sf::Event e;
while (this->pollEvent(e)) {
if (e.type == sf::Event::MouseButtonPressed)
{
//qDebug() << "ButtonPressed";
if (e.mouseButton.button == sf::Mouse::Left) {
//qDebug() << "Mouse Left Pressed";
if (!isHidden()) {
//qDebug() << "Not hidden";
sf::Vector2i mousePos = sf::Mouse::getPosition(*this);
perso.startMoving(mousePos.x, mousePos.y);
}
}
}
}
}
}
Perso.h quelques fonctions:
void startMoving(int x, int y) { //OK
//qDebug() << "startMoving starts.";
if (isMoving)
return;
sf::Vector2u tile = getTileIso(sf::Vector2i(x, y), m_tex->images.getPath("schemepng"));
if (tile.x > 0 && tile.y > 0 && m_posX < tile.x && m_posY < tile.y)
calculateMove(tile.x, tile.y);
//qDebug() << "Move calculated";
}
void calculateMove (unsigned int x, unsigned int y) { // OK
if (x == 0 && y == 0) return;
pathToGo.unsignedPathFinding(m_posX, m_posY, x, y);
isMoving = true;
//qDebug() << "calculateMove success.";
}
void updateMoving() { // OK
if (isMoving) {
if (mgoX != 0 || mgoY != 0) {
Orientation o;
if (mgoX > 0 && mgoY == 0)
o = Orientation::Droite;
if (mgoX > 0 && mgoY > 0 && mgoX == mgoY)
o = Orientation::DroiteBas;
if (mgoX == 0 && mgoY > 0)
o = Orientation::Bas;
if (j == Jambe::Gauche || j == Jambe::Normal)
orienterPerso(o, Jambe::Droite);
if (j == Jambe::Droite)
orienterPerso(o, Jambe::Gauche);
if (m_goX == 0 && m_goY == 0) {
mgoX = 0;
mgoY = 0;
isMoving = false;
return;
} else {
if (m_goX > 0) {
m_perso.move(4.f, 0.f);
m_pos.x += 4.f;
}
if (m_goY > 0) {
m_perso.move(0.f, 4.f);
m_pos.x += 4.f;
}
}
return;
}
if (pathToGo.x_isEmpty() || pathToGo.y_isEmpty()) {
pathToGo.eraseActualPath();
if (pathToGo.xPath_isEmpty() || pathToGo.yPath_isEmpty()) {
pathToGo.eraseTestPath();
orienterPerso(direction);
isMoving = false;
return;
} else
pathToGo.activateTestPath();
}
if (pathToGo.x_isEmpty() || pathToGo.y_isEmpty()) {
isMoving = false;
orienterPerso(direction);
return;
}
sf::Vector2i iPath = pathToGo.getNewPath();
mgoX = iPath.x;
mgoY = iPath.x;
for (int i(0); i<mgoX; i++)
m_goX += 32;
for (int i(0); i<mgoY; i++)
m_goY += 16;
return;
} else
return;
}
IsoMapPath (pathToGo est un IsoMapPath):
class IsoMapPath {
public:
IsoMapPath() : x(), y(), xPath(), yPath() {}
~IsoMapPath() {}
sf::Vector2i getNewPath(bool erase = true) {
int X(0), Y(0);
if (x.size() != 0) {
X = x[0];
if (erase)
x.pop_back();
}
if (y.size() != 0) {
Y = y[0];
if (erase)
y.pop_back();
}
return sf::Vector2i(X, Y);
}
void addPath(int X, int Y, bool eraseOldPath = false) {
if (eraseOldPath) {
x.erase(x.begin(), x.end());
y.erase(y.begin(), y.end());
}
x.push_back(X);
y.push_back(Y);
}
bool x_isEmpty() {
return(x.size() == 0);
}
bool y_isEmpty() {
return(y.size() == 0);
}
bool xPath_isEmpty() {
return(xPath.size() == 0);
}
bool yPath_isEmpty() {
return(yPath.size() == 0);
}
bool unsignedPathFinding(unsigned int _x, unsigned int _y, unsigned int __x, unsigned int __y) {
eraseTestPath();
unsigned int xToMove(0);
unsigned int yToMove(0);
for (unsigned int i(_x); i<__x; i++)
xToMove += 1;
for (unsigned int i(_y); i<__y; i++)
yToMove += 1;
if (xToMove == yToMove) {
xPath.push_back(xToMove);
yPath.push_back(yToMove);
//qDebug() << "Step 1";
return true;
}
if (xToMove == 0 && yToMove != 0) {
xPath.push_back(xToMove);
yPath.push_back(yToMove);
//qDebug() << "Step 2";
return true;
}
if (xToMove != 0 && yToMove == 0) {
xPath.push_back(xToMove);
yPath.push_back(yToMove);
//qDebug() << "Step 3";
return true;
}
if (_x % 2 == 0 && __x % 2 == 0) {
xPath.push_back(xToMove);
yPath.push_back(0);
xPath.push_back(0);
yPath.push_back(yToMove);
//qDebug() << "Step 4";
return true;
} if ((_x % 2 == 0 && __x % 2 != 0) || (_x % 2 != 0 && __x % 2 == 0)) {
if (yToMove > 0) {
xPath.push_back(1);
yPath.push_back(1);
xToMove -= 1;
yToMove -= 1;
xPath.push_back(xToMove);
yPath.push_back(0);
xPath.push_back(0);
yPath.push_back(yToMove);
//qDebug() << "Step 5";
return true;
} else
return false;
}
return false;
}
bool activateTestPath () {
eraseActualPath();
if (xPath_isEmpty() && yPath_isEmpty())
return false;
for (unsigned int i(0); i<xPath.size(); i++) {
x.push_back(xPath[i]);
}
for (unsigned int i(0); i<yPath.size(); i++) {
y.push_back(yPath[i]);
}
eraseTestPath();
return true;
}
void eraseActualPath() {
for (unsigned int i(0); i<xPath.size(); i++)
x.pop_back();
for (unsigned int i(0); i<yPath.size(); i++)
y.pop_back();
}
void eraseTestPath() {
for (unsigned int i(0); i<xPath.size(); i++)
xPath.pop_back();
for (unsigned int i(0); i<yPath.size(); i++)
yPath.pop_back();
}
private:
std::vector<int> x;
std::vector<int> y;
std::vector<int> xPath;
std::vector<int> yPath;
};