rat moves back with force when shot

This commit is contained in:
ericrius1 2015-11-11 11:01:34 -08:00
parent 40a15a15f3
commit 667464b493
3 changed files with 72 additions and 130 deletions

View file

@ -20,7 +20,6 @@
Script.include("../libraries/utils.js");
Script.include("../libraries/constants.js");
Script.include("../libraries/toolBars.js");
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
@ -54,6 +53,7 @@ var BARREL_OFFSETS = [ {
z: 0.04
} ];
var mapping = Controller.newMapping();
var validPoses = [ false, false ];
var barrelVectors = [ 0, 0 ];
@ -78,111 +78,6 @@ pointer.push(Overlays.addOverlay("line3d", {
lineWidth: LASER_WIDTH
}));
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
// Load some sound to use for loading and firing
var fireSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/GUN-SHOT2.raw");
var loadSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/Gun_Reload_Weapon22.raw");
var impactSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/BulletImpact2.raw");
var targetHitSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Space%20Invaders/hit.raw");
var targetLaunchSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Space%20Invaders/shoot.raw");
var audioOptions = {
volume: 0.9
}
var shotsFired = 0;
var shotTime = new Date();
var isLaunchButtonPressed = false;
var score = 0;
var bulletID = false;
var targetID = false;
// Create overlay buttons and reticle
var BUTTON_SIZE = 32;
var PADDING = 3;
var NUM_BUTTONS = 3;
var screenSize = Controller.getViewportDimensions();
var startX = screenSize.x / 2 - (NUM_BUTTONS * (BUTTON_SIZE + PADDING)) / 2;
var toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.gun.toolbar", function(screenSize) {
return {
x: startX,
y: (screenSize.y - (BUTTON_SIZE + PADDING)),
};
});
function shootBullet(position, velocity, grenade) {
var BULLET_SIZE = .09;
var BULLET_LIFETIME = 10.0;
var BULLET_GRAVITY = -0.25;
var GRENADE_VELOCITY = 15.0;
var GRENADE_SIZE = 0.35;
var GRENADE_GRAVITY = -9.8;
var bVelocity = grenade ? Vec3.multiply(GRENADE_VELOCITY, Vec3.normalize(velocity)) : velocity;
var bSize = grenade ? GRENADE_SIZE : BULLET_SIZE;
var bGravity = grenade ? GRENADE_GRAVITY : BULLET_GRAVITY;
bulletID = Entities.addEntity({
type: "Sphere",
position: position,
dimensions: {
x: bSize,
y: bSize,
z: bSize
},
color: {
red: 0,
green: 0,
blue: 0
},
velocity: bVelocity,
lifetime: BULLET_LIFETIME,
gravity: {
x: 0,
y: bGravity,
z: 0
},
damping: 0.01,
density: 8000,
ignoreCollisions: false,
collisionsWillMove: true
});
Script.addEventHandler(bulletID, "collisionWithEntity", entityCollisionWithEntity);
// Play firing sounds
audioOptions.position = position;
Audio.playSound(fireSound, audioOptions);
shotsFired++;
if ((shotsFired % RELOAD_INTERVAL) == 0) {
Audio.playSound(loadSound, audioOptions);
}
}
function keyPressEvent(event) {
// if our tools are off, then don't do anything
if (event.text == "t") {
var time = MIN_THROWER_DELAY + Math.random() * MAX_THROWER_DELAY;
Script.setTimeout(shootTarget, time);
} else if ((event.text == ".") || (event.text == "SPACE")) {
shootFromMouse(false);
} else if (event.text == ",") {
shootFromMouse(true);
} else if (event.text == "r") {
playLoadSound();
}
}
function playLoadSound() {
audioOptions.position = MyAvatar.leftHandPose.translation;
Audio.playSound(loadSound, audioOptions);
}
function update(deltaTime) {
// FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2]
@ -224,30 +119,24 @@ function update(deltaTime) {
function triggerChanged(side, value) {
var pressed = (value != 0);
if (pressed) {
var position = barrelTips[side];
var velocity = Vec3.multiply(BULLET_VELOCITY, Vec3.normalize(barrelVectors[side]));
shootBullet(position, velocity, false);
var pickRay = {
origin: barrelTips[side],
direction: Vec3.normalize(barrelVectors[side])
}
var intersection = Entities.findRayIntersection(pickRay, true);
if (intersection.intersects) {
if (intersection.properties.name === "rat") {
print("HOLY SHIT YOU JUST HIT A RAT!");
var forceDirection = JSON.stringify({forceDirection: barrelVectors[side]});
Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]);
}
}
}
}
function mousePressEvent(event) {
var clickedOverlay = Overlays.getOverlayAtPoint({
x: event.x,
y: event.y
});
if (clickedOverlay == offButton) {
Script.stop();
} else if (clickedOverlay == platformButton) {
var platformSize = 3;
makePlatform(-9.8, 1.0, platformSize);
} else if (clickedOverlay == gridButton) {
makeGrid("Box", 1.0, 3);
}
}
function scriptEnding() {
mapping.disable();
toolBar.cleanup();
for (var i = 0; i < pointer.length; ++i) {
Overlays.deleteOverlay(pointer[i]);
}
@ -259,9 +148,6 @@ function scriptEnding() {
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);
// Give a bit of time to load before playing sound
Script.setTimeout(playLoadSound, 2000);
mapping.from(Controller.Standard.LT).hysteresis(0.1, 0.5).to(function(value) {
triggerChanged(0, value);
});
@ -272,6 +158,4 @@ mapping.from(Controller.Standard.RT).hysteresis(0.1, 0.5).to(function(value) {
mapping.enable();
Script.scriptEnding.connect(scriptEnding);
Script.update.connect(update);
Controller.mousePressEvent.connect(mousePressEvent);
Controller.keyPressEvent.connect(keyPressEvent);
Script.update.connect(update);

41
examples/weapons/rat.js Normal file
View file

@ -0,0 +1,41 @@
//
// Rat.js
// examples/toybox/entityScripts
//
// Created by Eric Levin on11/11/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
/*global Rat, print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */
(function() {
var _this;
Rat = function() {
_this = this;
};
Rat.prototype = {
onHit: function(myId, params) {
print("OOWWW YOU SHOT ME!");
var force = JSON.parse(params[0]);
// print(JSON.stringify(force))
Entities.editEntity(this.entityID, {
velocity: force.forceDirection
});
},
preload: function(entityID) {
print("HEY HEY I'm a rat!");
this.entityID = entityID;
},
};
// entity scripts always need to return a newly constructed object of our type
return new Rat();
});

View file

@ -0,0 +1,17 @@
var scriptURL = Script.resolvePath('rat.js');
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation())));
var rat = Entities.addEntity({
type: "Box",
name: 'rat',
position: center,
dimensions: {x: 0.4, y: 0.4, z: 0.4},
color: {red: 200, green: 20, blue: 200},
collisionsWillMove: true,
script: scriptURL
});
function cleanup() {
Entities.deleteEntity(rat);
}
Script.scriptEnding.connect(cleanup)