about summary refs log tree commit diff stats
path: root/html/rogue/js/rogue.js
blob: 64716a47605de217f5ffdc01c1d9ef6cb6fbfc8d (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
window.gameState = null;

const initGame = () => {
    const canvas = document.getElementById('gameCanvas');
    const ctx = canvas.getContext('2d');
    
    // Target frame rate
    const FPS = 60;
    const FRAME_TIME = 1000 / FPS;
    let lastFrameTime = 0;

    // Set canvas to full viewport size
    const resizeCanvas = () => {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        // Clear any cached gradients when resizing
        if (window.gameState) {
            window.gameState.cachedGradient = null;
        }
    };

    window.addEventListener('resize', resizeCanvas);
    resizeCanvas();

    // Calculate initial player position at ground level
    const groundY = canvas.height - CONFIG.world.groundHeight;
    const initialPlayerY = groundY - CONFIG.player.height;

    // Game state
    let gameState = {
        time: 0,
        player: createPlayer(100, initialPlayerY),
        camera: createCamera(0, 0),
        world: createWorld(),
        debug: {
            enabled: false,
            mouseX: 0,
            mouseY: 0
        }
    };
    
    // Make gameState globally accessible
    window.gameState = gameState;

    // Set up input handlers
    setupInputHandlers(canvas, gameState);

    // Game loop
    const gameLoop = (timestamp) => {
        // Check if enough time has passed since last frame
        if (timestamp - lastFrameTime < FRAME_TIME) {
            requestAnimationFrame(gameLoop);
            return;
        }

        // Calculate delta time (capped at 1 second to prevent huge jumps)
        const deltaTime = Math.min(timestamp - lastFrameTime, 1000);
        lastFrameTime = timestamp;
        gameState.time = timestamp;

        // Clear the entire canvas
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        // Update
        gameState = updateGame(gameState, deltaTime);

        // Render
        render(ctx, gameState);

        // Schedule next frame
        requestAnimationFrame(gameLoop);
    };

    // Start the game loop
    lastFrameTime = performance.now();
    requestAnimationFrame(gameLoop);
};

const updateGame = (state, deltaTime) => {
    const updatedPlayer = updatePlayer(state.player, deltaTime);
    const updatedCamera = updateCamera(state.camera, updatedPlayer);

    return {
        ...state,
        player: updatedPlayer,
        camera: updatedCamera
    };
};

const render = (ctx, state) => {
    const groundY = ctx.canvas.height - state.world.groundHeight;

    // Cache sky gradient
    if (!state.cachedGradient) {
        state.cachedGradient = createSkyGradient(ctx, groundY);
    }

    const viewBounds = getViewBounds(state.camera);

    // Apply camera transform
    ctx.save();
    ctx.translate(-state.camera.x, -state.camera.y);

    // Clear the transformed canvas area
    ctx.clearRect(
        viewBounds.left,
        0,
        viewBounds.right - viewBounds.left,
        ctx.canvas.height
    );

    // Render layers
    renderBackground(ctx, state, groundY, viewBounds);
    renderBackgroundObjects(ctx, state, groundY, viewBounds);
    
    // Render platforms
    state.world.platforms.forEach(platform => {
        ctx.fillStyle = platform.color;
        ctx.fillRect(platform.x, platform.y, platform.width, platform.height);
    });

    renderPlayer(ctx, state.player);
    renderForegroundObjects(ctx, state, groundY, viewBounds);

    // Render ground line
    ctx.fillStyle = RENDER_CONSTANTS.GROUND_COLOR;
    ctx.fillRect(
        state.camera.x - 1000,
        groundY,
        ctx.canvas.width + 2000,
        1
    );

    // Handle debug rendering
    if (state.debug.enabled) {
        ctx.restore();
        renderDebugInfo(ctx, state);
    } else {
        ctx.restore();
    }
};

// Initialize the game when the window loads
window.addEventListener('load', initGame);