-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.py
80 lines (61 loc) · 1.91 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
import pygame as pg
from typing import TYPE_CHECKING, Dict
from customtypes import ColorValue
from settings import TILE_SIZE
from lights import Light
if TYPE_CHECKING:
from world import World
class ParticleManager:
def __init__(self) -> None:
self.particles: list[Particle] = []
def update(self, world):
self.particles = [
particle for particle in self.particles if particle.update(world)
]
def add(self, particles):
self.particles.extend(particles)
def draw(self, world: "World"):
world.surf.fblits(
[(p.img, (p.pos - world.camera) * TILE_SIZE) for p in self.particles]
)
class Particle:
img_cache: Dict[tuple[int, ColorValue], pg.Surface] = {}
def __init__(
self,
life_time: float,
pos: pg.Vector2,
vel: pg.Vector2,
size: int,
color: tuple,
accel: pg.Vector2 = pg.Vector2(0, 0)
):
self.pos = pos
self.vel = vel
self.time = 0
self.life_time = life_time
self.size = size
self.start_size = size
self.color = color
self.accel = accel
self.light = Light(self.size * 6, self.pos, self.color, self)
def update(self, world: "World"):
self.pos += self.vel * world.dt
self.vel += self.accel * world.dt
self.time += world.dt
if self.time >= self.life_time:
self.light.kill()
return 0
frac = 1 - self.time/ self.life_time
self.size = int(self.start_size * frac +1)
self.light.radius = int(self.start_size * frac * 6)
return 1
@property
def img(self):
key = (self.size, self.color)
if img := self.img_cache.get(key, None):
pass
else:
img = pg.Surface((self.size, self.size))
img.fill(self.color)
self.img_cache[key] = img
return img