diff options
Diffstat (limited to 'html/space/gameState.js')
-rw-r--r-- | html/space/gameState.js | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/html/space/gameState.js b/html/space/gameState.js new file mode 100644 index 0000000..620bc1f --- /dev/null +++ b/html/space/gameState.js @@ -0,0 +1,169 @@ +// Game state module for managing game objects and logic +import { inputState } from './input.js'; +import { getPlayerState } from './physics.js'; + +// Game objects +const planets = []; +const enemyShips = []; +const projectiles = []; +let lastEnemySpawn = 0; + +// Space dimensions +const SPACE_SIZE = 10000; // Increased from implicit 2000 +const PLANET_DISTANCE = 5000; // Increased from 1000 +const ENEMY_SPAWN_DISTANCE = 3000; // Increased from 500 +const ENEMY_SPAWN_INTERVAL = 5000; // 5 seconds +const MAX_ENEMIES = 5; + +// Initialize game state +export function initGameState() { + // Create planets + planets.push({ + position: { x: PLANET_DISTANCE, y: 0, z: 0 }, + radius: 500, // Increased from 200 + color: '#3498db' + }); + + planets.push({ + position: { x: -PLANET_DISTANCE, y: PLANET_DISTANCE/2, z: -PLANET_DISTANCE }, + radius: 400, // Increased from 150 + color: '#e74c3c' + }); + + // Reset other state + enemyShips.length = 0; + projectiles.length = 0; + lastEnemySpawn = Date.now(); + + // Create initial enemy ships + for (let i = 0; i < 5; i++) { + createEnemyShip(); + } +} + +// Create a new enemy ship +function createEnemyShip() { + const distance = ENEMY_SPAWN_DISTANCE + Math.random() * ENEMY_SPAWN_DISTANCE; + const angle = Math.random() * Math.PI * 2; + const height = (Math.random() - 0.5) * ENEMY_SPAWN_DISTANCE; + + enemyShips.push({ + position: { + x: Math.cos(angle) * distance, + y: height, + z: Math.sin(angle) * distance + }, + velocity: { + x: (Math.random() - 0.5) * 0.5, // Reduced from 2 + y: (Math.random() - 0.5) * 0.5, // Reduced from 2 + z: (Math.random() - 0.5) * 0.5 // Reduced from 2 + }, + health: 100 + }); +} + +// Update game state +export function updateGameState(deltaTime) { + const currentTime = Date.now(); + const player = getPlayerState(); + + // Spawn enemies + if (currentTime - lastEnemySpawn > ENEMY_SPAWN_INTERVAL && + enemyShips.length < MAX_ENEMIES) { + spawnEnemy(); + lastEnemySpawn = currentTime; + } + + // Update projectiles + projectiles.forEach((projectile, index) => { + projectile.position.x += projectile.velocity.x * deltaTime; + projectile.position.y += projectile.velocity.y * deltaTime; + projectile.position.z += projectile.velocity.z * deltaTime; + + // Remove if too old + if (currentTime - projectile.createdAt > 5000) { + projectiles.splice(index, 1); + } + }); + + // Update enemy ships + enemyShips.forEach((ship, index) => { + // Move ships + ship.position.x += ship.velocity.x * deltaTime; + ship.position.y += ship.velocity.y * deltaTime; + ship.position.z += ship.velocity.z * deltaTime; + + // Check if ship is too far away + const distance = Math.sqrt( + Math.pow(ship.position.x - player.position.x, 2) + + Math.pow(ship.position.y - player.position.y, 2) + + Math.pow(ship.position.z - player.position.z, 2) + ); + + if (distance > SPACE_SIZE) { + enemyShips.splice(index, 1); + createEnemyShip(); + } + }); + + // Handle firing + if (inputState.firePrimary) { + createProjectile('primary'); + } + if (inputState.fireSecondary) { + createProjectile('secondary'); + } +} + +// Create a new projectile +export function createProjectile(type) { + const player = getPlayerState(); + const speed = type === 'primary' ? 10 : 7.5; // Reduced from 20/15 + const damage = type === 'primary' ? 25 : 10; + + const cosY = Math.cos(player.rotation.y); + const sinY = Math.sin(player.rotation.y); + const cosX = Math.cos(player.rotation.x); + const sinX = Math.sin(player.rotation.x); + + projectiles.push({ + type, + position: { ...player.position }, + velocity: { + x: sinY * cosX * speed, + y: sinX * speed, + z: cosY * cosX * speed + }, + damage, + createdAt: Date.now() + }); +} + +// Spawn a new enemy ship +function spawnEnemy() { + const angle = Math.random() * Math.PI * 2; + const distance = ENEMY_SPAWN_DISTANCE; + + enemyShips.push({ + position: { + x: Math.cos(angle) * distance, + y: 0, + z: Math.sin(angle) * distance + }, + velocity: { + x: (Math.random() - 0.5) * 0.5, + y: (Math.random() - 0.5) * 0.5, + z: (Math.random() - 0.5) * 0.5 + }, + health: 100 + }); +} + +// Get game state for rendering +export function getGameState() { + return { + planets: [...planets], + enemyShips: [...enemyShips], + projectiles: [...projectiles] + }; +} \ No newline at end of file |