From 3ef4e45daefa735d0b2d2f2feb6b38208fb31f19 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 10:28:50 -0800 Subject: [PATCH 01/20] Flappy bird first draft --- examples/flappyBird.js | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 examples/flappyBird.js diff --git a/examples/flappyBird.js b/examples/flappyBird.js new file mode 100644 index 0000000000..95e018089e --- /dev/null +++ b/examples/flappyBird.js @@ -0,0 +1,71 @@ +// +// flappyBird.js +// +// Created by Clement 3/2/16 +// 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 +// + +// Constants + + + +// Class definitions +function Game() { + // public methods + this.start = function() { + if (!isRunning) { + setup(); + Script.update.connect(idle); + } + }; + + this.stop = function() { + if (isRunning) { + Script.update.disconnect(idle); + cleanup(); + } + }; + + // Private game state + var that = this; + var isRunning = false; + + // Game loop setup + function idle(deltaTime) { + inputs(); + update(deltaTime); + draw(); + }; + + // Private methods + function setup() { + print("setup"); + }; + function inputs() { + print("inputs"); + }; + function update(deltaTime) { + print("update: " + deltaTime); + }; + function draw() { + print("draw"); + }; + function cleanup() { + print("cleanup"); + }; +} + +// Script logic +function scriptStarting() { + var game = new Game(); + + Script.scriptEnding.connect(function() { + game.stop(); + }); + game.start(); +} + +scriptStarting(); \ No newline at end of file From 6ae27fe1f9ad965e6c7a133a337736e3bf67ff69 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 15:53:13 -0800 Subject: [PATCH 02/20] More flappy bird work --- examples/flappyBird.js | 271 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 258 insertions(+), 13 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 95e018089e..63366a6c75 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -9,59 +9,304 @@ // // Constants +var OBJECTS_LIFETIME = 1; +var G = 4.0; - +Number.prototype.clamp = function(min, max) { + return Math.min(Math.max(this, min), max); +}; // Class definitions function Game() { // public methods this.start = function() { if (!isRunning) { + isRunning = true; setup(); Script.update.connect(idle); } - }; + } this.stop = function() { if (isRunning) { Script.update.disconnect(idle); cleanup(); + isRunning = false; } - }; + } + this.keyPressed = function(event) { + if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) { + isJumping = true; + startedPlaying = true; + } + } + + // Constants + var spaceDimensions = { x: 1.5, y: 0.8, z: 0.01 }; + var spaceDistance = 1.5; + var spaceYOffset = 0.6; + + var jumpVelocity = 1.0; // Private game state var that = this; + var entityManager = new EntityManager(); var isRunning = false; + var startedPlaying = false; + + var coolDown = 1; + var lastLost = -coolDown; + + var gameTime = 0; + + var isJumping = false; + + var space = null + var board = null; + + var bird = null; + var birdXPos = spaceDimensions.x / 2.0; + var birdDimensions = 0.05; + + // ALong Y axis + var birdPos = spaceDimensions.y / 2.0; + var birdVel = 0.0; + var birdAcc = -G; + + var pipes = new Array(); + var lastPipe = 0; + var pipesInterval = 0.5; + var pipesVelocity = 1.0; + var pipeWidth = 0.05; + var pipeLength = 0.4; // Game loop setup function idle(deltaTime) { inputs(); update(deltaTime); draw(); - }; + } - // Private methods function setup() { print("setup"); - }; + + space = { + position: getSpacePosition(), + orientation: getSpaceOrientation(), + dimensions: getSpaceDimensions() + } + + board = entityManager.add({ + type: "Box", + position: space.position, + rotation: space.orientation, + dimensions: space.dimensions, + color: { red: 100, green: 200, blue: 200 } + }); + + bird = entityManager.add({ + type: "Sphere", + position: to3DPosition({ x: birdXPos, y: birdPos }), + dimensions: { x: birdDimensions, y: birdDimensions, z: birdDimensions }, + color: { red: 0, green: 0, blue: 255 } + }); + } function inputs() { - print("inputs"); - }; + //print("inputs"); + } function update(deltaTime) { - print("update: " + deltaTime); - }; + //print("update: " + deltaTime); + + // Keep entities alive + gameTime += deltaTime; + entityManager.update(deltaTime); + + if (!startedPlaying && (gameTime - lastLost) < coolDown) { + return; + } + + // Update Bird + if (!startedPlaying && birdPos < spaceDimensions.y / 2.0) { + isJumping = true; + } + // Apply jumps + if (isJumping) { + birdVel = jumpVelocity; + isJumping = false; + } + // Apply gravity + birdPos += deltaTime * (birdVel + deltaTime * birdAcc / 2.0); + birdVel += deltaTime * birdAcc; + + + // Move pipes forward + pipes.forEach(function(element) { + element.position -= deltaTime * pipesVelocity; + }); + // Delete pipes over the end + var count = 0; + while(count < pipes.length && pipes[count].position <= 0.0) { + entityManager.remove(pipes[count].id); + count++; + } + if (count > 0) { + pipes = pipes.splice(count); + } + // Move pipes forward + if (startedPlaying && gameTime - lastPipe > pipesInterval) { + print("New pipe"); + var newPipe = entityManager.add({ + type: "Box", + position: to3DPosition({ x: space.dimensions.x, y: 0.2 }), + dimensions: { x: pipeWidth, y: pipeLength, z: pipeWidth }, + color: { red: 0, green: 255, blue: 0 } + }); + pipes.push({ id: newPipe, position: space.dimensions.x }); + lastPipe = gameTime; + } + + + // Check lost + var hasLost = birdPos < 0.0 || birdPos > space.dimensions.y; + if (!hasLost) { + pipes.forEach(function(element) { + var deltaX = Math.abs(element.position - birdXPos); + if (deltaX < (birdDimensions + pipeWidth) / 2.0) { + var deltaY = birdPos - pipeLength; + if (deltaY < 0 || deltaY < birdDimensions / 2.0) { + hasLost = true; + } + } + }); + } + + // Cleanup + if (hasLost) { + birdPos = spaceDimensions.y / 2.0; + birdVel = 0.0; + startedPlaying = false; + lastLost = gameTime; + + + // Clearing pipes + print("Clearing pipes: " + pipes.length); + pipes.forEach(function(element) { + entityManager.remove(element.id); + }); + pipes = new Array(); + } + } function draw() { - print("draw"); - }; + //print("draw"); + Entities.editEntity(bird, { position: to3DPosition({ x: birdXPos, y: birdPos }) }); + + pipes.forEach(function(element) { + Entities.editEntity(element.id, { position: to3DPosition({ x: element.position, y: 0.2 }) }); + }); + } function cleanup() { print("cleanup"); - }; + entityManager.removeAll(); + } + + // Private methods + function getSpacePosition() { + var forward = Vec3.multiplyQbyV(MyAvatar.orientation, Vec3.FRONT); + var spacePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(spaceDistance, forward)); + return Vec3.sum(spacePosition, Vec3.multiply(spaceYOffset, Vec3.UP)); + } + function getSpaceOrientation() { + return MyAvatar.orientation; + } + function getSpaceDimensions() { + return spaceDimensions; + } + + + + function project(point, plane) { + var v = Vec3.subtract(point, plane.origin); + var dist = Vec3.dot(v, plane.normal); + return Vec3.subtract(point, Vec3.multiply(dist, v)); + } + function to3DPosition(position) { + var position2D = { + x: position.x - space.dimensions.x / 2.0, + y: position.y - space.dimensions.y / 2.0, + z: 0.0 + } + return Vec3.sum(space.position, Vec3.multiplyQbyV(space.orientation, position2D)); + } + function to2DPosition(position) { + var position3D = project(position, { + origin: Vec3.subtract(space.position, { + x: space.dimensions.x / 2.0, + y: space.dimensions.y / 2.0, + z: 0.0 + }), + normal: Vec3.multiplyQbyV(space.orientation, Vec3.FRONT) + }); + + var position2D = { + x: position3D.x.clamp(0.0, space.dimensions.x), + y: position3D.y.clamp(0.0, space.dimensions.y) + } + return position2D; + } + +} + +function EntityManager() { + var entities = new Array(); + var lifetime = OBJECTS_LIFETIME; + + this.setLifetime = function(newLifetime) { + lifetime = newLifetime; + this.update(); + } + this.add = function(properties) { + // Add to scene + properties.lifetime = lifetime; + var entityID = Entities.addEntity(properties); + // Add to array + entities.push({ id: entityID, properties: properties }); + + return entityID; + } + this.update = function(deltaTime) { + entities.forEach(function(element) { + // Get entity's age + var properties = Entities.getEntityProperties(element.id, ["age"]); + // Update entity's lifetime + Entities.editEntity(element.id, { lifetime: properties.age + lifetime }); + }); + } + this.remove = function(entityID) { + // Remove from scene + Entities.deleteEntity(entityID); + + // Remove from array + entities = entities.filter(function(element) { + return element.id !== entityID; + }); + } + this.removeAll = function() { + // Remove all from scene + entities.forEach(function(element) { + Entities.deleteEntity(element.id); + }); + // Remove all from array + entities = new Array(); + } } // Script logic function scriptStarting() { var game = new Game(); + Controller.keyPressEvent.connect(function(event) { + game.keyPressed(event); + }); Script.scriptEnding.connect(function() { game.stop(); }); From 247f1ef0e8682ae2beadaba01065dda056c5a15c Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 16:07:32 -0800 Subject: [PATCH 03/20] Make Bird its own Class --- examples/flappyBird.js | 87 +++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 27 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 63366a6c75..93335ee969 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -12,11 +12,60 @@ var OBJECTS_LIFETIME = 1; var G = 4.0; +var entityManager = new EntityManager(); + Number.prototype.clamp = function(min, max) { return Math.min(Math.max(this, min), max); }; // Class definitions +function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) { + var DIMENSION = 0.05; + var JUMP_VELOCITY = 1.0; + var xPosition = DEFAULT_X; + var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; + var color = { red: 0, green: 0, blue: 255 }; + + var yPosition = DEFAULT_Y; + var yVelocity = 0.0; + var yAcceleration = -G; + + this.position = function() { + return { x: xPosition, y: yPosition }; + } + this.size = function() { + return DIMENSION; + } + + var id = entityManager.add({ + type: "Sphere", + position: to3DPosition(this.position()), + dimensions: dimensions, + color: color + }); + + + this.jump = function() { + yVelocity = JUMP_VELOCITY; + } + this.update = function(deltaTime) { + yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); + yVelocity += deltaTime * yAcceleration; + } + this.draw = function() { + Entities.editEntity(id, { position: to3DPosition(this.position()) }); + } + this.reset = function() { + yPosition = DEFAULT_Y; + yVelocity = 0.0; + } +} + +function Pipe() { + +} + + function Game() { // public methods this.start = function() { @@ -50,7 +99,6 @@ function Game() { // Private game state var that = this; - var entityManager = new EntityManager(); var isRunning = false; var startedPlaying = false; @@ -65,13 +113,6 @@ function Game() { var board = null; var bird = null; - var birdXPos = spaceDimensions.x / 2.0; - var birdDimensions = 0.05; - - // ALong Y axis - var birdPos = spaceDimensions.y / 2.0; - var birdVel = 0.0; - var birdAcc = -G; var pipes = new Array(); var lastPipe = 0; @@ -104,12 +145,7 @@ function Game() { color: { red: 100, green: 200, blue: 200 } }); - bird = entityManager.add({ - type: "Sphere", - position: to3DPosition({ x: birdXPos, y: birdPos }), - dimensions: { x: birdDimensions, y: birdDimensions, z: birdDimensions }, - color: { red: 0, green: 0, blue: 255 } - }); + bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition); } function inputs() { //print("inputs"); @@ -126,17 +162,15 @@ function Game() { } // Update Bird - if (!startedPlaying && birdPos < spaceDimensions.y / 2.0) { + if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { isJumping = true; } // Apply jumps if (isJumping) { - birdVel = jumpVelocity; + bird.jump(); isJumping = false; } - // Apply gravity - birdPos += deltaTime * (birdVel + deltaTime * birdAcc / 2.0); - birdVel += deltaTime * birdAcc; + bird.update(deltaTime); // Move pipes forward @@ -167,13 +201,13 @@ function Game() { // Check lost - var hasLost = birdPos < 0.0 || birdPos > space.dimensions.y; + var hasLost = bird.position().y < 0.0 || bird.position().y > space.dimensions.y; if (!hasLost) { pipes.forEach(function(element) { - var deltaX = Math.abs(element.position - birdXPos); - if (deltaX < (birdDimensions + pipeWidth) / 2.0) { - var deltaY = birdPos - pipeLength; - if (deltaY < 0 || deltaY < birdDimensions / 2.0) { + var deltaX = Math.abs(element.position - bird.position().x); + if (deltaX < (bird.size() + pipeWidth) / 2.0) { + var deltaY = bird.position().y - pipeLength; + if (deltaY < 0 || deltaY < bird.size() / 2.0) { hasLost = true; } } @@ -182,8 +216,7 @@ function Game() { // Cleanup if (hasLost) { - birdPos = spaceDimensions.y / 2.0; - birdVel = 0.0; + bird.reset() startedPlaying = false; lastLost = gameTime; @@ -198,7 +231,7 @@ function Game() { } function draw() { //print("draw"); - Entities.editEntity(bird, { position: to3DPosition({ x: birdXPos, y: birdPos }) }); + bird.draw(); pipes.forEach(function(element) { Entities.editEntity(element.id, { position: to3DPosition({ x: element.position, y: 0.2 }) }); From dc1acd7a5bca4b1737669e58222b3e5b477b1410 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 16:40:53 -0800 Subject: [PATCH 04/20] Give pipes their own class --- examples/flappyBird.js | 165 +++++++++++++++++++++++++---------------- 1 file changed, 102 insertions(+), 63 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 93335ee969..a786b78947 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -61,10 +61,98 @@ function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) { } } -function Pipe() { +function Pipe(xPosition, height, to3DPosition) { + var velocity = 1.0; + var width = 0.05; + var color = { red: 0, green: 255, blue: 0 }; + this.position = function() { + return { x: xPosition, y: height / 2.0 }; + } + this.width = function() { + return width; + } + this.height = function() { + return height; + } + + var id = entityManager.add({ + type: "Box", + position: to3DPosition(this.position()), + dimensions: { x: width, y: height, z: width }, + color: color + }); + + this.update = function(deltaTime) { + xPosition -= deltaTime * velocity; + } + this.isColliding = function(bird) { + var deltaX = Math.abs(this.position().x - bird.position().x); + if (deltaX < (bird.size() + this.width()) / 2.0) { + var deltaY = bird.position().y - this.height(); + if (deltaY < 0 || deltaY < bird.size() / 2.0) { + return true; + } + } + + return false; + } + this.draw = function() { + Entities.editEntity(id, { position: to3DPosition(this.position()) }); + } + this.clear = function() { + entityManager.remove(id); + } } +function Pipes(newPipesPosition, to3DPosition) { + var lastPipe = 0; + var pipesInterval = 0.5; + + var pipes = new Array(); + + this.update = function(deltaTime, gameTime, startedPlaying) { + // Move pipes forward + pipes.forEach(function(element) { + element.update(deltaTime); + }); + // Delete pipes over the end + var count = 0; + while(count < pipes.length && pipes[count].position().x <= 0.0) { + pipes[count].clear(); + count++; + } + if (count > 0) { + pipes = pipes.splice(count); + } + // Make new pipes + if (startedPlaying && gameTime - lastPipe > pipesInterval) { + pipes.push(new Pipe(newPipesPosition, 0.4, to3DPosition)); + lastPipe = gameTime; + } + } + this.isColliding = function(bird) { + var isColliding = false; + + pipes.forEach(function(element) { + isColliding |= element.isColliding(bird); + }); + + return isColliding; + } + this.draw = function() { + // Clearing pipes + pipes.forEach(function(element) { + element.draw(); + }); + } + this.clear = function() { + pipes.forEach(function(element) { + element.clear(); + }); + pipes = new Array(); + } +} function Game() { // public methods @@ -95,8 +183,6 @@ function Game() { var spaceDistance = 1.5; var spaceYOffset = 0.6; - var jumpVelocity = 1.0; - // Private game state var that = this; var isRunning = false; @@ -111,15 +197,8 @@ function Game() { var space = null var board = null; - var bird = null; - - var pipes = new Array(); - var lastPipe = 0; - var pipesInterval = 0.5; - var pipesVelocity = 1.0; - var pipeWidth = 0.05; - var pipeLength = 0.4; + var pipes = null; // Game loop setup function idle(deltaTime) { @@ -146,6 +225,8 @@ function Game() { }); bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition); + + pipes = new Pipes(space.dimensions.x, to3DPosition); } function inputs() { //print("inputs"); @@ -172,70 +253,28 @@ function Game() { } bird.update(deltaTime); - - // Move pipes forward - pipes.forEach(function(element) { - element.position -= deltaTime * pipesVelocity; - }); - // Delete pipes over the end - var count = 0; - while(count < pipes.length && pipes[count].position <= 0.0) { - entityManager.remove(pipes[count].id); - count++; - } - if (count > 0) { - pipes = pipes.splice(count); - } - // Move pipes forward - if (startedPlaying && gameTime - lastPipe > pipesInterval) { - print("New pipe"); - var newPipe = entityManager.add({ - type: "Box", - position: to3DPosition({ x: space.dimensions.x, y: 0.2 }), - dimensions: { x: pipeWidth, y: pipeLength, z: pipeWidth }, - color: { red: 0, green: 255, blue: 0 } - }); - pipes.push({ id: newPipe, position: space.dimensions.x }); - lastPipe = gameTime; - } - + pipes.update(deltaTime, gameTime, startedPlaying); // Check lost - var hasLost = bird.position().y < 0.0 || bird.position().y > space.dimensions.y; - if (!hasLost) { - pipes.forEach(function(element) { - var deltaX = Math.abs(element.position - bird.position().x); - if (deltaX < (bird.size() + pipeWidth) / 2.0) { - var deltaY = bird.position().y - pipeLength; - if (deltaY < 0 || deltaY < bird.size() / 2.0) { - hasLost = true; - } - } - }); - } + var hasLost = bird.position().y < 0.0 || + bird.position().y > space.dimensions.y || + pipes.isColliding(bird); + // Cleanup if (hasLost) { - bird.reset() + print("Game Over!"); + bird.reset(); + pipes.clear(); + startedPlaying = false; lastLost = gameTime; - - - // Clearing pipes - print("Clearing pipes: " + pipes.length); - pipes.forEach(function(element) { - entityManager.remove(element.id); - }); - pipes = new Array(); } } function draw() { //print("draw"); bird.draw(); - - pipes.forEach(function(element) { - Entities.editEntity(element.id, { position: to3DPosition({ x: element.position, y: 0.2 }) }); - }); + pipes.draw(); } function cleanup() { print("cleanup"); From 553300a7834f3ba4dec981149b4baccd821f4031 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 17:37:26 -0800 Subject: [PATCH 05/20] Added a pipe on the top, randomized the height --- examples/flappyBird.js | 53 ++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index a786b78947..70e117110b 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -61,24 +61,27 @@ function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) { } } -function Pipe(xPosition, height, to3DPosition) { - var velocity = 1.0; +function Pipe(xPosition, yPosition, height, gap, to3DPosition) { + var velocity = 0.6; var width = 0.05; var color = { red: 0, green: 255, blue: 0 }; this.position = function() { - return { x: xPosition, y: height / 2.0 }; - } - this.width = function() { - return width; - } - this.height = function() { - return height; + return xPosition; } - var id = entityManager.add({ + var upHeight = yPosition - (height + gap); + var upYPosition = height + gap + upHeight / 2.0; + + var idUp = entityManager.add({ type: "Box", - position: to3DPosition(this.position()), + position: to3DPosition({ x: xPosition, y: upYPosition }), + dimensions: { x: width, y: upHeight, z: width }, + color: color + }); + var idDown = entityManager.add({ + type: "Box", + position: to3DPosition({ x: xPosition, y: height / 2.0 }), dimensions: { x: width, y: height, z: width }, color: color }); @@ -87,10 +90,11 @@ function Pipe(xPosition, height, to3DPosition) { xPosition -= deltaTime * velocity; } this.isColliding = function(bird) { - var deltaX = Math.abs(this.position().x - bird.position().x); - if (deltaX < (bird.size() + this.width()) / 2.0) { - var deltaY = bird.position().y - this.height(); - if (deltaY < 0 || deltaY < bird.size() / 2.0) { + var deltaX = Math.abs(this.position() - bird.position().x); + if (deltaX < (bird.size() + width) / 2.0) { + var upDistance = (yPosition - upHeight) - (bird.position().y + bird.size()); + var downDistance = (bird.position().y - bird.size()) - height; + if (upDistance <= 0 || downDistance <= 0) { return true; } } @@ -98,16 +102,18 @@ function Pipe(xPosition, height, to3DPosition) { return false; } this.draw = function() { - Entities.editEntity(id, { position: to3DPosition(this.position()) }); + Entities.editEntity(idUp, { position: to3DPosition({ x: xPosition, y: upYPosition }) }); + Entities.editEntity(idDown, { position: to3DPosition({ x: xPosition, y: height / 2.0 }) }); } this.clear = function() { - entityManager.remove(id); + entityManager.remove(idUp); + entityManager.remove(idDown); } } -function Pipes(newPipesPosition, to3DPosition) { +function Pipes(newPipesPosition, newPipesHeight, to3DPosition) { var lastPipe = 0; - var pipesInterval = 0.5; + var pipesInterval = 1.0; var pipes = new Array(); @@ -118,7 +124,7 @@ function Pipes(newPipesPosition, to3DPosition) { }); // Delete pipes over the end var count = 0; - while(count < pipes.length && pipes[count].position().x <= 0.0) { + while(count < pipes.length && pipes[count].position() <= 0.0) { pipes[count].clear(); count++; } @@ -127,7 +133,10 @@ function Pipes(newPipesPosition, to3DPosition) { } // Make new pipes if (startedPlaying && gameTime - lastPipe > pipesInterval) { - pipes.push(new Pipe(newPipesPosition, 0.4, to3DPosition)); + var min = 0.1; + var max = 0.4; + var height = Math.random() * (max - min) + min; + pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.3, to3DPosition)); lastPipe = gameTime; } } @@ -226,7 +235,7 @@ function Game() { bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition); - pipes = new Pipes(space.dimensions.x, to3DPosition); + pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); } function inputs() { //print("inputs"); From 811ed91e02c698de027502fe4ef070571acfeae8 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 18:02:57 -0800 Subject: [PATCH 06/20] Port to entity script --- examples/flappyBird.js | 730 ++++++++++++++++++++++------------------- 1 file changed, 393 insertions(+), 337 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 70e117110b..11bafef313 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -8,390 +8,446 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// Constants -var OBJECTS_LIFETIME = 1; -var G = 4.0; +(function() { + // Constants + var TRIGGER_CONTROLS = [ + Controller.Standard.LT, + Controller.Standard.RT, + ]; -var entityManager = new EntityManager(); + var OBJECTS_LIFETIME = 1; + var G = 4.0; -Number.prototype.clamp = function(min, max) { - return Math.min(Math.max(this, min), max); -}; + var entityManager = new EntityManager(); -// Class definitions -function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) { - var DIMENSION = 0.05; - var JUMP_VELOCITY = 1.0; - var xPosition = DEFAULT_X; - var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; - var color = { red: 0, green: 0, blue: 255 }; - - var yPosition = DEFAULT_Y; - var yVelocity = 0.0; - var yAcceleration = -G; + Number.prototype.clamp = function(min, max) { + return Math.min(Math.max(this, min), max); + }; - this.position = function() { - return { x: xPosition, y: yPosition }; - } - this.size = function() { - return DIMENSION; + // Class definitions + function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) { + var DIMENSION = 0.05; + var JUMP_VELOCITY = 1.0; + var xPosition = DEFAULT_X; + var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; + var color = { red: 0, green: 0, blue: 255 }; + + var yPosition = DEFAULT_Y; + var yVelocity = 0.0; + var yAcceleration = -G; + + this.position = function() { + return { x: xPosition, y: yPosition }; + } + this.size = function() { + return DIMENSION; + } + + var id = entityManager.add({ + type: "Sphere", + position: to3DPosition(this.position()), + dimensions: dimensions, + color: color + }); + + + this.jump = function() { + yVelocity = JUMP_VELOCITY; + } + this.update = function(deltaTime) { + yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); + yVelocity += deltaTime * yAcceleration; + } + this.draw = function() { + Entities.editEntity(id, { position: to3DPosition(this.position()) }); + } + this.reset = function() { + yPosition = DEFAULT_Y; + yVelocity = 0.0; + } } - var id = entityManager.add({ - type: "Sphere", - position: to3DPosition(this.position()), - dimensions: dimensions, - color: color - }); + function Pipe(xPosition, yPosition, height, gap, to3DPosition) { + var velocity = 0.6; + var width = 0.05; + var color = { red: 0, green: 255, blue: 0 }; + this.position = function() { + return xPosition; + } - this.jump = function() { - yVelocity = JUMP_VELOCITY; - } - this.update = function(deltaTime) { - yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); - yVelocity += deltaTime * yAcceleration; - } - this.draw = function() { - Entities.editEntity(id, { position: to3DPosition(this.position()) }); - } - this.reset = function() { - yPosition = DEFAULT_Y; - yVelocity = 0.0; - } -} + var upHeight = yPosition - (height + gap); + var upYPosition = height + gap + upHeight / 2.0; -function Pipe(xPosition, yPosition, height, gap, to3DPosition) { - var velocity = 0.6; - var width = 0.05; - var color = { red: 0, green: 255, blue: 0 }; + var idUp = entityManager.add({ + type: "Box", + position: to3DPosition({ x: xPosition, y: upYPosition }), + dimensions: { x: width, y: upHeight, z: width }, + color: color + }); + var idDown = entityManager.add({ + type: "Box", + position: to3DPosition({ x: xPosition, y: height / 2.0 }), + dimensions: { x: width, y: height, z: width }, + color: color + }); - this.position = function() { - return xPosition; + this.update = function(deltaTime) { + xPosition -= deltaTime * velocity; + } + this.isColliding = function(bird) { + var deltaX = Math.abs(this.position() - bird.position().x); + if (deltaX < (bird.size() + width) / 2.0) { + var upDistance = (yPosition - upHeight) - (bird.position().y + bird.size()); + var downDistance = (bird.position().y - bird.size()) - height; + if (upDistance <= 0 || downDistance <= 0) { + return true; + } + } + + return false; + } + this.draw = function() { + Entities.editEntity(idUp, { position: to3DPosition({ x: xPosition, y: upYPosition }) }); + Entities.editEntity(idDown, { position: to3DPosition({ x: xPosition, y: height / 2.0 }) }); + } + this.clear = function() { + entityManager.remove(idUp); + entityManager.remove(idDown); + } } - var upHeight = yPosition - (height + gap); - var upYPosition = height + gap + upHeight / 2.0; + function Pipes(newPipesPosition, newPipesHeight, to3DPosition) { + var lastPipe = 0; + var pipesInterval = 1.0; - var idUp = entityManager.add({ - type: "Box", - position: to3DPosition({ x: xPosition, y: upYPosition }), - dimensions: { x: width, y: upHeight, z: width }, - color: color - }); - var idDown = entityManager.add({ - type: "Box", - position: to3DPosition({ x: xPosition, y: height / 2.0 }), - dimensions: { x: width, y: height, z: width }, - color: color - }); + var pipes = new Array(); - this.update = function(deltaTime) { - xPosition -= deltaTime * velocity; + this.update = function(deltaTime, gameTime, startedPlaying) { + // Move pipes forward + pipes.forEach(function(element) { + element.update(deltaTime); + }); + // Delete pipes over the end + var count = 0; + while(count < pipes.length && pipes[count].position() <= 0.0) { + pipes[count].clear(); + count++; + } + if (count > 0) { + pipes = pipes.splice(count); + } + // Make new pipes + if (startedPlaying && gameTime - lastPipe > pipesInterval) { + var min = 0.1; + var max = 0.4; + var height = Math.random() * (max - min) + min; + pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.3, to3DPosition)); + lastPipe = gameTime; + } + } + this.isColliding = function(bird) { + var isColliding = false; + + pipes.forEach(function(element) { + isColliding |= element.isColliding(bird); + }); + + return isColliding; + } + this.draw = function() { + // Clearing pipes + pipes.forEach(function(element) { + element.draw(); + }); + } + this.clear = function() { + pipes.forEach(function(element) { + element.clear(); + }); + pipes = new Array(); + } } - this.isColliding = function(bird) { - var deltaX = Math.abs(this.position() - bird.position().x); - if (deltaX < (bird.size() + width) / 2.0) { - var upDistance = (yPosition - upHeight) - (bird.position().y + bird.size()); - var downDistance = (bird.position().y - bird.size()) - height; - if (upDistance <= 0 || downDistance <= 0) { - return true; + + function Game() { + // public methods + this.start = function() { + if (!isRunning) { + isRunning = true; + setup(); + // Script.update.connect(idle); } } - return false; - } - this.draw = function() { - Entities.editEntity(idUp, { position: to3DPosition({ x: xPosition, y: upYPosition }) }); - Entities.editEntity(idDown, { position: to3DPosition({ x: xPosition, y: height / 2.0 }) }); - } - this.clear = function() { - entityManager.remove(idUp); - entityManager.remove(idDown); - } -} - -function Pipes(newPipesPosition, newPipesHeight, to3DPosition) { - var lastPipe = 0; - var pipesInterval = 1.0; - - var pipes = new Array(); - - this.update = function(deltaTime, gameTime, startedPlaying) { - // Move pipes forward - pipes.forEach(function(element) { - element.update(deltaTime); - }); - // Delete pipes over the end - var count = 0; - while(count < pipes.length && pipes[count].position() <= 0.0) { - pipes[count].clear(); - count++; + this.stop = function() { + if (isRunning) { + // Script.update.disconnect(idle); + cleanup(); + isRunning = false; + } } - if (count > 0) { - pipes = pipes.splice(count); - } - // Make new pipes - if (startedPlaying && gameTime - lastPipe > pipesInterval) { - var min = 0.1; - var max = 0.4; - var height = Math.random() * (max - min) + min; - pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.3, to3DPosition)); - lastPipe = gameTime; - } - } - this.isColliding = function(bird) { - var isColliding = false; - pipes.forEach(function(element) { - isColliding |= element.isColliding(bird); - }); - - return isColliding; - } - this.draw = function() { - // Clearing pipes - pipes.forEach(function(element) { - element.draw(); - }); - } - this.clear = function() { - pipes.forEach(function(element) { - element.clear(); - }); - pipes = new Array(); - } -} - -function Game() { - // public methods - this.start = function() { - if (!isRunning) { - isRunning = true; - setup(); - Script.update.connect(idle); + // Game loop setup + var timestamp = 0; + this.idle = function() { + var now = Date.now(); + var deltaTime = (now - timestamp) / 1000.0; + if (timestamp === 0) { + deltaTime = 0; + } + inputs(); + update(deltaTime); + draw(); + timestamp = now; } - } - - this.stop = function() { - if (isRunning) { - Script.update.disconnect(idle); - cleanup(); - isRunning = false; + // this.keyPressed = function(event) { + // if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) { + // isJumping = true; + // startedPlaying = true; + // } + // } + + // Constants + var spaceDimensions = { x: 1.5, y: 0.8, z: 0.01 }; + var spaceDistance = 1.5; + var spaceYOffset = 0.6; + + // Private game state + var that = this; + var isRunning = false; + var startedPlaying = false; + + var coolDown = 1; + var lastLost = -coolDown; + + var gameTime = 0; + + var isJumping = false; + + var space = null + var board = null; + var bird = null; + var pipes = null; + + function setup() { + print("setup"); + + space = { + position: getSpacePosition(), + orientation: getSpaceOrientation(), + dimensions: getSpaceDimensions() + } + + // board = entityManager.add({ + // type: "Box", + // position: space.position, + // rotation: space.orientation, + // dimensions: space.dimensions, + // color: { red: 100, green: 200, blue: 200 } + // }); + + bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition); + + pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); } - } - this.keyPressed = function(event) { - if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) { + function inputs(triggerValue) { isJumping = true; startedPlaying = true; } - } + function update(deltaTime) { + //print("update: " + deltaTime); - // Constants - var spaceDimensions = { x: 1.5, y: 0.8, z: 0.01 }; - var spaceDistance = 1.5; - var spaceYOffset = 0.6; + // Keep entities alive + gameTime += deltaTime; + entityManager.update(deltaTime); - // Private game state - var that = this; - var isRunning = false; - var startedPlaying = false; + if (!startedPlaying && (gameTime - lastLost) < coolDown) { + return; + } - var coolDown = 1; - var lastLost = -coolDown; + // Update Bird + if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { + isJumping = true; + } + // Apply jumps + if (isJumping) { + bird.jump(); + isJumping = false; + } + bird.update(deltaTime); - var gameTime = 0; + pipes.update(deltaTime, gameTime, startedPlaying); - var isJumping = false; + // Check lost + var hasLost = bird.position().y < 0.0 || + bird.position().y > space.dimensions.y || + pipes.isColliding(bird); - var space = null - var board = null; - var bird = null; - var pipes = null; - // Game loop setup - function idle(deltaTime) { - inputs(); - update(deltaTime); - draw(); - } + // Cleanup + if (hasLost) { + print("Game Over!"); + bird.reset(); + pipes.clear(); - function setup() { - print("setup"); - - space = { - position: getSpacePosition(), - orientation: getSpaceOrientation(), - dimensions: getSpaceDimensions() + startedPlaying = false; + lastLost = gameTime; + } + } + function draw() { + //print("draw"); + bird.draw(); + pipes.draw(); + } + function cleanup() { + print("cleanup"); + entityManager.removeAll(); } - board = entityManager.add({ - type: "Box", - position: space.position, - rotation: space.orientation, - dimensions: space.dimensions, - color: { red: 100, green: 200, blue: 200 } - }); - - bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition); - - pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); - } - function inputs() { - //print("inputs"); - } - function update(deltaTime) { - //print("update: " + deltaTime); - - // Keep entities alive - gameTime += deltaTime; - entityManager.update(deltaTime); - - if (!startedPlaying && (gameTime - lastLost) < coolDown) { - return; + // Private methods + function getSpacePosition() { + var forward = Vec3.multiplyQbyV(MyAvatar.orientation, Vec3.FRONT); + var spacePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(spaceDistance, forward)); + return Vec3.sum(spacePosition, Vec3.multiply(spaceYOffset, Vec3.UP)); + } + function getSpaceOrientation() { + return MyAvatar.orientation; + } + function getSpaceDimensions() { + return spaceDimensions; } - // Update Bird - if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { - isJumping = true; + + + function project(point, plane) { + var v = Vec3.subtract(point, plane.origin); + var dist = Vec3.dot(v, plane.normal); + return Vec3.subtract(point, Vec3.multiply(dist, v)); } - // Apply jumps - if (isJumping) { - bird.jump(); - isJumping = false; - } - bird.update(deltaTime); - - pipes.update(deltaTime, gameTime, startedPlaying); - - // Check lost - var hasLost = bird.position().y < 0.0 || - bird.position().y > space.dimensions.y || - pipes.isColliding(bird); - - - // Cleanup - if (hasLost) { - print("Game Over!"); - bird.reset(); - pipes.clear(); - - startedPlaying = false; - lastLost = gameTime; - } - } - function draw() { - //print("draw"); - bird.draw(); - pipes.draw(); - } - function cleanup() { - print("cleanup"); - entityManager.removeAll(); - } - - // Private methods - function getSpacePosition() { - var forward = Vec3.multiplyQbyV(MyAvatar.orientation, Vec3.FRONT); - var spacePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(spaceDistance, forward)); - return Vec3.sum(spacePosition, Vec3.multiply(spaceYOffset, Vec3.UP)); - } - function getSpaceOrientation() { - return MyAvatar.orientation; - } - function getSpaceDimensions() { - return spaceDimensions; - } - - - - function project(point, plane) { - var v = Vec3.subtract(point, plane.origin); - var dist = Vec3.dot(v, plane.normal); - return Vec3.subtract(point, Vec3.multiply(dist, v)); - } - function to3DPosition(position) { - var position2D = { - x: position.x - space.dimensions.x / 2.0, - y: position.y - space.dimensions.y / 2.0, - z: 0.0 - } - return Vec3.sum(space.position, Vec3.multiplyQbyV(space.orientation, position2D)); - } - function to2DPosition(position) { - var position3D = project(position, { - origin: Vec3.subtract(space.position, { - x: space.dimensions.x / 2.0, - y: space.dimensions.y / 2.0, + function to3DPosition(position) { + var position2D = { + x: position.x - space.dimensions.x / 2.0, + y: position.y - space.dimensions.y / 2.0, z: 0.0 - }), - normal: Vec3.multiplyQbyV(space.orientation, Vec3.FRONT) - }); - - var position2D = { - x: position3D.x.clamp(0.0, space.dimensions.x), - y: position3D.y.clamp(0.0, space.dimensions.y) + } + return Vec3.sum(space.position, Vec3.multiplyQbyV(space.orientation, position2D)); } - return position2D; + function to2DPosition(position) { + var position3D = project(position, { + origin: Vec3.subtract(space.position, { + x: space.dimensions.x / 2.0, + y: space.dimensions.y / 2.0, + z: 0.0 + }), + normal: Vec3.multiplyQbyV(space.orientation, Vec3.FRONT) + }); + + var position2D = { + x: position3D.x.clamp(0.0, space.dimensions.x), + y: position3D.y.clamp(0.0, space.dimensions.y) + } + return position2D; + } + } -} + function EntityManager() { + var entities = new Array(); + var lifetime = OBJECTS_LIFETIME; -function EntityManager() { - var entities = new Array(); - var lifetime = OBJECTS_LIFETIME; + this.setLifetime = function(newLifetime) { + lifetime = newLifetime; + this.update(); + } + this.add = function(properties) { + // Add to scene + properties.lifetime = lifetime; + var entityID = Entities.addEntity(properties); + // Add to array + entities.push({ id: entityID, properties: properties }); - this.setLifetime = function(newLifetime) { - lifetime = newLifetime; - this.update(); + return entityID; + } + this.update = function(deltaTime) { + entities.forEach(function(element) { + // Get entity's age + var properties = Entities.getEntityProperties(element.id, ["age"]); + // Update entity's lifetime + Entities.editEntity(element.id, { lifetime: properties.age + lifetime }); + }); + } + this.remove = function(entityID) { + // Remove from scene + Entities.deleteEntity(entityID); + + // Remove from array + entities = entities.filter(function(element) { + return element.id !== entityID; + }); + } + this.removeAll = function() { + // Remove all from scene + entities.forEach(function(element) { + Entities.deleteEntity(element.id); + }); + // Remove all from array + entities = new Array(); + } } - this.add = function(properties) { - // Add to scene - properties.lifetime = lifetime; - var entityID = Entities.addEntity(properties); - // Add to array - entities.push({ id: entityID, properties: properties }); - return entityID; - } - this.update = function(deltaTime) { - entities.forEach(function(element) { - // Get entity's age - var properties = Entities.getEntityProperties(element.id, ["age"]); - // Update entity's lifetime - Entities.editEntity(element.id, { lifetime: properties.age + lifetime }); - }); - } - this.remove = function(entityID) { - // Remove from scene - Entities.deleteEntity(entityID); + PartableGame = function() { + this.equipped = false; + this.triggerValue = 0.0; + this.hand = 0; + this.game = null; + }; - // Remove from array - entities = entities.filter(function(element) { - return element.id !== entityID; - }); - } - this.removeAll = function() { - // Remove all from scene - entities.forEach(function(element) { - Entities.deleteEntity(element.id); - }); - // Remove all from array - entities = new Array(); - } -} + PartableGame.prototype = { + preload: function(entityID) { + this.entityID = entityID; + }, + unload: function() { + }, + startEquip: function(id, params) { + this.equipped = true; + this.hand = params[0] == "left" ? 0 : 1; -// Script logic -function scriptStarting() { - var game = new Game(); + this.game = new Game(); + this.game.start(); + }, + releaseEquip: function(id, params) { + this.equipped = false; - Controller.keyPressEvent.connect(function(event) { - game.keyPressed(event); - }); - Script.scriptEnding.connect(function() { - game.stop(); - }); - game.start(); -} + this.game.stop(); + delete this.game; + }, + continueEquip: function(id, params) { + if (!this.equipped) { + return; + } + this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[this.hand]); + + this.game.idle(); + }, + }; + + // entity scripts always need to return a newly constructed object of our type + return new PartableGame(); +}); + + +// // Script logic +// function scriptStarting() { +// var game = new Game(); + +// Controller.keyPressEvent.connect(function(event) { +// game.keyPressed(event); +// }); +// Script.scriptEnding.connect(function() { +// game.stop(); +// }); +// game.start(); +// } + +// scriptStarting(); -scriptStarting(); \ No newline at end of file From 48fe9b343ee180ca574b161930af13d14059a800 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 18:20:18 -0800 Subject: [PATCH 07/20] Couple trigger value fixes --- examples/flappyBird.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 11bafef313..2820d3afc7 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -189,13 +189,13 @@ // Game loop setup var timestamp = 0; - this.idle = function() { + this.idle = function(triggerValue) { var now = Date.now(); var deltaTime = (now - timestamp) / 1000.0; if (timestamp === 0) { deltaTime = 0; } - inputs(); + inputs(triggerValue); update(deltaTime); draw(); timestamp = now; @@ -251,8 +251,10 @@ pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); } function inputs(triggerValue) { - isJumping = true; - startedPlaying = true; + if (triggerValue > 0.5) { + isJumping = true; + startedPlaying = true; + } } function update(deltaTime) { //print("update: " + deltaTime); @@ -426,8 +428,7 @@ return; } this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[this.hand]); - - this.game.idle(); + this.game.idle(this.triggerValue); }, }; From cf52aeeed296f3e8b8be0c124af525276decc328 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 20:40:25 -0800 Subject: [PATCH 08/20] Use correctly resized avatar model with fly animation --- examples/flappyBird.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 2820d3afc7..49dec4c32a 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -29,9 +29,10 @@ var DIMENSION = 0.05; var JUMP_VELOCITY = 1.0; var xPosition = DEFAULT_X; - var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; var color = { red: 0, green: 0, blue: 255 }; + var dimensionsSet = false; + var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; var yPosition = DEFAULT_Y; var yVelocity = 0.0; var yAcceleration = -G; @@ -44,7 +45,18 @@ } var id = entityManager.add({ - type: "Sphere", + type: "Model", + modelURL: MyAvatar.skeletonModelURL, + animation: { + url: "http://hifi-content.s3.amazonaws.com/ozan/dev/anim/standard_anims_160127/fly.fbx", + running: true, + fps: 30, + firstFrame: 1.0, + lastFrame: 80.0, + currentFrame: 1.0, + loop: true, + hold: true + }, position: to3DPosition(this.position()), dimensions: dimensions, color: color @@ -55,11 +67,26 @@ yVelocity = JUMP_VELOCITY; } this.update = function(deltaTime) { + if (!dimensionsSet) { + var properties = Entities.getEntityProperties(id, ["naturalDimensions"]); + var naturalDimensions = properties.naturalDimensions; + if (naturalDimensions.x != 1 || naturalDimensions.y != 1 || naturalDimensions.z != 1) { + var max = Math.max(naturalDimensions.x, Math.max(naturalDimensions.y, naturalDimensions.z)); + dimensions.x = naturalDimensions.x / max * dimensions.x; + dimensions.y = naturalDimensions.y / max * dimensions.y; + dimensions.z = naturalDimensions.z / max * dimensions.z; + dimensionsSet = true; + } + }; + yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); yVelocity += deltaTime * yAcceleration; } this.draw = function() { - Entities.editEntity(id, { position: to3DPosition(this.position()) }); + Entities.editEntity(id, { + position: to3DPosition(this.position()), + dimensions: dimensions + }); } this.reset = function() { yPosition = DEFAULT_Y; From 21b3f88deeb9c2feba7cb0a02d8e9b18b9700147 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 21:59:40 -0800 Subject: [PATCH 09/20] Scale up/Add avatar rotation/Cleanup --- examples/flappyBird.js | 76 +++++++++++------------------------------- 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 49dec4c32a..d9440a8fe9 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -15,18 +15,17 @@ Controller.Standard.RT, ]; - var OBJECTS_LIFETIME = 1; var G = 4.0; - var entityManager = new EntityManager(); - Number.prototype.clamp = function(min, max) { return Math.min(Math.max(this, min), max); }; + + var entityManager = new EntityManager(); // Class definitions - function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) { - var DIMENSION = 0.05; + function Bird(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) { + var DIMENSION = 0.15; var JUMP_VELOCITY = 1.0; var xPosition = DEFAULT_X; var color = { red: 0, green: 0, blue: 255 }; @@ -58,6 +57,7 @@ hold: true }, position: to3DPosition(this.position()), + rotation: rotation, dimensions: dimensions, color: color }); @@ -95,7 +95,7 @@ } function Pipe(xPosition, yPosition, height, gap, to3DPosition) { - var velocity = 0.6; + var velocity = 0.4; var width = 0.05; var color = { red: 0, green: 255, blue: 0 }; @@ -146,7 +146,7 @@ function Pipes(newPipesPosition, newPipesHeight, to3DPosition) { var lastPipe = 0; - var pipesInterval = 1.0; + var pipesInterval = 2.0; var pipes = new Array(); @@ -166,10 +166,10 @@ } // Make new pipes if (startedPlaying && gameTime - lastPipe > pipesInterval) { - var min = 0.1; - var max = 0.4; + var min = 0.4; + var max = 0.7; var height = Math.random() * (max - min) + min; - pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.3, to3DPosition)); + pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.5, to3DPosition)); lastPipe = gameTime; } } @@ -202,13 +202,11 @@ if (!isRunning) { isRunning = true; setup(); - // Script.update.connect(idle); } } this.stop = function() { if (isRunning) { - // Script.update.disconnect(idle); cleanup(); isRunning = false; } @@ -235,7 +233,7 @@ // } // Constants - var spaceDimensions = { x: 1.5, y: 0.8, z: 0.01 }; + var spaceDimensions = { x: 2.0, y: 1.5, z: 0.01 }; var spaceDistance = 1.5; var spaceYOffset = 0.6; @@ -250,6 +248,9 @@ var gameTime = 0; var isJumping = false; + var lastJumpValue = 0.0; + var lastTriggerValue = 0.0; + var TRIGGER_THRESHOLD = 0.9; var space = null var board = null; @@ -273,15 +274,16 @@ // color: { red: 100, green: 200, blue: 200 } // }); - bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition); - + var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); + bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); } function inputs(triggerValue) { - if (triggerValue > 0.5) { + if (triggerValue > TRIGGER_THRESHOLD && lastTriggerValue < TRIGGER_THRESHOLD) { isJumping = true; startedPlaying = true; } + lastTriggerValue = triggerValue; } function update(deltaTime) { //print("update: " + deltaTime); @@ -346,13 +348,6 @@ return spaceDimensions; } - - - function project(point, plane) { - var v = Vec3.subtract(point, plane.origin); - var dist = Vec3.dot(v, plane.normal); - return Vec3.subtract(point, Vec3.multiply(dist, v)); - } function to3DPosition(position) { var position2D = { x: position.x - space.dimensions.x / 2.0, @@ -361,26 +356,10 @@ } return Vec3.sum(space.position, Vec3.multiplyQbyV(space.orientation, position2D)); } - function to2DPosition(position) { - var position3D = project(position, { - origin: Vec3.subtract(space.position, { - x: space.dimensions.x / 2.0, - y: space.dimensions.y / 2.0, - z: 0.0 - }), - normal: Vec3.multiplyQbyV(space.orientation, Vec3.FRONT) - }); - - var position2D = { - x: position3D.x.clamp(0.0, space.dimensions.x), - y: position3D.y.clamp(0.0, space.dimensions.y) - } - return position2D; - } - } function EntityManager() { + var OBJECTS_LIFETIME = 1; var entities = new Array(); var lifetime = OBJECTS_LIFETIME; @@ -462,20 +441,3 @@ // entity scripts always need to return a newly constructed object of our type return new PartableGame(); }); - - -// // Script logic -// function scriptStarting() { -// var game = new Game(); - -// Controller.keyPressEvent.connect(function(event) { -// game.keyPressed(event); -// }); -// Script.scriptEnding.connect(function() { -// game.stop(); -// }); -// game.start(); -// } - -// scriptStarting(); - From bef28859573d9a4319cc5c69d8cbe71183b5f643 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 22:25:30 -0800 Subject: [PATCH 10/20] Couple bounding box fixes --- examples/flappyBird.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index d9440a8fe9..d4de629e8d 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -39,8 +39,8 @@ this.position = function() { return { x: xPosition, y: yPosition }; } - this.size = function() { - return DIMENSION; + this.dimensions = function() { + return dimensions; } var id = entityManager.add({ @@ -77,7 +77,9 @@ dimensions.z = naturalDimensions.z / max * dimensions.z; dimensionsSet = true; } - }; + } else { + dimensions = Entities.getEntityProperties(id, ["dimensions"]).dimensions; + } yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); yVelocity += deltaTime * yAcceleration; @@ -124,9 +126,9 @@ } this.isColliding = function(bird) { var deltaX = Math.abs(this.position() - bird.position().x); - if (deltaX < (bird.size() + width) / 2.0) { - var upDistance = (yPosition - upHeight) - (bird.position().y + bird.size()); - var downDistance = (bird.position().y - bird.size()) - height; + if (deltaX < (bird.dimensions().z + width) / 2.0) { + var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y); + var downDistance = (bird.position().y - bird.dimensions().y) - height; if (upDistance <= 0 || downDistance <= 0) { return true; } From d5d5eaaf9c5905ee71a178e966e1c5e68785dbf5 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 2 Mar 2016 23:42:27 -0800 Subject: [PATCH 11/20] Fix cooldown --- examples/flappyBird.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index d4de629e8d..041535caa5 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -281,7 +281,9 @@ pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); } function inputs(triggerValue) { - if (triggerValue > TRIGGER_THRESHOLD && lastTriggerValue < TRIGGER_THRESHOLD) { + if (triggerValue > TRIGGER_THRESHOLD && + lastTriggerValue < TRIGGER_THRESHOLD && + (gameTime - lastLost) > coolDown) { isJumping = true; startedPlaying = true; } From f5822223898813e4ffcbbd045f91a44413b6e77a Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 3 Mar 2016 03:24:12 -0800 Subject: [PATCH 12/20] Couple additional features --- examples/flappyBird.js | 43 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 041535caa5..a9e901a9f8 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -58,10 +58,20 @@ }, position: to3DPosition(this.position()), rotation: rotation, - dimensions: dimensions, - color: color + dimensions: dimensions }); + this.changeModel = function(modelURL) { + dimensionsSet = false; + dimensions = { x: 0.10, y: 0.10, z: 0.01 }; + + Entities.editEntity(id, { + modelURL: modelURL, + rotation: Quat.multiply(rotation, Quat.fromPitchYawRollDegrees(0, -90, 0)), + dimensions: dimensions, + animation: {running: false} + }); + } this.jump = function() { yVelocity = JUMP_VELOCITY; @@ -98,7 +108,7 @@ function Pipe(xPosition, yPosition, height, gap, to3DPosition) { var velocity = 0.4; - var width = 0.05; + var width = 0.1; var color = { red: 0, green: 255, blue: 0 }; this.position = function() { @@ -109,13 +119,16 @@ var upYPosition = height + gap + upHeight / 2.0; var idUp = entityManager.add({ - type: "Box", + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", + rotation: Quat.fromPitchYawRollDegrees(180, 0, 0), position: to3DPosition({ x: xPosition, y: upYPosition }), dimensions: { x: width, y: upHeight, z: width }, color: color }); var idDown = entityManager.add({ - type: "Box", + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", position: to3DPosition({ x: xPosition, y: height / 2.0 }), dimensions: { x: width, y: height, z: width }, color: color @@ -259,6 +272,22 @@ var bird = null; var pipes = null; + var directions = ["UP", "DOWN", "LEFT", "RIGHT"]; + var sequence = [directions[0], directions[0], directions[1], directions[1], directions[2], directions[3], directions[2], directions[3], "b", "a"]; + var current = 0; + function keyPress(event) { + if (event.text === sequence[current]) { + ++current; + } else { + current = 0; + } + if (current === sequence.length) { + print("KONAMI CODE!!!"); + bird.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx"); + current = 0; + } + } + function setup() { print("setup"); @@ -279,6 +308,8 @@ var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); + + Controller.keyPressEvent.connect(keyPress); } function inputs(triggerValue) { if (triggerValue > TRIGGER_THRESHOLD && @@ -337,6 +368,8 @@ function cleanup() { print("cleanup"); entityManager.removeAll(); + + Controller.keyPressEvent.disconnect(keyPress); } // Private methods From 442bd22f4641be47dc97eafd4c9abc442dcc14ff Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 3 Mar 2016 09:56:11 -0800 Subject: [PATCH 13/20] New sounds + coins!!! --- examples/flappyBird.js | 109 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 11 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index a9e901a9f8..b13f93b727 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -36,6 +36,9 @@ var yVelocity = 0.0; var yAcceleration = -G; + var airSwipeSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Air Swipe 05.wav"); + var injector = null; + this.position = function() { return { x: xPosition, y: yPosition }; } @@ -54,7 +57,7 @@ lastFrame: 80.0, currentFrame: 1.0, loop: true, - hold: true + hold: false }, position: to3DPosition(this.position()), rotation: rotation, @@ -71,10 +74,19 @@ dimensions: dimensions, animation: {running: false} }); + + airSwipeSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/8bit Jump 03.wav"); + injector = null; } this.jump = function() { yVelocity = JUMP_VELOCITY; + + if (airSwipeSound.downloaded && !injector) { + injector = Audio.playSound(airSwipeSound, { position: to3DPosition(this.position()), volume: 0.05 }); + } else if (injector) { + injector.restart(); + } } this.update = function(deltaTime) { if (!dimensionsSet) { @@ -86,6 +98,10 @@ dimensions.y = naturalDimensions.y / max * dimensions.y; dimensions.z = naturalDimensions.z / max * dimensions.z; dimensionsSet = true; + + Entities.editEntity(id, { + dimensions: dimensions + }); } } else { dimensions = Entities.getEntityProperties(id, ["dimensions"]).dimensions; @@ -96,8 +112,7 @@ } this.draw = function() { Entities.editEntity(id, { - position: to3DPosition(this.position()), - dimensions: dimensions + position: to3DPosition(this.position()) }); } this.reset = function() { @@ -106,10 +121,47 @@ } } + function Coin(xPosition, yPosition, to3DPosition) { + var velocity = 0.4; + + this.position = function() { + return xPosition; + } + + var id = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/coin.fbx", + angularVelocity: { x: 0, y: 20, z: 0 }, + position: to3DPosition({ x: xPosition, y: yPosition }), + dimensions: { x: 0.0625, y: 0.0625, z: 0.0088 } + }); + + this.update = function(deltaTime) { + xPosition -= deltaTime * velocity; + } + this.isColliding = function(bird) { + var deltaX = Math.abs(this.position() - bird.position().x); + if (deltaX < (bird.dimensions().z + width) / 2.0) { + var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y); + var downDistance = (bird.position().y - bird.dimensions().y) - height; + if (upDistance <= 0 || downDistance <= 0) { + return true; + } + } + + return false; + } + this.draw = function() { + Entities.editEntity(id, { position: to3DPosition({ x: xPosition, y: yPosition }) }); + } + this.clear = function() { + entityManager.remove(id); + } + } + function Pipe(xPosition, yPosition, height, gap, to3DPosition) { var velocity = 0.4; var width = 0.1; - var color = { red: 0, green: 255, blue: 0 }; this.position = function() { return xPosition; @@ -123,15 +175,13 @@ modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", rotation: Quat.fromPitchYawRollDegrees(180, 0, 0), position: to3DPosition({ x: xPosition, y: upYPosition }), - dimensions: { x: width, y: upHeight, z: width }, - color: color + dimensions: { x: width, y: upHeight, z: width } }); var idDown = entityManager.add({ type: "Model", modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", position: to3DPosition({ x: xPosition, y: height / 2.0 }), - dimensions: { x: width, y: height, z: width }, - color: color + dimensions: { x: width, y: height, z: width } }); this.update = function(deltaTime) { @@ -164,12 +214,17 @@ var pipesInterval = 2.0; var pipes = new Array(); + var coins = new Array(); this.update = function(deltaTime, gameTime, startedPlaying) { // Move pipes forward pipes.forEach(function(element) { element.update(deltaTime); }); + // Move coins forward + coins.forEach(function(element) { + element.update(deltaTime); + }); // Delete pipes over the end var count = 0; while(count < pipes.length && pipes[count].position() <= 0.0) { @@ -179,12 +234,22 @@ if (count > 0) { pipes = pipes.splice(count); } - // Make new pipes + // Delete coins over the end + count = 0; + while(count < coins.length && coins[count].position() <= 0.0) { + coins[count].clear(); + count++; + } + if (count > 0) { + coins = coins.splice(count); + } + // Make new pipes and coins if (startedPlaying && gameTime - lastPipe > pipesInterval) { var min = 0.4; var max = 0.7; var height = Math.random() * (max - min) + min; pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.5, to3DPosition)); + coins.push(new Coin(newPipesPosition, height + 0.5 / 2.0, to3DPosition)); lastPipe = gameTime; } } @@ -198,16 +263,27 @@ return isColliding; } this.draw = function() { - // Clearing pipes + // Drawing pipes pipes.forEach(function(element) { element.draw(); }); + // Drawing coins + coins.forEach(function(element) { + element.draw(); + }); } this.clear = function() { + // Clearing pipes pipes.forEach(function(element) { element.clear(); }); pipes = new Array(); + + // Clearing coins + coins.forEach(function(element) { + element.clear(); + }); + coins = new Array(); } } @@ -257,7 +333,7 @@ var isRunning = false; var startedPlaying = false; - var coolDown = 1; + var coolDown = 1.5; var lastLost = -coolDown; var gameTime = 0; @@ -272,6 +348,9 @@ var bird = null; var pipes = null; + var gameOverSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Game Over.wav"); + var injector = null; + var directions = ["UP", "DOWN", "LEFT", "RIGHT"]; var sequence = [directions[0], directions[0], directions[1], directions[1], directions[2], directions[3], directions[2], directions[3], "b", "a"]; var current = 0; @@ -353,6 +432,14 @@ // Cleanup if (hasLost) { print("Game Over!"); + + if (gameOverSound.downloaded && !injector) { + injector = Audio.playSound(gameOverSound, { position: space.position, volume: 0.4 }); + } else if (injector) { + injector.restart(); + } + + bird.reset(); pipes.clear(); From d1f7cf6387409bab398458ba0c7cef0cfbff3239 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 3 Mar 2016 10:51:37 -0800 Subject: [PATCH 14/20] Added freeze on loss and points architecture --- examples/flappyBird.js | 72 +++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index b13f93b727..82f3c89086 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -123,30 +123,29 @@ function Coin(xPosition, yPosition, to3DPosition) { var velocity = 0.4; + var dimensions = { x: 0.0625, y: 0.0625, z: 0.0088 }; this.position = function() { - return xPosition; + return { x: xPosition, y: yPosition }; } var id = entityManager.add({ type: "Model", modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/coin.fbx", angularVelocity: { x: 0, y: 20, z: 0 }, - position: to3DPosition({ x: xPosition, y: yPosition }), - dimensions: { x: 0.0625, y: 0.0625, z: 0.0088 } + position: to3DPosition(this.position()), + dimensions:dimensions }); this.update = function(deltaTime) { xPosition -= deltaTime * velocity; } this.isColliding = function(bird) { - var deltaX = Math.abs(this.position() - bird.position().x); - if (deltaX < (bird.dimensions().z + width) / 2.0) { - var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y); - var downDistance = (bird.position().y - bird.dimensions().y) - height; - if (upDistance <= 0 || downDistance <= 0) { - return true; - } + var deltaX = Math.abs(this.position().x - bird.position().x); + var deltaY = Math.abs(this.position().Y - bird.position().Y); + if (deltaX < (bird.dimensions().x + dimensions.x) / 2.0 && + deltaX < (bird.dimensions().y + dimensions.y) / 2.0) { + return true; } return false; @@ -209,13 +208,16 @@ } } - function Pipes(newPipesPosition, newPipesHeight, to3DPosition) { + function Pipes(newPipesPosition, newPipesHeight, to3DPosition, moveScore) { var lastPipe = 0; var pipesInterval = 2.0; var pipes = new Array(); var coins = new Array(); + var coinsSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Coin.wav"); + var injector = null; + this.update = function(deltaTime, gameTime, startedPlaying) { // Move pipes forward pipes.forEach(function(element) { @@ -254,6 +256,27 @@ } } this.isColliding = function(bird) { + // Check coin collection + var collected = -1; + coins.forEach(function(element, index) { + if (element.isColliding(bird)) { + element.clear(); + collected = index; + moveScore(10); + + if (coinsSound.downloaded && !injector) { + injector = Audio.playSound(coinsSound, { position: to3DPosition({ x: newPipesPosition, y: newPipesHeight }), volume: 0.1 }); + } else if (injector) { + injector.restart(); + } + } + }); + if (collected > -1) { + coins.splice(collected, 1); + } + + + // Check collisions var isColliding = false; pipes.forEach(function(element) { @@ -311,6 +334,8 @@ if (timestamp === 0) { deltaTime = 0; } + gameTime += deltaTime; + inputs(triggerValue); update(deltaTime); draw(); @@ -367,6 +392,13 @@ } } + var isBoardReset = true; + + var score = 0; + function moveScore(delta) { + score += delta; + } + function setup() { print("setup"); @@ -386,11 +418,19 @@ var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); - pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition); + pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition, moveScore); Controller.keyPressEvent.connect(keyPress); } function inputs(triggerValue) { + if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) { + score = 0; + bird.reset(); + pipes.clear(); + + isBoardReset = true; + } + if (triggerValue > TRIGGER_THRESHOLD && lastTriggerValue < TRIGGER_THRESHOLD && (gameTime - lastLost) > coolDown) { @@ -403,10 +443,9 @@ //print("update: " + deltaTime); // Keep entities alive - gameTime += deltaTime; entityManager.update(deltaTime); - if (!startedPlaying && (gameTime - lastLost) < coolDown) { + if (!startedPlaying && (gameTime - lastLost) < coolDown && !isBoardReset) { return; } @@ -439,10 +478,7 @@ injector.restart(); } - - bird.reset(); - pipes.clear(); - + isBoardReset = false; startedPlaying = false; lastLost = gameTime; } From b00ff7a81cc040c4d2fc4a337b2b53c553ef52ba Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 3 Mar 2016 12:19:01 -0800 Subject: [PATCH 15/20] Display score and save high scores --- examples/flappyBird.js | 141 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 134 insertions(+), 7 deletions(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 82f3c89086..9df1c9a89e 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -189,8 +189,9 @@ this.isColliding = function(bird) { var deltaX = Math.abs(this.position() - bird.position().x); if (deltaX < (bird.dimensions().z + width) / 2.0) { - var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y); - var downDistance = (bird.position().y - bird.dimensions().y) - height; + var factor = 0.8; + var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y * factor); + var downDistance = (bird.position().y - bird.dimensions().y * factor) - height; if (upDistance <= 0 || downDistance <= 0) { return true; } @@ -310,7 +311,109 @@ } } - function Game() { + + function Score(space, bestScore) { + var score = 0; + var highScore = bestScore; + + var topOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.2, z: -0.2 }); + var topLeft = Vec3.sum(space.position, topOffset); + var bottomOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.0, z: -0.2 }); + var bottomLeft = Vec3.sum(space.position, bottomOffset); + + function numberUrl(number) { + return "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/" + number + ".fbx" + } + function digitPosition(digit) { + return Vec3.multiplyQbyV(space.orientation, { x: 0.3778 + digit * 0.0760, y: 0.0, z: 0.0 }); + } + this.score = function() { + return score; + } + this.highScore = function() { + return highScore; + } + + var bestId = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/best.fbx", + position: topLeft, + rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), + dimensions: { x: 0.2781, y: 0.0063, z: 0.1037 } + }); + var best0 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(0)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var best1 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(1)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var best2 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(2)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + + var scoreId = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/score.fbx", + position: bottomLeft, + rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), + dimensions: { x: 0.3678, y: 0.0063, z: 0.1037 } + }); + var score0 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(0)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var score1 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(1)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var score2 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(2)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + + this.moveScore = function(delta) { + score += delta; + if (score > highScore) { + highScore = score; + } + } + this.resetScore = function() { + score = 0; + } + + this.draw = function() { + Entities.editEntity(best0, { modelURL: numberUrl(Math.floor((highScore / 100) % 10)) }); + Entities.editEntity(best1, { modelURL: numberUrl(Math.floor((highScore / 10) % 10)) }); + Entities.editEntity(best2, { modelURL: numberUrl(Math.floor((highScore / 1) % 10)) }); + + Entities.editEntity(score0, { modelURL: numberUrl(Math.floor((score / 100) % 10)) }); + Entities.editEntity(score1, { modelURL: numberUrl(Math.floor((score / 10) % 10)) }); + Entities.editEntity(score2, { modelURL: numberUrl(Math.floor((score / 1) % 10)) }); + } + } + + function Game(bestScore) { // public methods this.start = function() { if (!isRunning) { @@ -372,6 +475,7 @@ var board = null; var bird = null; var pipes = null; + var score = null; var gameOverSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Game Over.wav"); var injector = null; @@ -394,9 +498,15 @@ var isBoardReset = true; - var score = 0; function moveScore(delta) { - score += delta; + score.moveScore(delta); + } + + this.score = function() { + return score.score(); + } + this.highScore = function() { + return score.highScore(); } function setup() { @@ -419,12 +529,13 @@ var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition, moveScore); + score = new Score(space, bestScore); Controller.keyPressEvent.connect(keyPress); } function inputs(triggerValue) { if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) { - score = 0; + score.resetScore(); bird.reset(); pipes.clear(); @@ -487,6 +598,7 @@ //print("draw"); bird.draw(); pipes.draw(); + score.draw(); } function cleanup() { print("cleanup"); @@ -564,6 +676,7 @@ } PartableGame = function() { + this.entityID = null; this.equipped = false; this.triggerValue = 0.0; this.hand = 0; @@ -580,12 +693,26 @@ this.equipped = true; this.hand = params[0] == "left" ? 0 : 1; - this.game = new Game(); + + var bestScore = 0; + var properties = Entities.getEntityProperties(this.entityID, ["userData"]); + var userData = JSON.parse(properties.userData); + if (userData.highScore) { + bestScore = userData.highScore; + } + + this.game = new Game(bestScore); this.game.start(); }, releaseEquip: function(id, params) { this.equipped = false; + var properties = Entities.getEntityProperties(this.entityID, ["userData"]); + var userData = JSON.parse(properties.userData); + userData.highScore = this.game.highScore(); + properties.userData = JSON.stringify(userData); + Entities.editEntity(this.entityID, properties); + this.game.stop(); delete this.game; }, From 10d55666f2af067a127729d566482ee7311993c2 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 3 Mar 2016 12:23:50 -0800 Subject: [PATCH 16/20] Changed coin value --- examples/flappyBird.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/flappyBird.js b/examples/flappyBird.js index 9df1c9a89e..11c8a82d52 100644 --- a/examples/flappyBird.js +++ b/examples/flappyBird.js @@ -263,7 +263,7 @@ if (element.isColliding(bird)) { element.clear(); collected = index; - moveScore(10); + moveScore(1); if (coinsSound.downloaded && !injector) { injector = Audio.playSound(coinsSound, { position: to3DPosition({ x: newPipesPosition, y: newPipesHeight }), volume: 0.1 }); From 0364703f86817a5e8249893f55a4d01a500bf44e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 3 Mar 2016 17:29:11 -0800 Subject: [PATCH 17/20] Move script to toybox --- examples/{flappyBird.js => toybox/flappyAvatars.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{flappyBird.js => toybox/flappyAvatars.js} (100%) diff --git a/examples/flappyBird.js b/examples/toybox/flappyAvatars.js similarity index 100% rename from examples/flappyBird.js rename to examples/toybox/flappyAvatars.js From f62a3ed8f6a94733c1f843473bbed0c5c88be111 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 4 Mar 2016 10:46:30 -0800 Subject: [PATCH 18/20] Remove tabs --- examples/toybox/flappyAvatars.js | 1298 +++++++++++++++--------------- 1 file changed, 649 insertions(+), 649 deletions(-) diff --git a/examples/toybox/flappyAvatars.js b/examples/toybox/flappyAvatars.js index 11c8a82d52..4c54187916 100644 --- a/examples/toybox/flappyAvatars.js +++ b/examples/toybox/flappyAvatars.js @@ -9,671 +9,671 @@ // (function() { - // Constants + // Constants var TRIGGER_CONTROLS = [ Controller.Standard.LT, Controller.Standard.RT, ]; - var G = 4.0; + var G = 4.0; - Number.prototype.clamp = function(min, max) { - return Math.min(Math.max(this, min), max); - }; - - var entityManager = new EntityManager(); + Number.prototype.clamp = function(min, max) { + return Math.min(Math.max(this, min), max); + }; + + var entityManager = new EntityManager(); - // Class definitions - function Bird(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) { - var DIMENSION = 0.15; - var JUMP_VELOCITY = 1.0; - var xPosition = DEFAULT_X; - var color = { red: 0, green: 0, blue: 255 }; - - var dimensionsSet = false; - var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; - var yPosition = DEFAULT_Y; - var yVelocity = 0.0; - var yAcceleration = -G; + // Class definitions + function Bird(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) { + var DIMENSION = 0.15; + var JUMP_VELOCITY = 1.0; + var xPosition = DEFAULT_X; + var color = { red: 0, green: 0, blue: 255 }; + + var dimensionsSet = false; + var dimensions = { x: DIMENSION, y: DIMENSION, z: DIMENSION }; + var yPosition = DEFAULT_Y; + var yVelocity = 0.0; + var yAcceleration = -G; - var airSwipeSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Air Swipe 05.wav"); - var injector = null; + var airSwipeSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Air Swipe 05.wav"); + var injector = null; - this.position = function() { - return { x: xPosition, y: yPosition }; - } - this.dimensions = function() { - return dimensions; - } + this.position = function() { + return { x: xPosition, y: yPosition }; + } + this.dimensions = function() { + return dimensions; + } - var id = entityManager.add({ - type: "Model", - modelURL: MyAvatar.skeletonModelURL, - animation: { - url: "http://hifi-content.s3.amazonaws.com/ozan/dev/anim/standard_anims_160127/fly.fbx", - running: true, - fps: 30, - firstFrame: 1.0, - lastFrame: 80.0, - currentFrame: 1.0, - loop: true, - hold: false - }, - position: to3DPosition(this.position()), - rotation: rotation, - dimensions: dimensions - }); + var id = entityManager.add({ + type: "Model", + modelURL: MyAvatar.skeletonModelURL, + animation: { + url: "http://hifi-content.s3.amazonaws.com/ozan/dev/anim/standard_anims_160127/fly.fbx", + running: true, + fps: 30, + firstFrame: 1.0, + lastFrame: 80.0, + currentFrame: 1.0, + loop: true, + hold: false + }, + position: to3DPosition(this.position()), + rotation: rotation, + dimensions: dimensions + }); - this.changeModel = function(modelURL) { - dimensionsSet = false; - dimensions = { x: 0.10, y: 0.10, z: 0.01 }; + this.changeModel = function(modelURL) { + dimensionsSet = false; + dimensions = { x: 0.10, y: 0.10, z: 0.01 }; - Entities.editEntity(id, { - modelURL: modelURL, - rotation: Quat.multiply(rotation, Quat.fromPitchYawRollDegrees(0, -90, 0)), - dimensions: dimensions, - animation: {running: false} - }); + Entities.editEntity(id, { + modelURL: modelURL, + rotation: Quat.multiply(rotation, Quat.fromPitchYawRollDegrees(0, -90, 0)), + dimensions: dimensions, + animation: {running: false} + }); - airSwipeSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/8bit Jump 03.wav"); - injector = null; - } + airSwipeSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/8bit Jump 03.wav"); + injector = null; + } - this.jump = function() { - yVelocity = JUMP_VELOCITY; + this.jump = function() { + yVelocity = JUMP_VELOCITY; if (airSwipeSound.downloaded && !injector) { injector = Audio.playSound(airSwipeSound, { position: to3DPosition(this.position()), volume: 0.05 }); } else if (injector) { - injector.restart(); + injector.restart(); } - } - this.update = function(deltaTime) { - if (!dimensionsSet) { - var properties = Entities.getEntityProperties(id, ["naturalDimensions"]); - var naturalDimensions = properties.naturalDimensions; - if (naturalDimensions.x != 1 || naturalDimensions.y != 1 || naturalDimensions.z != 1) { - var max = Math.max(naturalDimensions.x, Math.max(naturalDimensions.y, naturalDimensions.z)); - dimensions.x = naturalDimensions.x / max * dimensions.x; - dimensions.y = naturalDimensions.y / max * dimensions.y; - dimensions.z = naturalDimensions.z / max * dimensions.z; - dimensionsSet = true; - - Entities.editEntity(id, { - dimensions: dimensions - }); - } - } else { - dimensions = Entities.getEntityProperties(id, ["dimensions"]).dimensions; - } - - yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); - yVelocity += deltaTime * yAcceleration; - } - this.draw = function() { - Entities.editEntity(id, { - position: to3DPosition(this.position()) - }); - } - this.reset = function() { - yPosition = DEFAULT_Y; - yVelocity = 0.0; - } - } - - function Coin(xPosition, yPosition, to3DPosition) { - var velocity = 0.4; - var dimensions = { x: 0.0625, y: 0.0625, z: 0.0088 }; - - this.position = function() { - return { x: xPosition, y: yPosition }; - } - - var id = entityManager.add({ - type: "Model", - modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/coin.fbx", - angularVelocity: { x: 0, y: 20, z: 0 }, - position: to3DPosition(this.position()), - dimensions:dimensions - }); - - this.update = function(deltaTime) { - xPosition -= deltaTime * velocity; - } - this.isColliding = function(bird) { - var deltaX = Math.abs(this.position().x - bird.position().x); - var deltaY = Math.abs(this.position().Y - bird.position().Y); - if (deltaX < (bird.dimensions().x + dimensions.x) / 2.0 && - deltaX < (bird.dimensions().y + dimensions.y) / 2.0) { - return true; - } - - return false; - } - this.draw = function() { - Entities.editEntity(id, { position: to3DPosition({ x: xPosition, y: yPosition }) }); - } - this.clear = function() { - entityManager.remove(id); - } - } - - function Pipe(xPosition, yPosition, height, gap, to3DPosition) { - var velocity = 0.4; - var width = 0.1; - - this.position = function() { - return xPosition; - } - - var upHeight = yPosition - (height + gap); - var upYPosition = height + gap + upHeight / 2.0; - - var idUp = entityManager.add({ - type: "Model", - modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", - rotation: Quat.fromPitchYawRollDegrees(180, 0, 0), - position: to3DPosition({ x: xPosition, y: upYPosition }), - dimensions: { x: width, y: upHeight, z: width } - }); - var idDown = entityManager.add({ - type: "Model", - modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", - position: to3DPosition({ x: xPosition, y: height / 2.0 }), - dimensions: { x: width, y: height, z: width } - }); - - this.update = function(deltaTime) { - xPosition -= deltaTime * velocity; - } - this.isColliding = function(bird) { - var deltaX = Math.abs(this.position() - bird.position().x); - if (deltaX < (bird.dimensions().z + width) / 2.0) { - var factor = 0.8; - var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y * factor); - var downDistance = (bird.position().y - bird.dimensions().y * factor) - height; - if (upDistance <= 0 || downDistance <= 0) { - return true; - } - } - - return false; - } - this.draw = function() { - Entities.editEntity(idUp, { position: to3DPosition({ x: xPosition, y: upYPosition }) }); - Entities.editEntity(idDown, { position: to3DPosition({ x: xPosition, y: height / 2.0 }) }); - } - this.clear = function() { - entityManager.remove(idUp); - entityManager.remove(idDown); - } - } - - function Pipes(newPipesPosition, newPipesHeight, to3DPosition, moveScore) { - var lastPipe = 0; - var pipesInterval = 2.0; - - var pipes = new Array(); - var coins = new Array(); - - var coinsSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Coin.wav"); - var injector = null; - - this.update = function(deltaTime, gameTime, startedPlaying) { - // Move pipes forward - pipes.forEach(function(element) { - element.update(deltaTime); - }); - // Move coins forward - coins.forEach(function(element) { - element.update(deltaTime); - }); - // Delete pipes over the end - var count = 0; - while(count < pipes.length && pipes[count].position() <= 0.0) { - pipes[count].clear(); - count++; - } - if (count > 0) { - pipes = pipes.splice(count); - } - // Delete coins over the end - count = 0; - while(count < coins.length && coins[count].position() <= 0.0) { - coins[count].clear(); - count++; - } - if (count > 0) { - coins = coins.splice(count); - } - // Make new pipes and coins - if (startedPlaying && gameTime - lastPipe > pipesInterval) { - var min = 0.4; - var max = 0.7; - var height = Math.random() * (max - min) + min; - pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.5, to3DPosition)); - coins.push(new Coin(newPipesPosition, height + 0.5 / 2.0, to3DPosition)); - lastPipe = gameTime; - } - } - this.isColliding = function(bird) { - // Check coin collection - var collected = -1; - coins.forEach(function(element, index) { - if (element.isColliding(bird)) { - element.clear(); - collected = index; - moveScore(1); - - if (coinsSound.downloaded && !injector) { - injector = Audio.playSound(coinsSound, { position: to3DPosition({ x: newPipesPosition, y: newPipesHeight }), volume: 0.1 }); - } else if (injector) { - injector.restart(); - } - } - }); - if (collected > -1) { - coins.splice(collected, 1); - } - - - // Check collisions - var isColliding = false; - - pipes.forEach(function(element) { - isColliding |= element.isColliding(bird); - }); - - return isColliding; - } - this.draw = function() { - // Drawing pipes - pipes.forEach(function(element) { - element.draw(); - }); - // Drawing coins - coins.forEach(function(element) { - element.draw(); - }); - } - this.clear = function() { - // Clearing pipes - pipes.forEach(function(element) { - element.clear(); - }); - pipes = new Array(); - - // Clearing coins - coins.forEach(function(element) { - element.clear(); - }); - coins = new Array(); - } - } - - - function Score(space, bestScore) { - var score = 0; - var highScore = bestScore; - - var topOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.2, z: -0.2 }); - var topLeft = Vec3.sum(space.position, topOffset); - var bottomOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.0, z: -0.2 }); - var bottomLeft = Vec3.sum(space.position, bottomOffset); - - function numberUrl(number) { - return "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/" + number + ".fbx" - } - function digitPosition(digit) { - return Vec3.multiplyQbyV(space.orientation, { x: 0.3778 + digit * 0.0760, y: 0.0, z: 0.0 }); - } - this.score = function() { - return score; - } - this.highScore = function() { - return highScore; - } - - var bestId = entityManager.add({ - type: "Model", - modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/best.fbx", - position: topLeft, - rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), - dimensions: { x: 0.2781, y: 0.0063, z: 0.1037 } - }); - var best0 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(topLeft, digitPosition(0)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var best1 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(topLeft, digitPosition(1)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var best2 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(topLeft, digitPosition(2)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - - var scoreId = entityManager.add({ - type: "Model", - modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/score.fbx", - position: bottomLeft, - rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), - dimensions: { x: 0.3678, y: 0.0063, z: 0.1037 } - }); - var score0 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(bottomLeft, digitPosition(0)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var score1 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(bottomLeft, digitPosition(1)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var score2 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(bottomLeft, digitPosition(2)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - - this.moveScore = function(delta) { - score += delta; - if (score > highScore) { - highScore = score; - } - } - this.resetScore = function() { - score = 0; - } - - this.draw = function() { - Entities.editEntity(best0, { modelURL: numberUrl(Math.floor((highScore / 100) % 10)) }); - Entities.editEntity(best1, { modelURL: numberUrl(Math.floor((highScore / 10) % 10)) }); - Entities.editEntity(best2, { modelURL: numberUrl(Math.floor((highScore / 1) % 10)) }); - - Entities.editEntity(score0, { modelURL: numberUrl(Math.floor((score / 100) % 10)) }); - Entities.editEntity(score1, { modelURL: numberUrl(Math.floor((score / 10) % 10)) }); - Entities.editEntity(score2, { modelURL: numberUrl(Math.floor((score / 1) % 10)) }); - } - } - - function Game(bestScore) { - // public methods - this.start = function() { - if (!isRunning) { - isRunning = true; - setup(); - } - } - - this.stop = function() { - if (isRunning) { - cleanup(); - isRunning = false; - } - } - - // Game loop setup - var timestamp = 0; - this.idle = function(triggerValue) { - var now = Date.now(); - var deltaTime = (now - timestamp) / 1000.0; - if (timestamp === 0) { - deltaTime = 0; - } - gameTime += deltaTime; - - inputs(triggerValue); - update(deltaTime); - draw(); - timestamp = now; - } - // this.keyPressed = function(event) { - // if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) { - // isJumping = true; - // startedPlaying = true; - // } - // } - - // Constants - var spaceDimensions = { x: 2.0, y: 1.5, z: 0.01 }; - var spaceDistance = 1.5; - var spaceYOffset = 0.6; - - // Private game state - var that = this; - var isRunning = false; - var startedPlaying = false; - - var coolDown = 1.5; - var lastLost = -coolDown; - - var gameTime = 0; - - var isJumping = false; - var lastJumpValue = 0.0; - var lastTriggerValue = 0.0; - var TRIGGER_THRESHOLD = 0.9; - - var space = null - var board = null; - var bird = null; - var pipes = null; - var score = null; - - var gameOverSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Game Over.wav"); - var injector = null; - - var directions = ["UP", "DOWN", "LEFT", "RIGHT"]; - var sequence = [directions[0], directions[0], directions[1], directions[1], directions[2], directions[3], directions[2], directions[3], "b", "a"]; - var current = 0; - function keyPress(event) { - if (event.text === sequence[current]) { - ++current; - } else { - current = 0; - } - if (current === sequence.length) { - print("KONAMI CODE!!!"); - bird.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx"); - current = 0; - } - } - - var isBoardReset = true; - - function moveScore(delta) { - score.moveScore(delta); - } - - this.score = function() { - return score.score(); - } - this.highScore = function() { - return score.highScore(); - } - - function setup() { - print("setup"); - - space = { - position: getSpacePosition(), - orientation: getSpaceOrientation(), - dimensions: getSpaceDimensions() - } - - // board = entityManager.add({ - // type: "Box", - // position: space.position, - // rotation: space.orientation, - // dimensions: space.dimensions, - // color: { red: 100, green: 200, blue: 200 } - // }); - - var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); - bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); - pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition, moveScore); - score = new Score(space, bestScore); - - Controller.keyPressEvent.connect(keyPress); - } - function inputs(triggerValue) { - if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) { - score.resetScore(); - bird.reset(); - pipes.clear(); - - isBoardReset = true; - } - - if (triggerValue > TRIGGER_THRESHOLD && - lastTriggerValue < TRIGGER_THRESHOLD && - (gameTime - lastLost) > coolDown) { - isJumping = true; - startedPlaying = true; - } - lastTriggerValue = triggerValue; - } - function update(deltaTime) { - //print("update: " + deltaTime); - - // Keep entities alive - entityManager.update(deltaTime); - - if (!startedPlaying && (gameTime - lastLost) < coolDown && !isBoardReset) { - return; - } - - // Update Bird - if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { - isJumping = true; - } - // Apply jumps - if (isJumping) { - bird.jump(); - isJumping = false; - } - bird.update(deltaTime); - - pipes.update(deltaTime, gameTime, startedPlaying); - - // Check lost - var hasLost = bird.position().y < 0.0 || - bird.position().y > space.dimensions.y || - pipes.isColliding(bird); - - - // Cleanup - if (hasLost) { - print("Game Over!"); - - if (gameOverSound.downloaded && !injector) { - injector = Audio.playSound(gameOverSound, { position: space.position, volume: 0.4 }); - } else if (injector) { - injector.restart(); - } - - isBoardReset = false; - startedPlaying = false; - lastLost = gameTime; - } - } - function draw() { - //print("draw"); - bird.draw(); - pipes.draw(); - score.draw(); - } - function cleanup() { - print("cleanup"); - entityManager.removeAll(); - - Controller.keyPressEvent.disconnect(keyPress); - } - - // Private methods - function getSpacePosition() { - var forward = Vec3.multiplyQbyV(MyAvatar.orientation, Vec3.FRONT); - var spacePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(spaceDistance, forward)); - return Vec3.sum(spacePosition, Vec3.multiply(spaceYOffset, Vec3.UP)); - } - function getSpaceOrientation() { - return MyAvatar.orientation; - } - function getSpaceDimensions() { - return spaceDimensions; - } - - function to3DPosition(position) { - var position2D = { - x: position.x - space.dimensions.x / 2.0, - y: position.y - space.dimensions.y / 2.0, - z: 0.0 - } - return Vec3.sum(space.position, Vec3.multiplyQbyV(space.orientation, position2D)); - } - } - - function EntityManager() { - var OBJECTS_LIFETIME = 1; - var entities = new Array(); - var lifetime = OBJECTS_LIFETIME; - - this.setLifetime = function(newLifetime) { - lifetime = newLifetime; - this.update(); - } - this.add = function(properties) { - // Add to scene - properties.lifetime = lifetime; - var entityID = Entities.addEntity(properties); - // Add to array - entities.push({ id: entityID, properties: properties }); - - return entityID; - } - this.update = function(deltaTime) { - entities.forEach(function(element) { - // Get entity's age - var properties = Entities.getEntityProperties(element.id, ["age"]); - // Update entity's lifetime - Entities.editEntity(element.id, { lifetime: properties.age + lifetime }); - }); - } - this.remove = function(entityID) { - // Remove from scene - Entities.deleteEntity(entityID); - - // Remove from array - entities = entities.filter(function(element) { - return element.id !== entityID; - }); - } - this.removeAll = function() { - // Remove all from scene - entities.forEach(function(element) { - Entities.deleteEntity(element.id); - }); - // Remove all from array - entities = new Array(); - } - } + } + this.update = function(deltaTime) { + if (!dimensionsSet) { + var properties = Entities.getEntityProperties(id, ["naturalDimensions"]); + var naturalDimensions = properties.naturalDimensions; + if (naturalDimensions.x != 1 || naturalDimensions.y != 1 || naturalDimensions.z != 1) { + var max = Math.max(naturalDimensions.x, Math.max(naturalDimensions.y, naturalDimensions.z)); + dimensions.x = naturalDimensions.x / max * dimensions.x; + dimensions.y = naturalDimensions.y / max * dimensions.y; + dimensions.z = naturalDimensions.z / max * dimensions.z; + dimensionsSet = true; + + Entities.editEntity(id, { + dimensions: dimensions + }); + } + } else { + dimensions = Entities.getEntityProperties(id, ["dimensions"]).dimensions; + } + + yPosition += deltaTime * (yVelocity + deltaTime * yAcceleration / 2.0); + yVelocity += deltaTime * yAcceleration; + } + this.draw = function() { + Entities.editEntity(id, { + position: to3DPosition(this.position()) + }); + } + this.reset = function() { + yPosition = DEFAULT_Y; + yVelocity = 0.0; + } + } + + function Coin(xPosition, yPosition, to3DPosition) { + var velocity = 0.4; + var dimensions = { x: 0.0625, y: 0.0625, z: 0.0088 }; + + this.position = function() { + return { x: xPosition, y: yPosition }; + } + + var id = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/coin.fbx", + angularVelocity: { x: 0, y: 20, z: 0 }, + position: to3DPosition(this.position()), + dimensions:dimensions + }); + + this.update = function(deltaTime) { + xPosition -= deltaTime * velocity; + } + this.isColliding = function(bird) { + var deltaX = Math.abs(this.position().x - bird.position().x); + var deltaY = Math.abs(this.position().Y - bird.position().Y); + if (deltaX < (bird.dimensions().x + dimensions.x) / 2.0 && + deltaX < (bird.dimensions().y + dimensions.y) / 2.0) { + return true; + } + + return false; + } + this.draw = function() { + Entities.editEntity(id, { position: to3DPosition({ x: xPosition, y: yPosition }) }); + } + this.clear = function() { + entityManager.remove(id); + } + } + + function Pipe(xPosition, yPosition, height, gap, to3DPosition) { + var velocity = 0.4; + var width = 0.1; + + this.position = function() { + return xPosition; + } + + var upHeight = yPosition - (height + gap); + var upYPosition = height + gap + upHeight / 2.0; + + var idUp = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", + rotation: Quat.fromPitchYawRollDegrees(180, 0, 0), + position: to3DPosition({ x: xPosition, y: upYPosition }), + dimensions: { x: width, y: upHeight, z: width } + }); + var idDown = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/greenPipe.fbx", + position: to3DPosition({ x: xPosition, y: height / 2.0 }), + dimensions: { x: width, y: height, z: width } + }); + + this.update = function(deltaTime) { + xPosition -= deltaTime * velocity; + } + this.isColliding = function(bird) { + var deltaX = Math.abs(this.position() - bird.position().x); + if (deltaX < (bird.dimensions().z + width) / 2.0) { + var factor = 0.8; + var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y * factor); + var downDistance = (bird.position().y - bird.dimensions().y * factor) - height; + if (upDistance <= 0 || downDistance <= 0) { + return true; + } + } + + return false; + } + this.draw = function() { + Entities.editEntity(idUp, { position: to3DPosition({ x: xPosition, y: upYPosition }) }); + Entities.editEntity(idDown, { position: to3DPosition({ x: xPosition, y: height / 2.0 }) }); + } + this.clear = function() { + entityManager.remove(idUp); + entityManager.remove(idDown); + } + } + + function Pipes(newPipesPosition, newPipesHeight, to3DPosition, moveScore) { + var lastPipe = 0; + var pipesInterval = 2.0; + + var pipes = new Array(); + var coins = new Array(); + + var coinsSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Coin.wav"); + var injector = null; + + this.update = function(deltaTime, gameTime, startedPlaying) { + // Move pipes forward + pipes.forEach(function(element) { + element.update(deltaTime); + }); + // Move coins forward + coins.forEach(function(element) { + element.update(deltaTime); + }); + // Delete pipes over the end + var count = 0; + while(count < pipes.length && pipes[count].position() <= 0.0) { + pipes[count].clear(); + count++; + } + if (count > 0) { + pipes = pipes.splice(count); + } + // Delete coins over the end + count = 0; + while(count < coins.length && coins[count].position() <= 0.0) { + coins[count].clear(); + count++; + } + if (count > 0) { + coins = coins.splice(count); + } + // Make new pipes and coins + if (startedPlaying && gameTime - lastPipe > pipesInterval) { + var min = 0.4; + var max = 0.7; + var height = Math.random() * (max - min) + min; + pipes.push(new Pipe(newPipesPosition, newPipesHeight, height, 0.5, to3DPosition)); + coins.push(new Coin(newPipesPosition, height + 0.5 / 2.0, to3DPosition)); + lastPipe = gameTime; + } + } + this.isColliding = function(bird) { + // Check coin collection + var collected = -1; + coins.forEach(function(element, index) { + if (element.isColliding(bird)) { + element.clear(); + collected = index; + moveScore(1); + + if (coinsSound.downloaded && !injector) { + injector = Audio.playSound(coinsSound, { position: to3DPosition({ x: newPipesPosition, y: newPipesHeight }), volume: 0.1 }); + } else if (injector) { + injector.restart(); + } + } + }); + if (collected > -1) { + coins.splice(collected, 1); + } + + + // Check collisions + var isColliding = false; + + pipes.forEach(function(element) { + isColliding |= element.isColliding(bird); + }); + + return isColliding; + } + this.draw = function() { + // Drawing pipes + pipes.forEach(function(element) { + element.draw(); + }); + // Drawing coins + coins.forEach(function(element) { + element.draw(); + }); + } + this.clear = function() { + // Clearing pipes + pipes.forEach(function(element) { + element.clear(); + }); + pipes = new Array(); + + // Clearing coins + coins.forEach(function(element) { + element.clear(); + }); + coins = new Array(); + } + } + + + function Score(space, bestScore) { + var score = 0; + var highScore = bestScore; + + var topOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.2, z: -0.2 }); + var topLeft = Vec3.sum(space.position, topOffset); + var bottomOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.0, z: -0.2 }); + var bottomLeft = Vec3.sum(space.position, bottomOffset); + + function numberUrl(number) { + return "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/" + number + ".fbx" + } + function digitPosition(digit) { + return Vec3.multiplyQbyV(space.orientation, { x: 0.3778 + digit * 0.0760, y: 0.0, z: 0.0 }); + } + this.score = function() { + return score; + } + this.highScore = function() { + return highScore; + } + + var bestId = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/best.fbx", + position: topLeft, + rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), + dimensions: { x: 0.2781, y: 0.0063, z: 0.1037 } + }); + var best0 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(0)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var best1 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(1)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var best2 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(2)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + + var scoreId = entityManager.add({ + type: "Model", + modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/score.fbx", + position: bottomLeft, + rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), + dimensions: { x: 0.3678, y: 0.0063, z: 0.1037 } + }); + var score0 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(0)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var score1 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(1)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + var score2 = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(2)), + rotation: space.orientation, + dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } + }); + + this.moveScore = function(delta) { + score += delta; + if (score > highScore) { + highScore = score; + } + } + this.resetScore = function() { + score = 0; + } + + this.draw = function() { + Entities.editEntity(best0, { modelURL: numberUrl(Math.floor((highScore / 100) % 10)) }); + Entities.editEntity(best1, { modelURL: numberUrl(Math.floor((highScore / 10) % 10)) }); + Entities.editEntity(best2, { modelURL: numberUrl(Math.floor((highScore / 1) % 10)) }); + + Entities.editEntity(score0, { modelURL: numberUrl(Math.floor((score / 100) % 10)) }); + Entities.editEntity(score1, { modelURL: numberUrl(Math.floor((score / 10) % 10)) }); + Entities.editEntity(score2, { modelURL: numberUrl(Math.floor((score / 1) % 10)) }); + } + } + + function Game(bestScore) { + // public methods + this.start = function() { + if (!isRunning) { + isRunning = true; + setup(); + } + } + + this.stop = function() { + if (isRunning) { + cleanup(); + isRunning = false; + } + } + + // Game loop setup + var timestamp = 0; + this.idle = function(triggerValue) { + var now = Date.now(); + var deltaTime = (now - timestamp) / 1000.0; + if (timestamp === 0) { + deltaTime = 0; + } + gameTime += deltaTime; + + inputs(triggerValue); + update(deltaTime); + draw(); + timestamp = now; + } + // this.keyPressed = function(event) { + // if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) { + // isJumping = true; + // startedPlaying = true; + // } + // } + + // Constants + var spaceDimensions = { x: 2.0, y: 1.5, z: 0.01 }; + var spaceDistance = 1.5; + var spaceYOffset = 0.6; + + // Private game state + var that = this; + var isRunning = false; + var startedPlaying = false; + + var coolDown = 1.5; + var lastLost = -coolDown; + + var gameTime = 0; + + var isJumping = false; + var lastJumpValue = 0.0; + var lastTriggerValue = 0.0; + var TRIGGER_THRESHOLD = 0.9; + + var space = null + var board = null; + var bird = null; + var pipes = null; + var score = null; + + var gameOverSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/Game Over.wav"); + var injector = null; + + var directions = ["UP", "DOWN", "LEFT", "RIGHT"]; + var sequence = [directions[0], directions[0], directions[1], directions[1], directions[2], directions[3], directions[2], directions[3], "b", "a"]; + var current = 0; + function keyPress(event) { + if (event.text === sequence[current]) { + ++current; + } else { + current = 0; + } + if (current === sequence.length) { + print("KONAMI CODE!!!"); + bird.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx"); + current = 0; + } + } + + var isBoardReset = true; + + function moveScore(delta) { + score.moveScore(delta); + } + + this.score = function() { + return score.score(); + } + this.highScore = function() { + return score.highScore(); + } + + function setup() { + print("setup"); + + space = { + position: getSpacePosition(), + orientation: getSpaceOrientation(), + dimensions: getSpaceDimensions() + } + + // board = entityManager.add({ + // type: "Box", + // position: space.position, + // rotation: space.orientation, + // dimensions: space.dimensions, + // color: { red: 100, green: 200, blue: 200 } + // }); + + var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); + bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); + pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition, moveScore); + score = new Score(space, bestScore); + + Controller.keyPressEvent.connect(keyPress); + } + function inputs(triggerValue) { + if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) { + score.resetScore(); + bird.reset(); + pipes.clear(); + + isBoardReset = true; + } + + if (triggerValue > TRIGGER_THRESHOLD && + lastTriggerValue < TRIGGER_THRESHOLD && + (gameTime - lastLost) > coolDown) { + isJumping = true; + startedPlaying = true; + } + lastTriggerValue = triggerValue; + } + function update(deltaTime) { + //print("update: " + deltaTime); + + // Keep entities alive + entityManager.update(deltaTime); + + if (!startedPlaying && (gameTime - lastLost) < coolDown && !isBoardReset) { + return; + } + + // Update Bird + if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { + isJumping = true; + } + // Apply jumps + if (isJumping) { + bird.jump(); + isJumping = false; + } + bird.update(deltaTime); + + pipes.update(deltaTime, gameTime, startedPlaying); + + // Check lost + var hasLost = bird.position().y < 0.0 || + bird.position().y > space.dimensions.y || + pipes.isColliding(bird); + + + // Cleanup + if (hasLost) { + print("Game Over!"); + + if (gameOverSound.downloaded && !injector) { + injector = Audio.playSound(gameOverSound, { position: space.position, volume: 0.4 }); + } else if (injector) { + injector.restart(); + } + + isBoardReset = false; + startedPlaying = false; + lastLost = gameTime; + } + } + function draw() { + //print("draw"); + bird.draw(); + pipes.draw(); + score.draw(); + } + function cleanup() { + print("cleanup"); + entityManager.removeAll(); + + Controller.keyPressEvent.disconnect(keyPress); + } + + // Private methods + function getSpacePosition() { + var forward = Vec3.multiplyQbyV(MyAvatar.orientation, Vec3.FRONT); + var spacePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(spaceDistance, forward)); + return Vec3.sum(spacePosition, Vec3.multiply(spaceYOffset, Vec3.UP)); + } + function getSpaceOrientation() { + return MyAvatar.orientation; + } + function getSpaceDimensions() { + return spaceDimensions; + } + + function to3DPosition(position) { + var position2D = { + x: position.x - space.dimensions.x / 2.0, + y: position.y - space.dimensions.y / 2.0, + z: 0.0 + } + return Vec3.sum(space.position, Vec3.multiplyQbyV(space.orientation, position2D)); + } + } + + function EntityManager() { + var OBJECTS_LIFETIME = 1; + var entities = new Array(); + var lifetime = OBJECTS_LIFETIME; + + this.setLifetime = function(newLifetime) { + lifetime = newLifetime; + this.update(); + } + this.add = function(properties) { + // Add to scene + properties.lifetime = lifetime; + var entityID = Entities.addEntity(properties); + // Add to array + entities.push({ id: entityID, properties: properties }); + + return entityID; + } + this.update = function(deltaTime) { + entities.forEach(function(element) { + // Get entity's age + var properties = Entities.getEntityProperties(element.id, ["age"]); + // Update entity's lifetime + Entities.editEntity(element.id, { lifetime: properties.age + lifetime }); + }); + } + this.remove = function(entityID) { + // Remove from scene + Entities.deleteEntity(entityID); + + // Remove from array + entities = entities.filter(function(element) { + return element.id !== entityID; + }); + } + this.removeAll = function() { + // Remove all from scene + entities.forEach(function(element) { + Entities.deleteEntity(element.id); + }); + // Remove all from array + entities = new Array(); + } + } PartableGame = function() { this.entityID = null; @@ -694,11 +694,11 @@ this.hand = params[0] == "left" ? 0 : 1; - var bestScore = 0; + var bestScore = 0; var properties = Entities.getEntityProperties(this.entityID, ["userData"]); var userData = JSON.parse(properties.userData); if (userData.highScore) { - bestScore = userData.highScore; + bestScore = userData.highScore; } this.game = new Game(bestScore); @@ -721,7 +721,7 @@ return; } this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[this.hand]); - this.game.idle(this.triggerValue); + this.game.idle(this.triggerValue); }, }; From ab5a29b076a37479295061bc764fcadaf8bea470 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 4 Mar 2016 11:50:12 -0800 Subject: [PATCH 19/20] Bit of cleanup --- examples/toybox/flappyAvatars.js | 161 ++++++++++++------------------- 1 file changed, 60 insertions(+), 101 deletions(-) diff --git a/examples/toybox/flappyAvatars.js b/examples/toybox/flappyAvatars.js index 4c54187916..06a0a8e751 100644 --- a/examples/toybox/flappyAvatars.js +++ b/examples/toybox/flappyAvatars.js @@ -1,5 +1,6 @@ // -// flappyBird.js +// flappyAvatars.js +// examples/toybox // // Created by Clement 3/2/16 // Copyright 2015 High Fidelity, Inc. @@ -24,7 +25,7 @@ var entityManager = new EntityManager(); // Class definitions - function Bird(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) { + function Avatar(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) { var DIMENSION = 0.15; var JUMP_VELOCITY = 1.0; var xPosition = DEFAULT_X; @@ -140,11 +141,11 @@ this.update = function(deltaTime) { xPosition -= deltaTime * velocity; } - this.isColliding = function(bird) { - var deltaX = Math.abs(this.position().x - bird.position().x); - var deltaY = Math.abs(this.position().Y - bird.position().Y); - if (deltaX < (bird.dimensions().x + dimensions.x) / 2.0 && - deltaX < (bird.dimensions().y + dimensions.y) / 2.0) { + this.isColliding = function(avatar) { + var deltaX = Math.abs(this.position().x - avatar.position().x); + var deltaY = Math.abs(this.position().Y - avatar.position().Y); + if (deltaX < (avatar.dimensions().x + dimensions.x) / 2.0 && + deltaX < (avatar.dimensions().y + dimensions.y) / 2.0) { return true; } @@ -186,12 +187,12 @@ this.update = function(deltaTime) { xPosition -= deltaTime * velocity; } - this.isColliding = function(bird) { - var deltaX = Math.abs(this.position() - bird.position().x); - if (deltaX < (bird.dimensions().z + width) / 2.0) { + this.isColliding = function(avatar) { + var deltaX = Math.abs(this.position() - avatar.position().x); + if (deltaX < (avatar.dimensions().z + width) / 2.0) { var factor = 0.8; - var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y * factor); - var downDistance = (bird.position().y - bird.dimensions().y * factor) - height; + var upDistance = (yPosition - upHeight) - (avatar.position().y + avatar.dimensions().y * factor); + var downDistance = (avatar.position().y - avatar.dimensions().y * factor) - height; if (upDistance <= 0 || downDistance <= 0) { return true; } @@ -256,11 +257,11 @@ lastPipe = gameTime; } } - this.isColliding = function(bird) { + this.isColliding = function(avatar) { // Check coin collection var collected = -1; coins.forEach(function(element, index) { - if (element.isColliding(bird)) { + if (element.isColliding(avatar)) { element.clear(); collected = index; moveScore(1); @@ -281,7 +282,7 @@ var isColliding = false; pipes.forEach(function(element) { - isColliding |= element.isColliding(bird); + isColliding |= element.isColliding(avatar); }); return isColliding; @@ -321,11 +322,13 @@ var bottomOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.0, z: -0.2 }); var bottomLeft = Vec3.sum(space.position, bottomOffset); + var numberDimensions = { x: 0.0660, y: 0.1050, z: 0.0048 }; + function numberUrl(number) { return "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/" + number + ".fbx" } function digitPosition(digit) { - return Vec3.multiplyQbyV(space.orientation, { x: 0.3778 + digit * 0.0760, y: 0.0, z: 0.0 }); + return Vec3.multiplyQbyV(space.orientation, { x: 0.3778 + digit * (numberDimensions.x + 0.01), y: 0.0, z: 0.0 }); } this.score = function() { return score; @@ -334,6 +337,8 @@ return highScore; } + var numDigits = 3; + var bestId = entityManager.add({ type: "Model", modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/best.fbx", @@ -341,27 +346,16 @@ rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), dimensions: { x: 0.2781, y: 0.0063, z: 0.1037 } }); - var best0 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(topLeft, digitPosition(0)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var best1 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(topLeft, digitPosition(1)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var best2 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(topLeft, digitPosition(2)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); + var bestDigitsId = [] + for (var i = 0; i < numDigits; i++) { + bestDigitsId[i] = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(topLeft, digitPosition(i)), + rotation: space.orientation, + dimensions: numberDimensions + }); + } var scoreId = entityManager.add({ type: "Model", @@ -370,27 +364,16 @@ rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), dimensions: { x: 0.3678, y: 0.0063, z: 0.1037 } }); - var score0 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(bottomLeft, digitPosition(0)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var score1 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(bottomLeft, digitPosition(1)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); - var score2 = entityManager.add({ - type: "Model", - modelURL: numberUrl(0), - position: Vec3.sum(bottomLeft, digitPosition(2)), - rotation: space.orientation, - dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } - }); + var scoreDigitsId = [] + for (var i = 0; i < numDigits; i++) { + scoreDigitsId[i] = entityManager.add({ + type: "Model", + modelURL: numberUrl(0), + position: Vec3.sum(bottomLeft, digitPosition(i)), + rotation: space.orientation, + dimensions: numberDimensions + }); + } this.moveScore = function(delta) { score += delta; @@ -403,13 +386,13 @@ } this.draw = function() { - Entities.editEntity(best0, { modelURL: numberUrl(Math.floor((highScore / 100) % 10)) }); - Entities.editEntity(best1, { modelURL: numberUrl(Math.floor((highScore / 10) % 10)) }); - Entities.editEntity(best2, { modelURL: numberUrl(Math.floor((highScore / 1) % 10)) }); + for (var i = 0; i < numDigits; i++) { + Entities.editEntity(bestDigitsId[i], { modelURL: numberUrl(Math.floor((highScore / Math.pow(10, numDigits- i - 1)) % 10)) }); + } - Entities.editEntity(score0, { modelURL: numberUrl(Math.floor((score / 100) % 10)) }); - Entities.editEntity(score1, { modelURL: numberUrl(Math.floor((score / 10) % 10)) }); - Entities.editEntity(score2, { modelURL: numberUrl(Math.floor((score / 1) % 10)) }); + for (var i = 0; i < numDigits; i++) { + Entities.editEntity(scoreDigitsId[i], { modelURL: numberUrl(Math.floor(score / Math.pow(10, numDigits - i - 1)) % 10) }); + } } } @@ -444,12 +427,6 @@ draw(); timestamp = now; } - // this.keyPressed = function(event) { - // if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) { - // isJumping = true; - // startedPlaying = true; - // } - // } // Constants var spaceDimensions = { x: 2.0, y: 1.5, z: 0.01 }; @@ -471,9 +448,8 @@ var lastTriggerValue = 0.0; var TRIGGER_THRESHOLD = 0.9; - var space = null - var board = null; - var bird = null; + var space = null; + var avatar = null; var pipes = null; var score = null; @@ -490,8 +466,7 @@ current = 0; } if (current === sequence.length) { - print("KONAMI CODE!!!"); - bird.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx"); + avatar.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx"); current = 0; } } @@ -510,24 +485,14 @@ } function setup() { - print("setup"); - space = { position: getSpacePosition(), orientation: getSpaceOrientation(), dimensions: getSpaceDimensions() } - // board = entityManager.add({ - // type: "Box", - // position: space.position, - // rotation: space.orientation, - // dimensions: space.dimensions, - // color: { red: 100, green: 200, blue: 200 } - // }); - var rotation = Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(0, 90, 0)); - bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); + avatar = new Avatar(space.dimensions.x / 2.0, space.dimensions.y / 2.0, rotation, to3DPosition); pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition, moveScore); score = new Score(space, bestScore); @@ -536,7 +501,7 @@ function inputs(triggerValue) { if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) { score.resetScore(); - bird.reset(); + avatar.reset(); pipes.clear(); isBoardReset = true; @@ -551,8 +516,6 @@ lastTriggerValue = triggerValue; } function update(deltaTime) { - //print("update: " + deltaTime); - // Keep entities alive entityManager.update(deltaTime); @@ -560,29 +523,27 @@ return; } - // Update Bird - if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { + // Update Avatar + if (!startedPlaying && avatar.position().y < spaceDimensions.y / 2.0) { isJumping = true; } // Apply jumps if (isJumping) { - bird.jump(); + avatar.jump(); isJumping = false; } - bird.update(deltaTime); + avatar.update(deltaTime); pipes.update(deltaTime, gameTime, startedPlaying); // Check lost - var hasLost = bird.position().y < 0.0 || - bird.position().y > space.dimensions.y || - pipes.isColliding(bird); + var hasLost = avatar.position().y < 0.0 || + avatar.position().y > space.dimensions.y || + pipes.isColliding(avatar); // Cleanup if (hasLost) { - print("Game Over!"); - if (gameOverSound.downloaded && !injector) { injector = Audio.playSound(gameOverSound, { position: space.position, volume: 0.4 }); } else if (injector) { @@ -595,13 +556,11 @@ } } function draw() { - //print("draw"); - bird.draw(); + avatar.draw(); pipes.draw(); score.draw(); } function cleanup() { - print("cleanup"); entityManager.removeAll(); Controller.keyPressEvent.disconnect(keyPress); From 3a5165ab6642c8f46dac6ed2886e69f9924b5fb8 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 4 Mar 2016 11:52:42 -0800 Subject: [PATCH 20/20] Add json import for game entity --- .../{ => flappyAvatars}/flappyAvatars.js | 0 .../toybox/flappyAvatars/flappyAvatars.json | 34 +++++++++++++++++++ 2 files changed, 34 insertions(+) rename examples/toybox/{ => flappyAvatars}/flappyAvatars.js (100%) create mode 100644 examples/toybox/flappyAvatars/flappyAvatars.json diff --git a/examples/toybox/flappyAvatars.js b/examples/toybox/flappyAvatars/flappyAvatars.js similarity index 100% rename from examples/toybox/flappyAvatars.js rename to examples/toybox/flappyAvatars/flappyAvatars.js diff --git a/examples/toybox/flappyAvatars/flappyAvatars.json b/examples/toybox/flappyAvatars/flappyAvatars.json new file mode 100644 index 0000000000..ed6dae8851 --- /dev/null +++ b/examples/toybox/flappyAvatars/flappyAvatars.json @@ -0,0 +1,34 @@ +{ + "Entities": [ + { + "collisionsWillMove": 1, + "created": "2016-03-03T19:00:10Z", + "dimensions": { + "x": 0.11497055739164352, + "y": 0.11497056484222412, + "z": 0.11497056484222412 + }, + "dynamic": 1, + "id": "{ee5b25e6-aca2-4dc7-9462-51537d89c126}", + "modelURL": "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/cube.fbx", + "queryAACube": { + "scale": 0.5974045991897583, + "x": -5.1575918197631836, + "y": 23.078603744506836, + "z": 16.521066665649414 + }, + "rotation": { + "w": 0.92288088798522949, + "x": -0.10148775577545166, + "y": -0.13279926776885986, + "z": 0.34688329696655273 + }, + "script": "https://raw.githubusercontent.com/Atlante45/hifi/feat/hackaton/examples/toybox/flappyAvatars/flappyAvatars.js", + "scriptTimestamp": 1457031937425, + "shapeType": "box", + "type": "Model", + "userData": "{\"wearable\":{\"joints\":{\"RightHand\":[{\"x\":0.07079616189002991,\"y\":0.20177987217903137,\"z\":0.06374628841876984},{\"x\":-0.5863648653030396,\"y\":-0.46007341146469116,\"z\":0.46949487924575806,\"w\":-0.4733745753765106}],\"LeftHand\":[{\"x\":-0.018704339861869812,\"y\":0.20499876141548157,\"z\":0.08445858210325241},{\"x\":0.2061777561903,\"y\":-0.6629757881164551,\"z\":0.5865269303321838,\"w\":0.41706138849258423}]}},\"grabbableKey\":{\"invertSolidWhileHeld\":true},\"resetMe\":{\"resetMe\":true},\"highScore\":0}" + } + ], + "Version": 57 +}