Skip to content

Particle Effects

Shristi Gupta edited this page Sep 30, 2023 · 9 revisions

In order to make the game more realistic, the weather events are visible on the player's screen when they are occurring. This has been done by using Particle Effects. These effects remove the need for the current WeatherEventDisplay and make the UI significantly cleaner.

ParticleService

The ParticleService class renders the particle effects within the game which are triggered when a weather event begins. It acts as a comprehensive manager for all the particle effects in the game. This class includes the implementation of the enum ParticleEffectType and provides methods that allow for starting, rendering, and stopping effects based on their type or category, offering flexibility in how these visual effects are controlled during gameplay. Thus, this class ensures that particle effects are efficiently managed, with pooling mechanisms in place to optimize performance.

ParticleEffectType

The ParticleEffectType is an enum within the ParticleService class which defines the different types of particle effects in the game. Each type in this enum represents a particle effect that is associated with a category, its source path, and a minimum and maximum capacity of the number of particle effects. The type and the category have been specified in the ParticleEffectWrapper. The minimum and maximum capacities define the number of instances of the particle effect that the ParticleEffectPool should maintain. The minCapacity ensures that there's always a certain number of instances ready for use, while the maxCapacity ensures that the pool doesn't grow beyond a certain size, which could waste memory. Moreover, the enum provides a getter method (getCategory()) to access the category of the particle effect. This allows other parts of the game engine to retrieve this detail when needed.

Member Variables

  • queuedEffects: An ArrayList containing all ParticleEffectWrappers. These act as a container for the actual particle effects that are rendered.

  • particleEffects: A HashMap mapping each ParticleEffectType to its associated ParticleEffect. This particle effect is needed in the creation of the ParticleEffectPool. In order to ensure that the pool functions correctly, a reference to the particle effect needs to be maintained.

  • particleEffectPool: A HashMap containing ParticleEffectPools for each particle effect type. These are used to organise and reuse particle effects in an effective manner.

Methods

  • Constructor

The constructor initialises the data structures outlined before and loads all the ParticleEffects. It uses AssetManager which provides an efficient way to load particle effects. All teams are encouraged to use the same. Moreover, it appends elements to the lists maintaining particle effects and particle effect pools.

  • render

The render method is responsible for displaying all active particle effects on the screen. This is accomplished by first determining the player's current vector position. Then, the method goes through each ParticleEffectWrapper in the queuedEffects list and positions the PooledEffect accordingly. When a particle effect is complete, the render method ensures that it is reset and available for potential future use.

  • startEffect

The startEffect method is used to start a particle effect. It takes a ParticleEffectType as its parameter and retrieves an instance from its associated pool and wraps it in a ParticleEffectWrapper. Afterwards, the effect is added to the queuedEffects list, indicating that it is ready for rendering. The effect is then scaled and started.

  • stopEffect

The stopEffect method is called when a particle effect is complete and needs to be stopped. It takes a ParticleEffectType as its parameter. This method uses a predicate to get the corresponding particle effect wrapper and then filters the queuedEffects list based on this. The effects associated with them are released are then removed from the queuedEffects list.

  • stopEffectCategory

The stopEffectCategory method allows us to stop all particle effects pertaining to a certain category. Its behaviour is similar to that of the stopEffect method, with one important difference: the filtering process is based on the effect's category rather than its type. The method releases particle effects and removes them from the queuedEffects list.

ParticleEffectWrapper

The ParticleEffectWrapper class acts as a container or a wrapper around the actual particle effect, which in this context is represented by the PooledEffect class from the LibGDX framework. Similar to the RenderService class, this class is responsible for managing the current active particle effects, and rendering them in an efficient way in the game's render loop. It also specifies additional data about the particle effect such as category and type. The category represents the broad classification of the particle effect. For instance, The category for WeatherEvent is represented by a String “WEATHER_EVENT” and the type is its implementation i.e. “ACID_RAIN”. This implementation will be useful when more particle effects are added to the game. Moreover, the class also provides getter methods to access the PooledEffect, category, and type. This allows other parts of the game engine to retrieve and utilize these details as needed.

Clone this wiki locally