// // 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 // #debug // Script namespace var ChessGame = { BOARD: { modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/chess/Board.fbx", dimensions: { x: 773.191, y: 20.010, z: 773.191 }, numTiles: 10.0 }, KING: 0, QUEEN: 1, BISHOP: 2, KNIGHT: 3, ROOK: 4, PAWN: 5, board: {} }; ChessGame.getPieceInfo = function(type, isWhite) { var info = { modelURL: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/chess/", rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), textures: "", } if (!isWhite) { info.textures = "{ \"Map #12\": \"" + info.modelURL + "board_cherry.jpg\"}"; } switch(type) { case ChessGame.KING: info.modelURL += "King"; info.dimensions = { x: 110.496, y: 306.713, z: 110.496 }; 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 }; break; case ChessGame.KNIGHT: info.modelURL += "Knight"; info.dimensions = { x: 95.602, y: 186.939, z: 95.602 }; 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; default: print("Unknow chess piece type."); break; } info.modelURL += ".fbx" return info; } // Returns the top center point of tile i,j ChessGame.tilePosition = function(i, j) { var CENTER_OFFSET = 0.5; var relativePosition = { x: ((j + CENTER_OFFSET) - ChessGame.BOARD.numTiles / 2.0) * ChessGame.board.dimensions.x / ChessGame.BOARD.numTiles, y: ChessGame.board.dimensions.y / 2.0, z: (ChessGame.BOARD.numTiles / 2.0 - (i + CENTER_OFFSET)) * ChessGame.board.dimensions.z / ChessGame.BOARD.numTiles }; return Vec3.sum(ChessGame.board.position, Vec3.multiplyQbyV(ChessGame.board.rotation, relativePosition)); } ChessGame.makeBoard = function(position, rotation, dimensions) { ChessGame.board.position = position; ChessGame.board.rotation = rotation; ChessGame.board.dimensions = Vec3.multiply(dimensions / ChessGame.BOARD.dimensions.x, ChessGame.BOARD.dimensions); // scale by scale var userData = { grabbableKey: { grabbable: true }, } ChessGame.board.entity = Entities.addEntity({ type: "Model", modelURL: ChessGame.BOARD.modelURL, position: ChessGame.board.position, rotation: ChessGame.board.rotation, dimensions: ChessGame.board.dimensions, userData: JSON.stringify(userData), }); } ChessGame.makePiece = function(piece, i, j, isWhite) { var info = ChessGame.getPieceInfo(piece, isWhite); var boardRotation = ChessGame.board.rotation; if (isWhite) { boardRotation = Quat.multiply(boardRotation, Quat.fromPitchYawRollDegrees(0, 180, 0)); } var position = ChessGame.tilePosition(i, j); var rotation = Quat.multiply(boardRotation, info.rotation); var dimensions = Vec3.multiply(0.5 * ChessGame.board.dimensions.x / ChessGame.BOARD.dimensions.x, info.dimensions); Entities.addEntity({ type: "Model", parentID: ChessGame.board.entity, registrationPoint: { x: 0.5, y: 0.0, z: 0.5 }, position: position, rotation: rotation, dimensions: dimensions, modelURL: info.modelURL, textures: info.textures, script: "https://s3-us-west-1.amazonaws.com/hifi-content/clement/production/chess/chessPiece.js", userData: JSON.stringify({ grabbableKey: { grabbable: true }, }), }); } ChessGame.spawnChessBoard = function() { print("playChess.js started"); var boardPosition = Vec3.sum(MyAvatar.position, Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -2 })); var boardRotation = Quat.multiply(MyAvatar.orientation, Quat.fromPitchYawRollDegrees(0, 0, 0)); var boardDimensions = 1.0; // Setup board ChessGame.makeBoard(boardPosition, boardRotation, boardDimensions); if (ChessGame.board.entity) { // 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.scriptEnding.connect(function() { Entities.deleteEntity(ChessGame.board.entity); }); ChessGame.spawnChessBoard();