Effect Management
This module is responsible for managing different types of obstacles and perks in the game, including their random generation, placement, and sprite assignment. It provides methods to randomly generate obstacles and perks with safe placements and also handles the retrieval of corresponding EffectObject classes for each effect.
Enumerations
Obstacles
Defines all the possible obstacle types that can appear on the track:
- POTHOLE: Represents a pothole obstacle.
- PUDDLE: Represents a puddle obstacle.
- BANANA_PEEL: Represents a banana peel obstacle.
- SPIKES: Represents a spikes obstacle.
- OIL_SPILL: Represents an oil spill obstacle.
- ICE: Represents an ice obstacle.
- GRAVEL: Represents a gravel obstacle.
Perks
Defines all the possible perk types that can appear during gameplay:
- BOOST_STAR: Represents a boost star perk.
- WRENCH: Represents a wrench perk.
- ICE_CUBE: Represents an ice cube perk.
- INVISIBLE: Represents an invisible perk.
- NO_COLLISION: Represents a no collision perk.
EffectSprites
A type that can be either an obstacle or a perk, used for general effect handling.
Functions
getEffectObjectByName(name: EffectSprites): typeof EffectObject
This function retrieves the corresponding EffectObject class based on the effect name (either obstacle or perk). The effect name can either be an obstacle or a perk type.
Parameters:
name: A value of typeEffectSprites(either fromObstaclesorPerks).
Returns:
- The corresponding
EffectObjectclass for the effect.
getRandomObstacleSprite(): Obstacles
Returns a randomly selected obstacle sprite. Ice and gravel obstacles are excluded from this random selection.
Returns:
- A random value from
Obstacles, excludingICEandGRAVEL.
getRandomPerkSprite(): Perks
Returns a randomly selected perk sprite.
Returns:
- A random value from
Perks.
getRandomPosition(currentObstacles: EffectObject[]): Vec2D
Generates a random position for a new effect object (either obstacle or perk) that doesn’t collide with any existing obstacles. The position is selected from sampled points along the track path.
Parameters:
currentObstacles: An array of currentEffectObjects placed on the track to ensure no collisions with existing obstacles.
Returns:
- A
Vec2Dobject representing a valid random position on the track.
getRandomObstacles(n: number, currentEffectObjects: EffectObject[]): EffectObject[]
Generates n random obstacle effect objects and places them on the track at non-colliding positions.
Parameters:
n: The number of obstacles to generate.currentEffectObjects: The list of existingEffectObjects to avoid collisions.
Returns:
- An array of newly created
EffectObjects representing the obstacles.
getRandomPerks(n: number, currentEffectObjects: EffectObject[]): EffectObject[]
Generates n random perk effect objects and places them on the track at non-colliding positions.
Parameters:
n: The number of perks to generate.currentEffectObjects: The list of existingEffectObjects to avoid collisions.
Returns:
- An array of newly created
EffectObjects representing the perks.
Example Usage
import { getRandomObstacles, getRandomPerks } from "./effect-manager";import EffectObject from "../services/effect/effect-object";
const currentObstacles: EffectObject[] = []; // Assume this is the current list of obstacles on the track.
const newObstacles = getRandomObstacles(5, currentObstacles);const newPerks = getRandomPerks(3, currentObstacles);
console.log(newObstacles); // Logs an array of newly created obstacle objects.console.log(newPerks); // Logs an array of newly created perk objects.Error Handling
- If an invalid effect name is passed to the
getEffectObjectByName()function, it will throw an error:"effect not found". - If the track is not loaded when generating random positions, an error will be thrown:
"cannot generate objects when track is not loaded".