-
Notifications
You must be signed in to change notification settings - Fork 0
/
effect.py
301 lines (222 loc) · 10.5 KB
/
effect.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from setup import *
class BloodEffect:
LIFE_TIME = 1000
SPEED_RANGE = [250, 350]
RADIUS_RANGE = [3, 3.75]
blood_frames = [
[pygame.image.load(f"assets/effects/blood1/{i}.png").convert_alpha() for i in range(16)],
[pygame.image.load(f"assets/effects/blood2/{i}.png").convert_alpha() for i in range(16)],
[pygame.image.load(f"assets/effects/blood3/{i}.png").convert_alpha() for i in range(16)]
]
def __init__(self, position, radius, color=None):
self.position = pygame.Vector2(position)
self.radius = radius * lerp(self.RADIUS_RANGE[0], self.RADIUS_RANGE[1], random.random())
self.life_time = 0
self.current_frame = 0
self.surf_frames = random.choice(self.blood_frames)
if color is None:
color = random.choice(EFFECT_COLORS)
for frame in self.surf_frames:
px_array = pygame.PixelArray(frame)
px_array.replace(pygame.Color(250, 3, 35), pygame.Color(color))
px_array.close()
self.frame_txt = [Texture.from_surface(renderer, frame) for frame in self.surf_frames]
def update(self, delta):
self.life_time += delta
if self.life_time >= self.LIFE_TIME / len(self.surf_frames) * (self.current_frame + 1):
self.current_frame += 1
if self.life_time >= self.LIFE_TIME:
return True
def draw(self):
if self.life_time < self.LIFE_TIME:
self.frame_txt[self.current_frame].draw(None, pygame.Rect(self.position.x - self.radius,
self.position.y - self.radius,
self.radius * 2, self.radius * 2))
class BloodSplatter:
LIFE_TIME = 4000
FADE_TIME = 1000
RADIUS_RANGE = [2, 2.75]
ANGLE_OFFSET = 35
blood_frames = [
pygame.image.load("assets/effects/splatter/bloodslash1.png").convert_alpha(),
pygame.image.load("assets/effects/splatter/bloodslash2.png").convert_alpha()
]
LIGHT_COLOR1 = pygame.Color(110, 110, 110)
DARK_COLOR1 = pygame.Color(84, 84, 84)
LIGHT_COLOR2 = pygame.Color(83, 83, 83)
DARK_COLOR2 = pygame.Color(74, 74, 74)
color_frames = [dict(), dict()]
for c in EFFECT_COLORS:
for i, f in enumerate(blood_frames):
c_f = f.copy()
px_array = pygame.PixelArray(c_f)
px_array.replace(LIGHT_COLOR1 if i == 0 else LIGHT_COLOR2, pygame.Color(c))
px_array.replace(DARK_COLOR1 if i == 0 else DARK_COLOR2, pygame.Color(darken(c, 0.875)))
px_array.close()
color_frames[i][c] = c_f
def __init__(self, position, radius, angle, color=None):
self.position = pygame.Vector2(position)
self.radius = radius * lerp(self.RADIUS_RANGE[0], self.RADIUS_RANGE[1], random.random())
self.angle = angle
self.life_time = self.LIFE_TIME
self.fade_time = self.FADE_TIME
img_index = random.randint(0, len(self.blood_frames) - 1)
self.alpha = 255
if color is None:
color = random.choice(EFFECT_COLORS)
self.frame = Texture.from_surface(renderer, self.color_frames[img_index][color])
def update(self, delta):
if self.life_time > 0:
self.life_time -= delta
else:
self.fade_time -= delta
if self.fade_time <= 0 and self.life_time <= 0:
return True
if self.life_time <= 0:
self.alpha = int(lerp(0, 255, self.fade_time / self.FADE_TIME))
self.frame.alpha = self.alpha
def draw(self):
self.frame.draw(None, pygame.Rect(self.position.x - self.radius,
self.position.y - self.radius,
self.radius * 2, self.radius * 2),
self.angle + self.ANGLE_OFFSET, origin=(self.radius, self.radius))
class SplitEffect:
SPEED_PERCENT_RANGE = [65, 85]
gravity = 275
def __init__(self, position, frame, fruit_velocity, normal_velocity):
self.position = pygame.Vector2(position)
self.velocity = pygame.Vector2(fruit_velocity) * lerp(self.SPEED_PERCENT_RANGE[0], self.SPEED_PERCENT_RANGE[1],
random.random()) / 100 + pygame.Vector2(normal_velocity)
self.acceleration = pygame.Vector2(0, self.gravity)
self.angle = frame.angle
self.direction = random.choice([-1, 1])
self.frame = frame
self.width, self.height = self.frame.get_rect().width, self.frame.get_rect().height
def update(self, delta):
self.velocity += self.acceleration * delta / 1000
self.position += self.velocity * delta / 1000
self.angle += 360 * delta / 1000 / 10 * self.direction
if self.position.y - self.height / 2 > HEIGHT:
return True
def draw(self):
self.frame.angle = self.angle
# print(self.position)
self.frame.draw(None, pygame.Rect(self.position.x - self.width / 2,
self.position.y - self.height / 2,
self.width, self.height))
self.width, self.height = self.frame.get_rect().width, self.frame.get_rect().height
@staticmethod
def find_normals(v):
return pygame.Vector2(-v.y, v.x), pygame.Vector2(v.y, -v.x)
@staticmethod
def should_split(texture, angle, image_position, mouse_position, mouse_direction, radius):
if mouse_direction.x == 0:
mouse_direction.x += 0.0001
a = math.degrees(math.atan(mouse_direction.y / mouse_direction.x))
diagonal = math.sqrt(2 * ((radius * 2) ** 2))
txt = Texture(renderer, (diagonal, diagonal), target=True)
txt.blend_mode = pygame.BLEND_ADD
renderer.target = txt
texture.draw(None, pygame.Rect((diagonal - radius * 2) / 2, (diagonal - radius * 2) / 2, radius * 2,
radius * 2), angle - a, origin=(radius, radius))
renderer.target = None
img_size = pygame.Vector2(diagonal, diagonal)
center = img_size / 2
# finding end and start points of the splitting line
# [x,y] = mouse_position + t * mouse_direction # vector equation
# x = mouse_position.x + t * mouse_direction.x
# y = mouse_position.y + t * mouse_direction.y
mp = mouse_position - image_position + center
t1 = (- mp.x) / mouse_direction.x
p1 = mp + t1 * mouse_direction
p3 = (p1 - center).rotate(-a) + center
MIN_SPLIT = 0.25
slice_percent = clamp(p3.y, 0, img_size.y) / img_size.y
if MIN_SPLIT < slice_percent < 1 - MIN_SPLIT:
return True
return False
@staticmethod
def split_image(texture, angle, image_position, mouse_position, mouse_direction, radius):
if mouse_direction.x == 0:
mouse_direction.x += 0.0001
a = math.degrees(math.atan(mouse_direction.y / mouse_direction.x))
diagonal = math.sqrt(2 * ((radius * 2) ** 2))
txt = Texture(renderer, (diagonal, diagonal), target=True)
txt.blend_mode = pygame.BLEND_ADD
renderer.target = txt
texture.draw(None, pygame.Rect((diagonal - radius * 2) / 2, (diagonal - radius * 2) / 2, radius * 2,
radius * 2), angle - a, origin=(radius, radius))
renderer.target = None
img_size = pygame.Vector2(diagonal, diagonal)
center = img_size / 2
# finding end and start points of the splitting line
# [x,y] = mouse_position + t * mouse_direction # vector equation
# x = mouse_position.x + t * mouse_direction.x
# y = mouse_position.y + t * mouse_direction.y
mp = mouse_position - image_position + center
t1 = (- mp.x) / mouse_direction.x
p1 = mp + t1 * mouse_direction
p3 = (p1 - center).rotate(-a) + center
half1 = Image(txt, pygame.Rect(0, 0, img_size.x, clamp(p3.y, 0, img_size.y)))
half2 = Image(txt,
pygame.Rect(0, clamp(p3.y, 0, img_size.y), img_size.x, clamp(img_size.y - p3.y, 0, img_size.y)))
p5 = half1.get_rect().center - center
pos1 = p5.rotate(a) + image_position
p6 = half2.get_rect().center - center
pos2 = p6.rotate(a) + image_position
half1.angle = a
half2.angle = a
return half1, half2, pos1, pos2
class SlashEffect:
SLASH_SURFS = [pygame.image.load(f"assets/effects/sword_slashes/File{i}.png").convert_alpha()
for i in range(1, 7)]
LIFETIME = 600
def __init__(self, position, angle):
self.position = pygame.Vector2(position)
self.angle = angle
self.time = 0
self.current_frame = 0
self.slash_frames = [Texture.from_surface(renderer, frame) for frame in self.SLASH_SURFS]
def update(self, delta):
self.time += delta
if self.time >= self.LIFETIME / len(self.SLASH_SURFS):
self.time = 0
self.current_frame += 1
if self.current_frame >= len(self.SLASH_SURFS):
return True
def draw(self):
frame = self.slash_frames[self.current_frame]
frame.draw(None, self.position - pygame.Vector2(frame.width, frame.height) / 2, self.angle)
class FadeInEffect:
def __init__(self, fade_time=500):
self.fade_time = fade_time
self.surf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
self.surf.fill(BLACK)
self.time = self.fade_time
self.alpha = 255
self.txt = Texture.from_surface(renderer, self.surf)
def update(self, delta):
self.time -= delta
if self.time <= 0:
return True
self.alpha = int(lerp(0, 255, self.time / self.fade_time))
def draw(self):
self.txt.alpha = self.alpha
self.txt.draw(None, (0, 0))
class FadeOutEffect:
def __init__(self, fade_time=500, max_alpha=255):
self.fade_time = fade_time
self.max_alpha = max_alpha
self.surf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
self.surf.fill(BLACK)
self.time = 0
self.alpha = 0
self.txt = Texture.from_surface(renderer, self.surf)
def update(self, delta):
self.time += delta
if self.time >= self.fade_time:
return True
self.alpha = int(lerp(0, self.max_alpha, self.time / self.fade_time))
def draw(self):
self.txt.alpha = self.alpha
self.txt.draw(None, (0, 0))