import pygame from settings import * class Enemy: def __init__(self, x, y): # Position & physics self.pos = pygame.Vector2(x, y) self.rect = pygame.Rect(x, y, 28, 48) self.velocity = pygame.Vector2(0, 0) # Health self.health = ENEMY_HEALTH self.max_health = ENEMY_HEALTH self.alive = True # Combat self.attack_rect = pygame.Rect(x, y, 60, 60) self.attack_damage = ENEMY_ATTACK_DAMAGE self.attack_range = ENEMY_ATTACK_RANGE # Timers self.knockback_timer = 0 self.invulnerable_timer = 0 def update(self, dt, world, player): if not self.alive: return dt = min(dt, 0.05) # Gravity self.velocity.y += ENEMY_GRAVITY * dt if self.velocity.y > MAX_FALL_SPEED: self.velocity.y = MAX_FALL_SPEED # Basic AI: move toward player if self.knockback_timer <= 0: if player.rect.x < self.rect.x: self.velocity.x = -ENEMY_SPEED else: self.velocity.x = ENEMY_SPEED else: self.knockback_timer -= dt # Update horizontal position self.pos.x += self.velocity.x * dt self.rect.x = round(self.pos.x) self.handle_horizontal_collisions(world) # Update vertical position self.pos.y += self.velocity.y * dt self.rect.y = round(self.pos.y) self.handle_vertical_collisions(world) # Update attack rectangle self.attack_rect.center = self.rect.center # Decay invulnerability if self.invulnerable_timer > 0: self.invulnerable_timer -= dt # Check death if self.health <= 0: self.alive = False def handle_horizontal_collisions(self, world): for tile in world.get_nearby_tiles(self.rect): if tile["solid"] and self.rect.colliderect(tile["rect"]): if self.velocity.x > 0: self.rect.right = tile["rect"].left elif self.velocity.x < 0: self.rect.left = tile["rect"].right self.pos.x = self.rect.x self.velocity.x = 0 def handle_vertical_collisions(self, world): for tile in world.get_nearby_tiles(self.rect): if tile["solid"] and self.rect.colliderect(tile["rect"]): if self.velocity.y > 0: self.rect.bottom = tile["rect"].top elif self.velocity.y < 0: self.rect.top = tile["rect"].bottom self.pos.y = self.rect.y self.velocity.y = 0 def take_damage(self, damage, direction): if self.invulnerable_timer > 0 or not self.alive: return self.health -= damage self.invulnerable_timer = 0.3 self.knockback_timer = 0.15 # Knockback knockback_force = 400 * ENEMY_KNOCKBACK_RESISTANCE self.velocity.x = direction * knockback_force self.velocity.y = -300 if self.health <= 0: self.alive = False def draw(self, screen, camera): if not self.alive: return draw_rect = camera.apply(self.rect) # Flash when invulnerable color = (100, 100, 255) if self.invulnerable_timer > 0 else (0, 0, 255) pygame.draw.rect(screen, color, draw_rect) if SHOW_COLLIDERS: pygame.draw.rect(screen, (0, 255, 0), draw_rect, 2) attack_draw_rect = camera.apply(self.attack_rect) pygame.draw.rect(screen, (255, 0, 0), attack_draw_rect, 1) # Health bar health_bar_width = 28 health_bar_height = 4 health_percent = max(0, self.health / self.max_health) health_bar_rect = pygame.Rect( draw_rect.x, draw_rect.y - 8, health_bar_width * health_percent, health_bar_height ) pygame.draw.rect(screen, (0, 255, 0), health_bar_rect) pygame.draw.rect(screen, (255, 0, 0), pygame.Rect( draw_rect.x, draw_rect.y - 8, health_bar_width, health_bar_height ), 1)