Module AggiEngine.particles

Expand source code
from typing import Optional

import numpy as np

from AggiEngine import GameScreen


class Particles:

    def __init__(self, gameScreen: GameScreen, startColor: Optional[list] = None,
                 endColor: Optional[list] = None, shape: Optional[list] = None, rate=0.5,
                 count=100, endSize=0.01, sizeDecrease=0.95, colorFade=0.05):
        if not endColor:
            self.endColor = [1, 1, 1, 1]
        else:
            self.endColor = endColor

        if not startColor:
            self.startColor = [1, 1, 1, 1]
        else:
            self.startColor = startColor

        if not shape:
            self.shape = [[0, 0], [0.05, 0], [0.05, -0.05], [0, -0.05]]
        else:
            self.shape = shape

        self.gameScreen = gameScreen
        self.particles = []
        self.rate = rate
        self.count = count
        self.endSize = endSize
        self.sizeDecrease = sizeDecrease
        self.colorFade = colorFade
        self.time = 0

    def update(self) -> None:
        '''
        Updates the particles, color, size and  deletes old particles.
        '''
        toRemove = []
        for particle in self.particles:
            if abs(particle[1][0][0] - particle[1][1][0]) < self.endSize:
                toRemove.append(particle)

            shape = []
            for vert in particle[1]:
                shape.append([vert[0] * self.sizeDecrease, vert[1] * self.sizeDecrease])
            particle[1] = shape
            particle[2] = self.getColor(particle[-1])
            particle[-1] -= self.colorFade
            self.gameScreen.renderInfoList.append(particle)

        for particle in toRemove:
            self.particles.remove(particle)

        self.time += self.rate

    def emit(self, position: list) -> None:
        '''
        Appends are new particle to the particle list.
        '''
        
        if self.time > 1:
            if len(self.particles) < self.count:
                self.particles.append([-1, self.shape, self.getColor(0), position, 0, 0])
            self.time = 0

    def getColor(self, amount: float) -> list:
        '''
        Interpolates the start and end color values and returns the value.
        '''
        
        r = abs(self.startColor[0] - self.endColor[0]) * amount + self.startColor[0]
        g = abs(self.startColor[1] - self.endColor[1]) * amount + self.startColor[1]
        b = abs(self.startColor[2] - self.endColor[2]) * amount + self.startColor[2]
        a = abs(self.startColor[3] - self.endColor[3]) * amount + self.startColor[3]
        
        return [max(r, self.endColor[0]), max(g, self.endColor[1]), max(b, self.endColor[2]), max(a, self.endColor[3])]

Classes

class Particles (gameScreen: GameScreen, startColor: typing.Union[list, NoneType] = None, endColor: typing.Union[list, NoneType] = None, shape: typing.Union[list, NoneType] = None, rate=0.5, count=100, endSize=0.01, sizeDecrease=0.95, colorFade=0.05)
Expand source code
class Particles:

    def __init__(self, gameScreen: GameScreen, startColor: Optional[list] = None,
                 endColor: Optional[list] = None, shape: Optional[list] = None, rate=0.5,
                 count=100, endSize=0.01, sizeDecrease=0.95, colorFade=0.05):
        if not endColor:
            self.endColor = [1, 1, 1, 1]
        else:
            self.endColor = endColor

        if not startColor:
            self.startColor = [1, 1, 1, 1]
        else:
            self.startColor = startColor

        if not shape:
            self.shape = [[0, 0], [0.05, 0], [0.05, -0.05], [0, -0.05]]
        else:
            self.shape = shape

        self.gameScreen = gameScreen
        self.particles = []
        self.rate = rate
        self.count = count
        self.endSize = endSize
        self.sizeDecrease = sizeDecrease
        self.colorFade = colorFade
        self.time = 0

    def update(self) -> None:
        '''
        Updates the particles, color, size and  deletes old particles.
        '''
        toRemove = []
        for particle in self.particles:
            if abs(particle[1][0][0] - particle[1][1][0]) < self.endSize:
                toRemove.append(particle)

            shape = []
            for vert in particle[1]:
                shape.append([vert[0] * self.sizeDecrease, vert[1] * self.sizeDecrease])
            particle[1] = shape
            particle[2] = self.getColor(particle[-1])
            particle[-1] -= self.colorFade
            self.gameScreen.renderInfoList.append(particle)

        for particle in toRemove:
            self.particles.remove(particle)

        self.time += self.rate

    def emit(self, position: list) -> None:
        '''
        Appends are new particle to the particle list.
        '''
        
        if self.time > 1:
            if len(self.particles) < self.count:
                self.particles.append([-1, self.shape, self.getColor(0), position, 0, 0])
            self.time = 0

    def getColor(self, amount: float) -> list:
        '''
        Interpolates the start and end color values and returns the value.
        '''
        
        r = abs(self.startColor[0] - self.endColor[0]) * amount + self.startColor[0]
        g = abs(self.startColor[1] - self.endColor[1]) * amount + self.startColor[1]
        b = abs(self.startColor[2] - self.endColor[2]) * amount + self.startColor[2]
        a = abs(self.startColor[3] - self.endColor[3]) * amount + self.startColor[3]
        
        return [max(r, self.endColor[0]), max(g, self.endColor[1]), max(b, self.endColor[2]), max(a, self.endColor[3])]

Methods

def emit(self, position: list) ‑> NoneType

Appends are new particle to the particle list.

Expand source code
def emit(self, position: list) -> None:
    '''
    Appends are new particle to the particle list.
    '''
    
    if self.time > 1:
        if len(self.particles) < self.count:
            self.particles.append([-1, self.shape, self.getColor(0), position, 0, 0])
        self.time = 0
def getColor(self, amount: float) ‑> list

Interpolates the start and end color values and returns the value.

Expand source code
def getColor(self, amount: float) -> list:
    '''
    Interpolates the start and end color values and returns the value.
    '''
    
    r = abs(self.startColor[0] - self.endColor[0]) * amount + self.startColor[0]
    g = abs(self.startColor[1] - self.endColor[1]) * amount + self.startColor[1]
    b = abs(self.startColor[2] - self.endColor[2]) * amount + self.startColor[2]
    a = abs(self.startColor[3] - self.endColor[3]) * amount + self.startColor[3]
    
    return [max(r, self.endColor[0]), max(g, self.endColor[1]), max(b, self.endColor[2]), max(a, self.endColor[3])]
def update(self) ‑> NoneType

Updates the particles, color, size and deletes old particles.

Expand source code
def update(self) -> None:
    '''
    Updates the particles, color, size and  deletes old particles.
    '''
    toRemove = []
    for particle in self.particles:
        if abs(particle[1][0][0] - particle[1][1][0]) < self.endSize:
            toRemove.append(particle)

        shape = []
        for vert in particle[1]:
            shape.append([vert[0] * self.sizeDecrease, vert[1] * self.sizeDecrease])
        particle[1] = shape
        particle[2] = self.getColor(particle[-1])
        particle[-1] -= self.colorFade
        self.gameScreen.renderInfoList.append(particle)

    for particle in toRemove:
        self.particles.remove(particle)

    self.time += self.rate