Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

9 years ago by capgun

Hi guys, i've been having some trouble understanding the entity collision system.

I have a troop class and a base class - what i'm trying to achieve is:

while troop is colliding with base
- attack base over and over

This is what i've tried, I'm not understanding how to run the check method for constant collision detection. Any help would be very appreciated.

attack: function(target) {
   while (target.health > 0) {
       if (this.attackTimer.delta() > 0) {
           target.receiveDamage(this.attackPower, this);
       }
       this.attackTimer.set(this.attackTime);
    }
},

check: function(other) {
    if (other instanceof EntityEnemyBase) {
        this.attack(other);
     }
},

9 years ago by capgun

Came up with this solution - not sure if it's the best way to approach this problem


attack: function(target) {
    target.receiveDamage(10, this);
},

check: function(other) {
    if (other instanceof EntityEnemyBase) {
        this.attacking = true;
        this.attackTarget = other;
    }
},

update:function() {
    this.currentAnim = this.anims.idle;

    if (this.attacking) {
        this.attack(this.attackTarget);
    }

    this.parent();
}

9 years ago by stahlmanDesign

Somewhere in the update loop you will probably want to set this.attacking to false so that when the collision is over, the attacking state will stop.

9 years ago by Joncom

I think check will be called every frame that the two entities are touching, so your initial solution is probably nicer. However there is one problem with it:

attack: function(target) {
   while (target.health > 0) {
       if (this.attackTimer.delta() > 0) {
           target.receiveDamage(this.attackPower, this);
       }
       this.attackTimer.set(this.attackTime);
    }
}

This while loop will not end until the target is completely killed. And I'm guessing it should take more than a single frame to kill a target. Therefore I'm guessing this loop is causing some major studder issues?

Suggestion:

attack: function(target) {
   if (target.health > 0) {
       if (this.attackTimer.delta() > 0) {
           target.receiveDamage(this.attackPower, this);
           this.attackTimer.set(this.attackTime);
       }
    }
}

This limits your attack to once per frame and won't block.
Page 1 of 1
« first « previous next › last »