@directivegames/genesys.sdk 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -0
- package/dist/src/asset-pack/eslint.config.js +43 -0
- package/dist/src/asset-pack/scripts/postinstall.js +64 -0
- package/dist/src/asset-pack/src/index.js +1 -0
- package/dist/src/core/cli.js +306 -0
- package/dist/src/core/common.js +324 -0
- package/dist/src/core/index.js +6 -0
- package/dist/src/core/tools/build-project.js +450 -0
- package/dist/src/core/tools/index.js +2 -0
- package/dist/src/core/tools/new-asset-pack.js +150 -0
- package/dist/src/core/tools/new-project.js +292 -0
- package/dist/src/core/types.js +1 -0
- package/dist/src/dependencies.js +82 -0
- package/dist/src/electron/IpcSerializableError.js +38 -0
- package/dist/src/electron/api.js +7 -0
- package/dist/src/electron/backend/actions.js +56 -0
- package/dist/src/electron/backend/handler.js +441 -0
- package/dist/src/electron/backend/logging.js +41 -0
- package/dist/src/electron/backend/main.js +315 -0
- package/dist/src/electron/backend/menu.js +208 -0
- package/dist/src/electron/backend/state.js +201 -0
- package/dist/src/electron/backend/tools/const.js +9 -0
- package/dist/src/electron/backend/tools/file-server.js +383 -0
- package/dist/src/electron/backend/tools/open-project.js +261 -0
- package/dist/src/electron/backend/window.js +161 -0
- package/dist/src/templates/eslint.config.js +43 -0
- package/dist/src/templates/scripts/genesys/build-project.js +42 -0
- package/dist/src/templates/scripts/genesys/calc-bounding-box.js +205 -0
- package/dist/src/templates/scripts/genesys/common.js +36 -0
- package/dist/src/templates/scripts/genesys/const.js +9 -0
- package/dist/src/templates/scripts/genesys/dev/dump-default-scene.js +8 -0
- package/dist/src/templates/scripts/genesys/dev/generate-manifest.js +116 -0
- package/dist/src/templates/scripts/genesys/dev/launcher.js +39 -0
- package/dist/src/templates/scripts/genesys/dev/storage-provider.js +188 -0
- package/dist/src/templates/scripts/genesys/dev/update-template-scenes.js +67 -0
- package/dist/src/templates/scripts/genesys/doc-server.js +12 -0
- package/dist/src/templates/scripts/genesys/genesys-mcp.js +413 -0
- package/dist/src/templates/scripts/genesys/mcp/doc-tools.js +70 -0
- package/dist/src/templates/scripts/genesys/mcp/editor-functions.js +123 -0
- package/dist/src/templates/scripts/genesys/mcp/editor-tools.js +51 -0
- package/dist/src/templates/scripts/genesys/mcp/get-scene-state.js +26 -0
- package/dist/src/templates/scripts/genesys/mcp/run-subprocess.js +23 -0
- package/dist/src/templates/scripts/genesys/mcp/search-actors.js +703 -0
- package/dist/src/templates/scripts/genesys/mcp/search-assets.js +296 -0
- package/dist/src/templates/scripts/genesys/mcp/utils.js +234 -0
- package/dist/src/templates/scripts/genesys/misc.js +32 -0
- package/dist/src/templates/scripts/genesys/mock.js +5 -0
- package/dist/src/templates/scripts/genesys/place-actors.js +112 -0
- package/dist/src/templates/scripts/genesys/post-install.js +25 -0
- package/dist/src/templates/scripts/genesys/remove-engine-comments.js +113 -0
- package/dist/src/templates/scripts/genesys/storageProvider.js +146 -0
- package/dist/src/templates/scripts/genesys/validate-prefabs.js +115 -0
- package/dist/src/templates/src/index.js +20 -0
- package/dist/src/templates/src/templates/firstPerson/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/firstPerson/src/game.js +30 -0
- package/dist/src/templates/src/templates/firstPerson/src/player.js +60 -0
- package/dist/src/templates/src/templates/fps/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/fps/src/game.js +30 -0
- package/dist/src/templates/src/templates/fps/src/player.js +64 -0
- package/dist/src/templates/src/templates/fps/src/weapon.js +62 -0
- package/dist/src/templates/src/templates/freeCamera/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/freeCamera/src/game.js +30 -0
- package/dist/src/templates/src/templates/freeCamera/src/player.js +43 -0
- package/dist/src/templates/src/templates/sideScroller/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/sideScroller/src/const.js +43 -0
- package/dist/src/templates/src/templates/sideScroller/src/game.js +103 -0
- package/dist/src/templates/src/templates/sideScroller/src/level-generator.js +249 -0
- package/dist/src/templates/src/templates/sideScroller/src/player.js +105 -0
- package/dist/src/templates/src/templates/thirdPerson/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/thirdPerson/src/game.js +30 -0
- package/dist/src/templates/src/templates/thirdPerson/src/player.js +63 -0
- package/dist/src/templates/src/templates/vehicle/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/vehicle/src/base-vehicle.js +122 -0
- package/dist/src/templates/src/templates/vehicle/src/game.js +33 -0
- package/dist/src/templates/src/templates/vehicle/src/mesh-vehicle.js +189 -0
- package/dist/src/templates/src/templates/vehicle/src/player.js +102 -0
- package/dist/src/templates/src/templates/vehicle/src/primitive-vehicle.js +259 -0
- package/dist/src/templates/src/templates/vehicle/src/ui-hints.js +100 -0
- package/dist/src/templates/src/templates/vr-game/src/auto-imports.js +1 -0
- package/dist/src/templates/src/templates/vr-game/src/game.js +55 -0
- package/dist/src/templates/src/templates/vr-game/src/sample-vr-actor.js +29 -0
- package/dist/src/templates/vite.config.js +46 -0
- package/package.json +176 -0
- package/scripts/post-install.ts +143 -0
- package/src/asset-pack/.gitattributes +89 -0
- package/src/asset-pack/eslint.config.js +45 -0
- package/src/asset-pack/gitignore +11 -0
- package/src/asset-pack/scripts/postinstall.ts +81 -0
- package/src/asset-pack/src/index.ts +0 -0
- package/src/asset-pack/tsconfig.json +34 -0
- package/src/templates/.cursor/mcp.json +20 -0
- package/src/templates/.cursorignore +2 -0
- package/src/templates/.gitattributes +89 -0
- package/src/templates/.vscode/settings.json +6 -0
- package/src/templates/AGENTS.md +86 -0
- package/src/templates/CLAUDE.md +1 -0
- package/src/templates/README.md +24 -0
- package/src/templates/eslint.config.js +45 -0
- package/src/templates/gitignore +11 -0
- package/src/templates/index.html +34 -0
- package/src/templates/pnpm-lock.yaml +3676 -0
- package/src/templates/scripts/genesys/build-project.ts +51 -0
- package/src/templates/scripts/genesys/calc-bounding-box.ts +272 -0
- package/src/templates/scripts/genesys/common.ts +46 -0
- package/src/templates/scripts/genesys/const.ts +9 -0
- package/src/templates/scripts/genesys/dev/dump-default-scene.ts +11 -0
- package/src/templates/scripts/genesys/dev/generate-manifest.ts +146 -0
- package/src/templates/scripts/genesys/dev/launcher.ts +46 -0
- package/src/templates/scripts/genesys/dev/storage-provider.ts +229 -0
- package/src/templates/scripts/genesys/dev/update-template-scenes.ts +84 -0
- package/src/templates/scripts/genesys/doc-server.ts +16 -0
- package/src/templates/scripts/genesys/genesys-mcp.ts +526 -0
- package/src/templates/scripts/genesys/mcp/doc-tools.ts +86 -0
- package/src/templates/scripts/genesys/mcp/editor-functions.ts +151 -0
- package/src/templates/scripts/genesys/mcp/editor-tools.ts +73 -0
- package/src/templates/scripts/genesys/mcp/get-scene-state.ts +35 -0
- package/src/templates/scripts/genesys/mcp/run-subprocess.ts +30 -0
- package/src/templates/scripts/genesys/mcp/search-actors.ts +858 -0
- package/src/templates/scripts/genesys/mcp/search-assets.ts +380 -0
- package/src/templates/scripts/genesys/mcp/utils.ts +281 -0
- package/src/templates/scripts/genesys/misc.ts +42 -0
- package/src/templates/scripts/genesys/mock.ts +6 -0
- package/src/templates/scripts/genesys/place-actors.ts +179 -0
- package/src/templates/scripts/genesys/post-install.ts +30 -0
- package/src/templates/scripts/genesys/prefab.schema.json +85 -0
- package/src/templates/scripts/genesys/remove-engine-comments.ts +135 -0
- package/src/templates/scripts/genesys/run-mcp-inspector.bat +5 -0
- package/src/templates/scripts/genesys/storageProvider.ts +182 -0
- package/src/templates/scripts/genesys/validate-prefabs.ts +138 -0
- package/src/templates/src/index.ts +22 -0
- package/src/templates/src/templates/firstPerson/assets/default.genesys-scene +166 -0
- package/src/templates/src/templates/firstPerson/src/auto-imports.ts +0 -0
- package/src/templates/src/templates/firstPerson/src/game.ts +39 -0
- package/src/templates/src/templates/firstPerson/src/player.ts +63 -0
- package/src/templates/src/templates/fps/assets/default.genesys-scene +9460 -0
- package/src/templates/src/templates/fps/assets/models/SM_Beam_400.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_ChamferCube.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Floor_Thick_400x400.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Floor_Thick_400x400_Orange.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Floor_Thin_400x400.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Floor_Thin_400x400_Orange.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Ramp_400x400.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Rifle.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Wall_Thin_400x200.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Wall_Thin_400x200_Orange.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Wall_Thin_400x400.glb +0 -0
- package/src/templates/src/templates/fps/assets/models/SM_Wall_Thin_400x400_Orange.glb +0 -0
- package/src/templates/src/templates/fps/src/auto-imports.ts +0 -0
- package/src/templates/src/templates/fps/src/game.ts +39 -0
- package/src/templates/src/templates/fps/src/player.ts +69 -0
- package/src/templates/src/templates/fps/src/weapon.ts +54 -0
- package/src/templates/src/templates/freeCamera/assets/default.genesys-scene +166 -0
- package/src/templates/src/templates/freeCamera/src/auto-imports.ts +0 -0
- package/src/templates/src/templates/freeCamera/src/game.ts +39 -0
- package/src/templates/src/templates/freeCamera/src/player.ts +45 -0
- package/src/templates/src/templates/sideScroller/assets/default.genesys-scene +122 -0
- package/src/templates/src/templates/sideScroller/src/auto-imports.ts +0 -0
- package/src/templates/src/templates/sideScroller/src/const.ts +46 -0
- package/src/templates/src/templates/sideScroller/src/game.ts +122 -0
- package/src/templates/src/templates/sideScroller/src/level-generator.ts +361 -0
- package/src/templates/src/templates/sideScroller/src/player.ts +125 -0
- package/src/templates/src/templates/thirdPerson/assets/default.genesys-scene +166 -0
- package/src/templates/src/templates/thirdPerson/src/auto-imports.ts +0 -0
- package/src/templates/src/templates/thirdPerson/src/game.ts +39 -0
- package/src/templates/src/templates/thirdPerson/src/player.ts +61 -0
- package/src/templates/src/templates/vehicle/assets/default.genesys-scene +226 -0
- package/src/templates/src/templates/vehicle/assets/models/cyberTruck/chassis.glb +0 -0
- package/src/templates/src/templates/vehicle/assets/models/cyberTruck/wheel.glb +0 -0
- package/src/templates/src/templates/vehicle/src/auto-imports.ts +0 -0
- package/src/templates/src/templates/vehicle/src/base-vehicle.ts +145 -0
- package/src/templates/src/templates/vehicle/src/game.ts +43 -0
- package/src/templates/src/templates/vehicle/src/mesh-vehicle.ts +191 -0
- package/src/templates/src/templates/vehicle/src/player.ts +109 -0
- package/src/templates/src/templates/vehicle/src/primitive-vehicle.ts +266 -0
- package/src/templates/src/templates/vehicle/src/ui-hints.ts +101 -0
- package/src/templates/src/templates/vr-game/assets/default.genesys-scene +247 -0
- package/src/templates/src/templates/vr-game/src/auto-imports.ts +1 -0
- package/src/templates/src/templates/vr-game/src/game.ts +66 -0
- package/src/templates/src/templates/vr-game/src/sample-vr-actor.ts +26 -0
- package/src/templates/tsconfig.json +35 -0
- package/src/templates/vite.config.ts +52 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
|
|
2
|
+
import * as ENGINE from 'genesys.js';
|
|
3
|
+
import * as THREE from 'three';
|
|
4
|
+
import './auto-imports.js';
|
|
5
|
+
|
|
6
|
+
import { SideScrollerLevelGenerator } from './level-generator.js';
|
|
7
|
+
import { SideScrollerPlayer } from './player.js';
|
|
8
|
+
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// SIDE SCROLLER GAME
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
// A side-scroller game with animated character and infinite level generation
|
|
14
|
+
class SideScrollerGame extends ENGINE.BaseGameLoop {
|
|
15
|
+
private player: SideScrollerPlayer | null = null;
|
|
16
|
+
private playerController: ENGINE.PlayerController | null = null;
|
|
17
|
+
private levelGenerator: SideScrollerLevelGenerator | null = null;
|
|
18
|
+
|
|
19
|
+
protected override createLoadingScreen(): ENGINE.ILoadingScreen | null {
|
|
20
|
+
// enable the default loading screen
|
|
21
|
+
return new ENGINE.DefaultLoadingScreen();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected override async preStart(): Promise<void> {
|
|
25
|
+
this.createLevel();
|
|
26
|
+
this.createPlayer();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protected override tick(tickTime: ENGINE.TickTime): void {
|
|
30
|
+
super.tick(tickTime);
|
|
31
|
+
|
|
32
|
+
// Update level generation based on player position
|
|
33
|
+
if (this.player && this.levelGenerator) {
|
|
34
|
+
const playerPosition = this.player.getWorldPosition();
|
|
35
|
+
this.levelGenerator.updateLevel(playerPosition.x);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private createPlayer(): void {
|
|
40
|
+
const spawnX = 1;
|
|
41
|
+
// Create the side-scroller player with animated character
|
|
42
|
+
this.player = new SideScrollerPlayer({
|
|
43
|
+
position: new THREE.Vector3(spawnX, ENGINE.CHARACTER_HEIGHT / 2, 0),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Create the controller and possess the player
|
|
47
|
+
this.playerController = new ENGINE.PlayerController();
|
|
48
|
+
this.playerController.possess(this.player);
|
|
49
|
+
|
|
50
|
+
// Add both to the world
|
|
51
|
+
this.world.addActors(this.player, this.playerController);
|
|
52
|
+
this.setInitialPlayerPosition(spawnX);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Sets the initial position of the player.
|
|
57
|
+
* @param spawnX - The X coordinate to spawn the player at.
|
|
58
|
+
*/
|
|
59
|
+
private setInitialPlayerPosition(spawnX: number): void {
|
|
60
|
+
const groundHeight = this.findGroundHeightAtX(spawnX);
|
|
61
|
+
if (groundHeight !== null) {
|
|
62
|
+
this.player?.setWorldPosition(new THREE.Vector3(spawnX, groundHeight + ENGINE.CHARACTER_HEIGHT / 2, 0));
|
|
63
|
+
} else {
|
|
64
|
+
this.world.runInNextTick(() => {
|
|
65
|
+
this.setInitialPlayerPosition(spawnX);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Performs a hit test to find the ground height at a given X coordinate
|
|
72
|
+
* @param x - The X coordinate to test
|
|
73
|
+
* @returns The Y coordinate of the ground surface, or 0 if no ground is found
|
|
74
|
+
*/
|
|
75
|
+
private findGroundHeightAtX(x: number): number | null {
|
|
76
|
+
// Start from a high position and cast downward
|
|
77
|
+
const rayOrigin = new THREE.Vector3(x, 50, 0);
|
|
78
|
+
const rayDirection = new THREE.Vector3(0, -1, 0); // Downward
|
|
79
|
+
|
|
80
|
+
const hitResults = this.world.getPhysicsEngine()?.performHitTest({
|
|
81
|
+
origin: rayOrigin,
|
|
82
|
+
direction: rayDirection,
|
|
83
|
+
maxDistance: 100, // Cast ray down 100 units
|
|
84
|
+
stopOnFirstHit: true,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (hitResults && hitResults.length > 0) {
|
|
88
|
+
// Return the Y coordinate of the hit location (top surface of ground)
|
|
89
|
+
return hitResults[0].hitLocation.y;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private createLevel(): void {
|
|
96
|
+
// Create the level generator
|
|
97
|
+
this.levelGenerator = new SideScrollerLevelGenerator(this.world, {
|
|
98
|
+
enableDebugVisualization: false,
|
|
99
|
+
createGroundMesh: true
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Generate initial level chunks
|
|
103
|
+
this.levelGenerator.updateLevel(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public override destroy(): void {
|
|
107
|
+
// Clean up level generator
|
|
108
|
+
if (this.levelGenerator) {
|
|
109
|
+
this.levelGenerator.cleanup();
|
|
110
|
+
this.levelGenerator = null;
|
|
111
|
+
}
|
|
112
|
+
super.destroy();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function main(container: HTMLElement, gameId: string): ENGINE.IGameLoop {
|
|
117
|
+
const game = new SideScrollerGame(container, {
|
|
118
|
+
...ENGINE.BaseGameLoop.DEFAULT_OPTIONS,
|
|
119
|
+
gameId
|
|
120
|
+
});
|
|
121
|
+
return game;
|
|
122
|
+
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import * as ENGINE from 'genesys.js';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
|
|
4
|
+
import { COLORS, LEVEL_CONFIG } from './const.js';
|
|
5
|
+
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// LEVEL GENERATOR
|
|
8
|
+
// ============================================================================
|
|
9
|
+
|
|
10
|
+
// Interface for level generator configuration options
|
|
11
|
+
interface LevelGeneratorOptions {
|
|
12
|
+
chunkSize?: number;
|
|
13
|
+
chunksAhead?: number;
|
|
14
|
+
chunksBehind?: number;
|
|
15
|
+
platformHeightRange?: { min: number; max: number };
|
|
16
|
+
platformWidthRange?: { min: number; max: number };
|
|
17
|
+
obstacleHeightRange?: { min: number; max: number };
|
|
18
|
+
chunkMargin?: number;
|
|
19
|
+
minSpacing?: number;
|
|
20
|
+
enableDebugVisualization?: boolean;
|
|
21
|
+
createGroundMesh?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Interface for tracking level chunks
|
|
25
|
+
interface LevelChunk {
|
|
26
|
+
startX: number;
|
|
27
|
+
endX: number;
|
|
28
|
+
actors: ENGINE.Actor[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Interface for positioned elements within chunks
|
|
32
|
+
interface PositionedElement {
|
|
33
|
+
x: number;
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
type: 'platform' | 'obstacle';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Simple level generator for infinite side-scrolling levels
|
|
40
|
+
export class SideScrollerLevelGenerator {
|
|
41
|
+
private world: ENGINE.World;
|
|
42
|
+
// Map of chunk index to chunk
|
|
43
|
+
private levelChunks: Map<number, LevelChunk> = new Map();
|
|
44
|
+
|
|
45
|
+
// Configuration
|
|
46
|
+
private chunkSize: number;
|
|
47
|
+
private chunksAhead: number;
|
|
48
|
+
private chunksBehind: number;
|
|
49
|
+
private platformHeightRange: { min: number; max: number };
|
|
50
|
+
private platformWidthRange: { min: number; max: number };
|
|
51
|
+
private obstacleHeightRange: { min: number; max: number };
|
|
52
|
+
private chunkMargin: number;
|
|
53
|
+
private minSpacing: number;
|
|
54
|
+
private debugVisualizationEnabled: boolean = false;
|
|
55
|
+
private createGroundMesh: boolean = false;
|
|
56
|
+
|
|
57
|
+
constructor(world: ENGINE.World, options: LevelGeneratorOptions = {}) {
|
|
58
|
+
this.world = world;
|
|
59
|
+
|
|
60
|
+
// Apply defaults
|
|
61
|
+
this.chunkSize = options.chunkSize ?? LEVEL_CONFIG.CHUNK_SIZE;
|
|
62
|
+
this.chunksAhead = options.chunksAhead ?? LEVEL_CONFIG.CHUNKS_AHEAD;
|
|
63
|
+
this.chunksBehind = options.chunksBehind ?? LEVEL_CONFIG.CHUNKS_BEHIND;
|
|
64
|
+
this.platformHeightRange = options.platformHeightRange ?? LEVEL_CONFIG.PLATFORM_HEIGHT_RANGE;
|
|
65
|
+
this.platformWidthRange = options.platformWidthRange ?? LEVEL_CONFIG.PLATFORM_WIDTH_RANGE;
|
|
66
|
+
this.obstacleHeightRange = options.obstacleHeightRange ?? LEVEL_CONFIG.OBSTACLE_HEIGHT_RANGE;
|
|
67
|
+
this.chunkMargin = options.chunkMargin ?? LEVEL_CONFIG.CHUNK_MARGIN;
|
|
68
|
+
this.minSpacing = options.minSpacing ?? LEVEL_CONFIG.MIN_SPACING;
|
|
69
|
+
this.debugVisualizationEnabled = options.enableDebugVisualization ?? false;
|
|
70
|
+
this.createGroundMesh = options.createGroundMesh ?? false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public updateLevel(playerX: number): void {
|
|
74
|
+
const playerChunkIndex = this.getChunkIndex(playerX);
|
|
75
|
+
|
|
76
|
+
const validChunkIndices = [];
|
|
77
|
+
for (let i = playerChunkIndex - this.chunksBehind; i <= playerChunkIndex + this.chunksAhead; i++) {
|
|
78
|
+
this.generateChunk(i);
|
|
79
|
+
validChunkIndices.push(i);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (const index of this.levelChunks.keys()) {
|
|
83
|
+
if (!validChunkIndices.includes(index)) {
|
|
84
|
+
this.removeChunk(index);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private getChunkIndex(x: number): number {
|
|
90
|
+
return Math.floor(x / this.chunkSize);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private removeChunk(index: number): void {
|
|
94
|
+
const chunk = this.levelChunks.get(index);
|
|
95
|
+
if (chunk) {
|
|
96
|
+
this.world.removeActors(...chunk.actors);
|
|
97
|
+
this.levelChunks.delete(index);
|
|
98
|
+
console.log(`[LevelGenerator] Removed chunk at index ${index}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private generateChunk(index: number): void {
|
|
103
|
+
if (this.levelChunks.has(index)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const startX = index * this.chunkSize;
|
|
108
|
+
const endX = startX + this.chunkSize;
|
|
109
|
+
const actors: ENGINE.Actor[] = [];
|
|
110
|
+
|
|
111
|
+
// Generate positioned elements for this chunk
|
|
112
|
+
const elements = this.generatePositionedElements(startX);
|
|
113
|
+
|
|
114
|
+
// Create actors for each element
|
|
115
|
+
for (const element of elements) {
|
|
116
|
+
const actor = this.createElementActor(element);
|
|
117
|
+
actors.push(actor);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Add ground mesh if enabled
|
|
121
|
+
if (this.createGroundMesh) {
|
|
122
|
+
const groundActor = this.createGroundMeshActor(startX);
|
|
123
|
+
actors.push(groundActor);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Add debug visualization if enabled
|
|
127
|
+
if (this.debugVisualizationEnabled) {
|
|
128
|
+
const debugActor = this.createChunkDebugVisualization(startX);
|
|
129
|
+
actors.push(debugActor);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Add all actors to the world
|
|
133
|
+
this.world.addActors(...actors);
|
|
134
|
+
|
|
135
|
+
// Track this chunk
|
|
136
|
+
const chunk: LevelChunk = {
|
|
137
|
+
startX,
|
|
138
|
+
endX,
|
|
139
|
+
actors,
|
|
140
|
+
};
|
|
141
|
+
console.log(`[LevelGenerator] Generated chunk at index ${index}`);
|
|
142
|
+
this.levelChunks.set(index, chunk);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private generatePositionedElements(startX: number): PositionedElement[] {
|
|
146
|
+
const elements: PositionedElement[] = [];
|
|
147
|
+
const usableWidth = this.chunkSize - (this.chunkMargin * 2); // Leave margin on each side
|
|
148
|
+
const usableStartX = startX + this.chunkMargin;
|
|
149
|
+
|
|
150
|
+
// Simple alternating generation: platform -> obstacle -> platform -> obstacle...
|
|
151
|
+
const totalElements = Math.floor(usableWidth / this.minSpacing); // How many elements can fit
|
|
152
|
+
|
|
153
|
+
let currentX = usableStartX;
|
|
154
|
+
let isPlatform = true; // Start with platform
|
|
155
|
+
|
|
156
|
+
for (let i = 0; i < totalElements && currentX < (startX + this.chunkSize - this.chunkMargin); i++) {
|
|
157
|
+
if (isPlatform) {
|
|
158
|
+
// Generate platform with random dimensions
|
|
159
|
+
const width = this.platformWidthRange.min +
|
|
160
|
+
Math.random() * (this.platformWidthRange.max - this.platformWidthRange.min);
|
|
161
|
+
const height = this.platformHeightRange.min +
|
|
162
|
+
Math.random() * (this.platformHeightRange.max - this.platformHeightRange.min);
|
|
163
|
+
|
|
164
|
+
elements.push({
|
|
165
|
+
x: currentX + width / 2, // Center position
|
|
166
|
+
width: width,
|
|
167
|
+
height: height,
|
|
168
|
+
type: 'platform'
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
currentX += width + this.minSpacing; // Move past this platform plus spacing
|
|
172
|
+
|
|
173
|
+
} else {
|
|
174
|
+
// Generate obstacle with random dimensions
|
|
175
|
+
const width = LEVEL_CONFIG.OBSTACLE_WIDTH_RANGE.min +
|
|
176
|
+
Math.random() * (LEVEL_CONFIG.OBSTACLE_WIDTH_RANGE.max - LEVEL_CONFIG.OBSTACLE_WIDTH_RANGE.min);
|
|
177
|
+
const height = this.obstacleHeightRange.min +
|
|
178
|
+
Math.random() * (this.obstacleHeightRange.max - this.obstacleHeightRange.min);
|
|
179
|
+
|
|
180
|
+
elements.push({
|
|
181
|
+
x: currentX + width / 2, // Center position
|
|
182
|
+
width: width,
|
|
183
|
+
height: height,
|
|
184
|
+
type: 'obstacle'
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
currentX += width + this.minSpacing; // Move past this obstacle plus spacing
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Alternate between platform and obstacle
|
|
191
|
+
isPlatform = !isPlatform;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return elements;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private createElementActor(element: PositionedElement): ENGINE.Actor {
|
|
198
|
+
const geometry = new THREE.BoxGeometry(element.width, element.height, LEVEL_CONFIG.DEPTH);
|
|
199
|
+
const material = new THREE.MeshStandardMaterial({ color: element.type === 'platform' ? COLORS.PLATFORM : COLORS.OBSTACLE });
|
|
200
|
+
|
|
201
|
+
const meshComponent = new ENGINE.MeshComponent({
|
|
202
|
+
geometry: geometry,
|
|
203
|
+
material: material,
|
|
204
|
+
position: new THREE.Vector3(element.x, element.height / 2, 0), // Position at ground level
|
|
205
|
+
physicsOptions: {
|
|
206
|
+
enabled: true,
|
|
207
|
+
motionType: ENGINE.PhysicsMotionType.Static,
|
|
208
|
+
collisionProfile: ENGINE.DefaultCollisionProfile.BlockAll,
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Enable shadow casting and receiving
|
|
213
|
+
meshComponent.castShadow = true;
|
|
214
|
+
meshComponent.receiveShadow = true;
|
|
215
|
+
|
|
216
|
+
return new ENGINE.Actor({
|
|
217
|
+
rootComponent: meshComponent,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private createChunkDebugVisualization(startX: number): ENGINE.Actor {
|
|
222
|
+
// Create a wireframe box to visualize chunk boundaries
|
|
223
|
+
const chunkGeometry = new THREE.BoxGeometry(
|
|
224
|
+
this.chunkSize,
|
|
225
|
+
LEVEL_CONFIG.DEBUG_VISUALIZATION_HEIGHT,
|
|
226
|
+
LEVEL_CONFIG.DEPTH
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
const chunkMaterial = new THREE.MeshBasicMaterial({
|
|
230
|
+
color: LEVEL_CONFIG.DEBUG_VISUALIZATION_COLOR,
|
|
231
|
+
opacity: LEVEL_CONFIG.DEBUG_VISUALIZATION_OPACITY,
|
|
232
|
+
wireframe: true,
|
|
233
|
+
transparent: true,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const chunkBoundary = new ENGINE.Actor({
|
|
237
|
+
rootComponent: new ENGINE.MeshComponent({
|
|
238
|
+
geometry: chunkGeometry,
|
|
239
|
+
material: chunkMaterial,
|
|
240
|
+
position: new THREE.Vector3(
|
|
241
|
+
startX + this.chunkSize / 2,
|
|
242
|
+
LEVEL_CONFIG.DEBUG_VISUALIZATION_HEIGHT / 2,
|
|
243
|
+
0
|
|
244
|
+
),
|
|
245
|
+
physicsOptions: {
|
|
246
|
+
enabled: false,
|
|
247
|
+
},
|
|
248
|
+
}),
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
return chunkBoundary;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private createGroundMeshActor(startX: number): ENGINE.Actor {
|
|
255
|
+
// Create a ground mesh that spans the entire chunk width
|
|
256
|
+
const groundGeometry = new THREE.BoxGeometry(
|
|
257
|
+
this.chunkSize,
|
|
258
|
+
LEVEL_CONFIG.GROUND_HEIGHT,
|
|
259
|
+
LEVEL_CONFIG.DEPTH
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
const groundMaterial = new THREE.MeshStandardMaterial({
|
|
263
|
+
color: COLORS.GROUND,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const groundMeshComponent = new ENGINE.MeshComponent({
|
|
267
|
+
geometry: groundGeometry,
|
|
268
|
+
material: groundMaterial,
|
|
269
|
+
position: new THREE.Vector3(
|
|
270
|
+
startX + this.chunkSize / 2,
|
|
271
|
+
LEVEL_CONFIG.GROUND_Y_POSITION,
|
|
272
|
+
0
|
|
273
|
+
),
|
|
274
|
+
physicsOptions: {
|
|
275
|
+
enabled: true,
|
|
276
|
+
motionType: ENGINE.PhysicsMotionType.Static,
|
|
277
|
+
collisionProfile: ENGINE.DefaultCollisionProfile.BlockAll,
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// Enable shadow receiving
|
|
282
|
+
groundMeshComponent.receiveShadow = true;
|
|
283
|
+
|
|
284
|
+
return new ENGINE.Actor({
|
|
285
|
+
rootComponent: groundMeshComponent,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Gets detailed geometry information at a given x location.
|
|
291
|
+
* @param x - The x coordinate to query
|
|
292
|
+
* @returns An array of geometry info objects at the given x location
|
|
293
|
+
*/
|
|
294
|
+
public getGeometryInfoAtX(x: number): Array<{
|
|
295
|
+
type: 'ground' | 'platform' | 'obstacle';
|
|
296
|
+
topY: number;
|
|
297
|
+
bottomY: number;
|
|
298
|
+
width: number;
|
|
299
|
+
centerX: number;
|
|
300
|
+
}> {
|
|
301
|
+
const chunkIndex = this.getChunkIndex(x);
|
|
302
|
+
const chunk = this.levelChunks.get(chunkIndex);
|
|
303
|
+
|
|
304
|
+
if (!chunk) {
|
|
305
|
+
return [];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const geometries: Array<{
|
|
309
|
+
type: 'ground' | 'platform' | 'obstacle';
|
|
310
|
+
topY: number;
|
|
311
|
+
bottomY: number;
|
|
312
|
+
width: number;
|
|
313
|
+
centerX: number;
|
|
314
|
+
}> = [];
|
|
315
|
+
|
|
316
|
+
// Add ground mesh if enabled
|
|
317
|
+
if (this.createGroundMesh) {
|
|
318
|
+
const groundCenterY = LEVEL_CONFIG.GROUND_Y_POSITION;
|
|
319
|
+
const groundHalfHeight = LEVEL_CONFIG.GROUND_HEIGHT / 2;
|
|
320
|
+
geometries.push({
|
|
321
|
+
type: 'ground',
|
|
322
|
+
topY: groundCenterY + groundHalfHeight,
|
|
323
|
+
bottomY: groundCenterY - groundHalfHeight,
|
|
324
|
+
width: this.chunkSize,
|
|
325
|
+
centerX: chunk.startX + this.chunkSize / 2
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Add positioned elements (platforms and obstacles)
|
|
330
|
+
const elements = this.generatePositionedElements(chunk.startX);
|
|
331
|
+
|
|
332
|
+
for (const element of elements) {
|
|
333
|
+
const elementLeft = element.x - element.width / 2;
|
|
334
|
+
const elementRight = element.x + element.width / 2;
|
|
335
|
+
|
|
336
|
+
// Check if the x coordinate falls within this element's bounds
|
|
337
|
+
if (x >= elementLeft && x <= elementRight) {
|
|
338
|
+
geometries.push({
|
|
339
|
+
type: element.type,
|
|
340
|
+
topY: element.height,
|
|
341
|
+
bottomY: 0, // Elements are positioned at ground level (y=0 bottom)
|
|
342
|
+
width: element.width,
|
|
343
|
+
centerX: element.x
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Sort by top height (highest first)
|
|
349
|
+
geometries.sort((a, b) => b.topY - a.topY);
|
|
350
|
+
|
|
351
|
+
return geometries;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
public cleanup(): void {
|
|
355
|
+
for (const index of this.levelChunks.keys()) {
|
|
356
|
+
this.removeChunk(index);
|
|
357
|
+
}
|
|
358
|
+
this.levelChunks.clear();
|
|
359
|
+
console.log('[LevelGenerator] Cleaned up');
|
|
360
|
+
}
|
|
361
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import * as ENGINE from 'genesys.js';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
|
|
4
|
+
import { CAMERA_SETTINGS, PLAYER_MOVEMENT } from './const.js';
|
|
5
|
+
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// PLAYER PAWN
|
|
8
|
+
// ============================================================================
|
|
9
|
+
|
|
10
|
+
// The side-scroller player pawn class using animated character.
|
|
11
|
+
@ENGINE.GameClass()
|
|
12
|
+
export class SideScrollerPlayer extends ENGINE.ThirdPersonCharacterPawn {
|
|
13
|
+
camera: THREE.PerspectiveCamera;
|
|
14
|
+
characterMesh: ENGINE.GLTFMeshComponent | null = null;
|
|
15
|
+
smoothedCameraHeight: number | null = null;
|
|
16
|
+
smoothedLookAtHeight: number | null = null;
|
|
17
|
+
|
|
18
|
+
constructor(options: Omit<ENGINE.ThirdPersonCharacterPawnOptions, 'movementComponent' | 'camera' | 'rootComponent'>) {
|
|
19
|
+
// Create directional movement component for side-scrolling
|
|
20
|
+
const movementComponent = new ENGINE.DirectionalCharacterMovementComponent({
|
|
21
|
+
...ENGINE.DirectionalCharacterMovementComponent.DEFAULT_OPTIONS,
|
|
22
|
+
direction: ENGINE.CharacterMovementDirection.LeftRight, // Move left and right
|
|
23
|
+
autoMove: false, // Player controls movement manually
|
|
24
|
+
jumpSpeed: PLAYER_MOVEMENT.JUMP_SPEED,
|
|
25
|
+
maxSpeed: PLAYER_MOVEMENT.MAX_SPEED,
|
|
26
|
+
maxMidAirJumps: PLAYER_MOVEMENT.MAX_MID_AIR_JUMPS, // Allow double jump
|
|
27
|
+
// Physics and movement settings for platformer feel
|
|
28
|
+
accelerationLambda: PLAYER_MOVEMENT.ACCELERATION_LAMBDA,
|
|
29
|
+
decelerationLambda: PLAYER_MOVEMENT.DECELERATION_LAMBDA,
|
|
30
|
+
midAirAccelerationLambda: PLAYER_MOVEMENT.MID_AIR_ACCELERATION_LAMBDA,
|
|
31
|
+
midAirDecelerationLambda: PLAYER_MOVEMENT.MID_AIR_DECELERATION_LAMBDA,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Physics options for the player
|
|
35
|
+
const physicsOptions: ENGINE.ComponentPhysicsOptions = {
|
|
36
|
+
enabled: true,
|
|
37
|
+
motionType: ENGINE.PhysicsMotionType.KinematicVelocityBased,
|
|
38
|
+
collisionProfile: ENGINE.DefaultCollisionProfile.Character,
|
|
39
|
+
generateCollisionEvents: true,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Create root component with physics (invisible capsule for collision)
|
|
43
|
+
const rootComponent = new ENGINE.MeshComponent({
|
|
44
|
+
geometry: new THREE.CapsuleGeometry(
|
|
45
|
+
ENGINE.CHARACTER_WIDTH / 2,
|
|
46
|
+
ENGINE.CHARACTER_HEIGHT - ENGINE.CHARACTER_WIDTH
|
|
47
|
+
),
|
|
48
|
+
material: new THREE.MeshStandardMaterial({
|
|
49
|
+
color: 0xffff00,
|
|
50
|
+
visible: false, // Make invisible since we'll have the animated character mesh
|
|
51
|
+
transparent: true,
|
|
52
|
+
opacity: 0.5
|
|
53
|
+
}),
|
|
54
|
+
physicsOptions,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
super({
|
|
58
|
+
...options,
|
|
59
|
+
// Use default character model from engine assets
|
|
60
|
+
modelUrl: '@engine/assets/character/mannequinG.glb',
|
|
61
|
+
configUrl: '@engine/assets/character/config/mannequin-anim.json',
|
|
62
|
+
meshPosition: new THREE.Vector3(0, -ENGINE.CHARACTER_HEIGHT / 2, 0),
|
|
63
|
+
meshRotation: new THREE.Euler(0, Math.PI, 0), // Face right initially
|
|
64
|
+
meshScale: new THREE.Vector3(1, 1, 1),
|
|
65
|
+
movementComponent,
|
|
66
|
+
camera: null, // Do not attach the camera to the player directly
|
|
67
|
+
rootComponent,
|
|
68
|
+
enableDirectionalLightFollowing: true,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Enable shadow casting for the character
|
|
72
|
+
this.rootComponent.castShadow = true;
|
|
73
|
+
this.camera = new THREE.PerspectiveCamera(CAMERA_SETTINGS.FOV, 1, CAMERA_SETTINGS.NEAR, CAMERA_SETTINGS.FAR);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public override async doBeginPlay(): Promise<void> {
|
|
77
|
+
await super.doBeginPlay();
|
|
78
|
+
this.getWorld()?.setOverrideCamera(this.camera);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public override tickPrePhysics(deltaTime: number): void {
|
|
82
|
+
super.tickPrePhysics(deltaTime);
|
|
83
|
+
|
|
84
|
+
// manually update the camera position
|
|
85
|
+
const desiredCameraPosition = this.rootComponent.localToWorld(new THREE.Vector3(
|
|
86
|
+
CAMERA_SETTINGS.POSITION.x,
|
|
87
|
+
CAMERA_SETTINGS.POSITION.y,
|
|
88
|
+
CAMERA_SETTINGS.POSITION.z
|
|
89
|
+
));
|
|
90
|
+
const desiredCameraLookAt = this.rootComponent.localToWorld(new THREE.Vector3(
|
|
91
|
+
CAMERA_SETTINGS.LOOK_AT.x,
|
|
92
|
+
CAMERA_SETTINGS.LOOK_AT.y,
|
|
93
|
+
CAMERA_SETTINGS.LOOK_AT.z
|
|
94
|
+
));
|
|
95
|
+
this.smoothedCameraHeight ??= desiredCameraPosition.y;
|
|
96
|
+
this.smoothedLookAtHeight ??= desiredCameraLookAt.y;
|
|
97
|
+
|
|
98
|
+
this.smoothedCameraHeight = THREE.MathUtils.damp(this.smoothedCameraHeight, desiredCameraPosition.y, CAMERA_SETTINGS.DAMP_FACTOR, deltaTime);
|
|
99
|
+
this.smoothedLookAtHeight = THREE.MathUtils.damp(this.smoothedLookAtHeight, desiredCameraLookAt.y, CAMERA_SETTINGS.DAMP_FACTOR, deltaTime);
|
|
100
|
+
|
|
101
|
+
this.camera.position.x = desiredCameraPosition.x;
|
|
102
|
+
this.camera.position.y = this.smoothedCameraHeight;
|
|
103
|
+
this.camera.position.z = desiredCameraPosition.z;
|
|
104
|
+
|
|
105
|
+
this.camera.lookAt(new THREE.Vector3(
|
|
106
|
+
desiredCameraLookAt.x,
|
|
107
|
+
this.smoothedLookAtHeight,
|
|
108
|
+
desiredCameraLookAt.z
|
|
109
|
+
));
|
|
110
|
+
|
|
111
|
+
// change the direction of the character mesh based on the movement direction
|
|
112
|
+
this.characterMesh ??= this.getComponent(ENGINE.GLTFMeshComponent);
|
|
113
|
+
if (this.characterMesh && this.movementComponent) {
|
|
114
|
+
const movementComponent = this.movementComponent as ENGINE.DirectionalCharacterMovementComponent;
|
|
115
|
+
const velocity = movementComponent.getVelocities();
|
|
116
|
+
if (velocity.right > 0.1) {
|
|
117
|
+
// Moving right - face right (90 degrees)
|
|
118
|
+
this.characterMesh.rotation.set(0, Math.PI / 2, 0);
|
|
119
|
+
} else if (velocity.right < -0.1) {
|
|
120
|
+
// Moving left - face left (-90 degrees)
|
|
121
|
+
this.characterMesh.rotation.set(0, -Math.PI / 2, 0);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|