-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenemies.py
151 lines (128 loc) · 5.44 KB
/
enemies.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
import pygame
from settings import *
from entities import Entity
from support import *
class Inimigo(Entity):
def __init__(self,nome_inimigo,pos,grupo_sprite,obstaculo_sprites,damage_player):
super().__init__(grupo_sprite)
self.sprite_type = 'enemy'
self.status = 'idle'
self.image = pygame.image.load('graphics/inimigos/'+nome_inimigo+'/'+self.status+'/0.png').convert_alpha()
self.rect = self.image.get_rect(topleft=pos)
self.hitbox = self.rect.inflate(0,-10)
self.obstaculo_sprites = obstaculo_sprites
# Status do inimigo:
self.tipo_inimigo = nome_inimigo
info_tipo = enemy_data[self.tipo_inimigo]
self.health = info_tipo['health']
self.dano = info_tipo['dano']
self.attack = info_tipo['attack_type']
self.resistance = info_tipo['resistance']
self.speed = info_tipo['speed']
self.attack_radius = info_tipo['attack_radius']
self.notice_radius = info_tipo['notice_radius']
self.withdraw_radius = info_tipo['withdraw_radius']
self.attack_cooldown = info_tipo['cooldown_ataque']
self.startup_timer = info_tipo['startup_ataque']
#interacao com jogador
self.can_attack = True
self.attack_time = 0
self.attacking = False
self.launch_attack = False
self.interrupted = False
self.speed_multiplier = 1
self.damage_player = damage_player
#tempo de invensibilidade
self.vulneravel = True
self.tempo_ataque = 0
self.tempo_invencibilidade = 300
def cooldowns(self):
current_time = pygame.time.get_ticks()
if self.attacking:
if current_time - self.attacking_timer >= self.startup_timer:
self.launch_attack = True
self.attacking = False
if not self.can_attack:
if current_time - self.attack_time >= self.attack_cooldown:
self.can_attack = True
if not self.vulneravel:
if current_time - self.tempo_ataque >= self.tempo_invencibilidade:
self.vulneravel = True
def levar_dano(self, player, tipo_ataque):
if self.vulneravel:
self.interrupted = True
self.direction = -(self.get_pos_dir(player)[1])
if tipo_ataque == 'weapon':
self.health -= player.get_full_weapon_damage()
elif tipo_ataque == 'bola':
self.health -= 10
self.tempo_ataque = pygame.time.get_ticks()
self.vulneravel = False
def verificar_morte(self):
if self.health <= 0:
self.kill()
def get_pos_dir(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 reacao_dano(self):
if not self.vulneravel:
self.direction *= -self.resistance
def get_status(self, player):
distance = self.get_pos_dir(player)[0]
if distance <= self.attack_radius:
self.status = 'attack'
if distance <= self.withdraw_radius:
self.status = 'withdraw'
elif distance <= self.notice_radius:
self.status = 'move'
else:
self.status = 'idle'
def actions(self, player):
if self.status == 'attack' and self.can_attack:
if not self.attacking and not self.launch_attack:
self.attacking = True
self.attacking_timer = pygame.time.get_ticks()
self.speed_multiplier = 0.5
elif self.status == 'move':
self.direction = self.get_pos_dir(player)[1]
elif self.status == 'withdraw':
self.direction = -(self.get_pos_dir(player)[1])
else:
self.direction = pygame.math.Vector2()
if self.launch_attack and not self.interrupted:
self.launch_attack = False
self.attack_time = pygame.time.get_ticks()
self.can_attack = False
self.damage_player(self.dano,self.attack)
self.speed_multiplier = 1
if self.launch_attack and self.interrupted:
self.launch_attack = False
self.can_attack = True
self.interrupted = False
self.speed_multiplier = 1
def update(self):
self.move(self.speed*self.speed_multiplier)
self.cooldowns()
if not self.vulneravel:
self.image = pygame.image.load('graphics/inimigos/'+self.tipo_inimigo+'/attack_sprites/hurt.png')
alpha = self.flicker()
self.image.set_alpha(alpha)
else:
self.image.set_alpha(255)
def enemy_update(self,player):
self.get_status(player)
self.actions(player)
if self.status != 'attack':
self.image = pygame.image.load('graphics/inimigos/'+self.tipo_inimigo+'/'+self.status+'/0.png')
if self.attacking:
self.image = pygame.image.load('graphics/inimigos/'+self.tipo_inimigo+'/attack_sprites/attacking.png')
if not self.can_attack:
self.image = pygame.image.load('graphics/inimigos/'+self.tipo_inimigo+'/attack_sprites/cooldown.png')
self.verificar_morte()
self.reacao_dano()