import { GameEngine } from '@core/engine';
import { Player, Enemy, Projectile } from '@entities';
import { PhysicsSystem, RenderSystem } from '@systems';
import { Vector2, Quaternion } from '@math';
interface GameState {
players: Map<string, Player>;
enemies: Enemy[];
projectiles: Projectile[];
score: number;
level: number;
}
class GameLoop {
private engine: GameEngine;
private state: GameState;
private lastTime: number = 0;
constructor() {
this.engine = new GameEngine();
this.state = this.initializeState();
}
private initializeState(): GameState {
return {
players: new Map(),
enemies: [],
projectiles: [],
score: 0,
level: 1,
};
}
public update(deltaTime: number): void {
this.handleInput();
this.updatePhysics(deltaTime);
this.checkCollisions();
this.updateEntities(deltaTime);
this.render();
}
private handleInput(): void {
const input = this.engine.getInput();
for (const [id, player] of this.state.players) {
player.velocity = input.getMovement(id);
if (input.isActionPressed(id, 'fire')) {
this.spawnProjectile(player);
}
}
}
private spawnProjectile(player: Player): void {
const projectile = new Projectile({
position: player.position.clone(),
velocity: player.forward.scale(500),
owner: player.id,
});
this.state.projectiles.push(projectile);
}
private updatePhysics(dt: number): void {
PhysicsSystem.step(this.state, dt);
}
}
export default GameLoop;
import { GameEngine } from '@core/engine';
import { Player, Enemy, Projectile } from '@entities';
import { PhysicsSystem, RenderSystem } from '@systems';
import { Vector2, Quaternion } from '@math';
interface GameState {
players: Map<string, Player>;
enemies: Enemy[];
projectiles: Projectile[];
score: number;
level: number;
}
class GameLoop {
private engine: GameEngine;
private state: GameState;
private lastTime: number = 0;
constructor() {
this.engine = new GameEngine();
this.state = this.initializeState();
}
private initializeState(): GameState {
return {
players: new Map(),
enemies: [],
projectiles: [],
score: 0,
level: 1,
};
}
public update(deltaTime: number): void {
this.handleInput();
this.updatePhysics(deltaTime);
this.checkCollisions();
this.updateEntities(deltaTime);
this.render();
}
private handleInput(): void {
const input = this.engine.getInput();
for (const [id, player] of this.state.players) {
player.velocity = input.getMovement(id);
if (input.isActionPressed(id, 'fire')) {
this.spawnProjectile(player);
}
}
}
private spawnProjectile(player: Player): void {
const projectile = new Projectile({
position: player.position.clone(),
velocity: player.forward.scale(500),
owner: player.id,
});
this.state.projectiles.push(projectile);
}
private updatePhysics(dt: number): void {
PhysicsSystem.step(this.state, dt);
}
}
export default GameLoop;