Strategic Pathing Method for Tower Defense Mobs

This IS a repost of one that I made a few hours ago, until I realized I had out it in the wrong section. Any mods feel free to delete the other one as I think it is in the wrong section.

This is a slight continuation from my game loop question. So kudos to you if you helped me there and still have the patience to continue helping me. Anyway, my mobs in my tower defense game (block based & 2D) have adjustable movement speed and when I change it to more than one, my pathing method goes down the tubes. I have it counting up a variable every time the mob moves and if the variable reaches the blocksize, add one to the block coordinate, or subtract, depending on the direction the mob is currently moving. Then check the surrounding blocks and if one of them is a road block, change it’s direction to that block. Here is the code:


public void physic(double delta) {
		if (walkFrame >= walkSpeed) {
			if (imageIndex >= mobImages.length - 1) {
				imageIndex = 0;
			} else {
				imageIndex++;
			}
			if (direction == right) {
				x += MOVESPEED * delta;
			} else if (direction == upward) {
				y -= MOVESPEED * delta;
			} else if (direction == downward) {
				y += MOVESPEED * delta;
			} else if (direction == left) {
				x -= MOVESPEED * delta;
			}
			mobWalk += 1;
			// System.out.println(xC + " : " + yC);
			System.out.println(mobWalk);
			if (mobWalk >= Screen.room.blockSize) {
				if (direction == right) {
					xC += 1;
					hasRight = true;
				} else if (direction == left) {
					xC -= 1;
					hasLeft = true;
				} else if (direction == upward) {
					yC -= 1;
					hasUpward = true;
				} else if (direction == downward) {
					yC += 1;
					hasDownward = true;
				}

				if (!hasUpward) {
					try {
						if (Screen.room.block[yC + 1][xC].groundID == Value.groundRoad) {
							direction = downward;
						}
					} catch (Exception e) {
					}
				}

				if (!hasDownward) {
					try {
						if (Screen.room.block[yC - 1][xC].groundID == Value.groundRoad) {
							direction = upward;
						}
					} catch (Exception e) {
					}
				}

				if (!hasLeft) {
					try {
						if (Screen.room.block[yC][xC + 1].groundID == Value.groundRoad) {
							direction = right;
						}
					} catch (Exception e) {
					}
				}

				if (!hasRight) {
					try {
						if (Screen.room.block[yC][xC - 1].groundID == Value.groundRoad) {
							direction = left;
						}
					} catch (Exception e) {
					}
				}

				if (Screen.room.block[yC][xC].airID == Value.airCastle) {
					deleteMob();
					loseHealth();
				}

				mobWalk = 0;
				hasUpward = false;
				hasDownward = false;
				hasLeft = false;
				hasRight = false;
			}
			walkFrame = 0;
		} else {
			walkFrame += 1;
		}
	}

I am pretty sure there must be a better way to do this.
Thanks for any help. -cMp

Loop it as if it were moving by 1, (loop as many times as the movement speed)

From looking at the code it is hard to tell what it does or should do.

  • “physic” is not a good method name, maybe “updatePhysics” or something like that
  • the exception catch blocks are weird, if array indexes go out of bounds, the cause of it should be treated
  • is the image index for animation ? that should not be the responsibility of a physics routine
  • mobWalk, xC, walkFrame, has*, impossible to tell what they are for
  • what is the actual problem beyond “goes down the tube” ?

Wait, why is delta a double? ???