diff --git a/examples/gun.js b/examples/gun.js index 45b7487f25..29b60a94ad 100644 --- a/examples/gun.js +++ b/examples/gun.js @@ -19,11 +19,14 @@ var pitchFromMouse = 0; var isMouseDown = false; var BULLET_VELOCITY = 5.0; +var LEFT_BUTTON_3 = 3; // Load some sound to use for loading and firing var fireSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guns/GUN-SHOT2.raw"); var loadSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guns/Gun_Reload_Weapon22.raw"); var impactSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guns/BulletImpact2.raw"); +var targetLaunchSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guns/GUN-SHOT2.raw"); + var audioOptions = new AudioInjectionOptions(); audioOptions.volume = 0.9; @@ -34,6 +37,10 @@ for (t = 0; t < numberOfTriggers; t++) { triggerPulled[t] = false; } +var isLaunchButtonPressed = false; + +var score = 0; + // Create a reticle image in center of screen var screenSize = Controller.getViewportDimensions(); var reticle = Overlays.addOverlay("image", { @@ -46,14 +53,32 @@ var reticle = Overlays.addOverlay("image", { alpha: 1 }); +var text = Overlays.addOverlay("text", { + x: screenSize.x / 2 - 100, + y: screenSize.y / 2 - 50, + width: 150, + height: 50, + color: { red: 0, green: 0, blue: 0}, + textColor: { red: 255, green: 0, blue: 0}, + topMargin: 4, + leftMargin: 4, + text: "Score: " + score + }); + + +function printVector(string, vector) { + print(string + " " + vector.x + ", " + vector.y + ", " + vector.z); +} + function shootBullet(position, velocity) { var BULLET_SIZE = 0.02; + var BULLET_GRAVITY = -0.02; Particles.addParticle( { position: position, radius: BULLET_SIZE, color: { red: 200, green: 0, blue: 0 }, velocity: velocity, - gravity: { x: 0, y: -0.1, z: 0 }, + gravity: { x: 0, y: BULLET_GRAVITY, z: 0 }, damping: 0 }); // Play firing sounds @@ -61,8 +86,41 @@ function shootBullet(position, velocity) { Audio.playSound(fireSound, audioOptions); } +function shootTarget() { + var TARGET_SIZE = 0.25; + var TARGET_GRAVITY = -0.6; + var TARGET_UP_VELOCITY = 3.0; + var TARGET_FWD_VELOCITY = 5.0; + var DISTANCE_TO_LAUNCH_FROM = 3.0; + var camera = Camera.getPosition(); + //printVector("camera", camera); + var forwardVector = Quat.getFront(Camera.getOrientation()); + //printVector("forwardVector", forwardVector); + var newPosition = Vec3.sum(camera, Vec3.multiply(forwardVector, DISTANCE_TO_LAUNCH_FROM)); + //printVector("newPosition", newPosition); + var velocity = Vec3.multiply(forwardVector, TARGET_FWD_VELOCITY); + velocity.y += TARGET_UP_VELOCITY; + //printVector("velocity", velocity); + + Particles.addParticle( + { position: newPosition, + radius: TARGET_SIZE, + color: { red: 0, green: 200, blue: 200 }, + velocity: velocity, + gravity: { x: 0, y: TARGET_GRAVITY, z: 0 }, + lifetime: 1000.0, + damping: 0.99 }); + + // Play target shoot sound + audioOptions.position = newPosition; + Audio.playSound(targetLaunchSound, audioOptions); +} + + + function particleCollisionWithVoxel(particle, voxel, penetration) { Vec3.print('particleCollisionWithVoxel() ... penetration=', penetration); + var HOLE_SIZE = 0.125; var particleProperties = Particles.getParticleProperties(particle); var position = particleProperties.position; @@ -74,8 +132,24 @@ function particleCollisionWithVoxel(particle, voxel, penetration) { Audio.playSound(impactSound, audioOptions); } +function particleCollisionWithParticle(particle1, particle2) { + print("Particle/Particle!"); + score++; + Overlays.editOverlay(text, { text: "Score: " + score } ); + Particles.deleteParticle(particle1); + Particles.deleteParticle(particle2); +} + +function keyPressEvent(event) { + // if our tools are off, then don't do anything + if (event.text == "t") { + shootTarget(); + } +} + function update(deltaTime) { + // Check for mouseLook movement, update rotation // rotate body yaw for yaw received from mouse var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3Radians( { x: 0, y: yawFromMouse, z: 0 } )); @@ -87,6 +161,15 @@ function update(deltaTime) { MyAvatar.headPitch = newPitch; pitchFromMouse = 0; + // Check hydra controller for launch button press + if (!isLaunchButtonPressed && Controller.isButtonPressed(LEFT_BUTTON_3)) { + isLaunchButtonPressed = true; + shootTarget(); + } else if (isLaunchButtonPressed && !Controller.isButtonPressed(LEFT_BUTTON_3)) { + isLaunchButtonPressed = false; + + } + // Check hydra controller for trigger press var numberOfTriggers = Controller.getNumberOfTriggers(); @@ -175,14 +258,17 @@ function mouseMoveEvent(event) { function scriptEnding() { Overlays.deleteOverlay(reticle); + Overlays.deleteOverlay(text); } Particles.particleCollisionWithVoxel.connect(particleCollisionWithVoxel); +Particles.particleCollisionWithParticle.connect(particleCollisionWithParticle); Script.scriptEnding.connect(scriptEnding); Script.update.connect(update); Controller.mousePressEvent.connect(mousePressEvent); Controller.mouseReleaseEvent.connect(mouseReleaseEvent); Controller.mouseMoveEvent.connect(mouseMoveEvent); +Controller.keyPressEvent.connect(keyPressEvent); diff --git a/interface/src/GLCanvas.cpp b/interface/src/GLCanvas.cpp index ea04002ddb..504a07d3ed 100644 --- a/interface/src/GLCanvas.cpp +++ b/interface/src/GLCanvas.cpp @@ -13,9 +13,11 @@ #include #include +const int MSECS_PER_FRAME_WHEN_THROTTLED = 66; + GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer)), _throttleRendering(false), - _idleRenderInterval(64) + _idleRenderInterval(MSECS_PER_FRAME_WHEN_THROTTLED) { }