mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 15:09:24 +02:00
Merge pull request #5275 from howard-stearns/sword-button-away-hit-points
Add a button to create/toggle-away sword.
This commit is contained in:
commit
afdd4c862c
1 changed files with 117 additions and 26 deletions
|
@ -11,7 +11,7 @@
|
||||||
//
|
//
|
||||||
"use strict";
|
"use strict";
|
||||||
/*jslint vars: true*/
|
/*jslint vars: true*/
|
||||||
var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity.
|
var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print; // Referenced globals provided by High Fidelity.
|
||||||
|
|
||||||
var hand = "right";
|
var hand = "right";
|
||||||
var nullActionID = "00000000-0000-0000-0000-000000000000";
|
var nullActionID = "00000000-0000-0000-0000-000000000000";
|
||||||
|
@ -20,39 +20,86 @@ var controllerActive;
|
||||||
var stickID = null;
|
var stickID = null;
|
||||||
var actionID = nullActionID;
|
var actionID = nullActionID;
|
||||||
var dimensions = { x: 0.3, y: 0.1, z: 2.0 };
|
var dimensions = { x: 0.3, y: 0.1, z: 2.0 };
|
||||||
|
var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0);
|
||||||
|
|
||||||
var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx";
|
var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx";
|
||||||
var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx";
|
var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx";
|
||||||
var whichModel = "sword";
|
var whichModel = "sword";
|
||||||
|
var rezButton = Overlays.addOverlay("image", {
|
||||||
|
x: 100,
|
||||||
|
y: 380,
|
||||||
|
width: 32,
|
||||||
|
height: 32,
|
||||||
|
imageURL: "http://s3.amazonaws.com/hifi-public/images/delete.png",
|
||||||
|
color: {
|
||||||
|
red: 255,
|
||||||
|
green: 255,
|
||||||
|
blue: 255
|
||||||
|
},
|
||||||
|
alpha: 1
|
||||||
|
});
|
||||||
|
|
||||||
// sometimes if this is run immediately the stick doesn't get created? use a timer.
|
var health = 100;
|
||||||
Script.setTimeout(function () {
|
var display;
|
||||||
stickID = Entities.addEntity({
|
var isAway = false;
|
||||||
type: "Model",
|
function updateDisplay() {
|
||||||
modelURL: (whichModel === "sword") ? swordModel : stickModel,
|
var text = health.toString();
|
||||||
//compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
|
if (!display) {
|
||||||
shapeType: "box",
|
health = 100;
|
||||||
dimensions: dimensions,
|
display = Overlays.addOverlay("text", {
|
||||||
position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close
|
text: text,
|
||||||
rotation: MyAvatar.orientation,
|
font: { size: 20 },
|
||||||
damping: 0.1,
|
color: {red: 0, green: 255, blue: 0},
|
||||||
collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav",
|
backgroundColor: {red: 100, green: 100, blue: 100}, // Why doesn't this and the next work?
|
||||||
restitution: 0.01,
|
backgroundAlpha: 0.9,
|
||||||
collisionsWillMove: true
|
x: Window.innerWidth - 50,
|
||||||
});
|
y: 50
|
||||||
actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2},
|
});
|
||||||
hand: hand,
|
} else {
|
||||||
timeScale: 0.15});
|
Overlays.editOverlay(display, {text: text});
|
||||||
}, 3000);
|
}
|
||||||
|
}
|
||||||
|
function removeDisplay() {
|
||||||
function cleanUp() {
|
if (display) {
|
||||||
Entities.deleteEntity(stickID);
|
Overlays.deleteOverlay(display);
|
||||||
|
display = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanUp() {
|
||||||
|
if (stickID) {
|
||||||
|
Entities.deleteAction(stickID, actionID);
|
||||||
|
Entities.deleteEntity(stickID);
|
||||||
|
stickID = null;
|
||||||
|
actionID = null;
|
||||||
|
}
|
||||||
|
removeDisplay();
|
||||||
|
Overlays.deleteOverlay(rezButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeEnergy(collision, entityID) {
|
||||||
|
var id = entityID || collision.idA || collision.idB;
|
||||||
|
var entity = id && Entities.getEntityProperties(id);
|
||||||
|
var mass = entity ? (entity.density * entity.dimensions.x * entity.dimensions.y * entity.dimensions.z) : 1;
|
||||||
|
var linearVelocityChange = Vec3.length(collision.velocityChange);
|
||||||
|
var energy = 0.5 * mass * linearVelocityChange * linearVelocityChange;
|
||||||
|
return Math.min(Math.max(1.0, Math.round(energy)), 20);
|
||||||
|
}
|
||||||
|
function gotHit(collision) {
|
||||||
|
if (isAway) { return; }
|
||||||
|
var energy = computeEnergy(collision);
|
||||||
|
health -= energy;
|
||||||
|
updateDisplay();
|
||||||
|
}
|
||||||
|
function scoreHit(idA, idB, collision) {
|
||||||
|
if (isAway) { return; }
|
||||||
|
var energy = computeEnergy(collision, idA);
|
||||||
|
health += energy;
|
||||||
|
updateDisplay();
|
||||||
|
}
|
||||||
|
|
||||||
function positionStick(stickOrientation) {
|
function positionStick(stickOrientation) {
|
||||||
var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2};
|
var baseOffset = {x: 0.3, y: 0.0, z: -dimensions.z / 2}; // FIXME: don't move yourself by colliding with your own capsule. Fudge of 0.3 in x.
|
||||||
var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset);
|
var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset);
|
||||||
Entities.updateAction(stickID, actionID, {relativePosition: offset,
|
Entities.updateAction(stickID, actionID, {relativePosition: offset,
|
||||||
relativeRotation: stickOrientation});
|
relativeRotation: stickOrientation});
|
||||||
|
@ -60,7 +107,7 @@ function positionStick(stickOrientation) {
|
||||||
|
|
||||||
|
|
||||||
function mouseMoveEvent(event) {
|
function mouseMoveEvent(event) {
|
||||||
if (!stickID || actionID === nullActionID) {
|
if (!stickID || actionID === nullActionID || isAway) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var windowCenterX = Window.innerWidth / 2;
|
var windowCenterX = Window.innerWidth / 2;
|
||||||
|
@ -98,7 +145,51 @@ function update() {
|
||||||
positionStick(stickOrientation);
|
positionStick(stickOrientation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toggleAway() {
|
||||||
|
isAway = !isAway;
|
||||||
|
if (isAway) {
|
||||||
|
positionStick(AWAY_ORIENTATION);
|
||||||
|
removeDisplay();
|
||||||
|
} else {
|
||||||
|
updateDisplay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onClick(event) {
|
||||||
|
switch (Overlays.getOverlayAtPoint({x: event.x, y: event.y})) {
|
||||||
|
case rezButton:
|
||||||
|
if (!stickID) {
|
||||||
|
stickID = Entities.addEntity({
|
||||||
|
type: "Model",
|
||||||
|
modelURL: (whichModel === "sword") ? swordModel : stickModel,
|
||||||
|
//compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
|
||||||
|
shapeType: "box",
|
||||||
|
dimensions: dimensions,
|
||||||
|
position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close
|
||||||
|
rotation: MyAvatar.orientation,
|
||||||
|
damping: 0.1,
|
||||||
|
collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav",
|
||||||
|
restitution: 0.01,
|
||||||
|
collisionsWillMove: true
|
||||||
|
});
|
||||||
|
actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2},
|
||||||
|
hand: hand,
|
||||||
|
timeScale: 0.15});
|
||||||
|
if (actionID === nullActionID) {
|
||||||
|
print('*** FAILED TO MAKE SWORD ACTION ***');
|
||||||
|
cleanUp();
|
||||||
|
}
|
||||||
|
Script.addEventHandler(stickID, 'collisionWithEntity', scoreHit);
|
||||||
|
updateDisplay();
|
||||||
|
} else {
|
||||||
|
toggleAway();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Script.scriptEnding.connect(cleanUp);
|
Script.scriptEnding.connect(cleanUp);
|
||||||
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
||||||
|
Controller.mousePressEvent.connect(onClick);
|
||||||
Script.update.connect(update);
|
Script.update.connect(update);
|
||||||
|
MyAvatar.collisionWithEntity.connect(gotHit);
|
||||||
|
|
Loading…
Reference in a new issue