Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev console improvements #26

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,39 @@ is on very early stage. The script to make glb+meta from blender file is here:
[build_blender_scene.py](packages/core/blender_exporter/build_blender_scene.py)

### Console
Engine provides a simple console, which can be used at runtime (if enabled in world) by pressing \`. Your game can
provide custom console commands using `world.registerConsoleCommand` function
Engine provides a simple console, which can be used at runtime (if enabled in world) by pressing \`. Your game can
provide custom console commands using `GgStatic.instance.registerConsoleCommand` function.

#### Default global console commands
| Command | Arguments | Description |
|---------------|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `commands` | - | Print all available commands. List includes global commands and commands, specific to currently selected world. Run "world" to check which world is currently selected and "world {world_name}" to select desired world |
| `help` | `string` | Print doc string of provided command |
| `worlds` | - | Print all currently available worlds |
| `world` | `string?` | Get name of selected world or select world by name. Use "worlds" to get list of currently available worlds |
| `stats_panel` | `0\|1?` | Turn on/off stats panel, skip argument to toggle value |
| `debug_panel` | `0\|1?` | Turn on/off debug panel, skip argument to toggle value |
| `bind_key` | `string, ...string` | Bind a keyboard key by code to console command. Check key codes [here](https://www.toptal.com/developers/keycode). Use "unbind_key" command to unbind it |
| `unbind_key` | `string` | Unbind a keyboard key from console command |

#### Default world-specific console commands
| Command | Arguments | Description |
|---------------|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `timescale` | `float?` | Get current time scale of selected world clock or set it. Default value is 1.0 (no time scale applied) |
| `fps_limit` | `int?` | Get current tick rate limit of selected world clock or set it. 0 means no limit applied |
| `renderers` | - | Print all renderers in selected world |
| `debug_view` | `0\|1?, string?` | Turn on/off physics debug view, skip first argument to toggle value. Second argument expects renderer name, if not provided first renderer will be picked. Use "renderers" to get list of renderers in the world |
| `performance` | `int?, avg\|peak?` | Measure how much time was spent per entity in world. Arguments are samples amount (20 by default) and "peak" or "avg" choice, both arguments are optional. "avg" report sorts entities by average time consumed, "peak" records highest value for each entity |

#### Default 2D world-specific console commands
| Command | Arguments | Description |
|-----------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| `gravity` | `?float, ?float` | Get or set 2D world gravity vector. 1 argument sets vector {x: 0, y: value}, 2 arguments sets the whole vector. Default value is "9.82" or "0 9.82" |

#### Default 3D world-specific console commands
| Command | Arguments | Description |
|-----------|--------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `gravity` | `?float, ?float, ?float` | Get or set 3D world gravity vector. 1 argument sets vector {x: 0, y: 0, z: -value}, 3 arguments set the whole vector. Default value is "9.82" or "0 0 -9.82" |

## Support
You can support project by:
Expand Down
5 changes: 3 additions & 2 deletions examples/ammo-car-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { ThreeDisplayObject3dOpts, ThreeSceneComponent, ThreeVisualTypeDocRepo }
import { AmbientLight, DirectionalLight } from 'three';
import { AmmoPhysicsTypeDocRepo, AmmoWorldComponent } from '@gg-web-engine/ammo';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld<
ThreeVisualTypeDocRepo,
AmmoPhysicsTypeDocRepo,
Expand All @@ -22,8 +25,6 @@ const world = new Gg3dWorld<
new ThreeSceneComponent(),
new AmmoWorldComponent(),
);
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
world.init().then(async () => {
// init graphics
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
Expand Down
5 changes: 3 additions & 2 deletions examples/collision-groups-pool-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { ThreeSceneComponent, ThreeVisualTypeDocRepo } from '@gg-web-engine/thre
import { AmbientLight, DirectionalLight, Material, Mesh } from 'three';
import { AmmoPhysicsTypeDocRepo, AmmoWorldComponent } from '@gg-web-engine/ammo';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld<
ThreeVisualTypeDocRepo,
AmmoPhysicsTypeDocRepo,
Expand All @@ -23,8 +26,6 @@ const world = new Gg3dWorld<
world.physicsWorld.maxSubSteps = 25;

world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(
world.visualScene.factory.createPerspectiveCamera(),
Expand Down
5 changes: 3 additions & 2 deletions examples/collision-groups-pool-three-rapier3d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { ThreeSceneComponent, ThreeVisualTypeDocRepo } from '@gg-web-engine/thre
import { AmbientLight, DirectionalLight, Material, Mesh } from 'three';
import { Rapier3dPhysicsTypeDocRepo, Rapier3dWorldComponent } from '@gg-web-engine/rapier3d';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld<
ThreeVisualTypeDocRepo,
Rapier3dPhysicsTypeDocRepo,
Expand All @@ -21,8 +24,6 @@ const world = new Gg3dWorld<
new Rapier3dWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(
world.visualScene.factory.createPerspectiveCamera(),
Expand Down
5 changes: 3 additions & 2 deletions examples/collision-groups-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { ThreeSceneComponent } from '@gg-web-engine/three';
import { AmbientLight, DirectionalLight } from 'three';
import { AmmoWorldComponent } from '@gg-web-engine/ammo';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld(
new ThreeSceneComponent(),
new AmmoWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(
world.visualScene.factory.createPerspectiveCamera(),
Expand Down
5 changes: 3 additions & 2 deletions examples/collision-groups-three-rapier3d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { ThreeSceneComponent } from '@gg-web-engine/three';
import { AmbientLight, DirectionalLight } from 'three';
import { Rapier3dWorldComponent } from '@gg-web-engine/rapier3d';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld(
new ThreeSceneComponent(),
new Rapier3dWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(
world.visualScene.factory.createPerspectiveCamera(),
Expand Down
4 changes: 2 additions & 2 deletions examples/fly-city-three-ammo/src/app/game-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { CAR_SPECS, LAMBO_SPECS, TRUCK_SPECS } from './car-specs';
import { FlyCityPTypeDoc, FlyCityVTypeDoc, FlyCityWorld } from './app.component';
import { takeUntil } from 'rxjs/operators';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

export class GameFactory {
constructor(public readonly world: FlyCityWorld) {
}

public async initGame(canvas: HTMLCanvasElement): Promise<[Renderer3dEntity<FlyCityVTypeDoc>, MapGraph3dEntity<FlyCityVTypeDoc, FlyCityPTypeDoc>, Trigger3dEntity<FlyCityPTypeDoc>]> {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
await this.world.init();
const renderer = await this.initRenderer(canvas);
this.addLights();
Expand Down
5 changes: 3 additions & 2 deletions examples/framework-angular-three-ammo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { ThreeSceneComponent, ThreeVisualTypeDocRepo } from '@gg-web-engine/thre
import { AmmoPhysicsTypeDocRepo, AmmoWorldComponent } from '@gg-web-engine/ammo';
import { Subject } from 'rxjs';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

@Component({
selector: 'my-app',
standalone: true,
Expand All @@ -21,8 +24,6 @@ export class App implements OnInit, OnDestroy {
new AmmoWorldComponent(),
);
await this.world.init();
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;

const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = this.world.addRenderer(
Expand Down
42 changes: 15 additions & 27 deletions examples/framework-react-three-rapier3d/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import { useRef, useEffect } from 'react';
import {
Trigger3dEntity,
OrbitCameraController,
Gg3dWorld,
Entity3d,
GgStatic,
} from '@gg-web-engine/core';
import { useEffect, useRef } from 'react';
import { Entity3d, Gg3dWorld, GgStatic, OrbitCameraController, Trigger3dEntity } from '@gg-web-engine/core';
import { ThreeSceneComponent } from '@gg-web-engine/three';
import { Rapier3dWorldComponent } from '@gg-web-engine/rapier3d';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const isNewWorld = !GgStatic.instance.selectedWorld;
const world =
(GgStatic.instance.selectedWorld as Gg3dWorld) ||
new Gg3dWorld(new ThreeSceneComponent(), new Rapier3dWorldComponent());
if (isNewWorld) {
world.init().then(() => {
world.start();
GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;
});
}

function App() {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
const spawnTimerRef = useRef<number | null>(null);

useEffect(() => {
const initializeWorld = async () => {
while (!world.isRunning) {
await new Promise((r) => setTimeout(r, 50));
}
if (spawnTimerRef.current) {
clearInterval(spawnTimerRef.current);
for (const c of world.worldClock.children) {
c.dispose();
}
for (const e of world.children) {
if (!e.name.startsWith('fps_meter')) {
Expand All @@ -40,7 +34,7 @@ function App() {
}
const renderer = world.addRenderer(
world.visualScene.factory.createPerspectiveCamera({}),
canvasRef.current!
canvasRef.current!,
);
renderer.position = { x: 9, y: 12, z: 9 };

Expand All @@ -58,7 +52,7 @@ function App() {
world.physicsWorld.factory.createTrigger({
shape: 'BOX',
dimensions: { x: 1000, y: 1000, z: 1 },
})
}),
);
destroyTrigger.position = { x: 0, y: 0, z: -5 };
destroyTrigger.onEntityEntered.subscribe((entity) => {
Expand All @@ -70,7 +64,9 @@ function App() {
});
world.addEntity(destroyTrigger);

spawnTimerRef.current = window.setInterval(() => {
const spawnTimer = world.createClock(true);
spawnTimer.tickRateLimit = 2;
spawnTimer.tick$.subscribe(() => {
let item: Entity3d;
const random = Math.random();
if (random < 0.2) {
Expand Down Expand Up @@ -104,22 +100,14 @@ function App() {
y: Math.random() * 5 - 2.5,
z: 10,
};
}, 500);
};

initializeWorld();

// Cleanup on unmount or dependency change
return () => {
if (spawnTimerRef.current) {
clearInterval(spawnTimerRef.current);
}
});
};
initializeWorld().then();
}, []);

return (
<>
<canvas ref={canvasRef} id="gg"></canvas>
<canvas ref={canvasRef} id='gg'></canvas>
</>
);
}
Expand Down
5 changes: 3 additions & 2 deletions examples/glb-loader-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { ThreeSceneComponent, ThreeVisualTypeDocRepo } from '@gg-web-engine/thre
import { AmbientLight, DirectionalLight } from 'three';
import { AmmoPhysicsTypeDocRepo, AmmoWorldComponent } from '@gg-web-engine/ammo';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld<
ThreeVisualTypeDocRepo,
AmmoPhysicsTypeDocRepo,
Expand All @@ -13,8 +16,6 @@ const world = new Gg3dWorld<
new AmmoWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(world.visualScene.factory.createPerspectiveCamera(), canvas);
renderer.position = { x: 9, y: 12, z: 9 };
Expand Down
5 changes: 3 additions & 2 deletions examples/glb-loader-three-rapier3d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { ThreeSceneComponent, ThreeVisualTypeDocRepo } from '@gg-web-engine/thre
import { AmbientLight, DirectionalLight } from 'three';
import { Rapier3dPhysicsTypeDocRepo, Rapier3dWorldComponent } from '@gg-web-engine/rapier3d';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld<
ThreeVisualTypeDocRepo,
Rapier3dPhysicsTypeDocRepo,
Expand All @@ -13,8 +16,6 @@ const world = new Gg3dWorld<
new Rapier3dWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(world.visualScene.factory.createPerspectiveCamera(), canvas);
renderer.position = { x: 9, y: 12, z: 9 };
Expand Down
5 changes: 3 additions & 2 deletions examples/primitives-pixi-matter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Entity2d, Gg2dWorld, GgStatic } from '@gg-web-engine/core';
import { PixiSceneComponent } from '@gg-web-engine/pixi';
import { MatterWorldComponent } from '@gg-web-engine/matter';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg2dWorld(
new PixiSceneComponent(),
new MatterWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(canvas);

Expand Down
5 changes: 3 additions & 2 deletions examples/primitives-pixi-rapier2d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Entity2d, Gg2dWorld, GgStatic } from '@gg-web-engine/core';
import { PixiSceneComponent } from '@gg-web-engine/pixi';
import { Rapier2dWorldComponent } from '@gg-web-engine/rapier2d';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg2dWorld(
new PixiSceneComponent(),
new Rapier2dWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(canvas);

Expand Down
5 changes: 3 additions & 2 deletions examples/primitives-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Entity3d, Gg3dWorld, GgStatic, OrbitCameraController, Trigger3dEntity }
import { ThreeSceneComponent } from '@gg-web-engine/three';
import { AmmoWorldComponent } from '@gg-web-engine/ammo';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld(
new ThreeSceneComponent(),
new AmmoWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(world.visualScene.factory.createPerspectiveCamera(), canvas);
renderer.position = { x: 9, y: 12, z: 9 };
Expand Down
5 changes: 3 additions & 2 deletions examples/primitives-three-rapier3d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Entity3d, Gg3dWorld, GgStatic, OrbitCameraController, Trigger3dEntity }
import { ThreeSceneComponent } from '@gg-web-engine/three';
import { Rapier3dWorldComponent } from '@gg-web-engine/rapier3d';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld(
new ThreeSceneComponent(),
new Rapier3dWorldComponent(),
);
world.init().then(async () => {
GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
const renderer = world.addRenderer(world.visualScene.factory.createPerspectiveCamera(), canvas);
renderer.position = { x: 9, y: 12, z: 9 };
Expand Down
6 changes: 3 additions & 3 deletions examples/shooter-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { ThreeSceneComponent, ThreeVisualTypeDocRepo } from '@gg-web-engine/thre
import { AmbientLight, DirectionalLight, Mesh, MeshPhongMaterial, RepeatWrapping, TextureLoader } from 'three';
import { AmmoPhysicsTypeDocRepo, AmmoWorldComponent } from '@gg-web-engine/ammo';

GgStatic.instance.showStats = true;
GgStatic.instance.devConsoleEnabled = true;

const world = new Gg3dWorld<
ThreeVisualTypeDocRepo,
AmmoPhysicsTypeDocRepo,
Expand All @@ -15,9 +18,6 @@ const world = new Gg3dWorld<
world.physicsWorld.maxSubSteps = 10;
world.physicsWorld.fixedTimeStep = null;

GgStatic.instance.showStats = true;
// GgStatic.instance.devConsoleEnabled = true;

world.init().then(async () => {
// init graphics
const canvas = document.getElementById('gg')! as HTMLCanvasElement;
Expand Down
Loading