Add OrbitControls to GameLoop for improved camera navigation

This commit is contained in:
Matthew Mone 2025-12-19 15:08:54 -08:00
parent 5be96d2846
commit 8338adbaa4

View file

@ -1,4 +1,5 @@
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { VoxelGrid } from "../grid/VoxelGrid.js";
import { VoxelManager } from "../grid/VoxelManager.js";
// import { UnitManager } from "../managers/UnitManager.js";
@ -14,6 +15,7 @@ export class GameLoop {
this.scene = new THREE.Scene();
this.camera = null;
this.renderer = null;
this.controls = null;
this.grid = null;
this.voxelManager = null;
@ -39,6 +41,15 @@ export class GameLoop {
this.renderer.setClearColor(0x111111); // Dark background
container.appendChild(this.renderer.domElement);
// Setup OrbitControls
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.controls.enableDamping = true; // Smooth camera movement
this.controls.dampingFactor = 0.05;
this.controls.screenSpacePanning = false;
this.controls.minDistance = 5;
this.controls.maxDistance = 100;
this.controls.maxPolarAngle = Math.PI / 2; // Prevent going below ground
// Lighting
const ambient = new THREE.AmbientLight(0xffffff, 0.6);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
@ -78,7 +89,11 @@ export class GameLoop {
// Apply textures generated by the biome logic
this.voxelManager.updateMaterials(generator.generatedAssets);
this.voxelManager.update();
this.voxelManager.focusCamera(this.controls);
// Center camera using the focus target
if (this.controls) {
this.voxelManager.focusCamera(this.controls);
}
// 4. Initialize Units
// this.unitManager = new UnitManager();
@ -100,6 +115,10 @@ export class GameLoop {
// Update Logic
// TWEEN.update();
if (this.controls) {
this.controls.update();
}
// Render
this.renderer.render(this.scene, this.camera);
}
@ -107,5 +126,6 @@ export class GameLoop {
stop() {
this.isRunning = false;
// Cleanup Three.js resources if needed
if (this.controls) this.controls.dispose();
}
}