update bow to use parenting for prenotch string

This commit is contained in:
James B. Pollack 2016-01-25 12:36:03 -08:00
parent 78911ae9ca
commit 9be2a73d4a
2 changed files with 113 additions and 95 deletions

View file

@ -115,12 +115,15 @@
this.shootArrowSound = SoundCache.getSound(SHOOT_ARROW_SOUND_URL);
this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL);
this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL);
var userData = Entities.getEntityProperties(this.entityID).userData;
this.userData = JSON.parse(userData);
print('THIS USERDATA::: ' + userData)
this.preNotchString = this.userData.bowKey.preNotchString;
print('PRE NOTCH STRING:::' + this.preNotchString)
},
unload: function() {
this.deleteStrings();
Entities.deleteEntity(this.preNotchString);
Entities.deleteEntity(this.arrow);
},
@ -174,18 +177,6 @@
this.bowProperties = Entities.getEntityProperties(this.entityID);
//create a string across the bow when we pick it up
if (this.preNotchString === null) {
this.createPreNotchString();
}
if (this.preNotchString !== null && this.aiming === false) {
// print('DRAW PRE NOTCH STRING')
this.drawPreNotchStrings();
}
// create the notch detector that arrows will look for
if (this.aiming === true) {
Entities.editEntity(this.preNotchString, {
visible: false
@ -213,11 +204,9 @@
var data = getEntityCustomData('grabbableKey', this.entityID, {});
data.grabbable = true;
setEntityCustomData('grabbableKey', this.entityID, data);
Entities.deleteEntity(this.preNotchString);
Entities.deleteEntity(this.arrow);
this.aiming = false;
this.hasArrowNotched = false;
this.preNotchString = null;
}
},
@ -334,10 +323,6 @@
var bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset);
this.bottomStringPosition = Vec3.sum(bottomStringPosition, backOffset);
Entities.editEntity(this.preNotchString, {
position: this.topStringPosition
});
Entities.editEntity(this.topString, {
position: this.topStringPosition
});
@ -381,50 +366,6 @@
return [topVector, bottomVector];
},
createPreNotchString: function() {
this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]);
var stringProperties = {
type: 'Line',
position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET),
dimensions: LINE_DIMENSIONS,
visible: true,
dynamic: false,
collisionless: true,
userData: JSON.stringify({
grabbableKey: {
grabbable: false
}
})
};
this.preNotchString = Entities.addEntity(stringProperties);
},
drawPreNotchStrings: function() {
this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]);
this.updateStringPositions();
var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation));
var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET * 2);
Entities.editEntity(this.preNotchString, {
name: 'Hifi-Pre-Notch-String',
linePoints: [{
x: 0,
y: 0,
z: 0
}, Vec3.sum({
x: 0,
y: 0,
z: 0
}, downOffset)],
lineWidth: 5,
color: this.stringData.currentColor,
});
},
checkStringHand: function() {
//invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with
var triggerLookup;
@ -613,4 +554,4 @@
};
return new Bow();
});
});

View file

@ -10,6 +10,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var utilsPath = Script.resolvePath('../../libraries/utils.js')
Script.include(utilsPath)
var SCRIPT_URL = Script.resolvePath('bow.js');
@ -33,39 +35,114 @@ var center = Vec3.sum(Vec3.sum(MyAvatar.position, {
z: 0
}), Vec3.multiply(1, Quat.getFront(Camera.getOrientation())));
var bow = Entities.addEntity({
name: 'Hifi-Bow',
type: "Model",
modelURL: MODEL_URL,
position: center,
dimensions: BOW_DIMENSIONS,
dynamic: true,
gravity: BOW_GRAVITY,
shapeType: 'compound',
compoundShapeURL: COLLISION_HULL_URL,
script: SCRIPT_URL,
userData: JSON.stringify({
grabbableKey: {
invertSolidWhileHeld: true,
spatialKey: {
leftRelativePosition: {
x: -0.02,
y: 0.08,
z: 0.09
},
relativePosition: {
x: 0.02,
y: 0.08,
z: 0.09
},
relativeRotation: Quat.fromPitchYawRollDegrees(0, 90, -90)
var TOP_NOTCH_OFFSET = 0.6;
var BOTTOM_NOTCH_OFFSET = 0.6;
var LINE_DIMENSIONS = {
x: 5,
y: 5,
z: 5
};
var bow;
function makeBow() {
var bowProperties = {
name: 'Hifi-Bow',
type: "Model",
modelURL: MODEL_URL,
position: center,
dimensions: BOW_DIMENSIONS,
dynamic: true,
gravity: BOW_GRAVITY,
shapeType: 'compound',
compoundShapeURL: COLLISION_HULL_URL,
script: SCRIPT_URL,
userData: JSON.stringify({
grabbableKey: {
invertSolidWhileHeld: true,
spatialKey: {
leftRelativePosition: {
x: -0.02,
y: 0.08,
z: 0.09
},
relativePosition: {
x: 0.02,
y: 0.08,
z: 0.09
},
relativeRotation: Quat.fromPitchYawRollDegrees(0, 90, -90)
}
}
}
})
});
})
}
bow = Entities.addEntity(bowProperties);
createPreNotchString();
}
var preNotchString;
function createPreNotchString() {
var bowProperties = Entities.getEntityProperties(bow, ["position", "rotation", "userData"]);
var downVector = Vec3.multiply(-1, Quat.getUp(bowProperties.rotation));
var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET * 2);
var upVector = Quat.getUp(bowProperties.rotation);
var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET);
var backOffset = Vec3.multiply(-0.1, Quat.getFront(bowProperties.rotation));
var topStringPosition = Vec3.sum(bowProperties.position, upOffset);
topStringPosition = Vec3.sum(topStringPosition, backOffset);
var stringProperties = {
name: 'Hifi-Bow-Pre-Notch-String',
type: 'Line',
position: topStringPosition,
linePoints: [{
x: 0,
y: 0,
z: 0
}, Vec3.sum({
x: 0,
y: 0,
z: 0
}, downOffset)],
lineWidth: 5,
color: {
red: 255,
green: 255,
blue: 255
},
dimensions: LINE_DIMENSIONS,
visible: true,
dynamic: false,
collisionless: true,
parentID: bow,
userData: JSON.stringify({
grabbableKey: {
grabbable: false
}
})
};
preNotchString = Entities.addEntity(stringProperties);
var data = {
preNotchString: preNotchString
};
setEntityCustomData('bowKey', bow, data);
}
makeBow();
function cleanup() {
Entities.deleteEntity(bow);
Entities.deleteEntity(preNotchString);
}
Script.scriptEnding.connect(cleanup);
Script.scriptEnding.connect(cleanup);