Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/client/core/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SettingsManager } from './SettingsManager.ts';
import { Player } from '../../shared/Player.ts';
import { ShotHandler } from './ShotHandler.ts';
import * as THREE from 'three';
import { MappingMode } from './MappingMode.ts';

export class Game {
private localPlayer: Player;
Expand All @@ -24,6 +25,7 @@ export class Game {
private shotHandler: ShotHandler;
private inventoryManager: Inventory;
private mapLoader: MapLoader;
private mappingMode: MappingMode;
private healthIndicator: HealthIndicator;
private remoteItemRenderer: RemoteItemRenderer;
private gameIndex: number;
Expand Down Expand Up @@ -65,11 +67,13 @@ export class Game {
this.chatOverlay.setNetworking(this.networking);
this.chatOverlay.setInputHandler(this.inputHandler);
this.mapLoader = new MapLoader(this.renderer);
this.mappingMode = new MappingMode(this.renderer, this.inputHandler, this.localPlayer, this.mapLoader);
this.healthIndicator = new HealthIndicator(this.renderer, this.localPlayer, this.networking);
this.remoteItemRenderer = new RemoteItemRenderer(this.networking, this.renderer, this.shotHandler);
}

init() {
this.mappingMode.initFromLocation();
this.inventoryManager.init();
this.healthIndicator.init();
}
Expand All @@ -95,7 +99,11 @@ export class Game {
mark('inputs');
this.touchInputHandler.onFrame();
mark('touch');
this.collisionManager.collisionPeriodic(this.localPlayer);
this.mappingMode.onFrame(deltaTime);
mark('mapping');
if (!this.mappingMode.isNoClipEnabled()) {
this.collisionManager.collisionPeriodic(this.localPlayer);
}
mark('collision');
this.networking.updatePlayerData();
mark('netUpdate');
Expand Down Expand Up @@ -145,6 +153,7 @@ export class Game {
this.chatOverlay.destroy();
this.inputHandler.destroy();
this.touchInputHandler.destroy();
this.mappingMode.destroy();
this.renderer.destroy();
this.inventoryManager.destroy();
this.mapLoader.destroy();
Expand Down
73 changes: 69 additions & 4 deletions src/client/core/MapLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ import { AssetManager } from './AssetManager.ts';

THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;

export interface ClientMapJson {
name: string;
respawnPoints?: unknown[];
itemRespawnPoints?: unknown[];
capturePoints?: unknown[];
props?: unknown[];
staticPropExclusions?: string[];
}

export class MapLoader {
private scene: THREE.Scene;
private mapObject: THREE.Group | undefined;
private mapUrl: string = '';
private mapJson: ClientMapJson | null = null;
private mapJsonUrl: string = '';

constructor(renderer: Renderer) {
this.scene = renderer.getScene();
Expand All @@ -18,19 +29,73 @@ export class MapLoader {
public load(mapUrl: string) {
if (mapUrl === this.mapUrl) return;
this.mapUrl = mapUrl;
const mapJsonPromise = this.loadMapJson(mapUrl);
AssetManager.getInstance().loadAsset(mapUrl, (scene) => {
if (this.mapObject) this.scene.remove(this.mapObject);
this.mapObject = scene;
CollisionManager.staticGeometry(scene);
this.scene.add(this.mapObject);
mapJsonPromise.then((mapJson) => {
this.mapJson = mapJson;
this.applyStaticPropExclusions(scene, mapJson?.staticPropExclusions ?? []);
}).finally(() => {
if (this.mapObject) this.scene.remove(this.mapObject);
this.mapObject = scene;
CollisionManager.staticGeometry(scene);
this.scene.add(this.mapObject);
});
});
}

public getMapObject(): THREE.Group | undefined {
return this.mapObject;
}

public getMapJson(): ClientMapJson | null {
return this.mapJson;
}

public getMapJsonUrl(): string {
return this.mapJsonUrl;
}

public findStaticNodeByName(name: string): THREE.Object3D | null {
if (!this.mapObject) return null;
let result: THREE.Object3D | null = null;
this.mapObject.traverse((node) => {
if (!result && node.name === name) result = node;
});
return result;
}

private async loadMapJson(mapUrl: string): Promise<ClientMapJson | null> {
this.mapJsonUrl = mapUrl.replace(/\/map\.glb(?:\?.*)?$/, '/map.json');
if (!this.mapJsonUrl || this.mapJsonUrl === mapUrl) return null;
try {
const response = await fetch(this.mapJsonUrl);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return await response.json();
} catch (error) {
console.warn(`[MapLoader] Failed to load map metadata ${this.mapJsonUrl}:`, error);
return null;
}
}

private applyStaticPropExclusions(scene: THREE.Group, exclusions: string[]): void {
if (exclusions.length === 0) return;
const excludedNames = new Set(exclusions);
scene.traverse((node) => {
if (node.name && excludedNames.has(node.name)) {
node.visible = false;
node.userData.staticPropExcluded = true;
}
});
}

public destroy() {
if (this.mapObject) {
this.scene.remove(this.mapObject);
this.mapObject = undefined;
}
this.mapUrl = '';
this.mapJson = null;
this.mapJsonUrl = '';
// Remove the bounds tree if it exists
if (this.scene.userData.boundsTree) {
this.scene.userData.boundsTree.dispose();
Expand Down
Loading
Loading