Timed Effect Driver
The TimedEffectDriver class is responsible for managing timed effects in the game, such as temporary boosts, weakened grip, or invisibility. It ensures that effects are applied, updated, and removed at the appropriate times.
Effect Interface
export interface TimedEffect { canBeOverrided: boolean; // Indicates whether the effect can be overridden by a new effect of the same type startTimestamp: number; // The timestamp when the effect started duration: number; // Duration of the effect in milliseconds update(deltaTime: number): void; // Method to update the effect over time finish(): void; // Method called when the effect ends}Effect Types
The system supports multiple types of effects, including:
type EffectType = "boost" | "slip" | "damaged" | "nitro" | "freeze" | "invisible" | "no-collision";Class Implementation
export default class TimedEffectDriver { private _effects: Map<EffectType, TimedEffect> = new Map();Methods
update(deltaTime: number)
Updates all active effects, checking if they should expire and removing them when necessary.
update(deltaTime: number) { this._effects.forEach((effect, type) => { effect.update(deltaTime); if (GameTimeline.now() > effect.startTimestamp + effect.duration) { effect.finish(); this.cancelEffect(type); } });}addEffect(type: EffectType, effect: TimedEffect)
Adds a new effect. If an effect of the same type already exists and cannot be overridden, it is ignored.
addEffect(type: EffectType, effect: TimedEffect) { if (this._effects.has(type)) { if (!this._effects.get(type)!.canBeOverrided) { return; } this._effects.get(type)!.finish(); } this._effects.set(type, effect);}cancelEffect(type: EffectType)
Removes an effect from the active effects list.
cancelEffect(type: EffectType) { this._effects.delete(type);}finishEffect(type: EffectType)
Finishes an effect and removes it from the list.
finishEffect(type: EffectType) { const effect = this._effects.get(type); if (!effect) { console.warn(`Trying to finish effect which is not present on the player (${type})`); } effect?.finish(); this.cancelEffect(type);}hasEffect(type: EffectType): boolean
Checks if a specific effect is currently active.
hasEffect(type: EffectType): boolean { return this._effects.has(type);}get effects: number
Returns the number of active effects.
get effects() { return this._effects.size;}