advanced-games-library
Version:
Advanced Gaming Library for React Native - Four Complete Games with iOS Compatibility Fixes
225 lines (192 loc) • 5.7 kB
text/typescript
/**
* Demo Game Implementation
* A simple demo game to test compatibility and display a message
*/
import { BaseGame } from '../base/BaseGame';
import { GameConfig, GameResult, GameEventType, GameDifficulty, GameStatus, PlayerAction } from '../../core/types';
export interface DemoGameConfig extends GameConfig {
message?: string;
displayTime?: number; // seconds to display the message
}
export interface DemoGameState {
message: string;
displayTime: number;
startTime: number;
isComplete: boolean;
}
/**
* Demo Game Class
*/
export class DemoGame extends BaseGame {
private gameState: DemoGameState;
protected config!: DemoGameConfig;
constructor() {
super();
this.gameState = this.getInitialState();
// Set default config
this.config = {
gameId: 'demo-game',
difficulty: 'easy' as any,
customization: { theme: {} as any },
rules: {},
message: 'HELLO FROM GAMES FOLDER',
displayTime: 5
};
}
async initialize(config: DemoGameConfig): Promise<void> {
await super.initialize(config);
this.config = {
...this.config,
...config
};
this.gameState = {
message: this.config.message || 'HELLO FROM GAMES FOLDER',
displayTime: this.config.displayTime || 5,
startTime: 0,
isComplete: false
};
this.trackCustomEvent('demo_game_initialized', {
message: this.gameState.message,
displayTime: this.gameState.displayTime,
difficulty: this.config.difficulty
});
}
async start(): Promise<void> {
await super.start();
this.gameState.startTime = Date.now();
// Set a timer to complete the game after display time
setTimeout(() => {
if (this.state.status === GameStatus.PLAYING) {
this.completeGame();
}
}, this.gameState.displayTime * 1000);
this.trackCustomEvent('demo_game_started', {
message: this.gameState.message,
displayTime: this.gameState.displayTime
});
}
/**
* Get current game state for UI
*/
getGameState(): DemoGameState {
return { ...this.gameState };
}
/**
* Complete the game
*/
private completeGame(): void {
this.gameState.isComplete = true;
this.updateScore(100); // Give 100 points for completing
this.emit(GameEventType.GAME_COMPLETED, {
gameId: this.gameId,
message: this.gameState.message,
displayTime: this.gameState.displayTime
});
}
/**
* Get elapsed time since game started
*/
getElapsedTime(): number {
if (this.gameState.startTime === 0) {
return 0;
}
return (Date.now() - this.gameState.startTime) / 1000;
}
/**
* Get remaining time
*/
getRemainingTime(): number {
const elapsed = this.getElapsedTime();
return Math.max(0, this.gameState.displayTime - elapsed);
}
/**
* Reset the game
*/
reset(): void {
this.gameState = this.getInitialState();
this.state = this.createInitialState();
}
private getInitialState(): DemoGameState {
return {
message: 'HELLO FROM GAMES FOLDER',
displayTime: 5,
startTime: 0,
isComplete: false
};
}
// Abstract method implementations
public readonly gameId = 'demo-game';
public readonly name = 'Demo Game';
public readonly description = 'A simple demo game for testing compatibility';
public readonly category = 'demo';
public readonly version = '1.0.0';
public readonly minDifficulty = GameDifficulty.EASY;
public readonly maxDifficulty = GameDifficulty.EASY;
public readonly estimatedDuration = 1; // 1 minute
async initializeGameLogic(): Promise<void> {
// Demo game doesn't need complex initialization
}
startGameLogic(): void {
// Demo game starts immediately
}
pauseGameLogic(): void {
// Demo game can be paused
}
resumeGameLogic(): void {
// Demo game can be resumed
}
restartGameLogic(): void {
this.reset();
}
endGameLogic(): GameResult {
return {
gameId: this.gameId,
playerId: this.getCurrentPlayerId(),
score: this.state.currentScore,
maxScore: 100,
timeSpent: this.getElapsedTime(),
completed: this.gameState.isComplete,
difficulty: this.config.difficulty,
customData: {
message: this.gameState.message,
displayTime: this.gameState.displayTime
},
timestamp: new Date(),
sessionId: this.sessionId
};
}
processPlayerAction(action: PlayerAction): void {
// Demo game doesn't process complex player actions
// Just track that an action was received
this.trackCustomEvent('player_action_received', {
actionType: action.type,
timestamp: new Date()
});
}
validateGameConfig(config: DemoGameConfig): boolean {
return config.gameId === 'demo-game' &&
(config.message === undefined || typeof config.message === 'string') &&
(config.displayTime === undefined || config.displayTime > 0);
}
private getCurrentPlayerId(): string {
return 'demo-player';
}
}
/**
* Factory for creating DemoGame instances
*/
export class DemoGameFactory {
getGameInfo() {
return {
id: 'demo-game',
name: 'Demo Game',
description: 'A simple demo game for testing compatibility',
category: 'demo',
thumbnail: '/assets/demo-game-thumbnail.png',
version: '1.0.0'
};
}
createGame(): DemoGame {
return new DemoGame();
}
}