-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparticle.py
167 lines (132 loc) · 5.52 KB
/
particle.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
##!/usr/bin/env python3
from image_manager import ImageManager
from primitives import GameObject, Pose
import pygame
import random
import constants as c
class Particle():
def __init__(self):
self.age = 0
self.duration = None
self.dead = False
def get_alpha(self):
return 255
def get_scale(self):
return 1
def through(self, loading=1):
""" Increase loading argument to 'frontload' the animation. """
if self.duration is None:
return 0
else:
return 1 - (1 - self.age / self.duration)**loading
def update(self, dt, events):
self.age += dt
if self.duration and self.age > self.duration:
self.destroy()
def destroy(self):
self.dead = True
class CardParticle(Particle):
def __init__(self, position, direction):
super().__init__()
self.duration = 1.25
self.position = Pose(position, 0)
direction = Pose(direction, 0)
self.velocity = direction.copy()
self.velocity += Pose((random.random() * 25, random.random() * 25), 0)
self.velocity *= random.random() * 2 + 2
self.velocity.angle = random.random() * 1000 - 500
self.surf = ImageManager.load("assets/images/tripart.png")
self.base_scale = 1.25 - random.random() * 0.5
self.age += random.random() * self.duration * 0.5
def get_alpha(self):
return 255 - 255*self.through(2)
def get_scale(self):
return self.base_scale * (1 - 0.5 * self.through())
def update(self, dt, events):
super().update(dt, events)
self.position += self.velocity * dt
self.velocity *= 0.1**dt
def draw(self, surface, offset=(0, 0)):
my_surf = pygame.transform.rotate(self.surf, angle=self.position.angle)
my_surf = pygame.transform.scale(my_surf, (my_surf.get_width() * self.get_scale(), my_surf.get_height() * self.get_scale()))
my_surf.set_alpha(self.get_alpha())
x = offset[0] - my_surf.get_width()//2 + self.position.x
y = offset[1] - my_surf.get_height()//2 + self.position.y
surface.blit(my_surf, (x, y))
class RectParticle(Particle):
def __init__(self, position):
super().__init__()
self.duration = 0.4
self.position = Pose(position, 0)
self.surf = pygame.Surface((c.CARD_WIDTH, c.CARD_HEIGHT))
self.surf.fill((255, 255, 255))
def get_alpha(self):
return 255 - 255*self.through(2)
def get_scale(self):
return 1 + 0.75 * self.through(2)
def update(self, dt, events):
super().update(dt, events)
def draw(self, surface, offset=(0, 0)):
my_surf = pygame.transform.scale(self.surf, (self.surf.get_width() * self.get_scale(), self.surf.get_height() * self.get_scale()))
my_surf.set_alpha(self.get_alpha())
x = offset[0] - my_surf.get_width()//2 + self.position.x
y = offset[1] - my_surf.get_height()//2 + self.position.y
surface.blit(my_surf, (x, y))
class MulchParticle(Particle):
def __init__(self, tile):
super().__init__()
self.duration = 1
self.position = Pose((0, 0), 0)
self.surf = ImageManager.load_copy("assets/images/large_tripart.png")
self.filter = self.surf.copy()
self.filter.fill((255, 255, 255))
if tile.orientation == c.DOWN:
self.surf = pygame.transform.flip(self.surf, False, True)
def get_alpha(self):
return 255 - 255 * self.through(2)
def get_color(self):
r = 255 - 255 * self.through(2)
g = 255
b = 255 - 150 * self.through(2)
return r, g, b
def draw(self, surface, offset=(0, 0)):
my_surf = pygame.transform.scale(self.surf, (self.surf.get_width() * self.get_scale(), self.surf.get_height() * self.get_scale()))
my_surf.set_alpha(self.get_alpha())
self.filter.fill(self.get_color())
my_surf.blit(self.filter, (0, 0), special_flags=pygame.BLEND_MULT)
x = offset[0] - my_surf.get_width()//2 + self.position.x
y = offset[1] - my_surf.get_height()//2 + self.position.y
surface.blit(my_surf, (x, y))
class BloodParticle(Particle):
def __init__(self, tile, delay=0):
super().__init__()
self.age = -delay
self.duration = 2
self.position = Pose((0, 0), 0)
self.surf = ImageManager.load_copy("assets/images/blood_rune.png")
self.filter = self.surf.copy()
self.filter.fill((255, 255, 255))
if tile.orientation == c.DOWN:
self.surf = pygame.transform.flip(self.surf, False, True)
self.tile = tile
def get_alpha(self):
return min(255 - 255 * self.through(), self.through()*800)
def get_color(self):
if self.age < 0:
return (255, 150, 150)
r = 255
g = 128 - 128 * self.through(2)
b = 128 - 128 * self.through(2)
return r, g, b
def draw(self, surface, offset=(0, 0)):
my_surf = pygame.transform.scale(self.surf, (self.surf.get_width() * self.get_scale(), self.surf.get_height() * self.get_scale()))
self.filter.fill(self.get_color())
my_surf.blit(self.filter, (0, 0), special_flags=pygame.BLEND_MULT)
my_surf.set_alpha(self.get_alpha())
x = offset[0] - my_surf.get_width()//2 + self.position.x
y = offset[1] - my_surf.get_height()//2 + self.position.y
surface.blit(my_surf, (x, y))
def update(self, dt, events):
super().update(dt, events)
if self.through() > 0.15:
self.tile.state = c.HEALTHY