From 450e89df21fd5c56565342089d087694a6413420 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 10 Nov 2014 08:23:42 -0800 Subject: [PATCH 01/24] playChess script --- examples/playChess.js | 169 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 examples/playChess.js diff --git a/examples/playChess.js b/examples/playChess.js new file mode 100644 index 0000000000..660280c8f2 --- /dev/null +++ b/examples/playChess.js @@ -0,0 +1,169 @@ +// +// playChess.js +// examples +// +// Created by Clement Brisset on 11/08/2014 +// Copyright 2014 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 + +// Script namespace +var extraDebug = extraDebug || true; +var ChessGame = ChessGame || { + BOARD_SIZE: 8, + MAX_PLAYERS: 2, + ROWS: ['1', '2', '3', '4', '5', '6', '7', '8'], + COLUMNS: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], + + whitesTileColor: {red: 250, green: 195, blue: 135}, + blacksTileColor: {red: 190, green: 115, blue: 50}, + whiteKingURL: "http://public.highfidelity.io/models/attachments/King Piece.fst", + whiteKingDimensions: { x: 0.46, y: 1.0, z: 0.46 }, + blackKingURL: "http://public.highfidelity.io/models/attachments/King Piece.fst", + blackKingDimensions: { x: 0.46, y: 1.0, z: 0.46 }, + + board: null, + pieces: null +}; + + +// Board class +ChessGame.Board = (function(position, size) { + this.position = position; + this.size = size; + this.tileSize = this.size / ChessGame.BOARD_SIZE; + this.height = this.tileSize * 0.2; + this.tiles = new Array(); + + this.spawn(); +}); +// Applies operation to each tile where operation = function(i, j) +ChessGame.Board.prototype.apply = function(operation) { + for (var i = 0; i < ChessGame.BOARD_SIZE; i++) { + for (var j = 0; j < ChessGame.BOARD_SIZE; j++) { + operation.call(this, i, j); + } + } +} +// Returns tile position +ChessGame.Board.prototype.tilePosition = function(i, j) { + return { x: this.position.x + i * this.tileSize, + y: this.position.y, + z: this.position.z + j * this.tileSize }; +} +// Checks the color of the tile +ChessGame.Board.prototype.isWhite = function(x, y) { + return (x + y) % 2 != 0; +} +// Spawns the board using entities +ChessGame.Board.prototype.spawn = function() { + if (extraDebug) { + print("Spawning board..."); + } + + this.apply(function(i, j) { + var tile = Entities.addEntity({ + type: "Box", + position: this.tilePosition(i, j), + dimensions: { x: this.tileSize, y: this.height, z: this.tileSize }, + color: (this.isWhite(i, j)) ? ChessGame.whitesTileColor : ChessGame.blacksTileColor + }); + + if (j === 0) { + // Create new row if needed + this.tiles.push(new Array()); + } + this.tiles[i].push(tile); // Store tile + }); + print("Board spawned"); +} +// Cleans up the entities of the board +ChessGame.Board.prototype.cleanup = function() { + if (extraDebug) { + print("Cleaning up board..."); + } + + this.apply(function(i, j) { + Entities.deleteEntity(this.tiles[i][j]); + }); + print("Board cleaned up"); +} + +// Piece class +ChessGame.Piece = (function(position, size, url) { + this.position = position; + this.size = size; + this.entityProperties = { + type: "Model", + position: this.position, + dimensions: this.size, + modelURL: url + } + this.entity = null; +}); +// Spawns the piece +ChessGame.Piece.prototype.spawn = function() { + this.entity = Entities.addEntity(this.entityProperties); +} +// Cleans up the piece +ChessGame.Piece.prototype.cleanup = function() { + Entities.deleteEntity(this.entity); +} + + + + +// Player class +ChessGame.Player = (function() { + +}); + +ChessGame.update = function() { + +} + +ChessGame.scriptStarting = function() { + print("playChess.js started"); + ChessGame.board = new ChessGame.Board({ x: 1, y: 1, z: 1 }, 1); + ChessGame.pieces = new Array(); + + var url = "http://public.highfidelity.io/models/attachments/King Piece.fst"; + var dimensions = { x: 0.46, y: 1.0, z: 0.46 }; + + var rowsIndex = [0, 1, 6, 7]; + for(var i in rowsIndex) { + for(var j = 0; j < ChessGame.BOARD_SIZE; j++) { + var size = Vec3.multiply(dimensions, ChessGame.board.tileSize); + if (rowsIndex[i] == 1 || rowsIndex[i] == 6) { + size = Vec3.multiply(size, 0.7); + } + var position = Vec3.sum(ChessGame.board.tilePosition(rowsIndex[i], j), + { x: 0, y: (size.y + ChessGame.board.height) / 2.0, z: 0}); + var color = (rowsIndex[i] < 4) ? ChessGame.whitesTileColor : ChessGame.blacksTileColor; + + var piece = new ChessGame.Piece(position, size, url); + piece.spawn(); + ChessGame.pieces.push(piece); + } + } +} + +ChessGame.scriptEnding = function() { + // Cleaning up board + ChessGame.board.cleanup(); + delete ChessGame.board; + + // Cleaning up pieces + for(var i in ChessGame.pieces) { + ChessGame.pieces[i].cleanup(); + } + delete ChessGame.pieces; + + print("playChess.js finished"); +} + + +Script.update.connect(ChessGame.update) +Script.scriptEnding.connect(ChessGame.scriptEnding); +ChessGame.scriptStarting(); \ No newline at end of file From ba67b042793fe55b14e44dbcd9e901a2b15eff2d Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 10 Nov 2014 10:18:37 -0800 Subject: [PATCH 02/24] Switch members in EntityItemProperties to hack around corruption bug --- libraries/entities/src/EntityItemProperties.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 3ae2aa9c7a..6b22e8cba9 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -383,8 +383,8 @@ private: // NOTE: The following are pseudo client only properties. They are only used in clients which can access // properties of model geometry. But these properties are not serialized like other properties. QVector _sittingPoints; - glm::vec3 _naturalDimensions; QStringList _textureNames; + glm::vec3 _naturalDimensions; }; Q_DECLARE_METATYPE(EntityItemProperties); QScriptValue EntityItemPropertiesToScriptValue(QScriptEngine* engine, const EntityItemProperties& properties); From e4975c0e2bae6f72152727988421e1b1df1f94ba Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 11 Nov 2014 17:08:35 -0800 Subject: [PATCH 03/24] chessPiece script with sound --- examples/entityScripts/chessPiece.js | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/entityScripts/chessPiece.js diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js new file mode 100644 index 0000000000..5342e9cd4f --- /dev/null +++ b/examples/entityScripts/chessPiece.js @@ -0,0 +1,77 @@ +(function(){ + this.sound = null; + this.entityID = null; + this.properties = null; + this.updateProperties = function(entityID) { + if (this.entityID === null) { + this.entityID = entityID; + print("entityID=" + JSON.stringify(this.entityID)); + } + if (!entityID.isKnownID || this.entityID.id !== entityID.id) { + print("Something is very wrong: Bailing!"); + return; + } + this.properties = Entities.getEntityProperties(this.entityID); + if (!this.properties.isKnownID) { + print("Unknown entityID " + this.entityID.id + " should be self.") + } + } + this.updatePosition = function(mouseEvent) { + var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y) + var upVector = { x: 0, y: 1, z: 0 }; + var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction, + this.properties.position, upVector); + Entities.editEntity(this.entityID, { position: intersection }); + } + this.snapToGrid = function() { + var position = { x: 1, y: 1, z: 1 }; + var size = 1.0; + var tileSize = size / 8.0; + var height = tileSize * 0.2; + + + var relative = Vec3.subtract(this.properties.position, position); + var i = Math.floor(relative.x / tileSize); + var j = Math.floor(relative.z / tileSize); + + relative.x = (i + 0.5) * tileSize; + relative.z = (j + 0.5) * tileSize; + var finalPos = Vec3.sum(position, relative); + Entities.editEntity(this.entityID, { position: finalPos }); + } + // Pr, Vr are respectively the Ray's Point of origin and Vector director + // Pp, Np are respectively the Plane's Point of origin and Normal vector + this.rayPlaneIntersection = function(Pr, Vr, Pp, Np) { + var d = -Vec3.dot(Pp, Np); + var t = -(Vec3.dot(Pr, Np) + d) / Vec3.dot(Vr, Np); + return Vec3.sum(Pr, Vec3.multiply(t, Vr)); + } + this.maybeDownloadSound = function() { + if (this.sound === null) { + this.sound = new Sound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); + } + } + this.playSound = function() { + var options = new AudioInjectionOptions(); + options.position = this.properties.position; + options.volume = 0.5; + Audio.playSound(this.sound, options); + } + + + this.clickDownOnEntity = function(entityID, mouseEvent){ + this.maybeDownloadSound(); + this.updateProperties(entityID); + this.updatePosition(mouseEvent); + }; + this.holdingClickOnEntity = function(entityID, mouseEvent){ + this.updateProperties(entityID); + this.updatePosition(mouseEvent); + }; + this.clickReleaseOnEntity = function(entityID, mouseEvent){ + this.updateProperties(entityID); + this.updatePosition(mouseEvent); + this.snapToGrid(); + this.playSound(); + }; +}) \ No newline at end of file From f80068798f014aeef345bde3fca3f5e63fc2cada Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 11 Nov 2014 17:09:08 -0800 Subject: [PATCH 04/24] playChess script with autonomous pieces --- examples/playChess.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index 660280c8f2..deb9cb4501 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -30,13 +30,16 @@ var ChessGame = ChessGame || { // Board class ChessGame.Board = (function(position, size) { - this.position = position; this.size = size; this.tileSize = this.size / ChessGame.BOARD_SIZE; this.height = this.tileSize * 0.2; + + this.position = position; + this.position.x += this.tileSize / 2.0; + this.position.y += this.height / 2.0; + this.position.z += this.tileSize / 2.0; + this.tiles = new Array(); - - this.spawn(); }); // Applies operation to each tile where operation = function(i, j) ChessGame.Board.prototype.apply = function(operation) { @@ -98,7 +101,8 @@ ChessGame.Piece = (function(position, size, url) { type: "Model", position: this.position, dimensions: this.size, - modelURL: url + modelURL: url, + script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" } this.entity = null; }); @@ -113,7 +117,6 @@ ChessGame.Piece.prototype.cleanup = function() { - // Player class ChessGame.Player = (function() { @@ -147,6 +150,8 @@ ChessGame.scriptStarting = function() { ChessGame.pieces.push(piece); } } + + ChessGame.board.spawn(); } ChessGame.scriptEnding = function() { From b4585163dbb0c7c5d39fc7f2df207580cbee7974 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 14:14:37 -0800 Subject: [PATCH 05/24] Global board position / piece boundaries --- examples/entityScripts/chessPiece.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index 5342e9cd4f..fe68e2a13b 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -1,4 +1,6 @@ (function(){ + this.GRID_POSITION = { x: 1, y: 1, z: 1 }; + this.GRID_SIZE = 1.0; this.sound = null; this.entityID = null; this.properties = null; @@ -24,15 +26,16 @@ Entities.editEntity(this.entityID, { position: intersection }); } this.snapToGrid = function() { - var position = { x: 1, y: 1, z: 1 }; - var size = 1.0; + var position = this.GRID_POSITION; + var size = this.GRID_SIZE; var tileSize = size / 8.0; var height = tileSize * 0.2; - var relative = Vec3.subtract(this.properties.position, position); var i = Math.floor(relative.x / tileSize); var j = Math.floor(relative.z / tileSize); + i = Math.min(Math.max(0, i), 7); + j = Math.min(Math.max(-1, j), 8); relative.x = (i + 0.5) * tileSize; relative.z = (j + 0.5) * tileSize; @@ -58,7 +61,6 @@ Audio.playSound(this.sound, options); } - this.clickDownOnEntity = function(entityID, mouseEvent){ this.maybeDownloadSound(); this.updateProperties(entityID); From d25af3b4c9428c96ee0c7ec45f8a2dd4daa37088 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 14:15:18 -0800 Subject: [PATCH 06/24] Actual piece meshes --- examples/playChess.js | 183 +++++++++++++++++++++++++++++++++--------- 1 file changed, 147 insertions(+), 36 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index deb9cb4501..0ffaca5bb2 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -16,15 +16,57 @@ var ChessGame = ChessGame || { ROWS: ['1', '2', '3', '4', '5', '6', '7', '8'], COLUMNS: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - whitesTileColor: {red: 250, green: 195, blue: 135}, - blacksTileColor: {red: 190, green: 115, blue: 50}, - whiteKingURL: "http://public.highfidelity.io/models/attachments/King Piece.fst", - whiteKingDimensions: { x: 0.46, y: 1.0, z: 0.46 }, - blackKingURL: "http://public.highfidelity.io/models/attachments/King Piece.fst", - blackKingDimensions: { x: 0.46, y: 1.0, z: 0.46 }, - + whitesTileColor: { red: 250, green: 195, blue: 135 }, + blacksTileColor: { red: 190, green: 115, blue: 50 }, + board: null, - pieces: null + pieces: { + all: new Array(), + king: { + whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/King_White.fbx", + blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/King_Black.fbx", + dimensions: { x: 110.496, y: 306.713, z: 110.496 }, + rotation: Quat.fromPitchYawRollDegrees(0, 90, 0) + }, + queen: { + whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Queen_White.fbx", + blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Queen_Black.fbx", + dimensions: { x: 110.496, y: 257.459, z: 110.496 }, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0) + }, + bishop: { + whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Bishop_White.fbx", + blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Bishop_Black.fbx", + dimensions: { x: 102.002, y: 213.941, z: 102.002 }, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + white: [], + black: [] + }, + knight: { + whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Knight_White.fbx", + blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Knight_Black.fbx", + dimensions: { x: 95.602, y: 186.939, z: 95.602 }, + rotation: Quat.fromPitchYawRollDegrees(0, 180, 0), + white: [], + black: [] + }, + rook: { + whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Rook_White.fbx", + blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Rook_Black.fbx", + dimensions: { x: 101.024, y: 167.523, z: 101.024 }, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + white: [], + black: [] + }, + pawn: { + whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Pawn_White.fbx", + blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Pawn_Black.fbx", + dimensions: { x: 93.317, y: 138.747, z: 93.317 }, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + white: [], + black: [] + } + } }; @@ -94,14 +136,16 @@ ChessGame.Board.prototype.cleanup = function() { } // Piece class -ChessGame.Piece = (function(position, size, url) { +ChessGame.Piece = (function(position, size, url, rotation) { this.position = position; this.size = size; this.entityProperties = { type: "Model", position: this.position, + rotation: rotation, dimensions: this.size, modelURL: url, + //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js" script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" } this.entity = null; @@ -110,11 +154,30 @@ ChessGame.Piece = (function(position, size, url) { ChessGame.Piece.prototype.spawn = function() { this.entity = Entities.addEntity(this.entityProperties); } +// Updates the metadata stored by the piece +ChessGame.Piece.prototype.updateMetaData = function(metaDataString) { + Entities.editEntity(this.entity, { animationURL: metaDataString }); +} // Cleans up the piece ChessGame.Piece.prototype.cleanup = function() { Entities.deleteEntity(this.entity); } - +ChessGame.makePiece = function(properties, i, j, isWhite) { + var url = (isWhite) ? properties.whiteURL : properties.blackURL; + var size = Vec3.multiply(1.0 / ChessGame.pieces.king.dimensions.y, + properties.dimensions); + size = Vec3.multiply(1.5 * ChessGame.board.tileSize, size); + var rotation = (isWhite) ? properties.rotation + : Quat.multiply(Quat.fromPitchYawRollDegrees(0, 180, 0), + properties.rotation); + var position = Vec3.sum(ChessGame.board.tilePosition(i, j), + { x: 0, y: (size.y + ChessGame.board.height) / 2.0, z: 0}); + + var piece = new ChessGame.Piece(position, size, url, rotation); + piece.spawn(); + ChessGame.pieces.all.push(piece); + return piece; +} // Player class @@ -125,33 +188,81 @@ ChessGame.Player = (function() { ChessGame.update = function() { } +ChessGame.buildMetaDataString = function() { + var metadataObject = { + board: { + position: ChessGame.board.position, + size: ChessGame.board.size + } + }; + + return JSON.stringify(metadataObject); +} + +ChessGame.buildBoard = function() { + // Setup board + ChessGame.board = new ChessGame.Board({ x: 1, y: 1, z: 1 }, 1); + ChessGame.board.spawn(); + + // Setup white pieces + // King + var piece = ChessGame.makePiece(ChessGame.pieces.king, 0, 3, true); + ChessGame.pieces.king.white = piece; + // Queen + piece = ChessGame.makePiece(ChessGame.pieces.queen, 0, 4, true); + ChessGame.pieces.queen.white = piece; + // Bishop + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 0, 2, true); + ChessGame.pieces.bishop.white.push(piece); + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 0, 5, true); + ChessGame.pieces.bishop.white.push(piece); + // Knight + piece = ChessGame.makePiece(ChessGame.pieces.knight, 0, 1, true); + ChessGame.pieces.knight.white.push(piece); + piece = ChessGame.makePiece(ChessGame.pieces.knight, 0, 6, true); + ChessGame.pieces.knight.white.push(piece); + // Rook + piece = ChessGame.makePiece(ChessGame.pieces.rook, 0, 0, true); + ChessGame.pieces.rook.white.push(piece); + piece = ChessGame.makePiece(ChessGame.pieces.rook, 0, 7, true); + ChessGame.pieces.rook.white.push(piece); + for(var j = 0; j < ChessGame.BOARD_SIZE; j++) { + piece = ChessGame.makePiece(ChessGame.pieces.pawn, 1, j, true); + ChessGame.pieces.pawn.white.push(piece); + } + + // Setup black pieces + // King + var piece = ChessGame.makePiece(ChessGame.pieces.king, 7, 3, false); + ChessGame.pieces.king.white = piece; + // Queen + piece = ChessGame.makePiece(ChessGame.pieces.queen, 7, 4, false); + ChessGame.pieces.queen.white = piece; + // Bishop + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 7, 2, false); + ChessGame.pieces.bishop.white.push(piece); + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 7, 5, false); + ChessGame.pieces.bishop.white.push(piece); + // Knight + piece = ChessGame.makePiece(ChessGame.pieces.knight, 7, 1, false); + ChessGame.pieces.knight.white.push(piece); + piece = ChessGame.makePiece(ChessGame.pieces.knight, 7, 6, false); + ChessGame.pieces.knight.white.push(piece); + // Rook + piece = ChessGame.makePiece(ChessGame.pieces.rook, 7, 0, false); + ChessGame.pieces.rook.white.push(piece); + piece = ChessGame.makePiece(ChessGame.pieces.rook, 7, 7, false); + ChessGame.pieces.rook.white.push(piece); + for(var j = 0; j < ChessGame.BOARD_SIZE; j++) { + piece = ChessGame.makePiece(ChessGame.pieces.pawn, 6, j, false); + ChessGame.pieces.pawn.black.push(piece); + } +} + ChessGame.scriptStarting = function() { print("playChess.js started"); - ChessGame.board = new ChessGame.Board({ x: 1, y: 1, z: 1 }, 1); - ChessGame.pieces = new Array(); - - var url = "http://public.highfidelity.io/models/attachments/King Piece.fst"; - var dimensions = { x: 0.46, y: 1.0, z: 0.46 }; - - var rowsIndex = [0, 1, 6, 7]; - for(var i in rowsIndex) { - for(var j = 0; j < ChessGame.BOARD_SIZE; j++) { - var size = Vec3.multiply(dimensions, ChessGame.board.tileSize); - if (rowsIndex[i] == 1 || rowsIndex[i] == 6) { - size = Vec3.multiply(size, 0.7); - } - var position = Vec3.sum(ChessGame.board.tilePosition(rowsIndex[i], j), - { x: 0, y: (size.y + ChessGame.board.height) / 2.0, z: 0}); - var color = (rowsIndex[i] < 4) ? ChessGame.whitesTileColor : ChessGame.blacksTileColor; - - var piece = new ChessGame.Piece(position, size, url); - piece.spawn(); - ChessGame.pieces.push(piece); - } - } - - ChessGame.board.spawn(); + ChessGame.buildBoard(); } ChessGame.scriptEnding = function() { @@ -160,8 +271,8 @@ ChessGame.scriptEnding = function() { delete ChessGame.board; // Cleaning up pieces - for(var i in ChessGame.pieces) { - ChessGame.pieces[i].cleanup(); + for(var i in ChessGame.pieces.all) { + ChessGame.pieces.all[i].cleanup(); } delete ChessGame.pieces; From 76d45953fd24c0ff8a54c01b2e91be525103b4a1 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 15:34:14 -0800 Subject: [PATCH 07/24] switch to mesh board --- examples/playChess.js | 107 +++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 64 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index 0ffaca5bb2..35676af21b 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -20,6 +20,9 @@ var ChessGame = ChessGame || { blacksTileColor: { red: 190, green: 115, blue: 50 }, board: null, + boardURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Board.fbx", + boardDimensions: { x: 773.191, y: 20.010, z: 773.191 }, + sizeFactor: 1.0 / 773.191, pieces: { all: new Array(), king: { @@ -38,7 +41,7 @@ var ChessGame = ChessGame || { whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Bishop_White.fbx", blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Bishop_Black.fbx", dimensions: { x: 102.002, y: 213.941, z: 102.002 }, - rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + rotation: Quat.fromPitchYawRollDegrees(0, 90, 0), white: [], black: [] }, @@ -72,30 +75,18 @@ var ChessGame = ChessGame || { // Board class ChessGame.Board = (function(position, size) { - this.size = size; - this.tileSize = this.size / ChessGame.BOARD_SIZE; - this.height = this.tileSize * 0.2; + this.tileSize = size / 10.0; + this.dimensions = Vec3.multiply(ChessGame.sizeFactor * size, + ChessGame.boardDimensions); + this.position = Vec3.sum(position, Vec3.multiply(0.5, this.dimensions)); - this.position = position; - this.position.x += this.tileSize / 2.0; - this.position.y += this.height / 2.0; - this.position.z += this.tileSize / 2.0; - - this.tiles = new Array(); + this.entity = null; }); -// Applies operation to each tile where operation = function(i, j) -ChessGame.Board.prototype.apply = function(operation) { - for (var i = 0; i < ChessGame.BOARD_SIZE; i++) { - for (var j = 0; j < ChessGame.BOARD_SIZE; j++) { - operation.call(this, i, j); - } - } -} // Returns tile position ChessGame.Board.prototype.tilePosition = function(i, j) { - return { x: this.position.x + i * this.tileSize, - y: this.position.y, - z: this.position.z + j * this.tileSize }; + return { x: this.position.x + (i - 5.0 + 0.5) * this.tileSize, + y: this.position.y + this.dimensions.y / 2.0, + z: this.position.z + (j - 5.0 + 0.5) * this.tileSize }; } // Checks the color of the tile ChessGame.Board.prototype.isWhite = function(x, y) { @@ -107,20 +98,13 @@ ChessGame.Board.prototype.spawn = function() { print("Spawning board..."); } - this.apply(function(i, j) { - var tile = Entities.addEntity({ - type: "Box", - position: this.tilePosition(i, j), - dimensions: { x: this.tileSize, y: this.height, z: this.tileSize }, - color: (this.isWhite(i, j)) ? ChessGame.whitesTileColor : ChessGame.blacksTileColor - }); - - if (j === 0) { - // Create new row if needed - this.tiles.push(new Array()); - } - this.tiles[i].push(tile); // Store tile + this.entity = Entities.addEntity({ + type: "Model", + modelURL: ChessGame.boardURL, + position: this.position, + dimensions: this.dimensions }); + print("Board spawned"); } // Cleans up the entities of the board @@ -128,17 +112,15 @@ ChessGame.Board.prototype.cleanup = function() { if (extraDebug) { print("Cleaning up board..."); } - - this.apply(function(i, j) { - Entities.deleteEntity(this.tiles[i][j]); - }); + Entities.deleteEntity(this.entity); print("Board cleaned up"); } // Piece class ChessGame.Piece = (function(position, size, url, rotation) { - this.position = position; this.size = size; + this.position = position; + this.position.y += this.size.y / 2.0; this.entityProperties = { type: "Model", position: this.position, @@ -164,14 +146,11 @@ ChessGame.Piece.prototype.cleanup = function() { } ChessGame.makePiece = function(properties, i, j, isWhite) { var url = (isWhite) ? properties.whiteURL : properties.blackURL; - var size = Vec3.multiply(1.0 / ChessGame.pieces.king.dimensions.y, - properties.dimensions); - size = Vec3.multiply(1.5 * ChessGame.board.tileSize, size); + var size = Vec3.multiply(0.5 * ChessGame.sizeFactor, properties.dimensions); var rotation = (isWhite) ? properties.rotation : Quat.multiply(Quat.fromPitchYawRollDegrees(0, 180, 0), properties.rotation); - var position = Vec3.sum(ChessGame.board.tilePosition(i, j), - { x: 0, y: (size.y + ChessGame.board.height) / 2.0, z: 0}); + var position = ChessGame.board.tilePosition(i, j); var piece = new ChessGame.Piece(position, size, url, rotation); piece.spawn(); @@ -206,55 +185,55 @@ ChessGame.buildBoard = function() { // Setup white pieces // King - var piece = ChessGame.makePiece(ChessGame.pieces.king, 0, 3, true); + var piece = ChessGame.makePiece(ChessGame.pieces.king, 1, 4, true); ChessGame.pieces.king.white = piece; // Queen - piece = ChessGame.makePiece(ChessGame.pieces.queen, 0, 4, true); + piece = ChessGame.makePiece(ChessGame.pieces.queen, 1, 5, true); ChessGame.pieces.queen.white = piece; // Bishop - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 0, 2, true); + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 1, 3, true); ChessGame.pieces.bishop.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 0, 5, true); + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 1, 6, true); ChessGame.pieces.bishop.white.push(piece); // Knight - piece = ChessGame.makePiece(ChessGame.pieces.knight, 0, 1, true); + piece = ChessGame.makePiece(ChessGame.pieces.knight, 1, 2, true); ChessGame.pieces.knight.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.knight, 0, 6, true); + piece = ChessGame.makePiece(ChessGame.pieces.knight, 1, 7, true); ChessGame.pieces.knight.white.push(piece); // Rook - piece = ChessGame.makePiece(ChessGame.pieces.rook, 0, 0, true); + piece = ChessGame.makePiece(ChessGame.pieces.rook, 1, 1, true); ChessGame.pieces.rook.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.rook, 0, 7, true); + piece = ChessGame.makePiece(ChessGame.pieces.rook, 1, 8, true); ChessGame.pieces.rook.white.push(piece); - for(var j = 0; j < ChessGame.BOARD_SIZE; j++) { - piece = ChessGame.makePiece(ChessGame.pieces.pawn, 1, j, true); + for(var j = 1; j <= ChessGame.BOARD_SIZE; j++) { + piece = ChessGame.makePiece(ChessGame.pieces.pawn, 2, j, true); ChessGame.pieces.pawn.white.push(piece); } // Setup black pieces // King - var piece = ChessGame.makePiece(ChessGame.pieces.king, 7, 3, false); + var piece = ChessGame.makePiece(ChessGame.pieces.king, 8, 4, false); ChessGame.pieces.king.white = piece; // Queen - piece = ChessGame.makePiece(ChessGame.pieces.queen, 7, 4, false); + piece = ChessGame.makePiece(ChessGame.pieces.queen, 8, 5, false); ChessGame.pieces.queen.white = piece; // Bishop - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 7, 2, false); + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 8, 3, false); ChessGame.pieces.bishop.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 7, 5, false); + piece = ChessGame.makePiece(ChessGame.pieces.bishop, 8, 6, false); ChessGame.pieces.bishop.white.push(piece); // Knight - piece = ChessGame.makePiece(ChessGame.pieces.knight, 7, 1, false); + piece = ChessGame.makePiece(ChessGame.pieces.knight, 8, 2, false); ChessGame.pieces.knight.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.knight, 7, 6, false); + piece = ChessGame.makePiece(ChessGame.pieces.knight, 8, 7, false); ChessGame.pieces.knight.white.push(piece); // Rook - piece = ChessGame.makePiece(ChessGame.pieces.rook, 7, 0, false); + piece = ChessGame.makePiece(ChessGame.pieces.rook, 8, 1, false); ChessGame.pieces.rook.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.rook, 7, 7, false); + piece = ChessGame.makePiece(ChessGame.pieces.rook, 8, 8, false); ChessGame.pieces.rook.white.push(piece); - for(var j = 0; j < ChessGame.BOARD_SIZE; j++) { - piece = ChessGame.makePiece(ChessGame.pieces.pawn, 6, j, false); + for(var j = 1; j <= ChessGame.BOARD_SIZE; j++) { + piece = ChessGame.makePiece(ChessGame.pieces.pawn, 7, j, false); ChessGame.pieces.pawn.black.push(piece); } } From 6f8b702334bde740a3b905d383200bdc6e5ed981 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 15:34:39 -0800 Subject: [PATCH 08/24] New grid --- examples/entityScripts/chessPiece.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index fe68e2a13b..70a4a82c3b 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -28,14 +28,13 @@ this.snapToGrid = function() { var position = this.GRID_POSITION; var size = this.GRID_SIZE; - var tileSize = size / 8.0; - var height = tileSize * 0.2; + var tileSize = size / 10.0; var relative = Vec3.subtract(this.properties.position, position); var i = Math.floor(relative.x / tileSize); var j = Math.floor(relative.z / tileSize); - i = Math.min(Math.max(0, i), 7); - j = Math.min(Math.max(-1, j), 8); + i = Math.min(Math.max(1, i), 8); + j = Math.min(Math.max(0, j), 10); relative.x = (i + 0.5) * tileSize; relative.z = (j + 0.5) * tileSize; From 7193fcc89dfc523a62a8be5a56306382ae0c6638 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 17:12:27 -0800 Subject: [PATCH 09/24] Metadata for pieces --- examples/entityScripts/chessPiece.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index 70a4a82c3b..13d36d28fb 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -34,7 +34,7 @@ var i = Math.floor(relative.x / tileSize); var j = Math.floor(relative.z / tileSize); i = Math.min(Math.max(1, i), 8); - j = Math.min(Math.max(0, j), 10); + j = Math.min(Math.max(0, j), 9); relative.x = (i + 0.5) * tileSize; relative.z = (j + 0.5) * tileSize; @@ -59,10 +59,20 @@ options.volume = 0.5; Audio.playSound(this.sound, options); } + this.getMetadata = function() { + var metadataObject = JSON.parse(this.properties.animationURL); + if (metadataObject.gamePosition) { + this.GRID_POSITION = metadataObject.gamePosition; + } + if (metadataObject.gameSize) { + this.GRID_SIZE = metadataObject.gameSize; + } + } this.clickDownOnEntity = function(entityID, mouseEvent){ this.maybeDownloadSound(); this.updateProperties(entityID); + this.getMetadata(); this.updatePosition(mouseEvent); }; this.holdingClickOnEntity = function(entityID, mouseEvent){ From bada1d8e38d29d9099666557b30287627b2148e2 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 17:13:29 -0800 Subject: [PATCH 10/24] Cleaner code + metadata --- examples/playChess.js | 250 +++++++++++++++++++----------------------- 1 file changed, 114 insertions(+), 136 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index 35676af21b..ecfe1d12ae 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -8,89 +8,86 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +var gamePosition = { x: 1.0, y: 1.0, z: 1.0 }; +var gameSize = 1.0; + // Script namespace var extraDebug = extraDebug || true; var ChessGame = ChessGame || { - BOARD_SIZE: 8, - MAX_PLAYERS: 2, - ROWS: ['1', '2', '3', '4', '5', '6', '7', '8'], - COLUMNS: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], + BOARD: { + modelURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Board.fbx", + dimensions: { x: 773.191, y: 20.010, z: 773.191 }, + rotation: Quat.fromPitchYawRollDegrees(0, 90, 0), + numTiles: 10.0 + }, + KING: 0, QUEEN: 1, BISHOP: 2, KNIGHT: 3, ROOK: 4, PAWN: 5, - whitesTileColor: { red: 250, green: 195, blue: 135 }, - blacksTileColor: { red: 190, green: 115, blue: 50 }, - board: null, - boardURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Board.fbx", - boardDimensions: { x: 773.191, y: 20.010, z: 773.191 }, - sizeFactor: 1.0 / 773.191, - pieces: { - all: new Array(), - king: { - whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/King_White.fbx", - blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/King_Black.fbx", - dimensions: { x: 110.496, y: 306.713, z: 110.496 }, - rotation: Quat.fromPitchYawRollDegrees(0, 90, 0) - }, - queen: { - whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Queen_White.fbx", - blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Queen_Black.fbx", - dimensions: { x: 110.496, y: 257.459, z: 110.496 }, - rotation: Quat.fromPitchYawRollDegrees(0, 0, 0) - }, - bishop: { - whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Bishop_White.fbx", - blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Bishop_Black.fbx", - dimensions: { x: 102.002, y: 213.941, z: 102.002 }, - rotation: Quat.fromPitchYawRollDegrees(0, 90, 0), - white: [], - black: [] - }, - knight: { - whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Knight_White.fbx", - blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Knight_Black.fbx", - dimensions: { x: 95.602, y: 186.939, z: 95.602 }, - rotation: Quat.fromPitchYawRollDegrees(0, 180, 0), - white: [], - black: [] - }, - rook: { - whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Rook_White.fbx", - blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Rook_Black.fbx", - dimensions: { x: 101.024, y: 167.523, z: 101.024 }, - rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), - white: [], - black: [] - }, - pawn: { - whiteURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Pawn_White.fbx", - blackURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Pawn_Black.fbx", - dimensions: { x: 93.317, y: 138.747, z: 93.317 }, - rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), - white: [], - black: [] - } - } + pieces: new Array() }; +ChessGame.getPieceInfo = function(type, isWhite) { + var info = { + modelURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/", + dimensions: { x: 1.0, y: 1.0, z: 1.0 }, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0) + } + + switch(type) { + case ChessGame.KING: + info.modelURL += "King"; + info.dimensions = { x: 110.496, y: 306.713, z: 110.496 }; + info.rotation = Quat.fromPitchYawRollDegrees(0, 90, 0); + break; + case ChessGame.QUEEN: + info.modelURL += "Queen"; + info.dimensions = { x: 110.496, y: 257.459, z: 110.496 }; + break; + case ChessGame.BISHOP: + info.modelURL += "Bishop"; + info.dimensions = { x: 102.002, y: 213.941, z: 102.002 }; + info.rotation = Quat.fromPitchYawRollDegrees(0, 90, 0); + break; + case ChessGame.KNIGHT: + info.modelURL += "Knight"; + info.dimensions = { x: 95.602, y: 186.939, z: 95.602 }; + info.rotation = Quat.fromPitchYawRollDegrees(0, 180, 0); + break; + case ChessGame.ROOK: + info.modelURL += "Rook"; + info.dimensions = { x: 101.024, y: 167.523, z: 101.024 }; + break; + case ChessGame.PAWN: + info.modelURL += "Pawn"; + info.dimensions = { x: 93.317, y: 138.747, z: 93.317 }; + break; + } + info.modelURL += ((isWhite) ? "_White" : "_Black") + ".fbx" + + return info; +} + // Board class ChessGame.Board = (function(position, size) { - this.tileSize = size / 10.0; - this.dimensions = Vec3.multiply(ChessGame.sizeFactor * size, - ChessGame.boardDimensions); + this.dimensions = Vec3.multiply(1.0 / ChessGame.BOARD.dimensions.x, ChessGame.BOARD.dimensions); // 1 by 1 + this.dimensions = Vec3.multiply(size, this.dimensions); // size by size this.position = Vec3.sum(position, Vec3.multiply(0.5, this.dimensions)); + this.tileSize = size / ChessGame.BOARD.numTiles; this.entity = null; }); -// Returns tile position +// Returns the top center point of tile i,j ChessGame.Board.prototype.tilePosition = function(i, j) { - return { x: this.position.x + (i - 5.0 + 0.5) * this.tileSize, - y: this.position.y + this.dimensions.y / 2.0, - z: this.position.z + (j - 5.0 + 0.5) * this.tileSize }; + return { + x: this.position.x + (i - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize, + y: this.position.y + this.dimensions.y / 2.0, + z: this.position.z + (j - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize + }; } // Checks the color of the tile -ChessGame.Board.prototype.isWhite = function(x, y) { - return (x + y) % 2 != 0; +ChessGame.Board.prototype.isWhite = function(i, j) { + return (i + j) % 2 != 0; } // Spawns the board using entities ChessGame.Board.prototype.spawn = function() { @@ -100,7 +97,7 @@ ChessGame.Board.prototype.spawn = function() { this.entity = Entities.addEntity({ type: "Model", - modelURL: ChessGame.boardURL, + modelURL: ChessGame.BOARD.modelURL, position: this.position, dimensions: this.dimensions }); @@ -117,15 +114,15 @@ ChessGame.Board.prototype.cleanup = function() { } // Piece class -ChessGame.Piece = (function(position, size, url, rotation) { - this.size = size; +ChessGame.Piece = (function(position, dimensions, url, rotation) { + this.dimensions = dimensions; this.position = position; - this.position.y += this.size.y / 2.0; + this.position.y += this.dimensions.y / 2.0; this.entityProperties = { type: "Model", position: this.position, rotation: rotation, - dimensions: this.size, + dimensions: this.dimensions, modelURL: url, //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js" script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" @@ -137,110 +134,93 @@ ChessGame.Piece.prototype.spawn = function() { this.entity = Entities.addEntity(this.entityProperties); } // Updates the metadata stored by the piece -ChessGame.Piece.prototype.updateMetaData = function(metaDataString) { - Entities.editEntity(this.entity, { animationURL: metaDataString }); +ChessGame.Piece.prototype.updateMetadata = function(metadataString) { + Entities.editEntity(this.entity, { animationURL: metadataString }); } // Cleans up the piece ChessGame.Piece.prototype.cleanup = function() { Entities.deleteEntity(this.entity); } -ChessGame.makePiece = function(properties, i, j, isWhite) { - var url = (isWhite) ? properties.whiteURL : properties.blackURL; - var size = Vec3.multiply(0.5 * ChessGame.sizeFactor, properties.dimensions); - var rotation = (isWhite) ? properties.rotation +ChessGame.makePiece = function(piece, i, j, isWhite) { + var info = ChessGame.getPieceInfo(piece, isWhite); + var url = info.modelURL; + var size = Vec3.multiply(0.5 * (1.0 / ChessGame.BOARD.dimensions.x), info.dimensions); + var rotation = (isWhite) ? info.rotation : Quat.multiply(Quat.fromPitchYawRollDegrees(0, 180, 0), - properties.rotation); + info.rotation); var position = ChessGame.board.tilePosition(i, j); var piece = new ChessGame.Piece(position, size, url, rotation); piece.spawn(); - ChessGame.pieces.all.push(piece); + ChessGame.pieces.push(piece); return piece; } - -// Player class -ChessGame.Player = (function() { - -}); - -ChessGame.update = function() { - -} ChessGame.buildMetaDataString = function() { var metadataObject = { - board: { - position: ChessGame.board.position, - size: ChessGame.board.size - } + gamePosition: gamePosition, + gameSize: gameSize }; - return JSON.stringify(metadataObject); } ChessGame.buildBoard = function() { // Setup board - ChessGame.board = new ChessGame.Board({ x: 1, y: 1, z: 1 }, 1); + ChessGame.board = new ChessGame.Board(gamePosition, gameSize); ChessGame.board.spawn(); // Setup white pieces + var isWhite = true; + var row = 1; // King - var piece = ChessGame.makePiece(ChessGame.pieces.king, 1, 4, true); - ChessGame.pieces.king.white = piece; + var piece =ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); // Queen - piece = ChessGame.makePiece(ChessGame.pieces.queen, 1, 5, true); - ChessGame.pieces.queen.white = piece; + piece = ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); // Bishop - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 1, 3, true); - ChessGame.pieces.bishop.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 1, 6, true); - ChessGame.pieces.bishop.white.push(piece); + piece = ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); + piece = ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); // Knight - piece = ChessGame.makePiece(ChessGame.pieces.knight, 1, 2, true); - ChessGame.pieces.knight.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.knight, 1, 7, true); - ChessGame.pieces.knight.white.push(piece); + piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); + piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); // Rook - piece = ChessGame.makePiece(ChessGame.pieces.rook, 1, 1, true); - ChessGame.pieces.rook.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.rook, 1, 8, true); - ChessGame.pieces.rook.white.push(piece); - for(var j = 1; j <= ChessGame.BOARD_SIZE; j++) { - piece = ChessGame.makePiece(ChessGame.pieces.pawn, 2, j, true); - ChessGame.pieces.pawn.white.push(piece); + piece = ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); + piece = ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); + for(var j = 1; j <= 8; j++) { + piece = ChessGame.makePiece(ChessGame.PAWN, row + 1, j, isWhite); } // Setup black pieces + isWhite = false; + row = 8; // King - var piece = ChessGame.makePiece(ChessGame.pieces.king, 8, 4, false); - ChessGame.pieces.king.white = piece; + piece = ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); // Queen - piece = ChessGame.makePiece(ChessGame.pieces.queen, 8, 5, false); - ChessGame.pieces.queen.white = piece; + piece = ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); // Bishop - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 8, 3, false); - ChessGame.pieces.bishop.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.bishop, 8, 6, false); - ChessGame.pieces.bishop.white.push(piece); + piece = ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); + piece = ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); // Knight - piece = ChessGame.makePiece(ChessGame.pieces.knight, 8, 2, false); - ChessGame.pieces.knight.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.knight, 8, 7, false); - ChessGame.pieces.knight.white.push(piece); + piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); + piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); // Rook - piece = ChessGame.makePiece(ChessGame.pieces.rook, 8, 1, false); - ChessGame.pieces.rook.white.push(piece); - piece = ChessGame.makePiece(ChessGame.pieces.rook, 8, 8, false); - ChessGame.pieces.rook.white.push(piece); - for(var j = 1; j <= ChessGame.BOARD_SIZE; j++) { - piece = ChessGame.makePiece(ChessGame.pieces.pawn, 7, j, false); - ChessGame.pieces.pawn.black.push(piece); + piece = ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); + piece = ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); + for(var j = 1; j <= 8; j++) { + piece = ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); + } + + var metadataString = ChessGame.buildMetadataString(); + for (i in ChessGame.pieces) { + ChessGame.pieces[i].updateMetadata(metadataString); } } ChessGame.scriptStarting = function() { print("playChess.js started"); + gamePosition = Vec3.sum(MyAvatar.position, + Vec3.multiplyQbyV(MyAvatar.orientation, + { x: 0, y: 0, z: -1 })); ChessGame.buildBoard(); } @@ -250,15 +230,13 @@ ChessGame.scriptEnding = function() { delete ChessGame.board; // Cleaning up pieces - for(var i in ChessGame.pieces.all) { - ChessGame.pieces.all[i].cleanup(); + for(var i in ChessGame.pieces) { + ChessGame.pieces[i].cleanup(); } delete ChessGame.pieces; print("playChess.js finished"); } - -Script.update.connect(ChessGame.update) Script.scriptEnding.connect(ChessGame.scriptEnding); ChessGame.scriptStarting(); \ No newline at end of file From 16e243aec5bb9d13647beba6d7a79a7e87193ed6 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 12 Nov 2014 17:19:16 -0800 Subject: [PATCH 11/24] typo --- examples/playChess.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index ecfe1d12ae..cb60467492 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -124,8 +124,9 @@ ChessGame.Piece = (function(position, dimensions, url, rotation) { rotation: rotation, dimensions: this.dimensions, modelURL: url, + script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js" //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js" - script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" + //script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" } this.entity = null; }); @@ -156,7 +157,7 @@ ChessGame.makePiece = function(piece, i, j, isWhite) { return piece; } -ChessGame.buildMetaDataString = function() { +ChessGame.buildMetadataString = function() { var metadataObject = { gamePosition: gamePosition, gameSize: gameSize From 2498fd11a3017a1591c274abef03a2c8a201e672 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 13 Nov 2014 10:22:45 -0800 Subject: [PATCH 12/24] Preload sounds / use hash injector options --- examples/entityScripts/chessPiece.js | 33 ++++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index 13d36d28fb..39b4784849 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -1,6 +1,7 @@ (function(){ this.GRID_POSITION = { x: 1, y: 1, z: 1 }; this.GRID_SIZE = 1.0; + this.metadata = null; this.sound = null; this.entityID = null; this.properties = null; @@ -34,7 +35,7 @@ var i = Math.floor(relative.x / tileSize); var j = Math.floor(relative.z / tileSize); i = Math.min(Math.max(1, i), 8); - j = Math.min(Math.max(0, j), 9); + j = Math.min(Math.max(1, j), 8); relative.x = (i + 0.5) * tileSize; relative.z = (j + 0.5) * tileSize; @@ -53,33 +54,37 @@ this.sound = new Sound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); } } + this.playSound = function() { - var options = new AudioInjectionOptions(); - options.position = this.properties.position; - options.volume = 0.5; - Audio.playSound(this.sound, options); + if (this.sound.downloaded) { + Audio.playSound(this.sound, { position: this.properties.position }); + } } this.getMetadata = function() { - var metadataObject = JSON.parse(this.properties.animationURL); - if (metadataObject.gamePosition) { - this.GRID_POSITION = metadataObject.gamePosition; - } - if (metadataObject.gameSize) { - this.GRID_SIZE = metadataObject.gameSize; + if (this.properties.animationURL !== "") { + var metadataObject = JSON.parse(this.properties.animationURL); + if (metadataObject.gamePosition) { + this.GRID_POSITION = metadataObject.gamePosition; + } + if (metadataObject.gameSize) { + this.GRID_SIZE = metadataObject.gameSize; + } } } - this.clickDownOnEntity = function(entityID, mouseEvent){ + this.preload = function() { this.maybeDownloadSound(); + } + this.clickDownOnEntity = function(entityID, mouseEvent) { this.updateProperties(entityID); this.getMetadata(); this.updatePosition(mouseEvent); }; - this.holdingClickOnEntity = function(entityID, mouseEvent){ + this.holdingClickOnEntity = function(entityID, mouseEvent) { this.updateProperties(entityID); this.updatePosition(mouseEvent); }; - this.clickReleaseOnEntity = function(entityID, mouseEvent){ + this.clickReleaseOnEntity = function(entityID, mouseEvent) { this.updateProperties(entityID); this.updatePosition(mouseEvent); this.snapToGrid(); From ce0cb9fdc7e7e0c3823ea4f6dba4fd84ada4e428 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 13 Nov 2014 11:34:38 -0800 Subject: [PATCH 13/24] Better move / start of auto move dead piece --- examples/entityScripts/chessPiece.js | 85 +++++++++++++++++++++------- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index 39b4784849..17b186d396 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -5,10 +5,12 @@ this.sound = null; this.entityID = null; this.properties = null; + this.startingTile = null; + this.pieces = new Array(); + this.updateProperties = function(entityID) { if (this.entityID === null) { this.entityID = entityID; - print("entityID=" + JSON.stringify(this.entityID)); } if (!entityID.isKnownID || this.entityID.id !== entityID.id) { print("Something is very wrong: Bailing!"); @@ -19,27 +21,40 @@ print("Unknown entityID " + this.entityID.id + " should be self.") } } + this.updatePosition = function(mouseEvent) { var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y) var upVector = { x: 0, y: 1, z: 0 }; var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction, this.properties.position, upVector); + if (this.isOutsideGrid(this.getIndexPosition(intersection))) { + intersection = this.getAbsolutePosition(this.startingTile); + } Entities.editEntity(this.entityID, { position: intersection }); + //this.moveDeadPiece(); + } + this.isOutsideGrid = function(pos) { + return !(pos.i >= 0 && pos.j >= 0 && pos.i <= 9 && pos.j <= 9); + } + this.getIndexPosition = function(position) { + var tileSize = this.GRID_SIZE / 10.0; + var relative = Vec3.subtract(position, this.GRID_POSITION); + return { + i: Math.floor(relative.x / tileSize), + j: Math.floor(relative.z / tileSize) + } + } + this.getAbsolutePosition = function(pos) { + var tileSize = this.GRID_SIZE / 10.0; + var relative = Vec3.subtract(this.properties.position, this.GRID_POSITION); + relative.x = (pos.i + 0.5) * tileSize; + relative.z = (pos.j + 0.5) * tileSize; + + return Vec3.sum(this.GRID_POSITION, relative); } this.snapToGrid = function() { - var position = this.GRID_POSITION; - var size = this.GRID_SIZE; - var tileSize = size / 10.0; - - var relative = Vec3.subtract(this.properties.position, position); - var i = Math.floor(relative.x / tileSize); - var j = Math.floor(relative.z / tileSize); - i = Math.min(Math.max(1, i), 8); - j = Math.min(Math.max(1, j), 8); - - relative.x = (i + 0.5) * tileSize; - relative.z = (j + 0.5) * tileSize; - var finalPos = Vec3.sum(position, relative); + var pos = this.getIndexPosition(this.properties.position); + var finalPos = this.getAbsolutePosition((this.isOutsideGrid(pos)) ? this.startingTile : pos); Entities.editEntity(this.entityID, { position: finalPos }); } // Pr, Vr are respectively the Ray's Point of origin and Vector director @@ -56,8 +71,9 @@ } this.playSound = function() { - if (this.sound.downloaded) { + if (this.sound !== null && this.sound.downloaded) { Audio.playSound(this.sound, { position: this.properties.position }); + print("playing sound"); } } this.getMetadata = function() { @@ -69,6 +85,36 @@ if (metadataObject.gameSize) { this.GRID_SIZE = metadataObject.gameSize; } + if (metadataObject.pieces) { + this.pieces = metadataObject.pieces; + } + } + } + + + this.grab = function(mouseEvent) { + this.startingTile = this.getIndexPosition(this.properties.position); + this.updatePosition(mouseEvent); + } + this.move = function(mouseEvent) { + this.updatePosition(mouseEvent); + } + this.release = function(mouseEvent) { + this.updatePosition(mouseEvent); + this.snapToGrid(); + + this.playSound(); + } + this.moveDeadPiece = function() { + var myPos = this.getIndexPosition(this.properties.position); + for (var i = 0; i < this.pieces.length; i++) { + if (this.pieces[i].id != this.entityID.id) { + var piecePos = this.getIndexPosition(Entities.getEntityProperties(this.pieces[i]).position); + if (myPos.i === piecePos.i && myPos.j === piecePos.j) { + Entities.editEntity(this.pieces[i], { position: this.getAbsolutePosition({ i: 0, j: 0 })}); + break; + } + } } } @@ -76,18 +122,17 @@ this.maybeDownloadSound(); } this.clickDownOnEntity = function(entityID, mouseEvent) { + this.maybeDownloadSound(); this.updateProperties(entityID); this.getMetadata(); - this.updatePosition(mouseEvent); + this.grab(mouseEvent); }; this.holdingClickOnEntity = function(entityID, mouseEvent) { this.updateProperties(entityID); - this.updatePosition(mouseEvent); + this.move(mouseEvent); }; this.clickReleaseOnEntity = function(entityID, mouseEvent) { this.updateProperties(entityID); - this.updatePosition(mouseEvent); - this.snapToGrid(); - this.playSound(); + this.release(mouseEvent); }; }) \ No newline at end of file From ac34db14aa2e530b8349e8f01f5e092197c1f527 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 13 Nov 2014 11:35:14 -0800 Subject: [PATCH 14/24] Send pieces ID --- examples/playChess.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index cb60467492..1ed836b7ca 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -124,9 +124,9 @@ ChessGame.Piece = (function(position, dimensions, url, rotation) { rotation: rotation, dimensions: this.dimensions, modelURL: url, - script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js" + //script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js" //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js" - //script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" + script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" } this.entity = null; }); @@ -160,8 +160,12 @@ ChessGame.makePiece = function(piece, i, j, isWhite) { ChessGame.buildMetadataString = function() { var metadataObject = { gamePosition: gamePosition, - gameSize: gameSize + gameSize: gameSize, + pieces: new Array() }; + for (i in ChessGame.pieces) { + metadataObject.pieces.push(ChessGame.pieces[i].entity); + } return JSON.stringify(metadataObject); } From a1bd5623e1d453e87bd36545bd7e9af6648af458 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 12:04:49 -0800 Subject: [PATCH 15/24] refactor + solved network issues --- examples/entityScripts/chessPiece.js | 226 ++++++++++++++++----------- examples/playChess.js | 189 +++++++++++----------- 2 files changed, 241 insertions(+), 174 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index 17b186d396..f6a2c46d28 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -1,61 +1,101 @@ (function(){ - this.GRID_POSITION = { x: 1, y: 1, z: 1 }; - this.GRID_SIZE = 1.0; - this.metadata = null; - this.sound = null; + this.FIRST_TILE = null; // Global position of the first tile (1a) + this.TILE_SIZE = null; // Size of one tile + this.whitesDeadPieces = null; + this.blacksDeadPieces = null; + + this.wantDebug = false; + this.active = false; + this.entityID = null; this.properties = null; + this.boardID = null; + this.boardUserData = null; + + this.sound = null; this.startingTile = null; this.pieces = new Array(); + // All callbacks start by updating the properties this.updateProperties = function(entityID) { - if (this.entityID === null) { - this.entityID = entityID; - } - if (!entityID.isKnownID || this.entityID.id !== entityID.id) { - print("Something is very wrong: Bailing!"); - return; + // Piece ID + if (this.entityID === null || !this.entityID.isKnownID) { + this.entityID = Entities.identifyEntity(entityID); } + // Piece Properties this.properties = Entities.getEntityProperties(this.entityID); - if (!this.properties.isKnownID) { - print("Unknown entityID " + this.entityID.id + " should be self.") - } - } - - this.updatePosition = function(mouseEvent) { - var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y) - var upVector = { x: 0, y: 1, z: 0 }; - var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction, - this.properties.position, upVector); - if (this.isOutsideGrid(this.getIndexPosition(intersection))) { - intersection = this.getAbsolutePosition(this.startingTile); - } - Entities.editEntity(this.entityID, { position: intersection }); - //this.moveDeadPiece(); - } - this.isOutsideGrid = function(pos) { - return !(pos.i >= 0 && pos.j >= 0 && pos.i <= 9 && pos.j <= 9); - } - this.getIndexPosition = function(position) { - var tileSize = this.GRID_SIZE / 10.0; - var relative = Vec3.subtract(position, this.GRID_POSITION); - return { - i: Math.floor(relative.x / tileSize), - j: Math.floor(relative.z / tileSize) - } - } - this.getAbsolutePosition = function(pos) { - var tileSize = this.GRID_SIZE / 10.0; - var relative = Vec3.subtract(this.properties.position, this.GRID_POSITION); - relative.x = (pos.i + 0.5) * tileSize; - relative.z = (pos.j + 0.5) * tileSize; - return Vec3.sum(this.GRID_POSITION, relative); + // Board ID + if (this.boardID === null) { + // Read user data string and update boardID + var userData = JSON.parse(this.properties.userData); + var boardID = Entities.identifyEntity(userData.boardID); + if (boardID.isKnownID) { + this.boardID = boardID; + } else { + return; + } + } + + // Board User Data + this.updateUserData(); } - this.snapToGrid = function() { - var pos = this.getIndexPosition(this.properties.position); - var finalPos = this.getAbsolutePosition((this.isOutsideGrid(pos)) ? this.startingTile : pos); - Entities.editEntity(this.entityID, { position: finalPos }); + // Get an entity's user data + this.getEntityUserData = function(entityID) { + var properties = Entities.getEntityProperties(entityID); + + if (properties && properties.userData){ + return JSON.parse(properties.userData); + } else { + print("No user data found."); + return null; + } + } + // Updates user data related objects + this.updateUserData = function() { + // Get board's user data + if (this.boardID !== null && this.boardID.isKnownID) { + this.boardUserData = this.getEntityUserData(this.boardID); + if (!(this.boardUserData && + this.boardUserData.firstTile && + this.boardUserData.tileSize && + this.boardUserData.whitesDeadPieces && + this.boardUserData.blacksDeadPieces)) { + print("Incomplete boardUserData " + this.boardID.id); + } else { + this.FIRST_TILE = this.boardUserData.firstTile; + this.TILE_SIZE = this.boardUserData.tileSize; + this.whitesDeadPieces = this.boardUserData.whitesDeadPieces; + this.blacksDeadPieces = this.boardUserData.blacksDeadPieces; + + this.active = true; + } + } else { + print("Missing boardID:" + JSON.stringify(this.boardID)); + } + } + // Returns whether pos is inside the grid or not + this.isOutsideGrid = function(pos) { + return !(pos.i >= 0 && pos.j >= 0 && pos.i < 8 && pos.j < 8); + } + // Returns the tile coordinate + this.getIndexPosition = function(position) { + var halfTile = this.TILE_SIZE / 2.0; + var corner = Vec3.sum(this.FIRST_TILE, { x: -halfTile, y: 0.0, z: -halfTile }); + var relative = Vec3.subtract(position, corner); + return { + i: Math.floor(relative.x / this.TILE_SIZE), + j: Math.floor(relative.z / this.TILE_SIZE) + } + } + // Returns the world position + this.getAbsolutePosition = function(pos, halfHeight) { + var relative = { + x: pos.i * this.TILE_SIZE, + y: halfHeight, + z: pos.j * this.TILE_SIZE + } + return Vec3.sum(this.FIRST_TILE, relative); } // Pr, Vr are respectively the Ray's Point of origin and Vector director // Pp, Np are respectively the Plane's Point of origin and Normal vector @@ -64,75 +104,87 @@ var t = -(Vec3.dot(Pr, Np) + d) / Vec3.dot(Vr, Np); return Vec3.sum(Pr, Vec3.multiply(t, Vr)); } + // Download sound if needed this.maybeDownloadSound = function() { if (this.sound === null) { - this.sound = new Sound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); + this.sound = SoundCache.getSound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); } } - + // Play drop sound this.playSound = function() { - if (this.sound !== null && this.sound.downloaded) { + if (this.sound && this.sound.downloaded) { Audio.playSound(this.sound, { position: this.properties.position }); - print("playing sound"); } } - this.getMetadata = function() { - if (this.properties.animationURL !== "") { - var metadataObject = JSON.parse(this.properties.animationURL); - if (metadataObject.gamePosition) { - this.GRID_POSITION = metadataObject.gamePosition; - } - if (metadataObject.gameSize) { - this.GRID_SIZE = metadataObject.gameSize; - } - if (metadataObject.pieces) { - this.pieces = metadataObject.pieces; - } + // updates the piece position based on mouse input + this.updatePosition = function(mouseEvent) { + var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y) + var upVector = { x: 0, y: 1, z: 0 }; + var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction, + this.properties.position, upVector); + // if piece outside grid then send it back to the starting tile + if (this.isOutsideGrid(this.getIndexPosition(intersection))) { + intersection = this.getAbsolutePosition(this.startingTile, this.properties.dimensions.y / 2.0); } + Entities.editEntity(this.entityID, { position: intersection }); + } + // Snap piece to the center of the tile it is on + this.snapToGrid = function() { + var pos = this.getIndexPosition(this.properties.position); + var finalPos = this.getAbsolutePosition((this.isOutsideGrid(pos)) ? this.startingTile : pos, + this.properties.dimensions.y / 2.0); + Entities.editEntity(this.entityID, { position: finalPos }); + } + this.moveDeadPiece = function() { + return; + var myPos = this.getIndexPosition(this.properties.position); + for (var i = 0; i < this.pieces.length; i++) { + if (this.pieces[i].id != this.entityID.id) { + var piecePos = this.getIndexPosition(Entities.getEntityProperties(this.pieces[i]).position); + if (myPos.i === piecePos.i && myPos.j === piecePos.j) { + Entities.editEntity(this.pieces[i], { position: this.getAbsolutePosition({ i: 0, j: 0 })}); + break; + } + } + } } this.grab = function(mouseEvent) { - this.startingTile = this.getIndexPosition(this.properties.position); - this.updatePosition(mouseEvent); + if (this.active) { + this.startingTile = this.getIndexPosition(this.properties.position); + this.updatePosition(mouseEvent); + } } this.move = function(mouseEvent) { - this.updatePosition(mouseEvent); + if (this.active) { + this.updatePosition(mouseEvent); + } } this.release = function(mouseEvent) { - this.updatePosition(mouseEvent); - this.snapToGrid(); - - this.playSound(); - } - this.moveDeadPiece = function() { - var myPos = this.getIndexPosition(this.properties.position); - for (var i = 0; i < this.pieces.length; i++) { - if (this.pieces[i].id != this.entityID.id) { - var piecePos = this.getIndexPosition(Entities.getEntityProperties(this.pieces[i]).position); - if (myPos.i === piecePos.i && myPos.j === piecePos.j) { - Entities.editEntity(this.pieces[i], { position: this.getAbsolutePosition({ i: 0, j: 0 })}); - break; - } - } + if (this.active) { + this.updatePosition(mouseEvent); + this.snapToGrid(); + //this.moveDeadPiece(); TODO + this.playSound(); } } - this.preload = function() { + this.preload = function(entityID) { + this.updateProperties(entityID); // All callbacks start by updating the properties this.maybeDownloadSound(); } this.clickDownOnEntity = function(entityID, mouseEvent) { - this.maybeDownloadSound(); - this.updateProperties(entityID); - this.getMetadata(); + this.preload(entityID); // TODO : remove + this.updateProperties(entityID); // All callbacks start by updating the properties this.grab(mouseEvent); }; this.holdingClickOnEntity = function(entityID, mouseEvent) { - this.updateProperties(entityID); + this.updateProperties(entityID); // All callbacks start by updating the properties this.move(mouseEvent); }; this.clickReleaseOnEntity = function(entityID, mouseEvent) { - this.updateProperties(entityID); + this.updateProperties(entityID); // All callbacks start by updating the properties this.release(mouseEvent); }; }) \ No newline at end of file diff --git a/examples/playChess.js b/examples/playChess.js index 1ed836b7ca..da66d93677 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -69,16 +69,34 @@ ChessGame.getPieceInfo = function(type, isWhite) { // Board class -ChessGame.Board = (function(position, size) { +ChessGame.Board = (function(position, scale) { this.dimensions = Vec3.multiply(1.0 / ChessGame.BOARD.dimensions.x, ChessGame.BOARD.dimensions); // 1 by 1 - this.dimensions = Vec3.multiply(size, this.dimensions); // size by size - this.position = Vec3.sum(position, Vec3.multiply(0.5, this.dimensions)); - this.tileSize = size / ChessGame.BOARD.numTiles; - + this.dimensions = Vec3.multiply(scale, this.dimensions); // scale by scale + this.position = position + this.tileSize = scale / ChessGame.BOARD.numTiles; + + this.userDataObject = { + firstTile: this.tilePosition(1, 1), + tileSize: this.tileSize, + whitesDeadPieces: this.tilePosition(0,0), + blacksDeadPieces: this.tilePosition(9,9), + pieces: new Array() + } + this.entityProperties = { + type: "Model", + modelURL: ChessGame.BOARD.modelURL, + position: this.position, + dimensions: this.dimensions, + userData: this.buildUserDataString() + } this.entity = null; }); // Returns the top center point of tile i,j ChessGame.Board.prototype.tilePosition = function(i, j) { + if (!(this.position || this.dimensions || this.tileSize || ChessGame.BOARD.numTiles)) { + print("ChessGame.Board.prototype.tilePosition(): Called before proper initialisation, bailing."); + return null; + } return { x: this.position.x + (i - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize, y: this.position.y + this.dimensions.y / 2.0, @@ -89,19 +107,22 @@ ChessGame.Board.prototype.tilePosition = function(i, j) { ChessGame.Board.prototype.isWhite = function(i, j) { return (i + j) % 2 != 0; } +// Build the user data string +ChessGame.Board.prototype.buildUserDataString = function() { + return JSON.stringify(this.userDataObject); +} +// Updates the user data stored by the board +ChessGame.Board.prototype.updateUserData = function() { + var userDataString = this.buildUserDataString(); + this.entityProperties.userData = userDataString; + Entities.editEntity(this.entity, { userData: userDataString }); +} // Spawns the board using entities ChessGame.Board.prototype.spawn = function() { if (extraDebug) { print("Spawning board..."); } - - this.entity = Entities.addEntity({ - type: "Model", - modelURL: ChessGame.BOARD.modelURL, - position: this.position, - dimensions: this.dimensions - }); - + this.entity = Entities.addEntity(this.entityProperties); print("Board spawned"); } // Cleans up the entities of the board @@ -118,26 +139,35 @@ ChessGame.Piece = (function(position, dimensions, url, rotation) { this.dimensions = dimensions; this.position = position; this.position.y += this.dimensions.y / 2.0; + this.entityProperties = { type: "Model", position: this.position, rotation: rotation, dimensions: this.dimensions, modelURL: url, - //script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js" - //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js" - script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js" + //script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js", + //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js", + script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js", + userData: this.buildUserDataString() } this.entity = null; }); +// Build the user data string +ChessGame.Piece.prototype.buildUserDataString = function() { + if (!(ChessGame.board !== null || ChessGame.board.entity.isKnownID)) { + print("ChessGame.Piece.prototype.buildUserDataString(): Called before proper initialization, bailing."); + return null; + } + var userDataObject = { + boardID: ChessGame.board.entity, + }; + return JSON.stringify(userDataObject); +} // Spawns the piece ChessGame.Piece.prototype.spawn = function() { this.entity = Entities.addEntity(this.entityProperties); } -// Updates the metadata stored by the piece -ChessGame.Piece.prototype.updateMetadata = function(metadataString) { - Entities.editEntity(this.entity, { animationURL: metadataString }); -} // Cleans up the piece ChessGame.Piece.prototype.cleanup = function() { Entities.deleteEntity(this.entity); @@ -157,91 +187,76 @@ ChessGame.makePiece = function(piece, i, j, isWhite) { return piece; } -ChessGame.buildMetadataString = function() { - var metadataObject = { - gamePosition: gamePosition, - gameSize: gameSize, - pieces: new Array() - }; - for (i in ChessGame.pieces) { - metadataObject.pieces.push(ChessGame.pieces[i].entity); - } - return JSON.stringify(metadataObject); -} - -ChessGame.buildBoard = function() { - // Setup board - ChessGame.board = new ChessGame.Board(gamePosition, gameSize); - ChessGame.board.spawn(); - - // Setup white pieces - var isWhite = true; - var row = 1; - // King - var piece =ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); - // Queen - piece = ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); - // Bishop - piece = ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); - piece = ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); - // Knight - piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); - piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); - // Rook - piece = ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); - piece = ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); - for(var j = 1; j <= 8; j++) { - piece = ChessGame.makePiece(ChessGame.PAWN, row + 1, j, isWhite); - } - - // Setup black pieces - isWhite = false; - row = 8; - // King - piece = ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); - // Queen - piece = ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); - // Bishop - piece = ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); - piece = ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); - // Knight - piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); - piece = ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); - // Rook - piece = ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); - piece = ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); - for(var j = 1; j <= 8; j++) { - piece = ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); - } - - var metadataString = ChessGame.buildMetadataString(); - for (i in ChessGame.pieces) { - ChessGame.pieces[i].updateMetadata(metadataString); - } -} - - ChessGame.scriptStarting = function() { print("playChess.js started"); gamePosition = Vec3.sum(MyAvatar.position, Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 })); - ChessGame.buildBoard(); + // Setup board + ChessGame.board = new ChessGame.Board(gamePosition, gameSize); + ChessGame.board.spawn(); +} + +ChessGame.update = function() { + ChessGame.board.entity = Entities.identifyEntity(ChessGame.board.entity); + print(JSON.stringify(ChessGame.board.entity)); + if (ChessGame.board.entity.isKnownID && ChessGame.pieces.length == 0) { + // Setup white pieces + var isWhite = true; + var row = 1; + // King + ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); + // Queen + ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); + // Bishop + ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); + ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); + // Knight + ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); + ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); + // Rook + ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); + ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); + for(var j = 1; j <= 8; j++) { + ChessGame.makePiece(ChessGame.PAWN, row + 1, j, isWhite); + } + + // Setup black pieces + isWhite = false; + row = 8; + // King + ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); + // Queen + ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); + // Bishop + ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); + ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); + // Knight + ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); + ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); + // Rook + ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); + ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); + for(var j = 1; j <= 8; j++) { + ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); + } + + Script.update.disconnect(ChessGame.update); + } } ChessGame.scriptEnding = function() { // Cleaning up board ChessGame.board.cleanup(); - delete ChessGame.board; // Cleaning up pieces for(var i in ChessGame.pieces) { ChessGame.pieces[i].cleanup(); } - delete ChessGame.pieces; print("playChess.js finished"); } Script.scriptEnding.connect(ChessGame.scriptEnding); +Script.update.connect(ChessGame.update); ChessGame.scriptStarting(); \ No newline at end of file From cc35b83d318f4d3fcbaa43721da59423cd4a9742 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 12:19:26 -0800 Subject: [PATCH 16/24] update pieces id --- examples/playChess.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index da66d93677..1b3c643af7 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -199,7 +199,7 @@ ChessGame.scriptStarting = function() { ChessGame.update = function() { ChessGame.board.entity = Entities.identifyEntity(ChessGame.board.entity); - print(JSON.stringify(ChessGame.board.entity)); + if (ChessGame.board.entity.isKnownID && ChessGame.pieces.length == 0) { // Setup white pieces var isWhite = true; @@ -240,8 +240,25 @@ ChessGame.update = function() { for(var j = 1; j <= 8; j++) { ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); } - - Script.update.disconnect(ChessGame.update); + } + + if (ChessGame.pieces.length > 0) { + var unknown = 0; + ChessGame.board.userDataObject.pieces = new Array(); + for(var i = 1; i < ChessGame.pieces.length; i++) { + ChessGame.pieces[i].entity = Entities.identifyEntity(ChessGame.pieces[i].entity); + if (ChessGame.pieces[i].entity.isKnownID) { + ChessGame.board.userDataObject.pieces.push(ChessGame.pieces[i].entity) + } else { + unknown++; + } + } + ChessGame.board.updateUserData(); + + if (unknown === 0) { + print("Board complete"); + Script.update.disconnect(ChessGame.update); + } } } From 4c40f5e7de1734a109e79b7f9214b212c710e4ef Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 12:20:20 -0800 Subject: [PATCH 17/24] Changed entity scripts debug --- interface/src/entities/EntityTreeRenderer.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/interface/src/entities/EntityTreeRenderer.cpp b/interface/src/entities/EntityTreeRenderer.cpp index 99e4916c62..6357dfc71d 100644 --- a/interface/src/entities/EntityTreeRenderer.cpp +++ b/interface/src/entities/EntityTreeRenderer.cpp @@ -163,10 +163,12 @@ QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) { QString scriptContents = loadScriptContents(entity->getScript()); - if (QScriptEngine::checkSyntax(scriptContents).state() != QScriptSyntaxCheckResult::Valid) { + QScriptSyntaxCheckResult syntaxCheck = QScriptEngine::checkSyntax(scriptContents); + if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) { qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID; - qDebug() << " INVALID SYNTAX"; - qDebug() << " SCRIPT:" << scriptContents; + qDebug() << " " << syntaxCheck.errorMessage() << ":" + << syntaxCheck.errorLineNumber() << syntaxCheck.errorColumnNumber(); + qDebug() << " SCRIPT:" << entity->getScript(); return QScriptValue(); // invalid script } @@ -175,7 +177,7 @@ QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) { if (!entityScriptConstructor.isFunction()) { qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID; qDebug() << " NOT CONSTRUCTOR"; - qDebug() << " SCRIPT:" << scriptContents; + qDebug() << " SCRIPT:" << entity->getScript(); return QScriptValue(); // invalid script } From b948ff2dc88b6bb5da9a0d5abee3cae29117cc66 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 15:13:13 -0800 Subject: [PATCH 18/24] Move dead pieces --- examples/entityScripts/chessPiece.js | 29 +++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index f6a2c46d28..0c7c6da8a7 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -136,19 +136,34 @@ Entities.editEntity(this.entityID, { position: finalPos }); } this.moveDeadPiece = function() { - return; var myPos = this.getIndexPosition(this.properties.position); - for (var i = 0; i < this.pieces.length; i++) { - if (this.pieces[i].id != this.entityID.id) { - var piecePos = this.getIndexPosition(Entities.getEntityProperties(this.pieces[i]).position); + + for (var i = 0; i < this.boardUserData.pieces.length; i++) { + var piece = this.boardUserData.pieces[i]; + + if (piece.id != this.entityID.id) { + var properties = Entities.getEntityProperties(piece); + + var isWhite = properties.modelURL.search("White") !== -1; + var type = (properties.modelURL.search("King") !== -1) ? 4 : + (properties.modelURL.search("Queen") !== -1) ? 3 : + (properties.modelURL.search("Rook") !== -1) ? 2 : + (properties.modelURL.search("Knight") !== -1) ? 1 : + (properties.modelURL.search("Bishop") !== -1) ? 0 : + (properties.modelURL.search("Pawn") !== -1) ? -1 : -2; + + var piecePos = this.getIndexPosition(properties.position); if (myPos.i === piecePos.i && myPos.j === piecePos.j) { - Entities.editEntity(this.pieces[i], { position: this.getAbsolutePosition({ i: 0, j: 0 })}); + var position = this.getAbsolutePosition((isWhite) ? { i: type, j: -1 } : { i: 7 - type, j: 8 }, + properties.dimensions.y / 2.0); + Entities.editEntity(piece, { + position: position + }); break; } } } } - this.grab = function(mouseEvent) { if (this.active) { @@ -165,7 +180,7 @@ if (this.active) { this.updatePosition(mouseEvent); this.snapToGrid(); - //this.moveDeadPiece(); TODO + this.moveDeadPiece(); this.playSound(); } } From a6f57cb7e2bb31218576a8679157a7ccdedea6ea Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 15:13:54 -0800 Subject: [PATCH 19/24] Index error --- examples/playChess.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/playChess.js b/examples/playChess.js index 1b3c643af7..c60e1c6955 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -245,7 +245,7 @@ ChessGame.update = function() { if (ChessGame.pieces.length > 0) { var unknown = 0; ChessGame.board.userDataObject.pieces = new Array(); - for(var i = 1; i < ChessGame.pieces.length; i++) { + for(var i = 0; i < ChessGame.pieces.length; i++) { ChessGame.pieces[i].entity = Entities.identifyEntity(ChessGame.pieces[i].entity); if (ChessGame.pieces[i].entity.isKnownID) { ChessGame.board.userDataObject.pieces.push(ChessGame.pieces[i].entity) From 58796df843b43bc5e340f8f518a8d9d6054fc1a7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 15:17:09 -0800 Subject: [PATCH 20/24] scriptEntity url --- examples/playChess.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/playChess.js b/examples/playChess.js index c60e1c6955..8a72e1ebb8 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -146,9 +146,11 @@ ChessGame.Piece = (function(position, dimensions, url, rotation) { rotation: rotation, dimensions: this.dimensions, modelURL: url, - //script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js", - //script: "https://s3-us-west-1.amazonaws.com/highfidelity-dev/scripts/chessPiece.js", + /**/ + script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js", + /*/ script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js", + /**/ userData: this.buildUserDataString() } this.entity = null; From 94f3f461ce24eb9b93b6389a78749d7e03a5da9b Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 15:21:34 -0800 Subject: [PATCH 21/24] Cleanup script for chess boards --- examples/cleanupChessboards.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 examples/cleanupChessboards.js diff --git a/examples/cleanupChessboards.js b/examples/cleanupChessboards.js new file mode 100644 index 0000000000..926c02b8cf --- /dev/null +++ b/examples/cleanupChessboards.js @@ -0,0 +1,8 @@ +var entities = Entities.findEntities(MyAvatar.position, 10000); +var URL = "https://s3.amazonaws.com/hifi-public/models/props/chess/"; + +for(var i in entities) { + if (Entities.getEntityProperties(entities[i]).modelURL.slice(0, URL.length) === URL) { + Entities.deleteEntity(entities[i]); + } +} \ No newline at end of file From 87d472a5a966da0feaf9a0ffb65412f5125156ac Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 16:15:22 -0800 Subject: [PATCH 22/24] 2 space indent -> 4 space indent --- examples/entityScripts/chessPiece.js | 362 ++++++++++++------------ examples/playChess.js | 394 +++++++++++++-------------- 2 files changed, 379 insertions(+), 377 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index 0c7c6da8a7..ebe2df7e55 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -1,205 +1,207 @@ (function(){ - this.FIRST_TILE = null; // Global position of the first tile (1a) - this.TILE_SIZE = null; // Size of one tile - this.whitesDeadPieces = null; - this.blacksDeadPieces = null; + this.FIRST_TILE = null; // Global position of the first tile (1a) + this.TILE_SIZE = null; // Size of one tile + this.whitesDeadPieces = null; + this.blacksDeadPieces = null; - this.wantDebug = false; - this.active = false; + this.wantDebug = false; + this.active = false; - this.entityID = null; - this.properties = null; - this.boardID = null; - this.boardUserData = null; + this.entityID = null; + this.properties = null; + this.boardID = null; + this.boardUserData = null; - this.sound = null; - this.startingTile = null; - this.pieces = new Array(); + this.sound = null; + this.startingTile = null; + this.pieces = new Array(); - // All callbacks start by updating the properties - this.updateProperties = function(entityID) { - // Piece ID - if (this.entityID === null || !this.entityID.isKnownID) { - this.entityID = Entities.identifyEntity(entityID); - } - // Piece Properties - this.properties = Entities.getEntityProperties(this.entityID); + // All callbacks start by updating the properties + this.updateProperties = function(entityID) { + // Piece ID + if (this.entityID === null || !this.entityID.isKnownID) { + this.entityID = Entities.identifyEntity(entityID); + } + // Piece Properties + this.properties = Entities.getEntityProperties(this.entityID); - // Board ID - if (this.boardID === null) { - // Read user data string and update boardID - var userData = JSON.parse(this.properties.userData); - var boardID = Entities.identifyEntity(userData.boardID); - if (boardID.isKnownID) { - this.boardID = boardID; - } else { - return; - } - } + // Board ID + if (this.boardID === null) { + // Read user data string and update boardID + var userData = JSON.parse(this.properties.userData); + var boardID = Entities.identifyEntity(userData.boardID); + if (boardID.isKnownID) { + this.boardID = boardID; + } else { + return; + } + } - // Board User Data - this.updateUserData(); - } - // Get an entity's user data - this.getEntityUserData = function(entityID) { - var properties = Entities.getEntityProperties(entityID); - - if (properties && properties.userData){ - return JSON.parse(properties.userData); - } else { - print("No user data found."); - return null; + // Board User Data + this.updateUserData(); } - } - // Updates user data related objects - this.updateUserData = function() { - // Get board's user data - if (this.boardID !== null && this.boardID.isKnownID) { - this.boardUserData = this.getEntityUserData(this.boardID); - if (!(this.boardUserData && - this.boardUserData.firstTile && - this.boardUserData.tileSize && - this.boardUserData.whitesDeadPieces && - this.boardUserData.blacksDeadPieces)) { - print("Incomplete boardUserData " + this.boardID.id); - } else { - this.FIRST_TILE = this.boardUserData.firstTile; - this.TILE_SIZE = this.boardUserData.tileSize; - this.whitesDeadPieces = this.boardUserData.whitesDeadPieces; - this.blacksDeadPieces = this.boardUserData.blacksDeadPieces; + // Get an entity's user data + this.getEntityUserData = function(entityID) { + var properties = Entities.getEntityProperties(entityID); + + if (properties && properties.userData){ + return JSON.parse(properties.userData); + } else { + print("No user data found."); + return null; + } + } + // Updates user data related objects + this.updateUserData = function() { + // Get board's user data + if (this.boardID !== null && this.boardID.isKnownID) { + this.boardUserData = this.getEntityUserData(this.boardID); + + if (!(this.boardUserData && + this.boardUserData.firstTile && + this.boardUserData.tileSize && + this.boardUserData.whitesDeadPieces && + this.boardUserData.blacksDeadPieces && + this.boardUserData.pieces)) { + print("Incomplete boardUserData " + this.boardID.id); + } else { + this.FIRST_TILE = this.boardUserData.firstTile; + this.TILE_SIZE = this.boardUserData.tileSize; + this.whitesDeadPieces = this.boardUserData.whitesDeadPieces; + this.blacksDeadPieces = this.boardUserData.blacksDeadPieces; - this.active = true; - } - } else { - print("Missing boardID:" + JSON.stringify(this.boardID)); + this.active = true; + } + } else { + print("Missing boardID:" + JSON.stringify(this.boardID)); + } } - } - // Returns whether pos is inside the grid or not - this.isOutsideGrid = function(pos) { - return !(pos.i >= 0 && pos.j >= 0 && pos.i < 8 && pos.j < 8); - } - // Returns the tile coordinate - this.getIndexPosition = function(position) { - var halfTile = this.TILE_SIZE / 2.0; - var corner = Vec3.sum(this.FIRST_TILE, { x: -halfTile, y: 0.0, z: -halfTile }); - var relative = Vec3.subtract(position, corner); - return { - i: Math.floor(relative.x / this.TILE_SIZE), - j: Math.floor(relative.z / this.TILE_SIZE) + // Returns whether pos is inside the grid or not + this.isOutsideGrid = function(pos) { + return !(pos.i >= 0 && pos.j >= 0 && pos.i < 8 && pos.j < 8); } - } - // Returns the world position - this.getAbsolutePosition = function(pos, halfHeight) { - var relative = { - x: pos.i * this.TILE_SIZE, - y: halfHeight, - z: pos.j * this.TILE_SIZE + // Returns the tile coordinate + this.getIndexPosition = function(position) { + var halfTile = this.TILE_SIZE / 2.0; + var corner = Vec3.sum(this.FIRST_TILE, { x: -halfTile, y: 0.0, z: -halfTile }); + var relative = Vec3.subtract(position, corner); + return { + i: Math.floor(relative.x / this.TILE_SIZE), + j: Math.floor(relative.z / this.TILE_SIZE) + } } - return Vec3.sum(this.FIRST_TILE, relative); - } - // Pr, Vr are respectively the Ray's Point of origin and Vector director - // Pp, Np are respectively the Plane's Point of origin and Normal vector - this.rayPlaneIntersection = function(Pr, Vr, Pp, Np) { - var d = -Vec3.dot(Pp, Np); - var t = -(Vec3.dot(Pr, Np) + d) / Vec3.dot(Vr, Np); - return Vec3.sum(Pr, Vec3.multiply(t, Vr)); - } - // Download sound if needed - this.maybeDownloadSound = function() { - if (this.sound === null) { - this.sound = SoundCache.getSound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); + // Returns the world position + this.getAbsolutePosition = function(pos, halfHeight) { + var relative = { + x: pos.i * this.TILE_SIZE, + y: halfHeight, + z: pos.j * this.TILE_SIZE + } + return Vec3.sum(this.FIRST_TILE, relative); } - } - // Play drop sound - this.playSound = function() { - if (this.sound && this.sound.downloaded) { - Audio.playSound(this.sound, { position: this.properties.position }); + // Pr, Vr are respectively the Ray's Point of origin and Vector director + // Pp, Np are respectively the Plane's Point of origin and Normal vector + this.rayPlaneIntersection = function(Pr, Vr, Pp, Np) { + var d = -Vec3.dot(Pp, Np); + var t = -(Vec3.dot(Pr, Np) + d) / Vec3.dot(Vr, Np); + return Vec3.sum(Pr, Vec3.multiply(t, Vr)); } - } - // updates the piece position based on mouse input - this.updatePosition = function(mouseEvent) { - var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y) - var upVector = { x: 0, y: 1, z: 0 }; - var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction, - this.properties.position, upVector); - // if piece outside grid then send it back to the starting tile - if (this.isOutsideGrid(this.getIndexPosition(intersection))) { - intersection = this.getAbsolutePosition(this.startingTile, this.properties.dimensions.y / 2.0); + // Download sound if needed + this.maybeDownloadSound = function() { + if (this.sound === null) { + this.sound = SoundCache.getSound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); + } } - Entities.editEntity(this.entityID, { position: intersection }); - } - // Snap piece to the center of the tile it is on - this.snapToGrid = function() { - var pos = this.getIndexPosition(this.properties.position); - var finalPos = this.getAbsolutePosition((this.isOutsideGrid(pos)) ? this.startingTile : pos, - this.properties.dimensions.y / 2.0); - Entities.editEntity(this.entityID, { position: finalPos }); - } - this.moveDeadPiece = function() { - var myPos = this.getIndexPosition(this.properties.position); + // Play drop sound + this.playSound = function() { + if (this.sound && this.sound.downloaded) { + Audio.playSound(this.sound, { position: this.properties.position }); + } + } + // updates the piece position based on mouse input + this.updatePosition = function(mouseEvent) { + var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y) + var upVector = { x: 0, y: 1, z: 0 }; + var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction, + this.properties.position, upVector); + // if piece outside grid then send it back to the starting tile + if (this.isOutsideGrid(this.getIndexPosition(intersection))) { + intersection = this.getAbsolutePosition(this.startingTile, this.properties.dimensions.y / 2.0); + } + Entities.editEntity(this.entityID, { position: intersection }); + } + // Snap piece to the center of the tile it is on + this.snapToGrid = function() { + var pos = this.getIndexPosition(this.properties.position); + var finalPos = this.getAbsolutePosition((this.isOutsideGrid(pos)) ? this.startingTile : pos, + this.properties.dimensions.y / 2.0); + Entities.editEntity(this.entityID, { position: finalPos }); + } + this.moveDeadPiece = function() { + var myPos = this.getIndexPosition(this.properties.position); - for (var i = 0; i < this.boardUserData.pieces.length; i++) { - var piece = this.boardUserData.pieces[i]; + for (var i = 0; i < this.boardUserData.pieces.length; i++) { + var piece = this.boardUserData.pieces[i]; - if (piece.id != this.entityID.id) { - var properties = Entities.getEntityProperties(piece); + if (piece.id != this.entityID.id) { + var properties = Entities.getEntityProperties(piece); - var isWhite = properties.modelURL.search("White") !== -1; - var type = (properties.modelURL.search("King") !== -1) ? 4 : - (properties.modelURL.search("Queen") !== -1) ? 3 : - (properties.modelURL.search("Rook") !== -1) ? 2 : - (properties.modelURL.search("Knight") !== -1) ? 1 : - (properties.modelURL.search("Bishop") !== -1) ? 0 : - (properties.modelURL.search("Pawn") !== -1) ? -1 : -2; + var isWhite = properties.modelURL.search("White") !== -1; + var type = (properties.modelURL.search("King") !== -1) ? 4 : + (properties.modelURL.search("Queen") !== -1) ? 3 : + (properties.modelURL.search("Rook") !== -1) ? 2 : + (properties.modelURL.search("Knight") !== -1) ? 1 : + (properties.modelURL.search("Bishop") !== -1) ? 0 : + (properties.modelURL.search("Pawn") !== -1) ? -1 : -2; - var piecePos = this.getIndexPosition(properties.position); - if (myPos.i === piecePos.i && myPos.j === piecePos.j) { - var position = this.getAbsolutePosition((isWhite) ? { i: type, j: -1 } : { i: 7 - type, j: 8 }, - properties.dimensions.y / 2.0); - Entities.editEntity(piece, { - position: position - }); - break; - } - } - } - } + var piecePos = this.getIndexPosition(properties.position); + if (myPos.i === piecePos.i && myPos.j === piecePos.j) { + var position = this.getAbsolutePosition((isWhite) ? { i: type, j: -1 } : { i: 7 - type, j: 8 }, + properties.dimensions.y / 2.0); + Entities.editEntity(piece, { + position: position + }); + break; + } + } + } + } - this.grab = function(mouseEvent) { - if (this.active) { - this.startingTile = this.getIndexPosition(this.properties.position); - this.updatePosition(mouseEvent); + this.grab = function(mouseEvent) { + if (this.active) { + this.startingTile = this.getIndexPosition(this.properties.position); + this.updatePosition(mouseEvent); + } } - } - this.move = function(mouseEvent) { - if (this.active) { - this.updatePosition(mouseEvent); + this.move = function(mouseEvent) { + if (this.active) { + this.updatePosition(mouseEvent); + } } - } - this.release = function(mouseEvent) { - if (this.active) { - this.updatePosition(mouseEvent); - this.snapToGrid(); - this.moveDeadPiece(); - this.playSound(); + this.release = function(mouseEvent) { + if (this.active) { + this.updatePosition(mouseEvent); + this.snapToGrid(); + this.moveDeadPiece(); + this.playSound(); + } } - } - this.preload = function(entityID) { - this.updateProperties(entityID); // All callbacks start by updating the properties - this.maybeDownloadSound(); - } - this.clickDownOnEntity = function(entityID, mouseEvent) { - this.preload(entityID); // TODO : remove - this.updateProperties(entityID); // All callbacks start by updating the properties - this.grab(mouseEvent); - }; - this.holdingClickOnEntity = function(entityID, mouseEvent) { - this.updateProperties(entityID); // All callbacks start by updating the properties - this.move(mouseEvent); - }; - this.clickReleaseOnEntity = function(entityID, mouseEvent) { - this.updateProperties(entityID); // All callbacks start by updating the properties - this.release(mouseEvent); - }; + this.preload = function(entityID) { + this.updateProperties(entityID); // All callbacks start by updating the properties + this.maybeDownloadSound(); + } + this.clickDownOnEntity = function(entityID, mouseEvent) { + this.preload(entityID); // TODO : remove + this.updateProperties(entityID); // All callbacks start by updating the properties + this.grab(mouseEvent); + }; + this.holdingClickOnEntity = function(entityID, mouseEvent) { + this.updateProperties(entityID); // All callbacks start by updating the properties + this.move(mouseEvent); + }; + this.clickReleaseOnEntity = function(entityID, mouseEvent) { + this.updateProperties(entityID); // All callbacks start by updating the properties + this.release(mouseEvent); + }; }) \ No newline at end of file diff --git a/examples/playChess.js b/examples/playChess.js index 8a72e1ebb8..1b6882ed6e 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -14,266 +14,266 @@ var gameSize = 1.0; // Script namespace var extraDebug = extraDebug || true; var ChessGame = ChessGame || { - BOARD: { - modelURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Board.fbx", - dimensions: { x: 773.191, y: 20.010, z: 773.191 }, - rotation: Quat.fromPitchYawRollDegrees(0, 90, 0), - numTiles: 10.0 - }, - KING: 0, QUEEN: 1, BISHOP: 2, KNIGHT: 3, ROOK: 4, PAWN: 5, + BOARD: { + modelURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/Board.fbx", + dimensions: { x: 773.191, y: 20.010, z: 773.191 }, + rotation: Quat.fromPitchYawRollDegrees(0, 90, 0), + numTiles: 10.0 + }, + KING: 0, QUEEN: 1, BISHOP: 2, KNIGHT: 3, ROOK: 4, PAWN: 5, - board: null, - pieces: new Array() + board: null, + pieces: new Array() }; ChessGame.getPieceInfo = function(type, isWhite) { - var info = { - modelURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/", - dimensions: { x: 1.0, y: 1.0, z: 1.0 }, - rotation: Quat.fromPitchYawRollDegrees(0, 0, 0) - } + var info = { + modelURL: "https://s3.amazonaws.com/hifi-public/models/props/chess/", + dimensions: { x: 1.0, y: 1.0, z: 1.0 }, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0) + } - switch(type) { - case ChessGame.KING: - info.modelURL += "King"; - info.dimensions = { x: 110.496, y: 306.713, z: 110.496 }; - info.rotation = Quat.fromPitchYawRollDegrees(0, 90, 0); - break; - case ChessGame.QUEEN: - info.modelURL += "Queen"; - info.dimensions = { x: 110.496, y: 257.459, z: 110.496 }; - break; - case ChessGame.BISHOP: - info.modelURL += "Bishop"; - info.dimensions = { x: 102.002, y: 213.941, z: 102.002 }; - info.rotation = Quat.fromPitchYawRollDegrees(0, 90, 0); - break; - case ChessGame.KNIGHT: - info.modelURL += "Knight"; - info.dimensions = { x: 95.602, y: 186.939, z: 95.602 }; - info.rotation = Quat.fromPitchYawRollDegrees(0, 180, 0); - break; - case ChessGame.ROOK: - info.modelURL += "Rook"; - info.dimensions = { x: 101.024, y: 167.523, z: 101.024 }; - break; - case ChessGame.PAWN: - info.modelURL += "Pawn"; - info.dimensions = { x: 93.317, y: 138.747, z: 93.317 }; - break; - } - info.modelURL += ((isWhite) ? "_White" : "_Black") + ".fbx" + switch(type) { + case ChessGame.KING: + info.modelURL += "King"; + info.dimensions = { x: 110.496, y: 306.713, z: 110.496 }; + info.rotation = Quat.fromPitchYawRollDegrees(0, 90, 0); + break; + case ChessGame.QUEEN: + info.modelURL += "Queen"; + info.dimensions = { x: 110.496, y: 257.459, z: 110.496 }; + break; + case ChessGame.BISHOP: + info.modelURL += "Bishop"; + info.dimensions = { x: 102.002, y: 213.941, z: 102.002 }; + info.rotation = Quat.fromPitchYawRollDegrees(0, 90, 0); + break; + case ChessGame.KNIGHT: + info.modelURL += "Knight"; + info.dimensions = { x: 95.602, y: 186.939, z: 95.602 }; + info.rotation = Quat.fromPitchYawRollDegrees(0, 180, 0); + break; + case ChessGame.ROOK: + info.modelURL += "Rook"; + info.dimensions = { x: 101.024, y: 167.523, z: 101.024 }; + break; + case ChessGame.PAWN: + info.modelURL += "Pawn"; + info.dimensions = { x: 93.317, y: 138.747, z: 93.317 }; + break; + } + info.modelURL += ((isWhite) ? "_White" : "_Black") + ".fbx" - return info; + return info; } // Board class ChessGame.Board = (function(position, scale) { - this.dimensions = Vec3.multiply(1.0 / ChessGame.BOARD.dimensions.x, ChessGame.BOARD.dimensions); // 1 by 1 - this.dimensions = Vec3.multiply(scale, this.dimensions); // scale by scale - this.position = position - this.tileSize = scale / ChessGame.BOARD.numTiles; + this.dimensions = Vec3.multiply(1.0 / ChessGame.BOARD.dimensions.x, ChessGame.BOARD.dimensions); // 1 by 1 + this.dimensions = Vec3.multiply(scale, this.dimensions); // scale by scale + this.position = position + this.tileSize = scale / ChessGame.BOARD.numTiles; - this.userDataObject = { - firstTile: this.tilePosition(1, 1), - tileSize: this.tileSize, - whitesDeadPieces: this.tilePosition(0,0), - blacksDeadPieces: this.tilePosition(9,9), - pieces: new Array() - } - this.entityProperties = { - type: "Model", - modelURL: ChessGame.BOARD.modelURL, - position: this.position, - dimensions: this.dimensions, - userData: this.buildUserDataString() - } - this.entity = null; + this.userDataObject = { + firstTile: this.tilePosition(1, 1), + tileSize: this.tileSize, + whitesDeadPieces: this.tilePosition(0,0), + blacksDeadPieces: this.tilePosition(9,9), + pieces: new Array() + } + this.entityProperties = { + type: "Model", + modelURL: ChessGame.BOARD.modelURL, + position: this.position, + dimensions: this.dimensions, + userData: this.buildUserDataString() + } + this.entity = null; }); // Returns the top center point of tile i,j ChessGame.Board.prototype.tilePosition = function(i, j) { - if (!(this.position || this.dimensions || this.tileSize || ChessGame.BOARD.numTiles)) { - print("ChessGame.Board.prototype.tilePosition(): Called before proper initialisation, bailing."); - return null; - } - return { - x: this.position.x + (i - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize, - y: this.position.y + this.dimensions.y / 2.0, - z: this.position.z + (j - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize - }; + if (!(this.position || this.dimensions || this.tileSize || ChessGame.BOARD.numTiles)) { + print("ChessGame.Board.prototype.tilePosition(): Called before proper initialisation, bailing."); + return null; + } + return { + x: this.position.x + (i - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize, + y: this.position.y + this.dimensions.y / 2.0, + z: this.position.z + (j - ChessGame.BOARD.numTiles / 2.0 + 0.5) * this.tileSize + }; } // Checks the color of the tile ChessGame.Board.prototype.isWhite = function(i, j) { - return (i + j) % 2 != 0; + return (i + j) % 2 != 0; } // Build the user data string ChessGame.Board.prototype.buildUserDataString = function() { - return JSON.stringify(this.userDataObject); + return JSON.stringify(this.userDataObject); } // Updates the user data stored by the board ChessGame.Board.prototype.updateUserData = function() { - var userDataString = this.buildUserDataString(); - this.entityProperties.userData = userDataString; - Entities.editEntity(this.entity, { userData: userDataString }); + var userDataString = this.buildUserDataString(); + this.entityProperties.userData = userDataString; + Entities.editEntity(this.entity, { userData: userDataString }); } // Spawns the board using entities ChessGame.Board.prototype.spawn = function() { - if (extraDebug) { - print("Spawning board..."); - } - this.entity = Entities.addEntity(this.entityProperties); - print("Board spawned"); + if (extraDebug) { + print("Spawning board..."); + } + this.entity = Entities.addEntity(this.entityProperties); + print("Board spawned"); } // Cleans up the entities of the board ChessGame.Board.prototype.cleanup = function() { - if (extraDebug) { - print("Cleaning up board..."); - } - Entities.deleteEntity(this.entity); - print("Board cleaned up"); + if (extraDebug) { + print("Cleaning up board..."); + } + Entities.deleteEntity(this.entity); + print("Board cleaned up"); } // Piece class ChessGame.Piece = (function(position, dimensions, url, rotation) { - this.dimensions = dimensions; - this.position = position; - this.position.y += this.dimensions.y / 2.0; + this.dimensions = dimensions; + this.position = position; + this.position.y += this.dimensions.y / 2.0; - this.entityProperties = { - type: "Model", - position: this.position, - rotation: rotation, - dimensions: this.dimensions, - modelURL: url, - /**/ - script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js", - /*/ - script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js", - /**/ - userData: this.buildUserDataString() - } - this.entity = null; + this.entityProperties = { + type: "Model", + position: this.position, + rotation: rotation, + dimensions: this.dimensions, + modelURL: url, + /**/ + script: "https://s3.amazonaws.com/hifi-public/scripts/entityScripts/chessPiece.js", + /*/ + script: "file:/Users/clement/hifi/examples/entityScripts/chessPiece.js", + /**/ + userData: this.buildUserDataString() + } + this.entity = null; }); // Build the user data string ChessGame.Piece.prototype.buildUserDataString = function() { - if (!(ChessGame.board !== null || ChessGame.board.entity.isKnownID)) { - print("ChessGame.Piece.prototype.buildUserDataString(): Called before proper initialization, bailing."); - return null; - } - var userDataObject = { - boardID: ChessGame.board.entity, - }; - return JSON.stringify(userDataObject); + if (!(ChessGame.board !== null || ChessGame.board.entity.isKnownID)) { + print("ChessGame.Piece.prototype.buildUserDataString(): Called before proper initialization, bailing."); + return null; + } + var userDataObject = { + boardID: ChessGame.board.entity, + }; + return JSON.stringify(userDataObject); } // Spawns the piece ChessGame.Piece.prototype.spawn = function() { - this.entity = Entities.addEntity(this.entityProperties); + this.entity = Entities.addEntity(this.entityProperties); } // Cleans up the piece ChessGame.Piece.prototype.cleanup = function() { - Entities.deleteEntity(this.entity); + Entities.deleteEntity(this.entity); } ChessGame.makePiece = function(piece, i, j, isWhite) { - var info = ChessGame.getPieceInfo(piece, isWhite); - var url = info.modelURL; - var size = Vec3.multiply(0.5 * (1.0 / ChessGame.BOARD.dimensions.x), info.dimensions); - var rotation = (isWhite) ? info.rotation - : Quat.multiply(Quat.fromPitchYawRollDegrees(0, 180, 0), - info.rotation); - var position = ChessGame.board.tilePosition(i, j); + var info = ChessGame.getPieceInfo(piece, isWhite); + var url = info.modelURL; + var size = Vec3.multiply(0.5 * (1.0 / ChessGame.BOARD.dimensions.x), info.dimensions); + var rotation = (isWhite) ? info.rotation + : Quat.multiply(Quat.fromPitchYawRollDegrees(0, 180, 0), + info.rotation); + var position = ChessGame.board.tilePosition(i, j); - var piece = new ChessGame.Piece(position, size, url, rotation); - piece.spawn(); - ChessGame.pieces.push(piece); - return piece; + var piece = new ChessGame.Piece(position, size, url, rotation); + piece.spawn(); + ChessGame.pieces.push(piece); + return piece; } ChessGame.scriptStarting = function() { - print("playChess.js started"); - gamePosition = Vec3.sum(MyAvatar.position, - Vec3.multiplyQbyV(MyAvatar.orientation, - { x: 0, y: 0, z: -1 })); - // Setup board - ChessGame.board = new ChessGame.Board(gamePosition, gameSize); - ChessGame.board.spawn(); + print("playChess.js started"); + gamePosition = Vec3.sum(MyAvatar.position, + Vec3.multiplyQbyV(MyAvatar.orientation, + { x: 0, y: 0, z: -1 })); + // Setup board + ChessGame.board = new ChessGame.Board(gamePosition, gameSize); + ChessGame.board.spawn(); } ChessGame.update = function() { - ChessGame.board.entity = Entities.identifyEntity(ChessGame.board.entity); + ChessGame.board.entity = Entities.identifyEntity(ChessGame.board.entity); - if (ChessGame.board.entity.isKnownID && ChessGame.pieces.length == 0) { - // Setup white pieces - var isWhite = true; - var row = 1; - // King - ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); - // Queen - ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); - // Bishop - ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); - ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); - // Knight - ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); - ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); - // Rook - ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); - ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); - for(var j = 1; j <= 8; j++) { - ChessGame.makePiece(ChessGame.PAWN, row + 1, j, isWhite); + if (ChessGame.board.entity.isKnownID && ChessGame.pieces.length == 0) { + // Setup white pieces + var isWhite = true; + var row = 1; + // King + ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); + // Queen + ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); + // Bishop + ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); + ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); + // Knight + ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); + ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); + // Rook + ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); + ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); + for(var j = 1; j <= 8; j++) { + ChessGame.makePiece(ChessGame.PAWN, row + 1, j, isWhite); + } + + // Setup black pieces + isWhite = false; + row = 8; + // King + ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); + // Queen + ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); + // Bishop + ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); + ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); + // Knight + ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); + ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); + // Rook + ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); + ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); + for(var j = 1; j <= 8; j++) { + ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); + } } - // Setup black pieces - isWhite = false; - row = 8; - // King - ChessGame.makePiece(ChessGame.KING, row, 5, isWhite); - // Queen - ChessGame.makePiece(ChessGame.QUEEN, row, 4, isWhite); - // Bishop - ChessGame.makePiece(ChessGame.BISHOP, row, 3, isWhite); - ChessGame.makePiece(ChessGame.BISHOP, row, 6, isWhite); - // Knight - ChessGame.makePiece(ChessGame.KNIGHT, row, 2, isWhite); - ChessGame.makePiece(ChessGame.KNIGHT, row, 7, isWhite); - // Rook - ChessGame.makePiece(ChessGame.ROOK, row, 1, isWhite); - ChessGame.makePiece(ChessGame.ROOK, row, 8, isWhite); - for(var j = 1; j <= 8; j++) { - ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); - } - } - - if (ChessGame.pieces.length > 0) { - var unknown = 0; - ChessGame.board.userDataObject.pieces = new Array(); - for(var i = 0; i < ChessGame.pieces.length; i++) { - ChessGame.pieces[i].entity = Entities.identifyEntity(ChessGame.pieces[i].entity); - if (ChessGame.pieces[i].entity.isKnownID) { - ChessGame.board.userDataObject.pieces.push(ChessGame.pieces[i].entity) - } else { - unknown++; - } - } - ChessGame.board.updateUserData(); + if (ChessGame.pieces.length > 0) { + var unknown = 0; + ChessGame.board.userDataObject.pieces = new Array(); + for(var i = 0; i < ChessGame.pieces.length; i++) { + ChessGame.pieces[i].entity = Entities.identifyEntity(ChessGame.pieces[i].entity); + if (ChessGame.pieces[i].entity.isKnownID) { + ChessGame.board.userDataObject.pieces.push(ChessGame.pieces[i].entity) + } else { + unknown++; + } + } + ChessGame.board.updateUserData(); - if (unknown === 0) { - print("Board complete"); - Script.update.disconnect(ChessGame.update); + if (unknown === 0) { + print("Board complete"); + Script.update.disconnect(ChessGame.update); + } } - } } ChessGame.scriptEnding = function() { - // Cleaning up board - ChessGame.board.cleanup(); + // Cleaning up board + ChessGame.board.cleanup(); - // Cleaning up pieces - for(var i in ChessGame.pieces) { - ChessGame.pieces[i].cleanup(); - } + // Cleaning up pieces + for(var i in ChessGame.pieces) { + ChessGame.pieces[i].cleanup(); + } - print("playChess.js finished"); + print("playChess.js finished"); } Script.scriptEnding.connect(ChessGame.scriptEnding); From b1d29cf888745c847e3332b938936464405404f7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 17:27:33 -0800 Subject: [PATCH 23/24] user data cleanup --- examples/entityScripts/chessPiece.js | 16 +++++----------- examples/playChess.js | 23 ++--------------------- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index ebe2df7e55..f3e2c2aef3 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -1,8 +1,6 @@ (function(){ this.FIRST_TILE = null; // Global position of the first tile (1a) this.TILE_SIZE = null; // Size of one tile - this.whitesDeadPieces = null; - this.blacksDeadPieces = null; this.wantDebug = false; this.active = false; @@ -59,16 +57,11 @@ if (!(this.boardUserData && this.boardUserData.firstTile && - this.boardUserData.tileSize && - this.boardUserData.whitesDeadPieces && - this.boardUserData.blacksDeadPieces && - this.boardUserData.pieces)) { + this.boardUserData.tileSize)) { print("Incomplete boardUserData " + this.boardID.id); } else { this.FIRST_TILE = this.boardUserData.firstTile; this.TILE_SIZE = this.boardUserData.tileSize; - this.whitesDeadPieces = this.boardUserData.whitesDeadPieces; - this.blacksDeadPieces = this.boardUserData.blacksDeadPieces; this.active = true; } @@ -139,9 +132,10 @@ } this.moveDeadPiece = function() { var myPos = this.getIndexPosition(this.properties.position); - - for (var i = 0; i < this.boardUserData.pieces.length; i++) { - var piece = this.boardUserData.pieces[i]; + var others = Entities.findEntities(this.properties.position, this.properties.dimensions.y); + + for (var i = 0; i < others.length; i++) { + var piece = others[i]; if (piece.id != this.entityID.id) { var properties = Entities.getEntityProperties(piece); diff --git a/examples/playChess.js b/examples/playChess.js index 1b6882ed6e..57ed24f342 100644 --- a/examples/playChess.js +++ b/examples/playChess.js @@ -79,8 +79,7 @@ ChessGame.Board = (function(position, scale) { firstTile: this.tilePosition(1, 1), tileSize: this.tileSize, whitesDeadPieces: this.tilePosition(0,0), - blacksDeadPieces: this.tilePosition(9,9), - pieces: new Array() + blacksDeadPieces: this.tilePosition(9,9) } this.entityProperties = { type: "Model", @@ -242,25 +241,7 @@ ChessGame.update = function() { for(var j = 1; j <= 8; j++) { ChessGame.makePiece(ChessGame.PAWN, row - 1, j, isWhite); } - } - - if (ChessGame.pieces.length > 0) { - var unknown = 0; - ChessGame.board.userDataObject.pieces = new Array(); - for(var i = 0; i < ChessGame.pieces.length; i++) { - ChessGame.pieces[i].entity = Entities.identifyEntity(ChessGame.pieces[i].entity); - if (ChessGame.pieces[i].entity.isKnownID) { - ChessGame.board.userDataObject.pieces.push(ChessGame.pieces[i].entity) - } else { - unknown++; - } - } - ChessGame.board.updateUserData(); - - if (unknown === 0) { - print("Board complete"); - Script.update.disconnect(ChessGame.update); - } + Script.update.disconnect(ChessGame.update); } } From 4d7c5f2d4f2aca58f66e29576a5ebe6df5e20cc0 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 14 Nov 2014 17:35:20 -0800 Subject: [PATCH 24/24] Protect board from pieces --- examples/entityScripts/chessPiece.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/entityScripts/chessPiece.js b/examples/entityScripts/chessPiece.js index f3e2c2aef3..4d3fc5cc3d 100644 --- a/examples/entityScripts/chessPiece.js +++ b/examples/entityScripts/chessPiece.js @@ -147,9 +147,9 @@ (properties.modelURL.search("Knight") !== -1) ? 1 : (properties.modelURL.search("Bishop") !== -1) ? 0 : (properties.modelURL.search("Pawn") !== -1) ? -1 : -2; - + var piecePos = this.getIndexPosition(properties.position); - if (myPos.i === piecePos.i && myPos.j === piecePos.j) { + if (myPos.i === piecePos.i && myPos.j === piecePos.j && type !== -2) { var position = this.getAbsolutePosition((isWhite) ? { i: type, j: -1 } : { i: 7 - type, j: 8 }, properties.dimensions.y / 2.0); Entities.editEntity(piece, {