Skip to content

Commit

Permalink
Merge pull request #99 from zhiquanyeo/feature/zones
Browse files Browse the repository at this point in the history
Initial Zone implementation
  • Loading branch information
zhiquanyeo authored Jul 6, 2020
2 parents 2d39050 + 1691676 commit 9ae6c06
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/demos/demo1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ function main() {
};
const robot = simulator.addRobot(spec);

simulator.addZone({
type: "zone",
initialPosition: { x: 0, y: 0 },
baseColor: 0xff0000,
opacity: 0.4,
xLength: 2,
zLength: 1,
zoneId: "test-zone",
});

enum RobotMode {
LOOKING,
RUN_AWAY,
Expand Down
6 changes: 6 additions & 0 deletions src/engine/Sim3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IWallSpec,
IConeSpec,
ICylinderSpec,
IZoneSpec,
} from "./specs/CoreSpecs";
import { ObjectFactories } from "./objects/ObjectFactories";
import { BallHandle } from "./handles/BallHandle";
Expand All @@ -31,6 +32,7 @@ import { DEFAULT_WALL_THICKNESS, DEFAULT_WALL_HEIGHT } from "./objects/SimWall";
import { ObjectHandle } from "./handles/ObjectHandle";
import { CameraModeSpec, CameraMode } from "./specs/CameraSpecs";
import { CameraManager } from "./CameraManager";
import { ZoneHandle } from "./handles/ZoneHandle";

interface ISimObjectContainer {
type: string;
Expand Down Expand Up @@ -271,6 +273,10 @@ export class Sim3D {
this.handleRegistry.invalidateHandle(handle.ref.guid);
}

addZone(spec: IZoneSpec): ZoneHandle {
return this.addGameObject<ZoneHandle>(spec, ZoneHandle);
}

isDebugMode(): boolean {
return this.debugMesh.visible;
}
Expand Down
16 changes: 16 additions & 0 deletions src/engine/handles/ZoneHandle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ObjectHandle } from "./ObjectHandle";
import { Zone } from "../objects/Zone";

export class ZoneHandle extends ObjectHandle<Zone> {
setColor(color: number): void {
this._rootObject.setColor(color);
}

setOpacity(opacity: number): void {
this._rootObject.setOpacity(opacity);
}

getZoneId(): string {
return this._rootObject.getZoneId();
}
}
2 changes: 2 additions & 0 deletions src/engine/objects/ObjectFactories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { makeSimWall } from "./SimWall";
import { makeSimPyramid } from "./SimPyramid";
import { makeSimCone } from "./SimCone";
import { makeSimCylinder } from "./SimCylinder";
import { makeZone } from "./Zone";

export class ObjectFactories {
private _factories: Map<string, (spec: SimObjectSpec) => SimObject>;
Expand All @@ -28,6 +29,7 @@ export class ObjectFactories {
this.registerFactory("pyramid", makeSimPyramid);
this.registerFactory("cone", makeSimCone);
this.registerFactory("cylinder", makeSimCylinder);
this.registerFactory("zone", makeZone);
}

private registerFactory(
Expand Down
98 changes: 98 additions & 0 deletions src/engine/objects/Zone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as THREE from "three";
import { BodyDef, FixtureDef, Vec2, Box } from "planck-js";
import { IZoneSpec } from "../specs/CoreSpecs";
import { SimObject } from "./SimObject";
import { Vector2d } from "../SimTypes";

/**
* Factory method for creating a Zone
* @param spec
*/
export function makeZone(spec: IZoneSpec): Zone {
return new Zone(spec);
}

export class Zone extends SimObject {
private _bodySpecs: BodyDef;
private _fixtureSpecs: FixtureDef;

private _zoneId: string;

constructor(spec: IZoneSpec) {
super("Zone");

this._zoneId = spec.zoneId;

const initialPosition: Vector2d = { x: 0, y: 0 };

const materialSpecs: THREE.MeshBasicMaterialParameters = {
side: THREE.DoubleSide,
transparent: true,
};

if (spec.baseColor !== undefined) {
materialSpecs.color = spec.baseColor;
}

if (spec.opacity !== undefined) {
materialSpecs.opacity = spec.opacity;
}

if (spec.initialPosition) {
initialPosition.x = spec.initialPosition.x;
initialPosition.y = spec.initialPosition.y;
}

const zoneGeom = new THREE.PlaneGeometry(spec.xLength, spec.zLength);
const zoneMaterial = new THREE.MeshBasicMaterial(materialSpecs);
const zoneMesh = new THREE.Mesh(zoneGeom, zoneMaterial);

zoneMesh.position.y = 0;
zoneMesh.position.x = initialPosition.x;
zoneMesh.position.z = initialPosition.y;

// Rotate along the x axis
zoneMesh.rotation.x = Math.PI / 2;

this._mesh = zoneMesh;

// Set up the physics body
this._bodySpecs = {
type: "static",
position: new Vec2(initialPosition.x, initialPosition.y),
angle: 0,
};

this._fixtureSpecs = {
shape: new Box(spec.xLength / 2, spec.zLength / 2),
isSensor: true,
};
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
update(ms: number): void {
const bodyCenter = this._body.getWorldCenter();
this._mesh.position.x = bodyCenter.x;
this._mesh.position.z = bodyCenter.y;
}

getBodySpecs(): BodyDef {
return this._bodySpecs;
}

getFixtureDef(): FixtureDef {
return this._fixtureSpecs;
}

setColor(color: number): void {
(<THREE.MeshBasicMaterial>this._mesh.material).color.set(color);
}

setOpacity(opacity: number): void {
(<THREE.MeshBasicMaterial>this._mesh.material).opacity = opacity;
}

getZoneId(): string {
return this._zoneId;
}
}
12 changes: 11 additions & 1 deletion src/engine/specs/CoreSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export type SimObjectSpec =
| IWallSpec
| IPyramidSpec
| IConeSpec
| ICylinderSpec;
| ICylinderSpec
| IZoneSpec;

export interface IPhysicsProperties {
linearDamping?: number;
Expand Down Expand Up @@ -105,3 +106,12 @@ export interface ICustomMeshSpec {
rotation?: Vector3d;
scale?: Vector3d;
}

// Zone
export interface IZoneSpec extends IBaseSimObjectSpec {
type: "zone";
zoneId: string;
xLength: number;
zLength: number;
opacity?: number;
}

0 comments on commit 9ae6c06

Please sign in to comment.