-
Notifications
You must be signed in to change notification settings - Fork 0
/
npc.py
218 lines (162 loc) · 6.57 KB
/
npc.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from sprite_object import *
from player import *
from sound import *
from random import randint, random ,choice
class NPC(AnimatedSprite):
def __init__(self, game, path='resources/sprites/npc/soldier/0.png', pos=(7.5, 11.5),
scale=0.6, shift=0.38, animation_time=180):
super().__init__(game, path, pos, scale, shift, animation_time)
self.attack_images = self.get_images(self.path + '/attack')
self.death_images = self.get_images(self.path + '/death')
self.idle_images = self.get_images(self.path + '/idle')
self.pain_images = self.get_images(self.path + '/pain')
self.walk_images = self.get_images(self.path + '/walk')
self.attack_dist = 1.5
self.speed = 0.025
self.size = 20
self.health = 100
self.attack_damage = 1
self.alive = True
self.pain = False
self.ray_cast_value = True
self.frame_counter = 0
self.player_search_trigger = True
self.follow_player=True
self.attack_player=True
self.tick=0
def update(self):
self.check_animation_time()
self.get_sprite()
self.run_logic()
#debug 2d
# self.draw_ray_cast()
def check_wall(self, x, y):
return (x, y) not in self.game.map.world_map
def check_wall_collision(self, dx, dy):
if self.check_wall(int(self.x + dx * self.size), int(self.y)):
self.x += dx
if self.check_wall(int(self.x), int(self.y + dy * self.size)):
self.y += dy
def movement(self):
next_pos = self.game.pathfinding.get_path(self.map_pos, self.game.player.map_pos)
next_x, next_y = next_pos
#debug 2d
# pg.draw.rect(self.game.screen, 'blue', (100 * next_x, 100 * next_y, 100, 100))
if next_pos not in self.game.object_handler.npc_positions:
angle = math.atan2(next_y + 0.5 - self.y, next_x + 0.5 - self.x)
dx = math.cos(angle) * self.speed
dy = math.sin(angle) * self.speed
self.check_wall_collision(dx, dy)
def attack(self):
if self.animation_trigger:
self.game.player.get_damage(self.attack_damage)
self.attack_player=False
self.tick=pg.time.get_ticks()
def animate_death(self):
if not self.alive:
if self.game.global_trigger and self.frame_counter < len(self.death_images) - 1:
self.death_images.rotate(-1)
self.image = self.death_images[0]
self.frame_counter += 1
def animate_pain(self):
self.animate(self.pain_images)
if self.animation_trigger:
self.tick=pg.time.get_ticks()
self.follow_player=False
self.pain = False
def check_hit_in_npc(self):
if (self.game.player.axe) and self.ray_cast_value :
if HALF_WIDTH - self.sprite_half_width < self.screen_x < HALF_WIDTH + self.sprite_half_width:
# self.game.sound.npc_pain.play()
if self.game.player.axe:
self.game.player.axe = False
self.pain = True
self.health -= self.game.axe.damage
self.check_health()
def check_stun(self):
self.checktime=pg.time.get_ticks()
if (self.checktime - self.tick)>5500:
self.follow_player = True
self.attack_player = True
# debug
# print(self.tick,self.checktime)
def check_health(self):
if self.health < 1:
self.alive = False
def run_logic(self):
if self.alive:
self.ray_cast_value = self.ray_cast_player_npc()
self.check_stun()
self.check_hit_in_npc()
#print (self.x,self.y)
if self.pain:
self.animate_pain()
elif self.attack_player and self.follow_player :
if (self.dist < self.attack_dist) and self.ray_cast_value:
self.animate(self.attack_images)
self.attack()
else :
self.animate(self.walk_images)
self.movement()
else:
self.animate(self.idle_images)
else:
self.animate_death()
@property
def map_pos(self):
return int(self.x), int(self.y)
def ray_cast_player_npc(self):
if self.game.player.map_pos == self.map_pos:
return True
wall_dist_v, wall_dist_h = 0, 0
player_dist_v, player_dist_h = 0, 0
ox, oy = self.game.player.pos
x_map, y_map = self.game.player.map_pos
ray_angle = self.theta
sin_a = math.sin(ray_angle)
cos_a = math.cos(ray_angle)
# horizontals
y_hor, dy = (y_map + 1, 1) if sin_a > 0 else (y_map - 1e-6, -1)
depth_hor = (y_hor - oy) / sin_a
x_hor = ox + depth_hor * cos_a
delta_depth = dy / sin_a
dx = delta_depth * cos_a
for i in range(MAX_DEPTH):
tile_hor = int(x_hor), int(y_hor)
if tile_hor == self.map_pos:
player_dist_h = depth_hor
break
if tile_hor in self.game.map.world_map:
wall_dist_h = depth_hor
break
x_hor += dx
y_hor += dy
depth_hor += delta_depth
# verticals
x_vert, dx = (x_map + 1, 1) if cos_a > 0 else (x_map - 1e-6, -1)
depth_vert = (x_vert - ox) / cos_a
y_vert = oy + depth_vert * sin_a
delta_depth = dx / cos_a
dy = delta_depth * sin_a
for i in range(MAX_DEPTH):
tile_vert = int(x_vert), int(y_vert)
if tile_vert == self.map_pos:
player_dist_v = depth_vert
break
if tile_vert in self.game.map.world_map:
wall_dist_v = depth_vert
break
x_vert += dx
y_vert += dy
depth_vert += delta_depth
player_dist = max(player_dist_v, player_dist_h)
wall_dist = max(wall_dist_v, wall_dist_h)
if 0 < player_dist < wall_dist or not wall_dist:
return True
return False
#
def draw_ray_cast(self):
pg.draw.circle(self.game.screen, 'red', (100 * self.x, 100 * self.y), 15)
if self.ray_cast_player_npc():
pg.draw.line(self.game.screen, 'orange', (100 * self.game.player.x, 100 * self.game.player.y),
(100 * self.x, 100 * self.y), 2)