2023-10-04 02:37:28 +00:00
|
|
|
import pygame
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
from .entity import Entity
|
2023-10-04 02:37:28 +00:00
|
|
|
|
|
|
|
from effects.particle_effects import AnimationPlayer
|
|
|
|
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
class Enemy(Entity):
|
2023-10-04 02:37:28 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
def __init__(self,
|
|
|
|
name,
|
|
|
|
position,
|
|
|
|
groups,
|
|
|
|
visible_sprites,
|
|
|
|
obstacle_sprites
|
|
|
|
):
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
super().__init__(groups=groups,
|
|
|
|
visible_sprites=visible_sprites,
|
|
|
|
obstacle_sprites=obstacle_sprites,
|
|
|
|
attack_sprites=None,
|
|
|
|
attackable_sprites=None)
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
# Setup stats
|
|
|
|
self.sprite_type = 'enemy'
|
|
|
|
self.name = name
|
|
|
|
self.get_stats(self.sprite_type, monster_name=self.name)
|
2023-10-04 02:37:28 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
# Graphics Setup
|
2023-10-04 02:37:28 +00:00
|
|
|
self.animation_player = AnimationPlayer()
|
2024-04-23 18:43:39 +00:00
|
|
|
self.import_assets(position)
|
2023-10-04 02:37:28 +00:00
|
|
|
|
|
|
|
self.distance_direction_from_player = None
|
|
|
|
|
|
|
|
def get_action(self):
|
|
|
|
player_distance = sorted(
|
|
|
|
self.distance_direction_from_player, key=lambda x: x[0])[0]
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
if player_distance[0] < self.notice_radius and player_distance[0] >= self.attack_radius:
|
|
|
|
self.direction = player_distance[1]
|
|
|
|
self.status = "move"
|
|
|
|
self.move(
|
|
|
|
self.speed, self.hitbox, self.obstacle_sprites, self.rect)
|
|
|
|
elif player_distance[0] <= self.attack_radius:
|
|
|
|
self.status = 'attack'
|
2023-10-04 02:37:28 +00:00
|
|
|
else:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.status = 'idle'
|
2023-10-04 02:37:28 +00:00
|
|
|
|
|
|
|
def add_exp(self, player):
|
2024-04-23 18:43:39 +00:00
|
|
|
player.exp += self.exp
|
2023-10-04 02:37:28 +00:00
|
|
|
|
|
|
|
def check_death(self, player):
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.health <= 0:
|
2023-10-04 02:37:28 +00:00
|
|
|
self.add_exp(player)
|
2024-04-23 18:43:39 +00:00
|
|
|
self.trigger_death_particles(
|
|
|
|
self.rect.center, self.name, self.visible_sprites)
|
2023-10-04 02:37:28 +00:00
|
|
|
self.kill()
|
|
|
|
|
|
|
|
def get_damaged(self, player, attack_type):
|
2024-04-23 18:43:39 +00:00
|
|
|
if self.vulnerable:
|
2023-10-04 02:37:28 +00:00
|
|
|
for _, direction, attacking_player in self.distance_direction_from_player:
|
|
|
|
if attacking_player == player:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.direction = -direction
|
|
|
|
self.move(
|
|
|
|
self.speed * self.knockback, self.hitbox, self.obstacle_sprites, self.rect)
|
2023-10-04 02:37:28 +00:00
|
|
|
break
|
|
|
|
if attack_type == 'weapon':
|
2024-04-23 18:43:39 +00:00
|
|
|
self.health -= player.get_full_weapon_damage()
|
2023-10-04 02:37:28 +00:00
|
|
|
else:
|
2024-04-23 18:43:39 +00:00
|
|
|
self.health -= player.get_full_magic_damage()
|
2023-10-04 02:37:28 +00:00
|
|
|
self.check_death(player)
|
2024-04-23 18:43:39 +00:00
|
|
|
self.hurt_time = pygame.time.get_ticks()
|
|
|
|
self.vulnerable = False
|
2023-10-04 02:37:28 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
self.get_action()
|
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.animate(self.status, self.vulnerable)
|
|
|
|
self.image = self.image
|
|
|
|
self.rect = self.rect
|
2023-10-04 02:37:28 +00:00
|
|
|
|
2024-04-23 18:43:39 +00:00
|
|
|
self.cooldowns(self.vulnerable)
|