Array Utilities
Overview
Section titled “Overview”Array utilities provide essential functions for manipulating and working with arrays in your game. These utilities are designed to be type-safe and efficient, particularly useful for game state management, inventory systems, and randomization.
Array Operations
Section titled “Array Operations”Shuffling Arrays
Section titled “Shuffling Arrays”The shuffle
function randomly reorders elements in an array:
import { shuffle } from 'dill-pixel;
// Shuffle an array in placeconst cards = ['A', '2', '3', '4', '5'];shuffle(cards);// cards is now randomly reordered, e.g.: ['4', '2', 'A', '5', '3']
Random Element Selection
Section titled “Random Element Selection”Get a random element from an array:
import { getRandomElement } from 'dill-pixel;
const items = ['sword', 'shield', 'potion', 'gem'];const randomItem = getRandomElement(items);
Common Use Cases
Section titled “Common Use Cases”Card Games
Section titled “Card Games”import { shuffle } from 'dill-pixel;
class Deck { private cards: string[];
constructor() { // Initialize deck this.cards = ['A♠', 'K♠', 'Q♠' /* ... */];
// Shuffle the deck shuffle(this.cards); }
draw() { return this.cards.pop(); }
reset() { // Reshuffle all cards shuffle(this.cards); }}
Random Loot Generation
Section titled “Random Loot Generation”import { getRandomElement } from 'dill-pixel;
interface LootItem { id: string; rarity: 'common' | 'rare' | 'epic'; value: number;}
class LootSystem { private lootTable: LootItem[];
generateLoot(count: number): LootItem[] { const loot: LootItem[] = [];
for (let i = 0; i < count; i++) { loot.push(getRandomElement(this.lootTable)); }
return loot; }}
Level Generation
Section titled “Level Generation”import { shuffle } from 'dill-pixel;
class LevelGenerator { private roomTemplates: string[];
generateLevel(roomCount: number) { // Create copy of templates const templates = [...this.roomTemplates];
// Shuffle templates shuffle(templates);
// Use first n templates return templates.slice(0, roomCount); }}
Type Safety
Section titled “Type Safety”The array utilities are fully typed and work with any array type:
import { shuffle, getRandomElement } from 'dill-pixel;
// Works with any typeinterface Player { id: string; name: string; score: number;}
const players: Player[] = [ { id: '1', name: 'Alice', score: 100 }, { id: '2', name: 'Bob', score: 200 },];
// Type-safe shufflingshuffle(players);
// Type-safe random selectionconst randomPlayer: Player = getRandomElement(players);
Best Practices
Section titled “Best Practices”-
Use
shuffle
when you need to randomize the entire array -
Use
getRandomElement
for single random selection -
Create a copy before shuffling if you need to preserve the original order:
const original = [1, 2, 3, 4, 5];const shuffled = [...original];shuffle(shuffled); -
Consider caching random selections for performance:
class EnemySpawner {private enemyTypes = ['goblin', 'orc', 'troll'];private spawnQueue: string[] = [];getNextEnemy(): string {if (this.spawnQueue.length === 0) {// Refill queue with shuffled enemiesthis.spawnQueue = [...this.enemyTypes];shuffle(this.spawnQueue);}return this.spawnQueue.pop()!;}}
Performance Considerations
Section titled “Performance Considerations”Efficient Usage
Section titled “Efficient Usage”// Good: Shuffle in placeshuffle(array);
// Less Efficient: Creating new arrayconst shuffled = [...array].sort(() => Math.random() - 0.5);
// Good: Direct random accessconst item = getRandomElement(array);
// Less Efficient: Manual random indexconst item = array[Math.floor(Math.random() * array.length)];
Tips and Tricks
Section titled “Tips and Tricks”-
Use
shuffle
for initial randomization of game elements -
Combine with other utilities for complex behaviors:
import { shuffle, Color } from 'dill-pixel;// Create random color paletteconst colors = [Color.RED, Color.BLUE, Color.GREEN];shuffle(colors); -
Consider using
getRandomElement
for variety in game events:const events = ['spawn', 'powerup', 'obstacle'];const nextEvent = getRandomElement(events); -
Use type annotations to ensure type safety:
const <T>(arr: T[]): T => getRandomElement(arr);