-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.py
76 lines (62 loc) · 2.16 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
from random import choice, randint, random
import constants as co
import textures as tx
import utils
from tile import TileType
def get_texture(type: TileType):
if type == TileType.WATER:
return choice(tx.WATER_PARTICLES)
if type == TileType.NITROGEN:
return choice(tx.NITROGEN_PARTICLES)
if type == TileType.PHOSPHORUS:
return choice(tx.PHOSPHORUS_PARTICLES)
if 30 > type.value >= 20:
return choice(tx.BONUS_PARTICLES)
class Particle:
def __init__(self, x: int, y: int, vx: float, vy: float, type: TileType, fixed: bool = False) -> None:
self.vx = vx
self.vy = vy
self.type = type
self.texture = get_texture(self.type)
self.half_height = self.texture.get_height() / 2
self.x = x - self.half_height
self.y = y - self.half_height
self.life = co.PARTICLE_DURATION
self.done = False
self.is_fixed: bool = fixed
def age(self):
self.x += self.vx
self.y += self.vy
self.life -= 1
if self.life <= 0:
self.done = True
def is_visible(self, current_height: int):
return 0 <= self.y - current_height <= co.HEIGHT
@classmethod
def generate_extract_particle(cls, x: float, y: float, type: TileType, fixed: bool = False):
if random() < co.EXTRACT_PARTICLE_PROBABILITY:
return [
Particle(
*utils.generate_pos_velocity_in_disk(
co.EXTRACT_PARTICLE_RADIUS,
x, y,
utils.random_sym_float(co.EXTRACT_PARTICLE_SPEED), utils.random_sym_float(co.EXTRACT_PARTICLE_SPEED)
),
type,
fixed
)
]
else:
return []
@classmethod
def generate_bonus_particles(cls, x: float, y: float, type: TileType):
return [
Particle(
*utils.generate_circular_pos_velocity_in_disk(
co.TILE,
x, y
),
type,
False
) for count in range(co.BONUS_PARTICLES_COUNT)
]