Skip to content

Commit

Permalink
Merge pull request #256 from VCityTeam/dev-unstable
Browse files Browse the repository at this point in the history
2.33.6
  • Loading branch information
valentinMachado authored Nov 17, 2021
2 parents ce4460b + 04eddef commit c0c7d52
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 70 deletions.
17 changes: 14 additions & 3 deletions examples/LocalGame.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<script src="../dist/debug/udv.js"></script>
<!-- <script src="./assets/js/udv.js"></script> -->
<!-- <script src="../dist/debug/udv.js"></script> -->
<script src="./assets/js/udv.js"></script>
<script type="text/javascript">
const myWorld = new udv.Game.Shared.World({
name: 'My World',
Expand All @@ -36,8 +36,19 @@
},
});

//example of a local module pass to the localscript context
const localModules = {
myCustomModule: {
print() {
console.log('PRINT');
},
},
};

const app = new udv.Templates.LocalGame();
app.start(myWorld, './assets/config/local_game_config.json');
app.start(myWorld, './assets/config/local_game_config.json', {
localScriptModules: localModules,
});
</script>
</body>
</html>
16 changes: 8 additions & 8 deletions examples/assets/js/udv.js

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions examples/assets/localScripts/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,32 @@ module.exports = class Commands {
return new Shared.Command({ type: Shared.Command.TYPE.MOVE_RIGHT });
}
);

const worldComputer = localContext.getGameView().getInterpolator();

//send input manager command to the world at each computer tick
worldComputer.addAfterTickRequester(function () {
worldComputer.onCommands(inputManager.computeCommands());
});

//example of how to access its custom module
const myCustomModule = gameView.getLocalScriptModules()['myCustomModule'];
if (myCustomModule)
inputManager.addKeyInput('p', 'keydown', myCustomModule.print);
}

updateUI(go, localCtx) {
//update ui
this.fpsLabel.innerHTML = 'FPS = ' + Math.round(1000 / localCtx.getDt());
this.fpsLabel.innerHTML =
'View FPS = ' + Math.round(1000 / localCtx.getDt());
}

tick() {
const localContext = arguments[1];
const worldComputer = localContext.getGameView().getStateComputer();
const inputManager = localContext.getGameView().getInputManager();

//send input manager command to the world
worldComputer.addAfterTickRequester(function () {
const cmds = inputManager.computeCommands();
worldComputer.onCommands(cmds);
});

this.updateUI(arguments[0], localContext);
this.updateUI(arguments[0], arguments[1]);
}

update() {
if (!this.conf.worldComputerDt) debugger;
if (!this.conf.worldComputerDt == undefined) debugger;

this.worldDtLabel.innerHTML =
'World FPS = ' + Math.round(1000 / this.conf.worldComputerDt);
Expand Down
4 changes: 2 additions & 2 deletions examples/assets/localScripts/updateElevationGround.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ module.exports = class UpdateElevationGround {
}
}

