478 lines
12 KiB
JavaScript
Raw Normal View History

2015-11-10 17:25:54 -08:00
//
2015-11-12 16:20:17 -08:00
// pistol.js
2015-11-10 17:25:54 -08:00
// examples
//
2015-11-12 16:20:17 -08:00
// Created by Eric Levin on 11/12/2015
2015-11-10 17:25:54 -08:00
// Copyright 2013 High Fidelity, Inc.
//
// This is an example script that turns the hydra controllers and mouse into a entity gun.
2015-11-12 16:20:17 -08:00
// It reads the controller, watches for trigger pulls, and adds a force to any entity it hits
2015-11-10 17:25:54 -08:00
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
Script.include("../../../libraries/utils.js");
Script.include("../../../libraries/constants.js");
2015-11-10 17:25:54 -08:00
2015-11-23 15:22:34 -08:00
var GUN_FORCE =20;
2015-11-17 12:55:06 -08:00
Messages.sendMessage('Hifi-Hand-Disabler', "both");
2015-11-17 12:55:06 -08:00
2015-11-16 15:46:22 -08:00
var gameName = "Kill All The Rats!"
// var HOST = "localhost:5000"
var HOST = "desolate-bastion-1742.herokuapp.com";
var socketClient = new WebSocket("ws://" + HOST);
var username = GlobalServices.username;
var currentScore = 0;
function score() {
currentScore++;
socketClient.send(JSON.stringify({
username: username,
2015-11-16 15:46:22 -08:00
score: currentScore,
gameName: gameName
}))
}
2015-11-10 17:25:54 -08:00
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
2015-11-11 13:30:46 -08:00
var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw");
2015-11-13 14:00:03 -08:00
var LASER_LENGTH = 100;
2015-11-10 17:25:54 -08:00
var LASER_WIDTH = 2;
2015-11-11 13:30:46 -08:00
var POSE_CONTROLS = [Controller.Standard.LeftHand, Controller.Standard.RightHand];
var TRIGGER_CONTROLS = [Controller.Standard.LT, Controller.Standard.RT];
2015-11-10 17:25:54 -08:00
var MIN_THROWER_DELAY = 1000;
var MAX_THROWER_DELAY = 1000;
var RELOAD_INTERVAL = 5;
var GUN_MODEL = HIFI_PUBLIC_BUCKET + "cozza13/gun/m1911-handgun+1.fbx?v=4";
var BULLET_VELOCITY = 10.0;
2015-11-11 13:30:46 -08:00
var GUN_OFFSETS = [{
2015-11-17 15:12:52 -08:00
x: 0.04,
2015-11-10 17:25:54 -08:00
y: 0.26,
z: 0.04
}, {
x: 0.04,
y: 0.26,
z: 0.04
2015-11-11 13:30:46 -08:00
}];
2015-11-10 17:25:54 -08:00
2015-11-11 13:30:46 -08:00
var GUN_ORIENTATIONS = [Quat.fromPitchYawRollDegrees(0, 90, 90), Quat.fromPitchYawRollDegrees(0, -90, 270)];
2015-11-10 17:25:54 -08:00
2015-11-17 12:55:06 -08:00
//x -> y
//y -> z
// z -> x
2015-11-17 15:12:52 -08:00
var BARREL_OFFSETS = [ {
x: -0.12,
2015-11-10 17:25:54 -08:00
y: 0.12,
z: 0.04
}, {
2015-11-17 15:12:52 -08:00
x: 0.12,
2015-11-10 17:25:54 -08:00
y: 0.12,
z: 0.04
2015-11-17 15:12:52 -08:00
} ];
2015-11-10 17:25:54 -08:00
2015-11-11 11:01:34 -08:00
2015-11-13 10:55:09 -08:00
var pointers = [];
pointers.push(Overlays.addOverlay("line3d", {
start: ZERO_VECTOR,
end: ZERO_VECTOR,
color: COLORS.RED,
alpha: 1,
visible: true,
lineWidth: LASER_WIDTH
}));
pointers.push(Overlays.addOverlay("line3d", {
start: ZERO_VECTOR,
end: ZERO_VECTOR,
color: COLORS.RED,
alpha: 1,
visible: true,
lineWidth: LASER_WIDTH
}));
2015-11-10 17:25:54 -08:00
var mapping = Controller.newMapping();
2015-11-11 13:30:46 -08:00
var validPoses = [false, false];
var barrelVectors = [0, 0];
var barrelTips = [0, 0];
2015-11-10 17:25:54 -08:00
2015-11-11 14:50:16 -08:00
2015-11-13 11:15:44 -08:00
// If enabled, anything can be shot, otherwise, an entity needs to have "isShootable" set in its userData
2015-11-12 10:28:51 -08:00
var shootAnything = true;
2015-11-10 17:25:54 -08:00
function update(deltaTime) {
// FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2]
2015-11-11 13:30:46 -08:00
var tipPoses = [MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose];
2015-11-10 17:25:54 -08:00
for (var side = 0; side < 2; side++) {
// First check if the controller is valid
var controllerPose = Controller.getPoseValue(POSE_CONTROLS[side]);
validPoses[side] = controllerPose.valid;
// Need to adjust the laser
var tipPose = tipPoses[side];
var handRotation = tipPoses[side].rotation;
var barrelOffset = Vec3.multiplyQbyV(handRotation, BARREL_OFFSETS[side]);
barrelTips[side] = Vec3.sum(tipPose.translation, barrelOffset);
barrelVectors[side] = Vec3.multiplyQbyV(handRotation, {
x: 0,
y: 1,
z: 0
});
2015-11-13 10:55:09 -08:00
var laserTip = Vec3.sum(Vec3.multiply(LASER_LENGTH, barrelVectors[side]), barrelTips[side]);
// Update Lasers
Overlays.editOverlay(pointers[side], {
start: barrelTips[side],
end: laserTip,
alpha: 1,
});
2015-11-10 17:25:54 -08:00
}
}
2015-11-13 10:55:09 -08:00
function displayPointer(side) {
Overlays.editOverlay(pointers[side], {
visible: true
});
}
function hidePointer(side) {
Overlays.editOverlay(pointers[side], {
visible: false
});
}
function fire(side, value) {
2015-11-13 11:15:44 -08:00
if (value == 0) {
2015-11-13 10:55:09 -08:00
return;
2015-11-12 16:20:17 -08:00
}
2015-11-13 10:55:09 -08:00
Audio.playSound(fireSound, {
position: barrelTips[side],
2015-11-17 16:08:10 -08:00
volume: 0.5
2015-11-13 10:55:09 -08:00
});
var shotDirection = Vec3.normalize(barrelVectors[side]);
var pickRay = {
origin: barrelTips[side],
direction: shotDirection
};
createMuzzleFlash(barrelTips[side]);
2015-11-17 10:41:07 -08:00
var intersection = Entities.findRayIntersectionBlocking(pickRay, true);
2015-11-13 10:55:09 -08:00
if (intersection.intersects) {
2015-11-13 11:15:44 -08:00
Script.setTimeout(function() {
2015-11-17 09:48:34 -08:00
createEntityHitEffect(intersection.intersection);
if (shootAnything && intersection.properties.dynamic === 1) {
2016-01-14 09:38:18 -08:00
// Any dynamic entity can be shot
2015-11-13 11:15:44 -08:00
Entities.editEntity(intersection.entityID, {
velocity: Vec3.multiply(shotDirection, GUN_FORCE)
});
}
if (intersection.properties.name === "rat") {
score();
createBloodSplatter(intersection.intersection);
2015-11-17 12:55:06 -08:00
Entities.deleteEntity(intersection.entityID);
2015-11-13 17:08:17 -08:00
2015-11-13 11:15:44 -08:00
}
2015-11-13 14:00:03 -08:00
//Attempt to call entity method's shot method
var forceDirection = JSON.stringify({
forceDirection: shotDirection
});
Entities.callEntityMethod(intersection.entityID, 'onShot', [forceDirection]);
2015-11-17 12:55:06 -08:00
}, 0);
2015-11-13 11:15:44 -08:00
2015-11-10 17:25:54 -08:00
}
}
2015-11-11 13:30:46 -08:00
2015-11-10 17:25:54 -08:00
function scriptEnding() {
Messages.sendMessage('Hifi-Hand-Disabler', 'none');
2015-11-10 17:25:54 -08:00
mapping.disable();
2015-11-13 10:55:09 -08:00
for (var i = 0; i < pointers.length; ++i) {
Overlays.deleteOverlay(pointers[i]);
}
2015-11-10 17:25:54 -08:00
MyAvatar.detachOne(GUN_MODEL);
MyAvatar.detachOne(GUN_MODEL);
clearPose();
}
MyAvatar.attach(GUN_MODEL, "LeftHand", GUN_OFFSETS[0], GUN_ORIENTATIONS[0], 0.40);
MyAvatar.attach(GUN_MODEL, "RightHand", GUN_OFFSETS[1], GUN_ORIENTATIONS[1], 0.40);
2015-11-13 10:55:09 -08:00
function showPointer(side) {
Overlays.editOverlay(pointers[side], {
visible: true
});
}
2015-11-17 15:12:52 -08:00
2015-11-13 10:55:09 -08:00
mapping.from(Controller.Standard.LT).hysteresis(0.0, 0.5).to(function(value) {
fire(0, value);
2015-11-10 17:25:54 -08:00
});
2015-11-13 10:55:09 -08:00
mapping.from(Controller.Standard.RT).hysteresis(0.0, 0.5).to(function(value) {
fire(1, value);
2015-11-10 17:25:54 -08:00
});
mapping.enable();
Script.scriptEnding.connect(scriptEnding);
2015-11-11 13:30:46 -08:00
Script.update.connect(update);
2015-11-13 11:15:44 -08:00
function createEntityHitEffect(position) {
2015-11-11 13:30:46 -08:00
var flash = Entities.addEntity({
type: "ParticleEffect",
position: position,
2015-11-11 14:20:58 -08:00
lifetime: 4,
2015-11-11 14:28:30 -08:00
"name": "Flash Emitter",
2015-11-11 14:20:58 -08:00
"color": {
red: 228,
green: 128,
blue: 12
},
2015-11-11 13:30:46 -08:00
"maxParticles": 1000,
2015-11-12 10:28:51 -08:00
"lifespan": 0.15,
2015-11-11 14:20:58 -08:00
"emitRate": 1000,
"emitSpeed": 1,
2015-11-11 13:30:46 -08:00
"speedSpread": 0,
"emitOrientation": {
"x": -0.4,
"y": 1,
"z": -0.2,
"w": 0.7071068286895752
},
"emitDimensions": {
"x": 0,
"y": 0,
"z": 0
},
"polarStart": 0,
2015-11-11 14:20:58 -08:00
"polarFinish": Math.PI,
2015-11-11 13:30:46 -08:00
"azimuthStart": -3.1415927410125732,
"azimuthFinish": 2,
"emitAcceleration": {
"x": 0,
"y": 0,
"z": 0
},
"accelerationSpread": {
"x": 0,
"y": 0,
"z": 0
},
2015-11-12 10:28:51 -08:00
"particleRadius": 0.03,
"radiusSpread": 0.02,
2015-11-11 15:48:06 -08:00
"radiusStart": 0.02,
2015-11-12 10:28:51 -08:00
"radiusFinish": 0.03,
2015-11-11 14:20:58 -08:00
"colorSpread": {
red: 100,
green: 100,
blue: 20
},
2015-11-11 13:30:46 -08:00
"alpha": 1,
"alphaSpread": 0,
2015-11-11 14:20:58 -08:00
"alphaStart": 0,
"alphaFinish": 0,
"additiveBlending": true,
"textures": "http://ericrius1.github.io/PartiArt/assets/star.png"
});
Script.setTimeout(function() {
Entities.editEntity(flash, {
isEmitting: false
});
}, 100);
}
function createBloodSplatter(position) {
var splatter = Entities.addEntity({
type: "ParticleEffect",
position: position,
lifetime: 4,
2015-11-18 16:04:31 -08:00
"name": "Blood Splatter",
"color": {
red: 230,
green: 2,
blue: 30
},
"maxParticles": 1000,
"lifespan": 0.3,
"emitRate": 1000,
"emitSpeed": 0.5,
"speedSpread": 0,
"emitOrientation": {
"x": -0.4,
"y": 1,
"z": -0.2,
"w": 0.7071068286895752
},
"emitDimensions": {
"x": 0,
"y": 0,
"z": 0
},
"polarStart": 0,
"polarFinish": Math.PI,
"azimuthStart": -3.1415927410125732,
"azimuthFinish": 2,
"emitAcceleration": {
"x": 0,
"y": -5,
"z": 0
},
"accelerationSpread": {
"x": 0,
"y": 0,
"z": 0
},
"particleRadius": 0.05,
"radiusSpread": 0.03,
"radiusStart": 0.05,
"radiusFinish": 0.05,
"colorSpread": {
red: 40,
green: 0,
blue: 30
},
"alpha": 1,
"alphaSpread": 0,
"alphaStart": 0,
"alphaFinish": 0,
"textures": "http://ericrius1.github.io/PartiArt/assets/star.png"
});
Script.setTimeout(function() {
Entities.editEntity(splatter, {
isEmitting: false
});
}, 100)
}
2015-11-11 14:20:58 -08:00
function createMuzzleFlash(position) {
var smoke = Entities.addEntity({
type: "ParticleEffect",
position: position,
2015-11-11 14:28:30 -08:00
lifetime: 1,
2015-11-11 14:20:58 -08:00
"name": "Smoke Hit Emitter",
"maxParticles": 1000,
"lifespan": 4,
"emitRate": 20,
emitSpeed: 0,
"speedSpread": 0,
"emitDimensions": {
"x": 0,
"y": 0,
"z": 0
},
"polarStart": 0,
"polarFinish": 0,
"azimuthStart": -3.1415927410125732,
"azimuthFinish": 3.14,
"emitAcceleration": {
"x": 0,
"y": 0.5,
"z": 0
},
"accelerationSpread": {
"x": .2,
"y": 0,
"z": .2
},
2015-11-11 14:28:30 -08:00
"radiusSpread": .04,
"particleRadius": 0.07,
"radiusStart": 0.07,
"radiusFinish": 0.07,
"alpha": 0.7,
2015-11-11 14:20:58 -08:00
"alphaSpread": 0,
"alphaStart": 0,
"alphaFinish": 0,
2015-11-11 13:30:46 -08:00
"additiveBlending": 0,
"textures": "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png"
});
2015-11-11 14:20:58 -08:00
Script.setTimeout(function() {
Entities.editEntity(smoke, {
isEmitting: false
2015-11-13 14:00:03 -08:00
});
2015-11-11 14:20:58 -08:00
}, 100);
var flash = Entities.addEntity({
type: "ParticleEffect",
position: position,
lifetime: 4,
"name": "Muzzle Flash",
2015-11-11 14:20:58 -08:00
"color": {
red: 228,
green: 128,
blue: 12
},
"maxParticles": 1000,
"lifespan": 0.1,
"emitRate": 1000,
2015-11-11 14:28:30 -08:00
"emitSpeed": 0.5,
2015-11-11 14:20:58 -08:00
"speedSpread": 0,
"emitOrientation": {
"x": -0.4,
"y": 1,
"z": -0.2,
"w": 0.7071068286895752
},
"emitDimensions": {
"x": 0,
"y": 0,
"z": 0
},
"polarStart": 0,
"polarFinish": Math.PI,
"azimuthStart": -3.1415927410125732,
"azimuthFinish": 2,
"emitAcceleration": {
"x": 0,
"y": 0,
"z": 0
},
"accelerationSpread": {
"x": 0,
"y": 0,
"z": 0
},
2015-11-11 14:28:30 -08:00
"particleRadius": 0.05,
"radiusSpread": 0.01,
"radiusStart": 0.05,
"radiusFinish": 0.05,
2015-11-11 14:20:58 -08:00
"colorSpread": {
red: 100,
green: 100,
blue: 20
},
"alpha": 1,
"alphaSpread": 0,
"alphaStart": 0,
"alphaFinish": 0,
"additiveBlending": true,
"textures": "http://ericrius1.github.io/PartiArt/assets/star.png"
});
Script.setTimeout(function() {
Entities.editEntity(flash, {
isEmitting: false
});
}, 100)
2015-11-11 13:30:46 -08:00
2015-11-16 23:06:45 -08:00
}