Skip to content

Commit

Permalink
Add prettier and format multiscene example
Browse files Browse the repository at this point in the history
  • Loading branch information
NSTCG committed May 7, 2024
1 parent 48e4da1 commit 4d9b8b3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 55 deletions.
22 changes: 11 additions & 11 deletions multiscene/index-scene2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {SwitchScene} from './js/switch.js';

export default function registerSceneComponents(engine) {
/* wle:auto-register:start */
engine.registerComponent(Cursor);
engine.registerComponent(HandTracking);
engine.registerComponent(HowlerAudioListener);
engine.registerComponent(MouseLookComponent);
engine.registerComponent(PlayerHeight);
engine.registerComponent(TeleportComponent);
engine.registerComponent(VrModeActiveSwitch);
engine.registerComponent(WasdControlsComponent);
engine.registerComponent(Portal);
engine.registerComponent(SwitchScene);
/* wle:auto-register:end */
engine.registerComponent(Cursor);
engine.registerComponent(HandTracking);
engine.registerComponent(HowlerAudioListener);
engine.registerComponent(MouseLookComponent);
engine.registerComponent(PlayerHeight);
engine.registerComponent(TeleportComponent);
engine.registerComponent(VrModeActiveSwitch);
engine.registerComponent(WasdControlsComponent);
engine.registerComponent(Portal);
engine.registerComponent(SwitchScene);
/* wle:auto-register:end */
}
70 changes: 33 additions & 37 deletions multiscene/js/portal.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
import {Component, Property} from "@wonderlandengine/api";
import { SwitchScene } from './switch';
import { vec3 } from 'gl-matrix';
import {Component, Property} from '@wonderlandengine/api';
import {SwitchScene} from './switch';
import {vec3} from 'gl-matrix';

/**
* switch-scene
*/
export class Portal extends Component {
static TypeName = "portal";
/* Properties that are configurable in the editor */
static Properties = {
target: Property.object(),
translationDuration: Property.float(0.5),
initialTranslate: Property.float(15),
portalRotateSpeed: Property.float(1.0),
};
static TypeName = 'portal';
/* Properties that are configurable in the editor */
static Properties = {
target: Property.object(),
translationDuration: Property.float(0.5),
initialTranslate: Property.float(15),
portalRotateSpeed: Property.float(1.0),
};

onActivate() {
this.target.translateObject([0, 0, this.initialTranslate]);
}
onActivate() {
this.target.translateObject([0, 0, this.initialTranslate]);
}

update(dt) {
this.object.rotateAxisAngleDegObject(
[0, 0, 1],
dt * 10000 * this.portalRotateSpeed,
);
const playerPosition = this.target.getPositionWorld([]);
const portalPosition = this.object.getPositionWorld([]);
const sqDist= vec3.squaredDistance(playerPosition, portalPosition)
if (sqDist > 0.1) {
const distanceRemaining = Math.abs(
portalPosition[0] - playerPosition[0],
);
const alpha =
1 -
Math.exp(-dt / (this.translationDuration * distanceRemaining));
const newPosition= new Float32Array(3)
vec3.lerp(newPosition,playerPosition,portalPosition,alpha,)
this.target.setPositionWorld(newPosition);
}
else{
this.object.getComponent(SwitchScene).switch();
}
}
update(dt) {
this.object.rotateAxisAngleDegObject(
[0, 0, 1],
dt * 10000 * this.portalRotateSpeed
);
const playerPosition = this.target.getPositionWorld([]);
const portalPosition = this.object.getPositionWorld([]);
const sqDist = vec3.squaredDistance(playerPosition, portalPosition);
if (sqDist > 0.1) {
const distanceRemaining = Math.abs(portalPosition[0] - playerPosition[0]);
const alpha =
1 - Math.exp(-dt / (this.translationDuration * distanceRemaining));
const newPosition = new Float32Array(3);
vec3.lerp(newPosition, playerPosition, portalPosition, alpha);
this.target.setPositionWorld(newPosition);
} else {
this.object.getComponent(SwitchScene).switch();
}
}
}
2 changes: 1 addition & 1 deletion multiscene/js/scenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const Scenes = {
/** Main scene, loaded first. */
main: null,
/** Magical scene, reached via the portal */
second: null
second: null,
};
10 changes: 5 additions & 5 deletions multiscene/js/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {Scenes} from './scenes.js';

/** Component to switch between the main and second scene. */
export class SwitchScene extends Component {
static TypeName = 'switch-scene';
static TypeName = 'switch-scene';

switch() {
const nextScene = this.scene === Scenes.main ? Scenes.second : Scenes.main;
this.engine.switchTo(nextScene);
}
switch() {
const nextScene = this.scene === Scenes.main ? Scenes.second : Scenes.main;
this.engine.switchTo(nextScene);
}
}
37 changes: 37 additions & 0 deletions multiscene/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion multiscene/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "module",
"module": "js/index.js",
"scripts": {
"build": "echo \"The 'build' script is run by the editor and should produce your application bundle\""
"build": "echo \"The 'build' script is run by the editor and should produce your application bundle\"",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,css,md}\""
},
"keywords": [
"wonderland-engine"
Expand All @@ -16,5 +17,9 @@
"@wonderlandengine/components": "^1.1.4",
"@wonderlandengine/spatial-audio": "^1.1.2",
"gl-matrix": "^3.4.3"
},
"devDependencies": {
"@wonderlandengine/prettier-config": "^1.0.0",
"prettier": "^3.2.5"
}
}

0 comments on commit 4d9b8b3

Please sign in to comment.