From 11e52c8ae034bfc02390ca213a8ba075b93c6451 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Tue, 7 Jul 2015 14:41:03 -0700 Subject: [PATCH] Baseline versions of fight club scripts. --- examples/example/games/make-dummy.js | 70 ++++++++++++++++++ examples/example/games/sword.js | 104 +++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 examples/example/games/make-dummy.js create mode 100644 examples/example/games/sword.js diff --git a/examples/example/games/make-dummy.js b/examples/example/games/make-dummy.js new file mode 100644 index 0000000000..068a8b7f9a --- /dev/null +++ b/examples/example/games/make-dummy.js @@ -0,0 +1,70 @@ +// +// make-dummy.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Makes a boxing-dummy that responds to collisions. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +// +"use strict"; +/*jslint vars: true*/ +var Overlays, Entities, Controller, Script, MyAvatar, Vec3; // Referenced globals provided by High Fidelity. + +var HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; + +var rezButton = Overlays.addOverlay("image", { + x: 100, + y: 350, + width: 32, + height: 32, + imageURL: HIFI_PUBLIC_BUCKET + "images/close.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 +}); + + +function mousePressEvent(event) { + var clickedOverlay = Overlays.getOverlayAtPoint({ + x: event.x, + y: event.y + }); + + if (clickedOverlay === rezButton) { + var boxId; + + var position = Vec3.sum(MyAvatar.position, {x: 1.0, y: 0.4, z: 0.0}); + boxId = Entities.addEntity({ + type: "Box", + name: "dummy", + position: position, + dimensions: {x: 0.3, y: 0.7, z: 0.3}, + gravity: {x: 0.0, y: -3.0, z: 0.0}, + damping: 0.2, + collisionsWillMove: true + }); + + var pointToOffsetFrom = Vec3.sum(position, {x: 0.0, y: 2.0, z: 0.0}); + Entities.addAction("offset", boxId, {pointToOffsetFrom: pointToOffsetFrom, + linearDistance: 2.0, + // linearTimeScale: 0.005 + linearTimeScale: 0.1 + }); + } +} + + +function scriptEnding() { + Overlays.deleteOverlay(rezButton); +} + +Controller.mousePressEvent.connect(mousePressEvent); +Script.scriptEnding.connect(scriptEnding); diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js new file mode 100644 index 0000000000..9592c275f6 --- /dev/null +++ b/examples/example/games/sword.js @@ -0,0 +1,104 @@ +// stick.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Allow avatar to hold a stick +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +"use strict"; +/*jslint vars: true*/ +var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity. + +var hand = "right"; +var nullActionID = "00000000-0000-0000-0000-000000000000"; +var controllerID; +var controllerActive; +var stickID = null; +var actionID = nullActionID; +var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; + +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 whichModel = "sword"; + +// sometimes if this is run immediately the stick doesn't get created? use a timer. +Script.setTimeout(function () { + 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}); +}, 3000); + + +function cleanUp() { + Entities.deleteEntity(stickID); +} + + +function positionStick(stickOrientation) { + var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2}; + var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); + Entities.updateAction(stickID, actionID, {relativePosition: offset, + relativeRotation: stickOrientation}); +} + + +function mouseMoveEvent(event) { + if (!stickID || actionID === nullActionID) { + return; + } + var windowCenterX = Window.innerWidth / 2; + var windowCenterY = Window.innerHeight / 2; + var mouseXCenterOffset = event.x - windowCenterX; + var mouseYCenterOffset = event.y - windowCenterY; + var mouseXRatio = mouseXCenterOffset / windowCenterX; + var mouseYRatio = mouseYCenterOffset / windowCenterY; + + var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0); + positionStick(stickOrientation); +} + + +function initControls() { + if (hand === "right") { + controllerID = 3; // right handed + } else { + controllerID = 4; // left handed + } +} + + +function update() { + var palmPosition = Controller.getSpatialControlPosition(controllerID); + controllerActive = (Vec3.length(palmPosition) > 0); + if (!controllerActive) { + return; + } + + var stickOrientation = Controller.getSpatialControlRawRotation(controllerID); + var adjustment = Quat.fromPitchYawRollDegrees(180, 0, 0); + stickOrientation = Quat.multiply(stickOrientation, adjustment); + + positionStick(stickOrientation); +} + + +Script.scriptEnding.connect(cleanUp); +Controller.mouseMoveEvent.connect(mouseMoveEvent); +Script.update.connect(update);