//notify world with the right elevation
const computer = localContext.getGameView().getStateComputer();
//add commands to the computer directly because not produce by the inputmanager
const computer = localContext.getGameView().getInterpolator();
computer.onCommands([
new Command({ type: Command.TYPE.Z_UPDATE, data: z }),
]);
Expand Down
8 changes: 5 additions & 3 deletions examples/assets/worldScripts/avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ module.exports = class Avatar {
const worldContext = arguments[1];
const dt = worldContext.getDt();
const commands = worldContext.getCommands();
const speedTranslate = 0.0002;
const speedRotate = 0.000006;
const speedTranslate = 0.02;
const speedRotate = 0.0006;
const avatar = this.avatar;

const ls = avatar.computeRoot().getComponent(Shared.LocalScript.TYPE);
Expand All @@ -42,7 +42,7 @@ module.exports = class Avatar {
commands.forEach(function (cmd) {
switch (cmd.getType()) {
case Shared.Command.TYPE.MOVE_FORWARD:
console.log(dt * speedTranslate);
// console.log(dt * speedTranslate);
avatar.move(
avatar.computeForwardVector().setLength(dt * speedTranslate)
);
Expand All @@ -53,9 +53,11 @@ module.exports = class Avatar {
);
break;
case Shared.Command.TYPE.MOVE_LEFT:
// console.log(dt * speedRotate);
avatar.rotate(new Shared.THREE.Vector3(0, 0, speedRotate * dt));
break;
case Shared.Command.TYPE.MOVE_RIGHT:
// console.log(dt * speedRotate);
avatar.rotate(new Shared.THREE.Vector3(0, 0, -speedRotate * dt));
break;
case Shared.Command.TYPE.Z_UPDATE:
Expand Down
4 changes: 2 additions & 2 deletions examples/assets/worldScripts/worldGameManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ module.exports = class WorldGameManager {
const worldContext = arguments[1];
const dt = worldContext.getDt();
const commands = worldContext.getCommands();
const speedTranslate = 0.0005;
const speedRotate = 0.000005;
const speedTranslate = 0.05;
const speedRotate = 0.0005;
const zeppelin = this.zeppelin;

commands.forEach(function (cmd) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ud-viz",
"version": "2.33.5",
"version": "2.33.6",
"description": "A framework extension of itowns.",
"main": "src",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/Game/Components/AssetsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class AssetsManager {
} else {
//if shared an instance already existing is return
//TODO conf is the same for all the audio comp not allowing to have shared and not shared sound in the same comp
//TODO remove shared to well dispose sounds
if (options.shared) {
result = this.soundsBuffer[idSound][0];
if (!result) throw new Error('no sound');
Expand Down
6 changes: 6 additions & 0 deletions src/Game/Shared/GameObject/Components/Audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const AudioModule = class Audio {
return false;
}

dispose() {
for (let key in this.sounds) {
this.sounds[key].pause(); //TODO if shared just pause if not unload
}
}

/**
* Compute this to JSON
* @returns {JSON}
Expand Down
1 change: 1 addition & 0 deletions src/Game/Shared/GameObject/Components/LocalScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ LocalScriptModule.EVENT = {
TICK: 'tick', //every tick
ON_NEW_GAMEOBJECT: 'onNewGameObject', //when a go is added
UPDATE: 'update', //when component need to be updated with newer localScript component
DISPOSE: 'dispose', //world is dispose
};

module.exports = LocalScriptModule;
2 changes: 1 addition & 1 deletion src/Game/Shared/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ const WorldModule = class World {
g.traverse(function (child) {
child.executeWorldScripts(
WorldScriptComponent.EVENT.ON_LEAVE_COLLISION,
[worldContext]
[uuid, worldContext]
);
});
buffer.splice(i, 1); //remove from buffer
Expand Down
8 changes: 0 additions & 8 deletions src/Game/Shared/WorldContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ module.exports = class WorldContext {
return this.commands;
}

/**
*
* @param {Array[Command]} value
*/
setCommands(value) {
this.commands = value;
}

/**
*
* @returns {Library}
Expand Down
17 changes: 9 additions & 8 deletions src/Templates/DistantGame/DistantGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GameView } from '../../Views/Views';
export class DistantGame {
constructor(webSocketService, assetsManager, config) {
this.config = config;
this.stateComputer = new WorldStateInterpolator(
this.interpolator = new WorldStateInterpolator(
config.worldStateInterpolator
);
this.webSocketService = webSocketService;
Expand Down Expand Up @@ -36,14 +36,15 @@ export class DistantGame {
]);
}

reset(userData) {
reset(userData, localScriptModules) {
this.dispose(true);

const gV = new GameView({
assetsManager: this.assetsManager,
stateComputer: this.stateComputer,
interpolator: this.interpolator,
config: this.config,
userData: userData,
localScriptModules: localScriptModules,
});

const ctxGameView = gV.getLocalContext();
Expand All @@ -58,8 +59,8 @@ export class DistantGame {
this.gameView = gV;
}

start(userData = {}) {
this.reset(userData);
start(userData = {}, localScriptModules) {
this.reset(userData, localScriptModules);

const _this = this;

Expand All @@ -74,10 +75,10 @@ export class DistantGame {

if (_this.gameView.getLastState()) {
userData.firstGameView = false;
_this.start(userData);
_this.start(userData, localScriptModules);
}

_this.stateComputer.onFirstState(state);
_this.interpolator.onFirstState(state);
_this.gameView.writeUserData('avatarUUID', json.avatarUUID);
_this.gameView.start(state);
}
Expand All @@ -86,7 +87,7 @@ export class DistantGame {
this.webSocketService.on(
Constants.WEBSOCKET.MSG_TYPES.WORLDSTATE_DIFF,
(diffJSON) => {
_this.stateComputer.onNewDiff(new WorldStateDiff(diffJSON));
_this.interpolator.onNewDiff(new WorldStateDiff(diffJSON));
}
);
}
Expand Down
26 changes: 17 additions & 9 deletions src/Templates/DistantGame/WorldStateInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export class WorldStateInterpolator {
//DEBUG
this.lastTimeState = 0;

//local game
//local game optional (could work with a distant computer via websocket)
this.localComputer = localComputer;
if (localComputer) {
//register itself in the localcomputer
const _this = this;
_this.onFirstState(localComputer.computeCurrentState());
localComputer.addAfterTickRequester(function () {
_this._onNewState(localComputer.computeCurrentState());
_this.onNewState(localComputer.computeCurrentState());
});
}
}
Expand All @@ -46,7 +46,7 @@ export class WorldStateInterpolator {
* Add a new state
* @param {WorldState} state
*/
_onNewState(state) {
onNewState(state) {
if (!state) {
throw new Error('no state');
}
Expand All @@ -62,10 +62,11 @@ export class WorldStateInterpolator {

// Keep only one worldstate before the current server time
const index = this._computeIndexBaseState();

if (index > 0) {
const stateDeleted = this.states.splice(0, index);
for (let index = 0; index < stateDeleted.length; index++) {
const element = stateDeleted[index];
for (let iStateDel = 0; iStateDel < stateDeleted.length; iStateDel++) {
const element = stateDeleted[iStateDel];
if (!element.hasBeenConsumed()) this._notConsumedStates.push(element); //register states not consumed
}
}
Expand Down Expand Up @@ -107,6 +108,10 @@ export class WorldStateInterpolator {

//local computer wrapper methods

getLocalComputer() {
return this.localComputer;
}

getWorldContext() {
return this.localComputer.getWorldContext();
}
Expand All @@ -127,7 +132,7 @@ export class WorldStateInterpolator {
let last = this._getLastStateReceived();
if (!last) throw new Error('no last state');
let newState = last.add(diff);
this._onNewState(newState);
this.onNewState(newState);
}

/**
Expand All @@ -139,15 +144,17 @@ export class WorldStateInterpolator {
this.gameStart = Date.now();
this.states.length = 0;
this.lastTimeState = 0;
this._onNewState(state);
this.onNewState(state);
}

//StateComputer INTERFACE

/**
* wrapper function
* stop localcomputer if one
*/
stop() {}
stop() {
if (this.localComputer) this.localComputer.stop();
}

/**
* Compute the current world state
Expand Down Expand Up @@ -182,6 +189,7 @@ export class WorldStateInterpolator {
return result;
}

//When a view need the current it's called this function
computeCurrentStates() {
const result = this._notConsumedStates;
this._notConsumedStates = [];
Expand Down
3 changes: 2 additions & 1 deletion src/Templates/LocalGame/LocalGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export class LocalGame {
_this.gameView = new Views.GameView({
htmlParent: options.htmlParent || document.body,
assetsManager: assetsManager,
stateComputer: interpolator,
interpolator: interpolator,
config: config,
itownsControls: false,
localScriptModules: options.localScriptModules,
});

//start gameview tick
Expand Down
Loading

0 comments on commit c0c7d52

Please sign in to comment.