[Solved]DOTA

If you know this game maybe you can easily understand what I am asking.

I want that the tower will attack once it enters the attack perimeter so I used getBounds() with this.

And my problem is, it only attacks once. It will not attack anymore even if I am inside the perimeter.

Should I put the the attacks in ArrayList?

Thanks.

SOLUTION:


attacking = false;

posX = //starting x of the attack
posY = //starting y of the attack

x = posX;
y = posY;

if (in) {
  attack(x,y);
  attacking = true;
  if (hit()) {
     x = posX;
     y = posY;
  }
else if (!in) {
  if (attacking) {
     attack(x,y);
  if (hit) {
     x = posX;
     y = posY;
     attacking = false;
  }
}

:smiley:
What is this question? :smiley:
I completely honestly don’t understand why would you ask such a question. This is obviously a bug in your code. Maybe you don’t reset some kind of attack delay timer?

The code for Tower would help :slight_smile:

It’s simple, assuming your towers work like they do in dota, you have a list of targets within the tower range. Once a target enters tower range, it is entered into a list of targets. Once it leaves range, it is removed from the list of targets. The tower always fires at the first item in that list, if it exists. (of course, you’d have to add some extra code to prioritize champions after they attack, etc).

Thank you for the advices.

@Sir trollwarriror1

In the game DOTA, the tower will attack if there will be a non-allied unit in its perimeter.

ex: perimeter = 10

if char.x <= perimeter
tower.attack
if hit
attack.isVisible(false)

this is the logic that I am planning to do.

Is this in a method that is called each frame? It will only execute once if your doing anything else. All entity logic should be in the game loop, so it is checked every frame.

Also what library after are you using? That would also help. LibGDX has simple AABB collision (check the source for an idea) algorithm’s built in, so logic like this is capable of getting what you want:


If(towerRange.getBounds().overlaps(somethingElse.getBounds()){
// Do stuff to manage targets, store them etc
fire(target);

}else{
// do stuff to let the tower know there ain't nothing there

That is the problem.

I am not using any library such as LiGDX, MERcury, etc.

I am only using netbeans

should I start to learn LiGDX already?

There is no problem with not using libraries such as those… They are made in “general”. They won’t meet your specific needs ever…

Use libgdx for AABB collision? Really?

Depends how you feel about it, take a look at the LibGDX wiki and code examples, if there if basic syntax you don’t understand then sticking to the Java APIs is probably a better idea to keep learning Java lol

I know it’s not great but it has basic rectangle and circle classes, I hated using them

To answer your question, just put your code into an update loop. If you don’t know what that is, consider looking up threading and/or game loops for Java. If you do, then your code will work 100%.

To explain this a bit more: You have a main game loop which updates and renders the game. What you need is a basic AI for your towers. The most simple way of doing what you want is something like this: Each update loop through all towers. For each tower, check if there is a target in range. If a target is found, fire at it and set a cooldown variable to say 50 (50 updates until it can fire again) which is reduced by 1 each update.

DOTA towers have a specific AI that makes them lock on to a single target and fire at it until it dies. If you want to do something like that you’ll need to expand the AI quite a bit. Here’s some example code for the update function of such a tower.



if(cooldownLeft > 0){
    cooldownLeft--;
}

if(target != null){
    //We have a target from the last update. Check if it's still alive and in range.
    if(target.isDead() || !isInRange(target)){
        target = null;
    }
}

if(target == null){
    target = acquireTarget(); //Find something to murder
}

if(target != null && cooldownLeft == 0){
    fireAt(target);
    cooldownLeft = 50;
}

Thank you for the informations guys.

It helped me a lot.

:slight_smile: