How to perform a damage calculation with this scenario?

Hello guys! I am stuck on how can I perform damage calculation with this kind of scenario.
Screenshot_2021-11-27_00-25-00
I want each of the knights or orcs to clash each other here’s where am currently at.
I created a group for each lane:

// GROUPS
this.knightGroup_Row_1 = this.add.group();
this.knightGroup_Row_2 = this.add.group();
this.knightGroup_Row_3 = this.add.group();
this.knightGroup_Row_4 = this.add.group();

this.enemyGroup_Row_1 = this.add.group();
this.enemyGroup_Row_2 = this.add.group();
this.enemyGroup_Row_3 = this.add.group();
this.enemyGroup_Row_4 = this.add.group();

Collision detection:

this.physics.add.overlap(this.knightGroup_Row_1, this.enemyGroup_Row_1, (knight, enemy) => {
	console.log(enemy.texture.key)
	// k1RUN_sprite

	//KNIGHT
	if(knight.texture.key == "k1RUN_sprite"){
		knight.anims.play("k1_atk");
		knight.currentAttacking = enemy;
	}
	// ENEMY
	if(enemy.texture.key == "orcRUN_sprite"){
		enemy.anims.play("o1_atk");
	}
	knight.body.setVelocity(0, 0);
	enemy.body.setVelocity(0, 0);
	//if(enemy.health < 0) { enemy.destroy(); }
}, false, this);

//etc....

Now with this setup how can I perform damage calculation is it during animation or after animation?
Some say I need to create a hitbox but it’s pretty hard creating hitbox for each groups.

Thanks in advance!

I’d guess you want to do it after.

You need to interrupt the overlap somehow if the attack has already started.

this.physics.add.overlap(
  this.knightGroup_Row_1,
  this.enemyGroup_Row_1,
  (knight, enemy) => {
    console.log('overlap', knight.texture.key, enemy.texture.key);

    if (knight.currentAttacking) {
      console.log('already attacking');

      return;
    }

    if (knight.texture.key == 'k1RUN_sprite') {
      knight.currentAttacking = enemy;
      knight.anims.play('k1_atk');
      knight.anims.once('complete', () => {
        knight.currentAttacking = null;
        enemy.health -= 1;
      });
    }

    if (enemy.texture.key == 'orcRUN_sprite') {
      enemy.anims.play('o1_atk');
    }

    knight.body.setVelocity(0, 0);
    enemy.body.setVelocity(0, 0);
  },
  false,
  this
);
1 Like

Hi @samme! Thanks for the reply! Yes I did that. I performed damage calculation after the attack animation.
However, I have another problem with the sounds.
So what I did is I put the animation complete detection code into the knight’s prefab class.

this.on('animationcomplete', (anim) => {
	if(anim.key == "k1_die"){ this.destroy(); }else if(anim.key == "k1_atk"){
		// play sound here ... this.spearSwing.play();
		if(this.e.active){
			//console.log(this.e.health);
			if(this.e.health >= 0){
				this.e.health -= this.damage;
			}
			//console.log(this.e);
		}
	}
}, this);

When “A LOT” of Knights were spawned sounds are getting amplified that can make your ears bleed haha.
Is there a way of controlling it?
I am toying with this right now.

this.sndArray = this.scene.sound.getAll("spear");
this.sndArray.forEach((snd, index) => {
    if(snd.isPlaying){
        this.counter++;
    }
});

If multiple spear sounds is detected. Play only one. But it is quite hard.

If you do

this.spearSwing = this.add.sound(/*…*/);

only once I think there should be no problem.

1 Like

You mean I’ll declare this.spearSwing outside of the Knight’s prefab class?