Zombie game - Help needed for pathfinding
in
Programming Questions
•
1 year ago
Hello everyone, I wonder if anyone would help me with an issues I have regarding pathfinding.
I am making a 2D zombie shooter and I am working on making my zombies separate rather than group together. With a lot of help from a friend (who wrote this section of code) I have now got an algorithm that:
Checks for the nearest zombie
Works out the possible move it can make
Checks to see if there is another zombie in the way
If not then it can move
If there is, see if you can move on the x axis (and vice versa)
BUT! When the program is run, some of the zombies will not make the required move, so I have zombies dotted around the screen just not moving - unless I make the player move somewhere else to make the zombie move again.
I don't want to paste the whole program into this post as there is too much of it, but have a look at the above function and you may be able to help!
- void seekTarget() {
- Mover closestZombie = null;
- float distanceToClosestZombie = 10000; // arbitrary large number
- float distance;
- int tryX = x;
- int tryY = y;
- // find which of the other zombies is nearest to this one,
- // and what distance separates them
- for (Mover m: movers) {
- if (m == this) {
- // don't compare this zombie to itself!
- continue;
- }
- distance = dist(x, y, m.x, m.y);
- if (distance < distanceToClosestZombie) {
- // the zombie being tested is the closest found yet
- closestZombie = m;
- distanceToClosestZombie = distance;
- }
- }
- // work out the "ideal" new x and y coordinates, assuming
- // for now that the closest zombie is not in the way
- // (we'll find that out in a moment)
- /*if(targetX > x) {
- tryX++;
- //println("working");
- } else if(targetX < x) {
- tryX--;
- //println("working");
- }
- if (targetY > y) {
- tryY++;
- //println("working");
- } else if(targetY < y) {
- tryY--;
- //println("working");
- }*/
- if(targetX > player.x) {
- //tryX = tryX + int(zombieSpeed);
- tryX--;
- image(zombieLEFT, tryX, tryY);
- println("working");
- } else if(targetX < player.x) {
- tryX++;
- //tryX = tryX - int(zombieSpeed);
- image(zombieRIGHT, tryX, tryY);
- }
- if(targetY > player.y) {
- tryY--;
- //tryY = tryY - int(zombieSpeed);
- image(zombieUP, tryX, tryY);
- } else if(targetY < player.y) {
- tryY++;
- //tryY = tryY + int(zombieSpeed);
- image(zombieDOWN, tryX, tryY);
- }
- // if the closest zombie is not already too close, make the
- // ideal move computed above
- if (distanceToClosestZombie > MIN_ZOMBIE_SEPARATION) {
- x = tryX;
- y = tryY;
- }
- // otherwise, only make a move that will cause the separation
- // between this zombie and its closest neighbour to either
- // increase or remain the same
- else {
- if (dist(tryX, tryY, closestZombie.x, closestZombie.y) >=
- distanceToClosestZombie) {
- // the ideal move increases or doesn't change the separation
- x = tryX;
- y = tryY;
- }
- else if (dist(tryX, y, closestZombie.x, closestZombie.y) >=
- distanceToClosestZombie) {
- // only the horizontal component of the ideal move increases
- // or doesn't change the separation
- x = tryX;
- }
- else if (dist(x, tryY, closestZombie.x, closestZombie.y) >=
- distanceToClosestZombie) {
- // only the vertical component of the ideal move increases
- // or doesn't change the separation
- y = tryY;
- }
- }
- }
Ah - My mate thinks it is to do with the fact that I also have a move() function separate to this, and that I need to integrate it into the above function, below is the other move() function:
- void move() {
- int wouldBeX = x;
- int wouldBeY = y;
- //Move along X axis to find player
- if (x < player.x) {
- x++;
- image(zombieRIGHT, x, y);
- }
- else if (x > player.x) {
- x--;
- image(zombieLEFT, x, y);
- }
- //Move along Y axis to find player
- if (y < player.y) {
- y++;
- image(zombieDOWN, x, y);
- }
- else if (y > player.y) {
- y--;
- image(zombieUP, x, y);
- }
- for (Mover m: movers) {
- //tryToMove(m, wouldBeX, wouldBeY);
- }
- }
Thanks!
1