From 07f589639faea5b470737037fd9fedc0608c5d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filips=20Kalni=C5=86=C5=A1?= Date: Mon, 16 Mar 2026 07:44:52 +0000 Subject: [PATCH] Update enemy.py --- enemy.py | 65 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/enemy.py b/enemy.py index 7ad35fa..8c9e97b 100644 --- a/enemy.py +++ b/enemy.py @@ -1,29 +1,39 @@ 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) - self.pos = pygame.Vector2(x, y) + + # 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 - # Attack hitbox - self.attack_rect = pygame.Rect(x, y, 60, 60) - def update(self, dt, world, player): + if not self.alive: + return + dt = min(dt, 0.05) - # Apply gravity (FIX for flying) + # Gravity self.velocity.y += ENEMY_GRAVITY * dt if self.velocity.y > MAX_FALL_SPEED: self.velocity.y = MAX_FALL_SPEED - # Basic AI: follow player + # Basic AI: move toward player if self.knockback_timer <= 0: if player.rect.x < self.rect.x: self.velocity.x = -ENEMY_SPEED @@ -32,83 +42,82 @@ class Enemy: else: self.knockback_timer -= dt - # Update position + # 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 rect position - self.attack_rect.centerx = self.rect.centerx - self.attack_rect.centery = self.rect.centery + # 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): - """Handle horizontal collision""" 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): - """Handle vertical collision""" 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): - """Take damage and apply knockback""" - if self.invulnerable_timer > 0: + if self.invulnerable_timer > 0 or not self.alive: return self.health -= damage self.invulnerable_timer = 0.3 self.knockback_timer = 0.15 - # Apply knockback (affected by resistance) + # Knockback knockback_force = 400 * ENEMY_KNOCKBACK_RESISTANCE self.velocity.x = direction * knockback_force - self.velocity.y = -300 # Slight upward knockback + 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 - if self.invulnerable_timer > 0: - color = (100, 100, 255) # Light blue - else: - color = (0, 0, 255) # Blue - + 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) - # Draw attack hitbox attack_draw_rect = camera.apply(self.attack_rect) pygame.draw.rect(screen, (255, 0, 0), attack_draw_rect, 1) - # Draw health bar + # Health bar health_bar_width = 28 health_bar_height = 4 - health_percent = self.health / self.max_health - + health_percent = max(0, self.health / self.max_health) health_bar_rect = pygame.Rect( draw_rect.x, draw_rect.y - 8,