Skip to content

Commit

Permalink
Zone now detects when MainCamera is within it's bounds
Browse files Browse the repository at this point in the history
-detections for when a camera exists or enters a zone.
  • Loading branch information
Aitolda committed Oct 10, 2024
1 parent e45004a commit 04a35fa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/modules/entity/components/controllers/ZoneEntityController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ export class ZoneEntityController extends EntityController {
private _zoneMesh: Mesh | null = null;

constructor(entity: IZoneEntity) {
super(entity, ZoneEntityController.typeName);
// Extract the numeric part from the zone's ID
const zoneNumber = entity.id.split('_')[1];
// Create a unique ID for this controller
const controllerId = `ZoneEntityController_${zoneNumber}`;

super(entity, controllerId);
this._zoneEntity = entity;
}

Expand All @@ -89,11 +94,11 @@ export class ZoneEntityController extends EntityController {
*/
// eslint-disable-next-line class-methods-use-this
public get componentType(): string {
return ZoneEntityController.typeName;
return this.id; // This will now return the unique "ZoneEntityController_XXX" ID
}

static get typeName(): string {
return "ZoneEntityController";
return "ZoneEntityController"; // This remains the same for the class type
}

public onInitialize(): void {
Expand Down Expand Up @@ -387,13 +392,16 @@ export class ZoneEntityController extends EntityController {

this._zoneMesh.isPickable = true;

console.log(`Zone mesh setup complete for zone ${this._zoneEntity.id}.`);
// Update the metadata to use the controller's ID
this._zoneMesh.metadata = { zoneController: this };

console.log(`Zone mesh setup complete for zone ${this.id}.`);
console.log(`Zone mesh name: ${this._zoneMesh.name}`);
console.log(`Zone mesh parent: ${this._zoneMesh.parent?.name}`);
console.log(`Zone position: ${this._gameObject.position.toString()}`);
console.log(`Zone mesh local scaling: ${this._zoneMesh.scaling.toString()}`);
} else {
console.warn(`Zone mesh or zone entity is undefined in _setupZoneMesh for zone ${this._zoneEntity?.id}`);
console.warn(`Zone mesh or zone entity is undefined in _setupZoneMesh for zone ${this.id}`);
}
}

Expand All @@ -402,4 +410,8 @@ export class ZoneEntityController extends EntityController {
// This method may still be needed for other zone-related updates,
// but remove any skybox-specific code
}

public get zoneMesh(): Mesh | null {
return this._zoneMesh;
}
}
45 changes: 45 additions & 0 deletions src/modules/scene/ZoneManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Scene, Vector3 } from "@babylonjs/core";
import { ZoneEntityController } from "../entity/components/controllers/ZoneEntityController";
import Log from "@Modules/debugging/log";

export class ZoneManager {
private _scene: Scene;
private _currentZone: ZoneEntityController | null = null;

constructor(scene: Scene) {
this._scene = scene;
}

public update(): void {
const camera = this._scene.activeCamera;
if (!camera) return;

const cameraPosition = camera.position;
let newZone: ZoneEntityController | null = null;

this._scene.meshes.forEach(mesh => {
const zoneController = mesh.metadata?.zoneController as ZoneEntityController | undefined;
if (zoneController && this.isPointInside(cameraPosition, zoneController)) {
newZone = zoneController;
}
});

if (newZone !== this._currentZone) {
if (this._currentZone) {
Log.debug(Log.types.ENTITIES, `Exiting zone: ${this._currentZone.id}`);
}
if (newZone) {
Log.debug(Log.types.ENTITIES, `Entering zone: ${newZone.id}`);
} else {
Log.debug(Log.types.ENTITIES, "Not in any zone");
}
this._currentZone = newZone;
}
}

private isPointInside(point: Vector3, zoneController: ZoneEntityController): boolean {
const zoneMesh = zoneController.zoneMesh;
if (!zoneMesh) return false;
return zoneMesh.intersectsPoint(point);
}
}
6 changes: 6 additions & 0 deletions src/modules/scene/controllers/sceneController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import { Vector3, Ray } from "@babylonjs/core";
import { ScriptComponent } from "@Modules/script";
import type { VScene } from "@Modules/scene/vscene";
import Log from "@Modules/debugging/log";
import { ZoneManager } from "../ZoneManager";

const DEFAULT_GRAVITY = 9.81;
const GROUND_DETECTION_LENGTH = 5; // FIXME: This is not a good system for detecting the ground.

export class SceneController extends ScriptComponent {
private _vscene: VScene;
private _zoneManager: ZoneManager;
public isGravityApplied = false;

constructor(vscene: VScene) {
super(SceneController.typeName);
this._vscene = vscene;
this._zoneManager = new ZoneManager(this._scene);
}

public get componentType(): string {
Expand Down Expand Up @@ -77,6 +80,9 @@ export class SceneController extends ScriptComponent {
) {
this.applyGravity();
}

// Update zone detection
this._zoneManager.update();
}

public onSceneReady(): void {
Expand Down

0 comments on commit 04a35fa

Please sign in to comment.