refactor + solved network issues

This commit is contained in:
Atlante45 2014-11-14 12:04:49 -08:00
parent 4afcf9b240
commit a1bd5623e1
2 changed files with 241 additions and 174 deletions

View file

@ -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);
};
})

View file

@ -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();