-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenemy.py
More file actions
146 lines (124 loc) · 5.09 KB
/
Copy pathenemy.py
File metadata and controls
146 lines (124 loc) · 5.09 KB
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
import pygame
from settings import *
from entity import Entity
from support import *
from random import randint
class Enemy(Entity):
def __init__(self, monster_name,pos,groups, obstacle_sprites,damage_player,trigger_death_particles):
super().__init__(groups)
self.sprite_type = 'enemy'
# graphics setup
self.import_graphics(monster_name)
self.status = 'idle'
self.image = self.animations[self.status][self.frame_index]
self.rect = self.image.get_rect(topleft = pos)
# movement
self.rect = self.image.get_rect(topleft = pos)
self.hitbox = self.rect.inflate(0, -10)
self.obstacle_sprites = obstacle_sprites
# stats
self.monster_name = monster_name
self.monster_info = monster_data[self.monster_name]
self.health = self.monster_info['health']
self.exp = self.monster_info['exp']
self.speed = self.monster_info['speed']
self.attack_damage = self.monster_info['damage']
self.resistance = self.monster_info['resistance']
self.attack_radius = self.monster_info['attack_radius']
self.notice_radius = self.monster_info['notice_radius']
self.attack_type = self.monster_info['attack_type']
# player interactions
self.can_attack = True
self.attack_time = None
self.attack_cooldown = 400
self.damage_player = damage_player
self.trigger_death_particles = trigger_death_particles
# Vulnerability cooldowns
self.vulberable = True
self.hit_time = None
self.invincibility_duration = 400
def import_graphics(self,name):
self.animations = {'idle': [], 'move': [], 'attack': []}
main_path = f'./assets/chrono/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)
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.vulberable:
alpha = self.wave_value()
self.image.set_alpha(alpha)
else:
self.image.set_alpha(255)
def cooldowns(self):
current_time = pygame.time.get_ticks()
# AGGRO COOLDOWN
if not self.can_attack:
current_time = pygame.time.get_ticks()
if current_time - self.attack_time >= self.attack_cooldown:
self.can_attack =True
# VULNERABILITY COOLDOWN
if not self.vulberable:
if current_time - self.hit_time >= self.invincibility_duration:
self.vulberable = True
def get_damage(self,player,attack_type):
if self.vulberable:
self.direction = self.get_player_distance_direction(player)[1]
if attack_type == 'weapon':
self.health -= player.get_full_weapon_damage()
# Player's Burst goes up
player.burst += randint(1,5)
else:
self.health += player.get_full_magic_damage()
self.hit_time = pygame.time.get_ticks()
self.vulberable = False
def check_death(self,player):
if self.health <= 0:
player.gain_exp(self.exp)
self.kill()
self.trigger_death_particles(self.rect.center,self.monster_name)
def hit_reaction(self):
if not self.vulberable:
self.direction *= -self.resistance
def update(self):
self.hit_reaction()
self.move(self.speed)
self.animate()
self.cooldowns()
def enemy_update(self,player):
self.get_status(player)
self.actions(player)
self.check_death(player)