1import pygame
2import random
3from pygame.locals import *
4
5from locals import *
6
7class Particle (pygame.sprite.Sprite):
8    def __init__(self, position, vect, colour, acceleration, size, life, opacity, underwater = True):
9        pygame.sprite.Sprite.__init__(self)
10
11        self.rect = pygame.Rect(position[0], position[1], size, size)
12        self.vect = vect
13        self.colour = colour
14        self.acceleration = acceleration
15        self.initial_life = life
16        self.life = life
17        self.opacity = opacity
18        self.underwater = underwater
19
20        self.image = pygame.Surface([int(size), int(size)])#, SRCALPHA, 32)
21        self.image.fill((255,0,255))
22        self.image.set_colorkey((255,0,255))
23
24        pygame.draw.ellipse(self.image, self.colour, self.image.get_rect())
25
26        if Variables.alpha:
27            self.image.set_alpha(self.life * 255 * self.opacity / self.initial_life)
28
29    def update(self):
30        self.rect.left += self.vect[0]
31        self.rect.top += self.vect[1]
32        self.vect[0] += self.acceleration[0]
33        self.vect[1] += self.acceleration[1]
34        if self.life > 0:
35            self.life -= 1
36
37        if not self.underwater and self.vect[1] > 0.0:
38            self.life = 0
39
40        if Variables.alpha:
41            self.image.set_alpha(self.life * 255 * self.opacity / self.initial_life)
42
43class Particles (pygame.sprite.Sprite):
44    def __init__(self):
45        pygame.sprite.Sprite.__init__(self)
46
47        self.particles = []
48        self.particle_sprites = pygame.sprite.Group()
49        if Variables.alpha:
50            self.image = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), SRCALPHA, 32)
51        else:
52            self.image = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
53            self.image.set_colorkey((255,0,255))
54        self.rect = self.image.get_rect()
55
56    def update(self):
57        self.image.fill((255,0,255,0))
58
59        self.particle_sprites.draw(self.image)
60
61        for p in self.particles:
62            p.update()
63            if p.life <= 0:
64                self.particles.remove(p)
65                self.particle_sprites.remove(p)
66
67    def add_blood_particle(self, position):
68        particle = Particle(position, [random.random() * 5 - 2.5, random.random() * 5 - 2.5], [230, 30, 20], [0.0, 0.7], random.random() * 5.0 + 1.0, random.random() * 30, 1.0)
69        self.particles.append(particle)
70        self.particle_sprites.add(particle)
71
72    def add_explosion_particle(self, position):
73        particle = Particle(position, [random.random() * 5 - 2.5, random.random() * 5 - 2.5], [230, 30 + random.random() * 200, 20], [0.0, 0.2], random.random() * 7.0 + 1.0, random.random() * 30, 1.0)
74        self.particles.append(particle)
75        self.particle_sprites.add(particle)
76
77    def add_water_particle(self, position):
78        particle = Particle(position, [random.random() * 5 - 2.5, -random.random() * 2.5 - 2.0], (20,60,180), (0.0, 0.3), random.random() * 5.0 + 1.0, random.random() * 30, 0.5, underwater = False)
79        self.particles.append(particle)
80        self.particle_sprites.add(particle)
81
82    def add_debris_particle(self, position):
83        particle = Particle(position, [random.random() * 5 - 2.5, random.random() * 5 - 2.5], [90, 90, 90], [0.0, 0.2], random.random() * 7.0 + 1.0, random.random() * 30, 1.0)
84        self.particles.append(particle)
85        self.particle_sprites.add(particle)
86
87    def add_wood_particle(self, position):
88        particle = Particle(position, [random.random() * 5 - 2.5, random.random() * 5 - 2.5], [148, 69, 6], [0.0, 0.2], random.random() * 7.0 + 1.0, random.random() * 30, 1.0)
89        self.particles.append(particle)
90        self.particle_sprites.add(particle)
91
92    def add_steam_particle(self, position):
93        particle = Particle(position, [-random.random() * 0.3, -random.random() * 0.1], [240, 240, 240], [-0.1, -0.00002], random.random() * 10.0 + 1.0, random.random() * 30, 0.5)
94        self.particles.append(particle)
95        self.particle_sprites.add(particle)
96
97    def add_fire_steam_particle(self, position):
98        particle = Particle(position, [-random.random() * 0.3, -random.random() * 0.1], [255, 210, 170], [-0.1, -0.00002], random.random() * 11.0 + 1.0, random.random() * 30, 0.4, False)
99        self.particles.append(particle)
100        self.particle_sprites.add(particle)
101
102    def add_trace_particle(self, position):
103        particle = Particle(position, [0.0, 0.0], [170, 170, 170], [0.0, 0.0], 6.0, 5+random.random() * 5, 0.1+random.random()*0.1, False)
104        self.particles.append(particle)
105        self.particle_sprites.add(particle)
106