Bit of cleanup

This commit is contained in:
Atlante45 2016-03-04 11:50:12 -08:00
parent f62a3ed8f6
commit ab5a29b076

View file

@ -1,5 +1,6 @@
// //
// flappyBird.js // flappyAvatars.js
// examples/toybox
// //
// Created by Clement 3/2/16 // Created by Clement 3/2/16
// Copyright 2015 High Fidelity, Inc. // Copyright 2015 High Fidelity, Inc.
@ -24,7 +25,7 @@
var entityManager = new EntityManager(); var entityManager = new EntityManager();
// Class definitions // Class definitions
function Bird(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) { function Avatar(DEFAULT_X, DEFAULT_Y, rotation, to3DPosition) {
var DIMENSION = 0.15; var DIMENSION = 0.15;
var JUMP_VELOCITY = 1.0; var JUMP_VELOCITY = 1.0;
var xPosition = DEFAULT_X; var xPosition = DEFAULT_X;
@ -140,11 +141,11 @@
this.update = function(deltaTime) { this.update = function(deltaTime) {
xPosition -= deltaTime * velocity; xPosition -= deltaTime * velocity;
} }
this.isColliding = function(bird) { this.isColliding = function(avatar) {
var deltaX = Math.abs(this.position().x - bird.position().x); var deltaX = Math.abs(this.position().x - avatar.position().x);
var deltaY = Math.abs(this.position().Y - bird.position().Y); var deltaY = Math.abs(this.position().Y - avatar.position().Y);
if (deltaX < (bird.dimensions().x + dimensions.x) / 2.0 && if (deltaX < (avatar.dimensions().x + dimensions.x) / 2.0 &&
deltaX < (bird.dimensions().y + dimensions.y) / 2.0) { deltaX < (avatar.dimensions().y + dimensions.y) / 2.0) {
return true; return true;
} }
@ -186,12 +187,12 @@
this.update = function(deltaTime) { this.update = function(deltaTime) {
xPosition -= deltaTime * velocity; xPosition -= deltaTime * velocity;
} }
this.isColliding = function(bird) { this.isColliding = function(avatar) {
var deltaX = Math.abs(this.position() - bird.position().x); var deltaX = Math.abs(this.position() - avatar.position().x);
if (deltaX < (bird.dimensions().z + width) / 2.0) { if (deltaX < (avatar.dimensions().z + width) / 2.0) {
var factor = 0.8; var factor = 0.8;
var upDistance = (yPosition - upHeight) - (bird.position().y + bird.dimensions().y * factor); var upDistance = (yPosition - upHeight) - (avatar.position().y + avatar.dimensions().y * factor);
var downDistance = (bird.position().y - bird.dimensions().y * factor) - height; var downDistance = (avatar.position().y - avatar.dimensions().y * factor) - height;
if (upDistance <= 0 || downDistance <= 0) { if (upDistance <= 0 || downDistance <= 0) {
return true; return true;
} }
@ -256,11 +257,11 @@
lastPipe = gameTime; lastPipe = gameTime;
} }
} }
this.isColliding = function(bird) { this.isColliding = function(avatar) {
// Check coin collection // Check coin collection
var collected = -1; var collected = -1;
coins.forEach(function(element, index) { coins.forEach(function(element, index) {
if (element.isColliding(bird)) { if (element.isColliding(avatar)) {
element.clear(); element.clear();
collected = index; collected = index;
moveScore(1); moveScore(1);
@ -281,7 +282,7 @@
var isColliding = false; var isColliding = false;
pipes.forEach(function(element) { pipes.forEach(function(element) {
isColliding |= element.isColliding(bird); isColliding |= element.isColliding(avatar);
}); });
return isColliding; return isColliding;
@ -321,11 +322,13 @@
var bottomOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.0, z: -0.2 }); var bottomOffset = Vec3.multiplyQbyV(space.orientation, { x: -0.1, y: 0.0, z: -0.2 });
var bottomLeft = Vec3.sum(space.position, bottomOffset); var bottomLeft = Vec3.sum(space.position, bottomOffset);
var numberDimensions = { x: 0.0660, y: 0.1050, z: 0.0048 };
function numberUrl(number) { function numberUrl(number) {
return "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/" + number + ".fbx" return "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/" + number + ".fbx"
} }
function digitPosition(digit) { 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() { this.score = function() {
return score; return score;
@ -334,6 +337,8 @@
return highScore; return highScore;
} }
var numDigits = 3;
var bestId = entityManager.add({ var bestId = entityManager.add({
type: "Model", type: "Model",
modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/best.fbx", 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)), rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)),
dimensions: { x: 0.2781, y: 0.0063, z: 0.1037 } dimensions: { x: 0.2781, y: 0.0063, z: 0.1037 }
}); });
var best0 = entityManager.add({ var bestDigitsId = []
type: "Model", for (var i = 0; i < numDigits; i++) {
modelURL: numberUrl(0), bestDigitsId[i] = entityManager.add({
position: Vec3.sum(topLeft, digitPosition(0)), type: "Model",
rotation: space.orientation, modelURL: numberUrl(0),
dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } position: Vec3.sum(topLeft, digitPosition(i)),
}); rotation: space.orientation,
var best1 = entityManager.add({ dimensions: numberDimensions
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({ var scoreId = entityManager.add({
type: "Model", type: "Model",
@ -370,27 +364,16 @@
rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)), rotation: Quat.multiply(space.orientation, Quat.fromPitchYawRollDegrees(90, 0, 0)),
dimensions: { x: 0.3678, y: 0.0063, z: 0.1037 } dimensions: { x: 0.3678, y: 0.0063, z: 0.1037 }
}); });
var score0 = entityManager.add({ var scoreDigitsId = []
type: "Model", for (var i = 0; i < numDigits; i++) {
modelURL: numberUrl(0), scoreDigitsId[i] = entityManager.add({
position: Vec3.sum(bottomLeft, digitPosition(0)), type: "Model",
rotation: space.orientation, modelURL: numberUrl(0),
dimensions: { x: 0.0660, y: 0.1050, z: 0.0048 } position: Vec3.sum(bottomLeft, digitPosition(i)),
}); rotation: space.orientation,
var score1 = entityManager.add({ dimensions: numberDimensions
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) { this.moveScore = function(delta) {
score += delta; score += delta;
@ -403,13 +386,13 @@
} }
this.draw = function() { this.draw = function() {
Entities.editEntity(best0, { modelURL: numberUrl(Math.floor((highScore / 100) % 10)) }); for (var i = 0; i < numDigits; i++) {
Entities.editEntity(best1, { modelURL: numberUrl(Math.floor((highScore / 10) % 10)) }); Entities.editEntity(bestDigitsId[i], { modelURL: numberUrl(Math.floor((highScore / Math.pow(10, numDigits- i - 1)) % 10)) });
Entities.editEntity(best2, { modelURL: numberUrl(Math.floor((highScore / 1) % 10)) }); }
Entities.editEntity(score0, { modelURL: numberUrl(Math.floor((score / 100) % 10)) }); for (var i = 0; i < numDigits; i++) {
Entities.editEntity(score1, { modelURL: numberUrl(Math.floor((score / 10) % 10)) }); Entities.editEntity(scoreDigitsId[i], { modelURL: numberUrl(Math.floor(score / Math.pow(10, numDigits - i - 1)) % 10) });
Entities.editEntity(score2, { modelURL: numberUrl(Math.floor((score / 1) % 10)) }); }
} }
} }
@ -444,12 +427,6 @@
draw(); draw();
timestamp = now; timestamp = now;
} }
// this.keyPressed = function(event) {
// if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) {
// isJumping = true;
// startedPlaying = true;
// }
// }
// Constants // Constants
var spaceDimensions = { x: 2.0, y: 1.5, z: 0.01 }; var spaceDimensions = { x: 2.0, y: 1.5, z: 0.01 };
@ -471,9 +448,8 @@
var lastTriggerValue = 0.0; var lastTriggerValue = 0.0;
var TRIGGER_THRESHOLD = 0.9; var TRIGGER_THRESHOLD = 0.9;
var space = null var space = null;
var board = null; var avatar = null;
var bird = null;
var pipes = null; var pipes = null;
var score = null; var score = null;
@ -490,8 +466,7 @@
current = 0; current = 0;
} }
if (current === sequence.length) { if (current === sequence.length) {
print("KONAMI CODE!!!"); avatar.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx");
bird.changeModel("https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/mario.fbx");
current = 0; current = 0;
} }
} }
@ -510,24 +485,14 @@
} }
function setup() { function setup() {
print("setup");
space = { space = {
position: getSpacePosition(), position: getSpacePosition(),
orientation: getSpaceOrientation(), orientation: getSpaceOrientation(),
dimensions: getSpaceDimensions() 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)); 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); pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition, moveScore);
score = new Score(space, bestScore); score = new Score(space, bestScore);
@ -536,7 +501,7 @@
function inputs(triggerValue) { function inputs(triggerValue) {
if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) { if (!startedPlaying && !isBoardReset && (gameTime - lastLost) > coolDown) {
score.resetScore(); score.resetScore();
bird.reset(); avatar.reset();
pipes.clear(); pipes.clear();
isBoardReset = true; isBoardReset = true;
@ -551,8 +516,6 @@
lastTriggerValue = triggerValue; lastTriggerValue = triggerValue;
} }
function update(deltaTime) { function update(deltaTime) {
//print("update: " + deltaTime);
// Keep entities alive // Keep entities alive
entityManager.update(deltaTime); entityManager.update(deltaTime);
@ -560,29 +523,27 @@
return; return;
} }
// Update Bird // Update Avatar
if (!startedPlaying && bird.position().y < spaceDimensions.y / 2.0) { if (!startedPlaying && avatar.position().y < spaceDimensions.y / 2.0) {
isJumping = true; isJumping = true;
} }
// Apply jumps // Apply jumps
if (isJumping) { if (isJumping) {
bird.jump(); avatar.jump();
isJumping = false; isJumping = false;
} }
bird.update(deltaTime); avatar.update(deltaTime);
pipes.update(deltaTime, gameTime, startedPlaying); pipes.update(deltaTime, gameTime, startedPlaying);
// Check lost // Check lost
var hasLost = bird.position().y < 0.0 || var hasLost = avatar.position().y < 0.0 ||
bird.position().y > space.dimensions.y || avatar.position().y > space.dimensions.y ||
pipes.isColliding(bird); pipes.isColliding(avatar);
// Cleanup // Cleanup
if (hasLost) { if (hasLost) {
print("Game Over!");
if (gameOverSound.downloaded && !injector) { if (gameOverSound.downloaded && !injector) {
injector = Audio.playSound(gameOverSound, { position: space.position, volume: 0.4 }); injector = Audio.playSound(gameOverSound, { position: space.position, volume: 0.4 });
} else if (injector) { } else if (injector) {
@ -595,13 +556,11 @@
} }
} }
function draw() { function draw() {
//print("draw"); avatar.draw();
bird.draw();
pipes.draw(); pipes.draw();
score.draw(); score.draw();
} }
function cleanup() { function cleanup() {
print("cleanup");
entityManager.removeAll(); entityManager.removeAll();
Controller.keyPressEvent.disconnect(keyPress); Controller.keyPressEvent.disconnect(keyPress);