From e6d8f09410c9f9a501cdcbe4cbe38d090b36f4f0 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Mon, 17 Feb 2025 15:21:45 -0500 Subject: [PATCH] It's always the small, simple changes that cause the most problems. Invulnerability system hopefully working for real this time. --- src/enemy.py | 45 +++++++++++++++++++++------------------------ src/level.py | 7 ++++++- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/enemy.py b/src/enemy.py index 22d133b..b19c613 100644 --- a/src/enemy.py +++ b/src/enemy.py @@ -49,10 +49,12 @@ class Enemy(Object): # Initialize vulnerability system self.hasVulnerabilitySystem = kwargs.get('has_vulnerability', False) if self.hasVulnerabilitySystem: - self.isVulnerable = kwargs.get('is_vulnerable', False) # For enemies with vulnerability, default to invulnerable + self.isVulnerable = False # Start invulnerable self.vulnerabilityTimer = pygame.time.get_ticks() - self.vulnerabilityDuration = kwargs.get('vulnerability_duration', 1000) + self.vulnerabilityDuration = kwargs.get('vulnerability_duration', 2000) self.invulnerabilityDuration = kwargs.get('invulnerability_duration', 5000) + soundName = f"{self.enemyType}_is_vulnerable" if self.isVulnerable else self.enemyType + self.channel = obj_play(self.sounds, soundName, self.level.player.xPos, self.xPos) else: self.isVulnerable = True @@ -67,6 +69,7 @@ class Enemy(Object): self.movementSpeed *= speedMultiplier # Spiders are faster self.attackPattern = {'type': 'hunter'} # Spiders actively hunt the player self.turnThreshold = 3 # Spiders turn around quickly to chase player + @property def xPos(self): @@ -94,30 +97,25 @@ class Enemy(Object): """Update enemy position and handle attacks""" if not self.isActive or self.health <= 0: return - - # Set initial sound for enemies with vulnerability system - if self.hasVulnerabilitySystem and self.channel is None: - soundName = f"{self.enemyType}_is_vulnerable" if self.isVulnerable else self.enemyType - self.channel = obj_play(self.sounds, soundName, player.xPos, self.xPos) - - # Enemy vulnerability state management + + # Initialize sound for enemies with vulnerability system immediately upon creation if self.hasVulnerabilitySystem: - if self.isVulnerable and (currentTime - self.vulnerabilityTimer > self.vulnerabilityDuration): - # Switch to invulnerable - self.isVulnerable = False + if self.channel is None: + soundName = f"{self.enemyType}_is_vulnerable" if self.isVulnerable else self.enemyType + self.channel = obj_play(self.sounds, soundName, player.xPos, self.xPos) + # Update existing channel position + else: + self.channel = obj_update(self.channel, player.xPos, self.xPos) + + # Check for vulnerability state change + if currentTime - self.vulnerabilityTimer >= (self.vulnerabilityDuration if self.isVulnerable else self.invulnerabilityDuration): + self.isVulnerable = not self.isVulnerable self.vulnerabilityTimer = currentTime - # Change sound back to base enemy sound + if self.channel: obj_stop(self.channel) - self.channel = obj_play(self.sounds, self.enemyType, player.xPos, self.xPos) - elif not self.isVulnerable and (currentTime - self.vulnerabilityTimer > self.invulnerabilityDuration): - # Switch to vulnerable - self.isVulnerable = True - self.vulnerabilityTimer = currentTime - # Change to vulnerable sound - if self.channel: - obj_stop(self.channel) - self.channel = obj_play(self.sounds, f"{self.enemyType}_is_vulnerable", player.xPos, self.xPos) + soundName = f"{self.enemyType}_is_vulnerable" if self.isVulnerable else self.enemyType + self.channel = obj_play(self.sounds, soundName, player.xPos, self.xPos) # Check if player has entered territory if not self.hunting: @@ -226,8 +224,7 @@ class Enemy(Object): def take_damage(self, amount): """Handle enemy taking damage""" - # Enemy can only take damage when vulnerable - if not self.isVulnerable: + if self.hasVulnerabilitySystem and not self.isVulnerable: return self.health -= amount diff --git a/src/level.py b/src/level.py index e8bad99..c2bff65 100644 --- a/src/level.py +++ b/src/level.py @@ -152,7 +152,11 @@ class Level: spawn_type=obj.get("spawn_type", "zombie"), spawn_cooldown=obj.get("spawn_cooldown", 2000), spawn_chance=obj.get("spawn_chance", 25), - spawn_distance=obj.get("spawn_distance", 5) + spawn_distance=obj.get("spawn_distance", 5), + has_vulnerability=obj.get("has_vulnerability", False), + is_vulnerable=obj.get("is_vulnerable", False), + vulnerability_duration=obj.get("vulnerability_duration", 1000), + invulnerability_duration=obj.get("invulnerability_duration", 5000) ) self.enemies.append(enemy) else: @@ -221,6 +225,7 @@ class Level: enemy.update(currentTime, self.player) + # Only handle audio for non-vulnerability enemies if not enemy.hasVulnerabilitySystem: if enemy.channel is None or not enemy.channel.get_busy(): enemy.channel = obj_play(self.sounds, enemy.enemyType, self.player.xPos, enemy.xPos)