Hello guys, I’ve mean to post this like a week ago, and I wrote a really cool post… and then, unintentionally I pressed Crl+W (in Chrome) and, bye bye nice post… So, I don’t know if this one is gonna be as explicative as the other one. Btw, I don’t really know if “General” is the category for this kind of troubleshooting, so feel free to move this post to where it fits. And, I must say, I’m spanish speaker, so you’ll probably find some writing mistakes…
Well, im working on this Tower Defense game, the first game in jME that I intend to finish, the others where just to get knowing the SDK.
The game won’t be commercial, it’s mainly for learning purposes and fun.
The main question in this thread, is going to be what is giving me trouble right now… It doesn’t seem to hard to fix, but i’m overwhelmed with this issue, so i’m probably not looking it the right way.
Context:
I have Towers and Creeps (Creep == Monster/Enemy). Towers attacks any creep that is in their range of fire, each x seconds, wich is the tower fire rate. The only reason a Tower stops attacking a Creep, is when neither the creep is out of range, or death.
A group of Creeps is generated each X seconds (depending on the user’s level), and this group will approach the user’s base (for now position (0,0,0) ).
When a Creep is created, it’s also added to an ArrayList of creeps, wich are the creeps in the game right now. (they’re also detached when they die)
So, I’ve a TowerControl added to each tower Spatial, wich is in charge of computing the distance from this Tower to each creep in the world, till it find’s one (the first) that is in the tower’s range, and shoot at it, every x seconds until the creep is dead or not in range any more.
I’m gonna attach some code from the TowerControl class, just the relevant parts. You can see the whole code at the repository.
[java]@Override
protected void controlUpdate(float tpf) {
// if (gamePlayAppState.getCreepList().size() > 0) {
if (target == null || target.getState() == CREEP_STATE.DEATH) {
target = getNextTarget();
}
if ((timeToNextShoot <= 0) && (target != null && target.getState() == CREEP_STATE.ALIVE)) {
if (isInRange(target)) {
shoot(target);
timeToNextShoot = tower.getFireRate();
} else {
target = getNextTarget();
}
}
if (timeToNextShoot <= tower.getFireRate() - 1) {
int c = gamePlayAppState.getTowerNode().getChildIndex(bulletSp);
if (c > 0) {
gamePlayAppState.getTowerNode().getChild©.removeFromParent();
}
}
// if (target != null) {
timeToNextShoot -= tpf;
// } else {
// timeToNextShoot = 0;
// }
}
private Creep getNextTarget() {
for (Creep creep : gamePlayAppState.getCreepList()) {
if (isInRange(creep)) {
return creep;
// System.out.println("Tower " + tower.getName() + " Found Creep " + creep.getName());
}
}
return null;
}
private Boolean isInRange(Creep creep) { //spatial is the tower.
// float distanceFromTower = creep.getSpatial().getLocalTranslation().subtract(spatial.getLocalTranslation()).length();
float distanceFromTower = spatial.getLocalTranslation().distance(creep.getSpatial().getLocalTranslation());
System.err.println(creep.getName() + " Dis > " + distanceFromTower);
return distanceFromTower <= tower.getRange();
}
[/java]
Now, this works. And it works pretty good for what I need right now…
But
For some reason, it only works the first time Creep’s are created. To explain:
Creeps are created, towers shoot at them, they die or reach user base without problem. Everything works fine till here.
Then, the second “wave” of creeps is created (at same position as the first one) and for some reason, the distanceFromTower variable in the isInRange() method in the TowerControl class is giving a higher value than it did at the first wave although the creeps are at the same position.
At the third wave of creeps, this is getting even worst and tower’s wont attack any creep, obviously becouse they think creeps are not in range.
So, I belive that the computing distance part is fine, I took the formula from the math cheat sheet. Also, when the first, second, and third (and every) wave of creep is created, the spatials are showing at the same position in the screen.
I decided also to add a video, where you can see the console showing the distance from one tower to creeps.
You must be aware that I aint no graphic designer
[video]TD Bugs - YouTube
This tower has a range of 60, as you can see, at the begening, the distance is ~30, at the second wave its like ~45, and the third wave it’s ~ 55.
This has made me think, that maybe my entire messure system (for tower range) is wrong…
Remeber that you can check the whole source code here
Thank you very much for your time. If you have any suggestion beyond this problem, please feel free to suggest.
Thanks!!