about summary refs log tree commit diff stats
path: root/html/space/gameState.js
blob: 620bc1f2478355d7a14993d69c5fc8cdb3b93b14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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]
    };
}