-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.py
153 lines (128 loc) · 5.31 KB
/
enemy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pygame
from settings import *
from entity import Entity
from support import *
class Enemy(Entity):
def __init__(self, monster_name, pos, groups, obstacle_sprites, damage_player, trigger_death_particles, add_exp):
# configuração geral
super().__init__(groups)
self.sprite_type = 'enemy'
# configuração dos gráficos
self.import_graphics(monster_name)
self.status = 'idle'
self.image = self.animations[self.status][self.frame_index]
# movimento
self.rect = self.image.get_rect(topleft = pos)
self.hitbox = self.rect.inflate(0, -10)
self.obstacle_sprites = obstacle_sprites
# estatísticas
self.monster_name = monster_name
monster_info = monster_data[self.monster_name]
self.health = monster_info['health']
self.exp = monster_info['exp']
self.speed = monster_info['speed']
self.attack_damage = monster_info['damage']
self.resistance = monster_info['resistance']
self.attack_radius = monster_info['attack_radius']
self.notice_radius = monster_info['notice_radius']
self.attack_type = monster_info['attack_type']
# interação com o jogador
self.can_attack = True
self.attack_time = None
self.attack_cooldown = 400
self.damage_player = damage_player
self.trigger_death_particles = trigger_death_particles
self.add_exp = add_exp
# temporizador de invencibilidade
self.vulnerable = True
self.hit_time = None
self.invincibility_duration = 300
self.death_sound = pygame.mixer.Sound('audio/death.wav')
self.death_sound.set_volume(0.2)
self.hit_sound = pygame.mixer.Sound('audio/hit.wav')
self.hit_sound.set_volume(0.2)
self.attack_sound = pygame.mixer.Sound(monster_info['attack_sound'])
self.attack_sound.set_volume(0.3)
def import_graphics(self, name):
self.animations = {'idle':[], 'move':[], 'attack':[]}
main_path = f'graphics/monsters/{name}/'
for animation in self.animations.keys():
self.animations[animation] = import_folder(main_path+animation)
def get_player_distance_direction(self, player):
enemy_vec = pygame.math.Vector2(self.rect.center)
player_vec = pygame.math.Vector2(player.rect.center)
distance = (player_vec - enemy_vec).magnitude()
if distance > 0:
direction = (player_vec - enemy_vec).normalize()
else:
direction = pygame.math.Vector2()
return (distance, direction)
def get_status(self, player):
distance =self.get_player_distance_direction(player)[0]
if distance <= self.attack_radius and self.can_attack:
if self.status != 'attack':
self.frame_index = 0
self.status = 'attack'
elif distance <= self.notice_radius:
self.status = 'move'
else:
self.status = 'idle'
def actions(self,player):
if self.status == 'attack':
self.attack_time = pygame.time.get_ticks()
self.damage_player(self.attack_damage, self.attack_type)
self.attack_sound.play()
elif self.status == 'move':
self.direction = self.get_player_distance_direction(player)[1]
else:
self.direction = pygame.math.Vector2()
def animate(self):
animation = self.animations[self.status]
self.frame_index += self.animation_speed
if self.frame_index >= len(animation):
if self.status == 'attack':
self.can_attack = False
self.frame_index = 0
self.image = animation[int(self.frame_index)]
self.rect = self.image.get_rect(center = self.hitbox.center)
if not self.vulnerable:
alpha = self.wave_value()
self.image.set_alpha(alpha)
else:
self.image.set_alpha(255)
def cooldowns(self):
current_time = pygame.time.get_ticks()
if not self.can_attack:
if current_time - self.attack_time >= self.attack_cooldown:
self.can_attack = True
if not self.vulnerable:
if current_time - self.hit_time >= self.invincibility_duration:
self.vulnerable = True
def get_damage(self, player, attack_type):
if self.vulnerable:
self.hit_sound.play()
self.direction = self.get_player_distance_direction(player)[1]
if attack_type == 'weapon':
self.health -= player.get_full_weapon_damage()
else:
self.health -= player.get_full_magic_damage()
self.hit_time = pygame.time.get_ticks()
self.vulnerable = False
def check_death(self):
if self.health <= 0:
self.kill()
self.trigger_death_particles(self.rect.center, self.monster_name)
self.add_exp(self.exp)
self.death_sound.play()
def hit_reaction(self):
if not self.vulnerable:
self.direction = (-self.resistance) * self.direction
def update(self):
self.hit_reaction()
self.move(self.speed)
self.animate()
self.cooldowns()
self.check_death()
def enemy_update(self,player):
self.get_status(player)
self.actions(player)