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