diff options
Diffstat (limited to 'html/space/gameState.js')
-rw-r--r-- | html/space/gameState.js | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/html/space/gameState.js b/html/space/gameState.js index 85f56b0..620bc1f 100644 --- a/html/space/gameState.js +++ b/html/space/gameState.js @@ -6,11 +6,14 @@ import { getPlayerState } from './physics.js'; 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() { @@ -27,6 +30,11 @@ export function initGameState() { 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(); @@ -56,8 +64,28 @@ function createEnemyShip() { // 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 @@ -85,28 +113,10 @@ export function updateGameState(deltaTime) { if (inputState.fireSecondary) { createProjectile('secondary'); } - - // 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 projectiles that are too far away - const distance = Math.sqrt( - Math.pow(projectile.position.x - player.position.x, 2) + - Math.pow(projectile.position.y - player.position.y, 2) + - Math.pow(projectile.position.z - player.position.z, 2) - ); - - if (distance > SPACE_SIZE/2) { - projectiles.splice(index, 1); - } - }); } // Create a new projectile -function createProjectile(type) { +export function createProjectile(type) { const player = getPlayerState(); const speed = type === 'primary' ? 10 : 7.5; // Reduced from 20/15 const damage = type === 'primary' ? 25 : 10; @@ -124,7 +134,28 @@ function createProjectile(type) { y: sinX * speed, z: cosY * cosX * speed }, - damage + 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 }); } |