File size: 2,984 Bytes
7b453e7 7ffaa9e 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7ffaa9e 7b453e7 7ffaa9e 7b453e7 7ffaa9e 7b453e7 7ffaa9e 7b453e7 7ffaa9e 7b453e7 7e312e3 7ffaa9e 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7ffaa9e 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 |
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 |
import { createTerminal } from './terminal.js';
import { createInputHandler } from './input.js';
import { Scene } from './scene.js';
import { LayerType } from './layer.js'; // Only need LayerType
import { mergeConfig, getConfig } from './config.js';
import { state } from './state.js';
import { Time } from './time.js';
import { Physics } from './physics.js';
function start(userConfig) {
if (state.engineStarted) {
console.error("Engine.start() called more than once!");
return;
}
// Initialize config first
mergeConfig(userConfig);
state.engineStarted = true;
// Create and setup canvas
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
state.ctx = canvas.getContext('2d');
// Create scene first
state.scene = new Scene();
state.scene.start();
// Define and call resizeCanvas after scene creation
const resizeCanvas = () => {
state.canvasWidth = canvas.width = window.innerWidth;
state.canvasHeight = canvas.height = window.innerHeight;
// Now camera is available
state.scene.camera.setViewport(state.canvasWidth, state.canvasHeight);
};
resizeCanvas();
// Create input handler with canvas and scene
state.inputHandler = createInputHandler(canvas, state.scene);
// Create terminal last
const terminalLayer = state.scene.getLayer(LayerType.TERMINAL);
state.terminal = createTerminal(canvas, state.ctx, state.inputHandler);
terminalLayer.addEntity(state.terminal);
const config = getConfig(); // Get config at the start function level
// Add resize listener
window.addEventListener('resize', resizeCanvas);
}
export function run(userConfig) {
state.currentConfig = userConfig;
if (!state.engineStarted) {
start(userConfig);
}
const config = getConfig();
state.angularSpeed = config.initialAngularSpeed;
state.physics = new Physics(config);
// Game functions
function Update(deltaTime) {
// Clear the canvas
state.ctx.clearRect(0, 0, state.canvasWidth, state.canvasHeight);
// Update camera
state.scene.camera.update(deltaTime);
// Update scene (this will update entities)
state.scene.update(deltaTime);
// FPS calculation
state.fps = Math.round(1 / deltaTime);
// Draw scene (now includes debug layer)
state.scene.draw(state.ctx);
// Draw terminal text
state.terminal.drawText();
}
function FixedUpdate(deltaTime) {
state.physics.update(deltaTime);
}
// Game loop
function gameLoop() {
state.animationFrameId = requestAnimationFrame(gameLoop);
Time.update();
while (Time.accumulatedTime >= Time.fixedDeltaTime) {
FixedUpdate(Time.fixedDeltaTime);
Time.resetAccumulator();
}
Update(Time.deltaTime);
}
// Start the game loop
gameLoop();
}
|