Feat(Player) - Refactor respawn logic; add respawn grace period and update attack range for boss enemy.

This commit is contained in:
Félix MARQUET
2025-04-11 09:21:14 +02:00
parent 46dc047db3
commit 3f33f35792
3 changed files with 31 additions and 8 deletions

View File

@@ -801,7 +801,7 @@
"damage": 1,
"behavior": "boss",
"attack_interval": 1.0,
"attack_range": 1000,
"attack_range": 500,
"sprite_sheet": "assets/map/enemy/boss.gif",
"size": [200,200]
}

View File

@@ -93,6 +93,10 @@ class Player(Entity):
# Initilize mixer
pygame.mixer.init()
self.just_respawned = False
self.respawn_grace_time = 0.5
self.respawn_timer = 0
def load_images(self):
"""Load images for the player"""
try:
@@ -430,11 +434,6 @@ class Player(Entity):
if self.vel.y <= 0:
self.highest_position = self.pos.y
if self.vel.y > 0:
fall_distance = self.pos.y - self.highest_position
if fall_distance > 500:
self.death()
if self.vel.x > 0:
self.facing_right = True
elif self.vel.x < 0:
@@ -442,6 +441,21 @@ class Player(Entity):
self.floating_texts = [text for text in self.floating_texts if text.update()]
if self.just_respawned:
self.respawn_timer += 1 / self.game_resources.FPS
if self.respawn_timer >= self.respawn_grace_time:
self.just_respawned = False
self.respawn_timer = 0
self.highest_position = self.pos.y
else:
if self.vel.y <= 0:
self.highest_position = self.pos.y
if self.vel.y > 0:
fall_distance = self.pos.y - self.highest_position
if fall_distance > 500:
self.death()
def take_damage(self, amount=1):
"""Reduce life number if not invulnerable"""
if not self.invulnerable:
@@ -750,3 +764,13 @@ class Player(Entity):
text_y = start_y + (projectile_size - projectiles_text.get_height()) // 2
surface.blit(projectiles_text, (text_x, text_y))
def respawn_at_checkpoint(self, x, y):
self.pos.x = x
self.pos.y = y
self.highest_position = y
self.vel = self.game_resources.vec(0, 0)
self.jumping = False
self.invulnerable = True
self.invulnerable_timer = 0
self.just_respawned = True

View File

@@ -109,8 +109,7 @@ def reset_game_with_checkpoint(map_name, game_resources):
# If checkpoint exists, respawn player at checkpoint
if checkpoint_pos:
player.pos = game_resources.vec(checkpoint_pos[0], checkpoint_pos[1])
player.update_rect()
player.respawn_at_checkpoint(checkpoint_pos[0], checkpoint_pos[1])
return player, platforms, all_sprites, background, checkpoints, collectibles