From e3e917b4b7d77d5e58ed7ad20ce73991c00c6573 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 21 Sep 2016 09:23:45 -0700 Subject: [PATCH] Add tutorial StartZone and Zone entity scripts --- tutorial/tutorialStartZone.js | 29 ++++++++ tutorial/tutorialZone.js | 123 ++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 tutorial/tutorialStartZone.js create mode 100644 tutorial/tutorialZone.js diff --git a/tutorial/tutorialStartZone.js b/tutorial/tutorialStartZone.js new file mode 100644 index 0000000000..5f5fa47b94 --- /dev/null +++ b/tutorial/tutorialStartZone.js @@ -0,0 +1,29 @@ +(function() { + var TutorialStartZone = function() { + }; + + TutorialStartZone.prototype = { + preload: function(entityID) { + this.entityID = entityID; + }, + enterEntity: function() { + // send message to outer zone + print("ENTERED THE TUTORIAL START AREA"); + var parentID = Entities.getEntityProperties(this.entityID, 'parentID').parentID; + print("HERE", parentID); + if (parentID) { + print("HERE2"); + Entities.callEntityMethod(parentID, 'start'); + print("HERE4"); + } else { + print("HERE3"); + print("ERROR: No parent id found on tutorial start zone"); + } + }, + leaveEntity: function() { + print("EXITED THE TUTORIAL START AREA"); + } + }; + + return new TutorialStartZone(); +}); diff --git a/tutorial/tutorialZone.js b/tutorial/tutorialZone.js new file mode 100644 index 0000000000..97aa906052 --- /dev/null +++ b/tutorial/tutorialZone.js @@ -0,0 +1,123 @@ +// A user designates ownership of the tutorial by creating a child entity (token) +// of the tutorial zone. The entity should have a short lifetime (5 seconds), and +// should have it's lifetime reset every second. +// +// * When you enter the "tutorial" begin zone +// * If the tutorial is owned +// * Show a "waiting" text, and check for ownership periodically +// * If the tutorial is not owned +// * Create the ownership token, begin tutorial +// * For extra safety, to avoid races, check after 1 second to confirm that +// another user hasn't created a token. If they have, use some method to +// resolve the conflict. +// * Once the user has finished the tutorial, stop creating the token to +// release ownership. +// +// * The tutorial will expose a local message API for controlling the tutorial +// * A special script will be used to: +// * Create a key shortcut to go to the beginning of the tutorial +// * +// + +if (!Function.prototype.bind) { + Function.prototype.bind = function(oThis) { + if (typeof this !== 'function') { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function() {}, + fBound = function() { + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + if (this.prototype) { + // Function.prototype doesn't have a prototype property + fNOP.prototype = this.prototype; + } + fBound.prototype = new fNOP(); + + return fBound; + }; +} + +(function() { + Script.include("file:///C:/Users/Ryan/dev/hifi/tutorial/ownershipToken.js"); + Script.include("file:///C:/Users/Ryan/dev/hifi/tutorial/tutorial.js"); + + var TutorialZone = function() { + this.token = null; + }; + + TutorialZone.prototype = { + keyReleaseHandler: function(event) { + print(event.text); + if (event.text == ",") { + if (!this.tutorialManager.startNextStep()) { + this.tutorialManager.startTutorial(); + } + } else if (event.text == "F11") { + this.tutorialManager.restartStep(); + } else if (event.text == "F10") { + MyAvatar.shouldRenderLocally = !MyAvatar.shouldRenderLocally; + } else if (event.text == "r") { + this.tutorialManager.stopTutorial(); + this.tutorialManager.startTutorial(); + } + }, + preload: function(entityID) { + this.entityID = entityID; + }, + start: function() { + print("Got start"); + var self = this; + if (!this.token) { + this.token = new OwnershipToken(Math.random() * 100000, this.entityID, { + onGainedOwnership: function(token) { + print("GOT OWNERSHIP"); + if (!self.tutorialManager) { + self.tutorialManager = new TutorialManager(); + } + self.tutorialManager.startTutorial(); + print("making bound release handler"); + self.keyReleaseHandlerBound = self.keyReleaseHandler.bind(self); + print("binding"); + Controller.keyReleaseEvent.connect(self.keyReleaseHandlerBound); + print("done"); + }, + onLostOwnership: function(token) { + print("LOST OWNERSHIP"); + if (self.tutorialManager) { + print("stopping tutorial.."); + self.tutorialManager.stopTutorial(); + print("done"); + Controller.keyReleaseEvent.disconnect(self.keyReleaseHandlerBound); + } else { + print("no tutorial manager..."); + } + } + }); + } + }, + + enterEntity: function() { + print("ENTERED THE TUTORIAL AREA"); + }, + leaveEntity: function() { + print("EXITED THE TUTORIAL AREA"); + if (this.token) { + print("destroying token"); + this.token.destroy(); + this.token = null; + } + } + }; + + return new TutorialZone(); +});