From ffbb9da9929703cedcbc067c86c3d3936bc2d096 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Thu, 6 Feb 2025 15:20:47 -0500 Subject: [PATCH] Fixed attack sound sometimes not playing. Fixed a problem where a single attack was doing multiple attack damage. The means I reaqlly need to reword the first three levels, because they are very hard now lol. --- src/catapult.py | 2 +- src/level.py | 15 +++++---------- src/weapon.py | 10 ++++++++++ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/catapult.py b/src/catapult.py index fca0764..0ea4edb 100644 --- a/src/catapult.py +++ b/src/catapult.py @@ -138,4 +138,4 @@ class Catapult(Object): pumpkin.stop_sound(self.sounds, player.xPos) pumpkin.isActive = False self.activePumpkins.remove(pumpkin) - speak("Hit by a pumpkin!") + self.sounds['player_takes_damage'].play() diff --git a/src/level.py b/src/level.py index ed61c28..5c2d7ae 100644 --- a/src/level.py +++ b/src/level.py @@ -198,19 +198,14 @@ class Level: # Only get attack range if attack is active if self.player.currentWeapon and self.player.currentWeapon.is_attack_active(currentTime): attackRange = self.player.currentWeapon.get_attack_range(self.player.xPos, self.player.facingRight) - + # Check for enemy hits for enemy in self.enemies: if enemy.isActive and enemy.xPos >= attackRange[0] and enemy.xPos <= attackRange[1]: - if self.weapon_hit_channel is None or not self.weapon_hit_channel.get_busy(): - self.weapon_hit_channel = self.sounds[self.player.currentWeapon.hitSound].play() - self.weapon_hit_channel = play_sound(self.sounds[self.player.currentWeapon.hitSound]) - - enemy.take_damage(self.player.currentWeapon.damage) - else: - # Reset weapon hit channel when not attacking - if self.weapon_hit_channel is not None and not self.weapon_hit_channel.get_busy(): - self.weapon_hit_channel = None + # Only damage and play sound if this is a new hit for this attack + if self.player.currentWeapon.register_hit(enemy, currentTime): + play_sound(self.sounds[self.player.currentWeapon.hitSound]) + enemy.take_damage(self.player.currentWeapon.damage) # Check for coffin hits for obj in self.objects: diff --git a/src/weapon.py b/src/weapon.py index 1461d62..51536b4 100644 --- a/src/weapon.py +++ b/src/weapon.py @@ -8,6 +8,7 @@ class Weapon: self.cooldown = cooldown # Milliseconds between attacks self.attackDuration = attackDuration # Milliseconds the attack is active self.lastAttackTime = 0 + self.hitEnemies = set() def can_attack(self, currentTime): """Check if enough time has passed since last attack""" @@ -24,6 +25,7 @@ class Weapon: """Begin an attack and return True if allowed""" if self.can_attack(currentTime): self.lastAttackTime = currentTime + self.hitEnemies.clear() # Clear hit enemies for new attack return True return False @@ -31,3 +33,11 @@ class Weapon: """Check if the attack is still in its active frames""" timeSinceAttack = currentTime - self.lastAttackTime return 0 <= timeSinceAttack <= self.attackDuration + + def register_hit(self, enemy, currentTime): + """Register a hit on an enemy for this attack. Returns True if it's a new hit.""" + if self.is_attack_active(currentTime): + if enemy not in self.hitEnemies: + self.hitEnemies.add(enemy) + return True + return False