Skip to content

Commit

Permalink
upgrade(NativeCommandManager) possibility to customize speed
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinMachado authored and mathieuLivebardon committed Jul 13, 2023
1 parent 36ca08b commit dc1c73f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 97 deletions.
2 changes: 1 addition & 1 deletion examples/AvatarGame.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
.ID_SCRIPT
],
variables: {
speedRotate: 0.0005
defaultSpeedRotate: 0.0005
}
},
ExternalScript: {
Expand Down
2 changes: 1 addition & 1 deletion examples/DragAndDropAvatar.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
],
variables: {
idRenderDataAvatar: 'avatar', // render data your avatar should use
speedRotate: 0.0005
defaultSpeedRotate: 0.0005
}
},
ExternalScript: {
Expand Down
2 changes: 1 addition & 1 deletion examples/ZeppelinGame.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
.ID_SCRIPT
],
variables: {
speedRotate: 0.0005
defaultSpeedRotate: 0.0005
}
},
ExternalScript: {
Expand Down
2 changes: 1 addition & 1 deletion examples/assets/js/ShowRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ const ShowRoom = class {
],
variables: {
idRenderDataAvatar: idRenderDataAvatar,
speedRotate: 0.0005,
defaultSpeedRotate: 0.0005,
},
},
ExternalScript: {
Expand Down
33 changes: 33 additions & 0 deletions packages/shared/src/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,37 @@ function computeFileFormat(filename) {
return filename.slice(indexLastPoint + 1);
}

/**
* Check if the element is alreeady included in the array if not push it
*
* @param {Array} array - array where to push the element
* @param {*} element - element to push
* @returns {boolean} true if pushed false otherwise
*/
function arrayPushOnce(array, element) {
if (!array.includes(element)) {
array.push(element);
return true;
}
return false;
}

/**
* Remove an element if it's present in an array
*
* @param {Array} array - array to remove element from
* @param {*} element - element to remove
* @returns {boolean} true if removed false otherwise
*/
function removeFromArray(array, element) {
const index = array.indexOf(element);
if (index) {
array.splice(index, 1);
return true;
}
return false;
}

module.exports = {
PartialString: PartialString,
StringComposer: StringComposer,
Expand All @@ -605,4 +636,6 @@ module.exports = {
objectParse: objectParse,
objectParseNumeric: objectParseNumeric,
computeFileFormat: computeFileFormat,
arrayPushOnce: arrayPushOnce,
removeFromArray: removeFromArray,
};
170 changes: 77 additions & 93 deletions packages/shared/src/Game/ScriptTemplate/NativeCommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const Data = require('../../Data');
const defaultVariables = {
angleMin: Math.PI / 5,
angleMax: 2 * Math.PI - Math.PI / 10,
speedTranslate: 0.04,
speedRotate: 0.00001,
defaultSpeedTranslate: 0.04,
defaultSpeedRotate: 0.00001,
};

/**
Expand Down Expand Up @@ -53,35 +53,41 @@ const NativeCommandManager = class extends ScriptBase {
externalScriptComponent = updatedObject3D.getComponent(Component.TYPE);
}

let indexObjectMoving = -1;

switch (type) {
case Constants.COMMAND.MOVE_FORWARD:
NativeCommandManager.moveForward(
updatedObject3D,
this.variables.speedTranslate * this.context.dt,
this.computeObjectSpeedTranslate(updatedObject3D) * this.context.dt,
this.map,
data.withMap
);
break;
case Constants.COMMAND.MOVE_BACKWARD:
NativeCommandManager.moveBackward(
updatedObject3D,
this.variables.speedTranslate * this.context.dt,
this.computeObjectSpeedTranslate(updatedObject3D) * this.context.dt,
this.map,
data.withMap
);
break;
case Constants.COMMAND.ROTATE_LEFT:
NativeCommandManager.rotate(
updatedObject3D,
new THREE.Vector3(0, 0, this.variables.speedRotate * this.context.dt)
new THREE.Vector3(
0,
0,
this.computeObjectSpeedRotate(updatedObject3D) * this.context.dt
)
);
break;
case Constants.COMMAND.ROTATE_RIGHT:
NativeCommandManager.rotate(
updatedObject3D,
new THREE.Vector3(0, 0, -this.variables.speedRotate * this.context.dt)
new THREE.Vector3(
0,
0,
-this.computeObjectSpeedRotate(updatedObject3D) * this.context.dt
)
);
break;
case Constants.COMMAND.UPDATE_TRANSFORM:
Expand Down Expand Up @@ -121,117 +127,77 @@ const NativeCommandManager = class extends ScriptBase {
externalScriptComponent.getModel().variables[data.variableName] =
data.variableValue;
updatedObject3D.setOutdated(true);
// console.log(
// 'update ',
// data.nameVariable,
// ' set with ',
// data.variableValue
// );
}
break;
case Constants.COMMAND.MOVE_FORWARD_START:
if (
!this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START].includes(
updatedObject3D
)
) {
this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START].push(
updatedObject3D
);
}
Data.arrayPushOnce(
this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_FORWARD_END:
indexObjectMoving =
this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START].indexOf(
updatedObject3D
);
if (indexObjectMoving >= 0)
this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START].splice(
indexObjectMoving,
1
);
Data.removeFromArray(
this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_BACKWARD_START:
if (
!this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START].includes(
updatedObject3D
)
) {
this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START].push(
updatedObject3D
);
}
Data.arrayPushOnce(
this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_BACKWARD_END:
indexObjectMoving =
this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START].indexOf(
updatedObject3D
);
if (indexObjectMoving >= 0)
this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START].splice(
indexObjectMoving,
1
);
Data.removeFromArray(
this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_LEFT_START:
if (
!this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START].includes(
updatedObject3D
)
) {
this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START].push(
updatedObject3D
);
}
Data.arrayPushOnce(
this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_LEFT_END:
indexObjectMoving =
this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START].indexOf(
updatedObject3D
);
if (indexObjectMoving >= 0)
this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START].splice(
indexObjectMoving,
1
);
Data.removeFromArray(
this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_RIGHT_START:
if (
!this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START].includes(
updatedObject3D
)
) {
this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START].push(
updatedObject3D
);
}
Data.arrayPushOnce(
this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START],
updatedObject3D
);
break;
case Constants.COMMAND.MOVE_RIGHT_END:
indexObjectMoving =
this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START].indexOf(
updatedObject3D
);
if (indexObjectMoving >= 0)
this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START].splice(
indexObjectMoving,
1
);
Data.removeFromArray(
this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START],
updatedObject3D
);
break;
case Constants.COMMAND.ROTATE:
if (data.vector) {
if (!isNaN(data.vector.x)) {
updatedObject3D.rotateX(
data.vector.x * this.context.dt * this.variables.speedRotate
data.vector.x *
this.context.dt *
this.computeObjectSpeedRotate(updatedObject3D)
);
}
if (!isNaN(data.vector.y)) {
updatedObject3D.rotateY(
data.vector.y * this.context.dt * this.variables.speedRotate
data.vector.y *
this.context.dt *
this.computeObjectSpeedRotate(updatedObject3D)
);
}
if (!isNaN(data.vector.z)) {
updatedObject3D.rotateZ(
data.vector.z * this.context.dt * this.variables.speedRotate
data.vector.z *
this.context.dt *
this.computeObjectSpeedRotate(updatedObject3D)
);
}
this.clampRotation(updatedObject3D);
Expand All @@ -249,33 +215,51 @@ const NativeCommandManager = class extends ScriptBase {
}
}

/**
* If you want to have different translation speed for gameobject you should override this method
*
* @returns {number} speed of the translation
*/
computeObjectSpeedTranslate() {
return this.variables.defaultSpeedTranslate;
}

/**
* If you want to have different rotation speed for gameobject you should override this method
*
* @returns {number} speed of the rotation
*/
computeObjectSpeedRotate() {
return this.variables.defaultSpeedRotate;
}

tick() {
// move objectsMoving
this.objectsMoving[Constants.COMMAND.MOVE_FORWARD_START].forEach((o) => {
NativeCommandManager.moveForward(
o,
this.variables.speedTranslate * this.context.dt,
this.computeObjectSpeedTranslate(o) * this.context.dt,
this.map
);
});
this.objectsMoving[Constants.COMMAND.MOVE_BACKWARD_START].forEach((o) => {
NativeCommandManager.moveBackward(
o,
this.variables.speedTranslate * this.context.dt,
this.computeObjectSpeedTranslate(o) * this.context.dt,
this.map
);
});
this.objectsMoving[Constants.COMMAND.MOVE_LEFT_START].forEach((o) => {
NativeCommandManager.moveLeft(
o,
this.variables.speedTranslate * this.context.dt,
this.computeObjectSpeedTranslate(o) * this.context.dt,
this.map
);
});
this.objectsMoving[Constants.COMMAND.MOVE_RIGHT_START].forEach((o) => {
NativeCommandManager.moveRight(
o,
this.variables.speedTranslate * this.context.dt,
this.computeObjectSpeedTranslate(o) * this.context.dt,
this.map
);
});
Expand Down

0 comments on commit dc1c73f

Please sign in to comment.