From 0a083c070f9c00b719431da0f1d07e429b7b6587 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 30 Oct 2015 20:21:53 -0700 Subject: [PATCH] color busters cooperative game --- examples/color_busters/colorBusterWand.js | 226 ++++++++++++++++++ .../color_busters/createColorBusterCubes.js | 89 +++++++ .../color_busters/createColorBusterWand.js | 55 +++++ 3 files changed, 370 insertions(+) create mode 100644 examples/color_busters/colorBusterWand.js create mode 100644 examples/color_busters/createColorBusterCubes.js create mode 100644 examples/color_busters/createColorBusterWand.js diff --git a/examples/color_busters/colorBusterWand.js b/examples/color_busters/colorBusterWand.js new file mode 100644 index 0000000000..26948da91b --- /dev/null +++ b/examples/color_busters/colorBusterWand.js @@ -0,0 +1,226 @@ +(function() { + + //seconds that the combination lasts + var COMBINED_COLOR_DURATION = 10; + + var INDICATOR_OFFSET_UP = 0.5; + + var REMOVE_CUBE_SOUND_URL = ''; + var COMBINE_COLORS_SOUND_URL = ''; + + var _this; + + function ColorWand() { + _this = this; + }; + + ColorBusterWand.prototype = { + combinedColorsTimer: null, + + preload: function(entityID) { + print("preload"); + this.entityID = entityID; + this.REMOVE_CUBE_SOUND = SoundCache.getSound(REMOVE_CUBE_SOUND_URL); + this.COMBINE_COLORS_SOUND = SoundCache.getSound(COMBINE_COLORS_SOUND_URL); + + }, + + entityCollisionWithEntity: function(me, otherEntity, collision) { + + var otherProperties = Entities.getEntityProperties(otherEntity, ["name", "userData"]); + var myProperties = Entities.getEntityProperties(otherEntity, ["userData"]); + var myUserData = JSON.parse(myProperties.userData); + var otherUserData = JSON.parse(otherProperties.userData); + + if (otherProperties.name === 'Hifi-ColorBusterWand') { + if (otherUserData.hifiColorBusterWandKey.colorLocked !== true && myUserData.hifiColorBusterWandKey.colorLocked !== true) { + if (otherUserData.hifiColorBusterWandKey.originalColorName === myUserData.hifiColorBusterWandKey.originalColorName) { + return; + } else { + this.combineColorsWithOtherWand(otherUserData.hifiColorBusterWandKey.originalColorName, myUserData.hifiColorBusterWandKey.originalColorName); + } + } + } + + if (otherProperties.name === 'Hifi-ColorBusterCube') { + if (otherUserData.hifiColorBusterCubeKey.originalColorName === myUserData.hifiColorBusterWandKey.currentColor) { + removeCubeOfSameColor(otherEntity); + } + + } + }, + + combineColorsWithOtherWand: function(otherColor, myColor) { + var newColor; + + if ((otherColor === 'red' && myColor == 'yellow') || (myColor === 'red' && otherColor === 'yellow')) { + //orange + newColor = 'orange'; + } + + if ((otherColor === 'red' && myColor == 'blue') || (myColor === 'red' && otherColor === 'blue')) { + //violet + newColor = 'violet'; + } + + if ((otherColor === 'blue' && myColor == 'yellow') || (myColor === 'blue' && otherColor === 'yellow')) { + //green. + newColor = 'green'; + } + + _this.combinedColorsTimer = Script.setTimeout(function() { + _this.resetToOriginalColor(myColor); + _this.combinedColorsTimer = null; + }, COMBINED_COLOR_DURATION * 1000); + + setEntityCustomData(hifiColorWandKey, this.entityID, { + owner: MyAvatar.sessionUUID, + currentColor: newColor + originalColorName: myColor, + colorLocked: false + }); + + _this.setCurrentColor(newColor); + + }, + + setCurrentColor: function(newColor) { + + var color; + + if (newColor === 'orange') { + color = { + red: 255, + green: 165, + blue: 0 + }; + } + + if (newColor === 'violet') { + color = { + red: 128, + green: 0, + blue: 128 + }; + } + + if (newColor === 'green') { + color = { + red: 0, + green: 255, + blue: 0 + }; + } + + if (newColor === 'red') { + color = { + red: 255, + green: 0, + blue: 0 + }; + } + + if (newColor === 'yellow') { + color = { + red: 255, + green: 255, + blue: 0 + }; + } + + if (newColor === 'blue') { + color = { + red: 0, + green: 0, + blue: 255 + }; + } + + Entities.editEntity(this.colorIndicator, { + color: color + }) + }, + + resetToOriginalColor: function(myColor) { + setEntityCustomData(hifiColorWandKey, this.entityID, { + owner: MyAvatar.sessionUUID, + currentColor: myColor + originalColorName: myColor, + colorLocked: true + }); + + this.setCurrentColor(myColor); + }, + + removeCubeOfSameColor: function(cube) { + Entities.callEntityMethod(cube, 'cubeEnding'); + Entities.deleteEntity(cube); + }, + + startNearGrab: function() { + this.currentProperties = Entities.getEntityProperties(this.entityID); + this.createColorIndicator(); + }, + + continueNearGrab: function() { + this.currentProperties = Entities.getEntityProperties(this.entityID); + this.updateColorIndicatorLocation(); + }, + + releaseGrab: function() { + this.deleteEntity(this.colorIndicator); + if (this.combinedColorsTimer !== null) { + Script.clearTimeout(this.combinedColorsTimer); + } + + }, + + createColorIndicator: function() { + var properties = { + name: 'Hifi-ColorBusterIndicator', + type: 'Box', + dimensions: COLOR_INDICATOR_DIMENSIONS, + color: this.currentProperties.position, + position: this.currentProperties.position + } + + this.colorIndicator = Entities.addEntity(properties); + }, + + updateColorIndicatorLocation: function() { + + var position; + + var upVector = Quat.getUp(this.currentProperties.rotation); + var indicatorVector = Vec3.multiply(upVector, INDICATOR_OFFSET_UP); + position = Vec3.sum(this.currentProperties.position, indicatorVector); + + var properties = { + position: position, + rotation: this.currentProperties.rotation + } + + Entities.editEntity(this.colorIndicator, properties); + }, + + + playSoundAtCurrentPosition: function(removeCubeSound) { + var position = Entities.getEntityProperties(this.entityID, "position").position; + + var audioProperties = { + volume: 0.25, + position: position + }; + + if (removeCubeSound) { + Audio.playSound(this.REMOVE_CUBE_SOUND, audioProperties); + } else { + Audio.playSound(this.COMBINE_COLORS_SOUND, audioProperties); + } + }, + + + }; + + return new ColorBusterWand(); +}); \ No newline at end of file diff --git a/examples/color_busters/createColorBusterCubes.js b/examples/color_busters/createColorBusterCubes.js new file mode 100644 index 0000000000..ad7b8b5307 --- /dev/null +++ b/examples/color_busters/createColorBusterCubes.js @@ -0,0 +1,89 @@ +var CUBE_DIMENSIONS = { + x: 1, + y: 1, + z: 1 +}; + +var NUMBER_OF_CUBES_PER_SIDE: 20; + +var STARTING_CORNER_POSITION = { + x: 0, + y: 0, + z: 0 +} + +var STARTING_COLORS = [ + ['red', { + red: 255, + green: 0, + blue: 0 + }], + ['yellow', { + red: 255, + green: 255, + blue: 0 + }], + ['blue', { + red: 0, + green: 0, + blue: 255 + }], + ['orange', { + red: 255, + green: 165, + blue: 0 + }], + ['violet', { + red: 128, + green: 0, + blue: 128 + }], + ['green', { + red: 0, + green: 255, + blue: 0 + }] +] + +function chooseStartingColor() { + var startingColor = STARTING_COLORS[Math.floor(Math.random() * STARTING_COLORS.length)]; + return startingColor +} + +function createColorBusterCube(row, column, vertical) { + + var startingColor = chooseStartingColor(); + var colorBusterCubeProperties = { + name: 'Hifi-ColorBusterWand', + type: 'Model', + url: COLOR_WAND_MODEL_URL, + dimensions: COLOR_WAND_DIMENSIONS, + position: { + x: row, + y: vertical, + z: column + } + userData: JSON.stringify({ + hifiColorBusterCubeKey: { + originalColorName: startingColor[0], + + } + }) + }; + + return Entities.addEntity(colorBusterCubeProperties); +} + +function createBoard() { + var vertical; + for (vertical = 0; vertical === NUMBER_OF_CUBES_PER_SIDE) { + var row; + var column; + //create a single layer + for (row = 0; row === NUMBER_OF_CUBES_PER_SIDE; row++) { + for (column = 0; column === NUMBER_OF_CUBES_PER_SIDE; column++) { + this.createColorBusterCube(row, column, vertical) + } + } + } +} \ No newline at end of file diff --git a/examples/color_busters/createColorBusterWand.js b/examples/color_busters/createColorBusterWand.js new file mode 100644 index 0000000000..68e6ecfee5 --- /dev/null +++ b/examples/color_busters/createColorBusterWand.js @@ -0,0 +1,55 @@ +var COLOR_WAND_MODEL_URL = ''; +var COLOR_WAND_DIMENSIONS = { + x: 0, + y: 0, + z: 0 +}; +var COLOR_WAND_START_POSITION = { + x: 0, + y: 0, + z: 0 +}; +var STARTING_COLORS = [ + ['red', { + red: 255, + green: 0, + blue: 0 + }], + ['yellow', { + red: 255, + green: 255, + blue: 0 + }], + ['blue', { + red: 0, + green: 0, + blue: 255 + }] +] + +function chooseStartingColor() { + var startingColor = STARTING_COLORS[Math.floor(Math.random() * STARTING_COLORS.length)]; + return startingColor +} + +function createColorBusterWand() { + + var startingColor = chooseStartingColor(); + var colorBusterWandProperties = { + name: 'Hifi-ColorBusterWand', + type: 'Model', + url: COLOR_WAND_MODEL_URL, + dimensions: COLOR_WAND_DIMENSIONS, + position: COLOR_WAND_START_POSITION, + userData: JSON.stringify({ + hifiColorBusterWandKey: { + owner: MyAvatar.sessionUUID, + currentColor: startingColor[1] + originalColorName: startingColor[0], + colorLocked: false + } + }) + }; + + Entities.addEntity(colorBusterWandProperties); +} \ No newline at end of file