From 2b4d7239ed9a31886396a747301f66576414a652 Mon Sep 17 00:00:00 2001 From: samuelmaddock Date: Mon, 1 Oct 2012 04:30:48 -0400 Subject: [PATCH] Board is now shared, revised robber --- client/index.html | 2 + client/js/catan.js | 49 +++++ client/js/enums.js | 16 +- client/js/main.js | 15 +- server/board.old.js | 356 ---------------------------------- server/game.js | 107 +++------- server/turnmanager.js | 2 +- {server => shared}/board.js | 127 ++++++------ shared/entities/BaseEntity.js | 40 ++-- shared/entities/HexCorner.js | 16 +- shared/entities/HexEdge.js | 9 +- shared/entities/HexTile.js | 48 ++--- shared/entities/Robber.js | 28 ++- shared/player.js | 7 - shared/schemas/classic.js | 41 ++-- 15 files changed, 277 insertions(+), 586 deletions(-) delete mode 100644 server/board.old.js rename {server => shared}/board.js (76%) diff --git a/client/index.html b/client/index.html index 63f391a..995ba85 100644 --- a/client/index.html +++ b/client/index.html @@ -119,6 +119,8 @@ + + diff --git a/client/js/catan.js b/client/js/catan.js index d4af7ee..7dd602c 100644 --- a/client/js/catan.js +++ b/client/js/catan.js @@ -105,4 +105,53 @@ CATAN.onSelectEntity = function(ent) { id: ent.getEntId() }); } +} + +CATAN.getAvailableBuildings = function() { + var ply = CATAN.LocalPlayer; + + var list = []; + + var corners = this.board.getCorners(); + for(var i in corners) { + var corner = corners[i]; + if(corner.canBuild()) { + list.push(corner); + } + }; + + var edges = this.board.getEdges(); + for(var i in edges) { + var edge = edges[i]; + if(edge.canBuild(ply)) { + list.push(edge); + } + }; + + /*var corners = ply.getCorners(); + for(var i in corners) { + var corner = corners[i]; + + var adjCorners = corner.getAdjacentCorners(); + for(var j in adjCorners) { + if(adjCorners[j].canBuild()) { + list.push(adjCorners[j].getEntId()); + } + } + + var adjEdges = corner.getAdjacentEdges(); + for(var j in adjEdges) { + + var adjEdge = adjEdges[j]; + if(adjEdge.canBuild(ply)) { + list.push(adjEdge.getEntId()); + } + + // TODO: Add adjacent roads + + } + + }*/ + + return list; } \ No newline at end of file diff --git a/client/js/enums.js b/client/js/enums.js index 76693eb..93476c4 100644 --- a/client/js/enums.js +++ b/client/js/enums.js @@ -11,4 +11,18 @@ RESOURCE_ORE = 5; var BUILDING_ROAD = 0, BUILDING_SETTLEMENT = 1, -BUILDING_CITY = 2; \ No newline at end of file +BUILDING_CITY = 2; + +var CORNER_TL = 0, +CORNER_TR = 1, +CORNER_R = 2, +CORNER_BR = 3, +CORNER_BL = 4, +CORNER_L = 5; + +var EDGE_T = 0, +EDGE_TR = 1, +EDGE_BR = 2, +EDGE_B = 3, +EDGE_BL = 4, +EDGE_TL = 5; \ No newline at end of file diff --git a/client/js/main.js b/client/js/main.js index c32a48c..839b308 100644 --- a/client/js/main.js +++ b/client/js/main.js @@ -79,6 +79,8 @@ CATAN.connectToServer = function(id, isEvent) { }; CATAN.onConnection = function() { + this.board = new CATAN.Board(); + this.server.emit( 'playerReady', { name: CATAN.getName() } ); this.chat = this.GUI.create("Chatbox"); @@ -117,7 +119,7 @@ CATAN.setupSocket = function(socket) { socket.on('boardEntities', function (data) { for(var i in data.ents) { - var ent = CATAN.ents.create(data.name); + var ent = CATAN.ents.getById(data.ents[i].id); ent.setup(data.ents[i]); } }); @@ -199,12 +201,12 @@ CATAN.setupSocket = function(socket) { }); socket.on('RobberMoved', function (data) { - var hex = CATAN.ents.getById(data.id); - hex.setRobber(); - if(CATAN.LocalPlayer.isTurn()) { CATAN.Game.collisionObjects.length = 0; } + + var tile = CATAN.ents.getById(data.id); + CATAN.board.getRobber().setTile(tile); }); socket.on('GiveResources', function (data) { @@ -223,8 +225,9 @@ CATAN.setupSocket = function(socket) { socket.on('PlayerStartBuild', function (data) { CATAN.Game.collisionObjects.length = 0 - for(var i in data.available) { - var ent = CATAN.ents.getById(data.available[i]); + var available = CATAN.getAvailableBuildings(); + for(var i in available) { + var ent = available[i]; ent.show(0.33); CATAN.Game.collisionObjects.push( ent.getMesh() ); } diff --git a/server/board.old.js b/server/board.old.js deleted file mode 100644 index 588b2eb..0000000 --- a/server/board.old.js +++ /dev/null @@ -1,356 +0,0 @@ -/** - * @author Samuel Maddock / http://samuelmaddock.com/ - */ - -THREE = require('./vector.js'); - -// Require base entity first -require('../shared/entities/BaseEntity.js'); - -// Board entities -require('../shared/entities/HexTile.js'); -require('../shared/entities/HexCorner.js'); -require('../shared/entities/HexEdge.js'); -require('../shared/entities/Robber.js'); - -CATAN.Board = function(game) { - - CATAN.EntityCount = 0; // reset - - this.game = game; - - this.cellWidth = 5; - this.cellHeight = 5; - - this.hexRadius = 64; - - this.hexTiles = []; - this.hexCorners = []; - this.hexEdges = []; - - this.setup(); - -} - -/* ----------------------------------------------- - Catan Board Setup - - Below is unoptimized prototype code, this - should be fixed up later. -------------------------------------------------*/ -CATAN.Board.prototype = { - - setup: function() { - this.Schema = this.game.getSchema(); - this.Grid = this.game.getSchema().getGrid(); - this.resourceCount = this.Schema.getResources(); - this.numTokens = this.Schema.getNumberTokens(); - - // Update later for possible expansions? - this.cellHeight = this.Grid.length; - this.cellWidth = this.Grid[0].length; - - // Setup hex grid tiles - for (y = 0; y < this.cellHeight; y++) { - for (x = 0; x < this.cellWidth; x++) { - - var tile = this.Grid[y][x]; - - if (tile != 0) { // Check if grid coordinate is valid - - tile = this.setupHexTile(x,y); - this.setupResource(tile); - this.setupNumberToken(tile); - - this.hexTiles.push( tile ); - - } - - } - } - - // Create corner and edge entities - var corners = []; - var edges = []; - for(var i in this.hexTiles) { - - var tile = this.hexTiles[i]; - - // Get corner positions - var positions = [ - tile.getCornerPosition(CORNER_L), - tile.getCornerPosition(CORNER_TL), - tile.getCornerPosition(CORNER_TR), - tile.getCornerPosition(CORNER_R), - tile.getCornerPosition(CORNER_BR), - tile.getCornerPosition(CORNER_BL) - ] - - for(var j in positions) { - - var pos = positions[j]; // get corner vector - var posStr = Math.floor(pos.x) + "," + Math.floor(pos.Y) + "," + Math.floor(pos.z); // hash string - - var corner; - if (!corners[posStr]) { // create new corner entity - - corner = CATAN.ents.create('HexCorner'); - corner.setPosition(positions[j]); - corner.Id = this.hexCorners.length + 1; // unique Id - - corners[posStr] = corner; // set reference for later - - this.hexCorners.push(corner); - this.game.entities.push(corner); - - } else { // duplicate corner - - corner = corners[posStr]; - - } - - tile.AdjacentCorners.push(corner); - corner.AdjacentTiles.push(tile); - - } - - // Get edge orientations - var orientations = [ - tile.getEdgePosAng(EDGE_T), - tile.getEdgePosAng(EDGE_TR), - tile.getEdgePosAng(EDGE_BR), - tile.getEdgePosAng(EDGE_B), - tile.getEdgePosAng(EDGE_BL), - tile.getEdgePosAng(EDGE_TL) - ] - - for(var j in orientations) { - - var pos = orientations[j].pos; // get edge vector - var ang = orientations[j].ang; // get edge vector - - var posStr = Math.floor(pos.x) + "," + Math.floor(pos.Y) + "," + Math.floor(pos.z); // hash string - - var edge; - if (!edges[posStr]) { // create new edge entity - - edge = CATAN.ents.create('HexEdge'); - edge.Id = this.hexEdges.length + 1; // unique Id - edge.setPosition(orientations[j].pos); - edge.setAngle(orientations[j].ang); - - edges[posStr] = edge; // set reference for later - - this.hexEdges.push(edge); - this.game.entities.push(edge); - - } else { // duplicate edge - - edge = edges[posStr]; - - } - - tile.AdjacentEdges.push(edge); - edge.AdjacentTiles.push(tile); - - } - - } - delete corners; - delete edges; - - // Establish adjacencies - this.findAdjacentCorners() - this.findAdjacentEdges() - - - // Create docks - }, - - setupHexTile: function(x, y) { - // Create new HexGridCell object - var offset = this.getWorldHexOffset() - var hexTile = CATAN.ents.create('HexTile'); - hexTile.Id = this.hexTiles.length + 1; - hexTile.setRadius(this.hexRadius); - hexTile.setGridIndex(x,y,offset); - - // Replace value in grid - this.Grid[y][x] = hexTile; - this.game.entities.push(hexTile); - - return hexTile; - }, - - setupResource: function(tile) { - // Get random possible key - var randResource = Math.floor( Math.random() * this.resourceCount.length ) - - // Select resource - tile.Resource = this.resourceCount[randResource]; - - this.resourceCount.splice(randResource,1); // remove resource - - // Setup robber if the resource is desert - if (tile.Resource == RESOURCE_DESERT) { - - // TODO: Remove this check - if(this.robber) { - console.log("DUPLICATE ROBBER!"); - } - - tile.setRobber(this); - - } - }, - - /* ----------------------------------------------- - CATAN.Board.prototype.setupNumberToken( HexTile ) - - Desc: Assign number token to hex tile - ------------------------------------------------*/ - setupNumberToken: function(tile) { - if(tile.Resource == RESOURCE_DESERT) return; // Desert doesn't have a number token - var randToken = Math.floor( Math.random() * this.numTokens.length ) - tile.NumberToken = this.numTokens[randToken]; - this.numTokens.splice(randToken,1); // remove resource - }, - - findAdjacentCorners: function() { - // for corners - for(var i in this.hexCorners) { - - var c = this.hexCorners[i]; // get corner - - // Loop through all other corners - for(var j in this.hexCorners) { - - var c2 = this.hexCorners[j]; // get corner to be compared - - if(c.Id != c2.Id) { // check for same corner - - var distance = Math.floor( c.position.distanceTo(c2.position) ); - - if (distance <= this.hexRadius) { // if the distance is small enough, the corner is adjacent - c.AdjacentCorners.push(c2); - } - - } - - } - - // Loop through all edges - for(var j in this.hexEdges) { - - var e = this.hexEdges[j]; // get edge to be compared - var distance = Math.floor( c.position.distanceTo(e.position) ); - - if (distance <= this.hexRadius) { // if the distance is small enough, the edge is adjacent - c.AdjacentEdges.push(e); - e.AdjacentCorners.push(c); - } - - } - - } - }, - - findAdjacentEdges: function() { - for(var i in this.hexEdges) { - - var e = this.hexEdges[i]; // get edge - - // Loop through all other corners - for(var j in this.hexEdges) { - - var e2 = this.hexEdges[j]; // get edge to be compared - - if(e.Id != e2.Id) { // check for same edge - - var distance = Math.floor( e.position.distanceTo(e2.position) ); - - if (distance <= this.hexRadius) { // if the distance is small enough, the edge is adjacent - e.AdjacentEdges.push(e2); - } - - } - - } - - // Loop through all edges - for(var j in this.hexEdges) { - - var e2 = this.hexEdges[j]; // get edge to be compared - var distance = Math.floor( e.position.distanceTo(e2.position) ); - - if (distance <= this.hexRadius) { // if the distance is small enough, the edge is adjacent - e.AdjacentEdges.push(e2); - } - - } - - } - }, - - /* ----------------------------------------------- - CATAN.Board.prototype.getWorldHexOffset - - Desc: Returns the grid offset to center - the board - ------------------------------------------------*/ - getWorldHexOffset: function() { - var r = this.hexRadius, - w = r * 2, - h = r * Math.sqrt(3); - - return new THREE.Vector3( ( (this.cellWidth * w) - r ) / 2, 0, (this.cellHeight * h) / 2 ); - }, - - getBuildingByID: function(id, BUILDING_ENUM) { - if(BUILDING_ENUM == BUILDING_ROAD) { - return this.getEdgeByID(id); - } else if(BUILDING_ENUM == BUILDING_SETTLEMENT || BUILDING_ENUM == BUILDING_CITY) { - return this.getCornerByID(id); - } - }, - - getCornerByID: function(id) { - for(var i in this.hexCorners) { - var c = this.hexCorners[i]; - if (c.Id == id) return c; - } - }, - - getEdgeByID: function(id) { - for(var i in this.hexEdges) { - var e = this.hexEdges[i]; - if (e.Id == id) return e; - } - }, - - getAvailableBuildings: function(ply, BUILDING_ENUM) { - var entities = this.board.getBuildingsByType(BUILDING_ENUM); - - var buildable = []; - for(var i in entities) { - if(entities[i].canBuild(ply)) { - buildable.push(entities[i].getEntId()); - } - } - - return buildable; - }, - - getTiles: function() { - return this.hexTiles; - }, - - getCorners: function() { - return this.hexCorners; - }, - - getEdges: function() { - return this.hexEdges; - } - -} \ No newline at end of file diff --git a/server/game.js b/server/game.js index 7e89173..aa5be24 100644 --- a/server/game.js +++ b/server/game.js @@ -1,5 +1,5 @@ require('../shared/player.js'); -require('./board.js'); +require('../shared/board.js'); require('./turnmanager.js'); CATAN.Game = function(ply,name,schema,public) { @@ -462,36 +462,7 @@ CATAN.Game.prototype = { return; } - // Let players know where they can build - - // TODO: Move board to shared so players can figure this out - var list = []; - - var corners = ply.getCorners(); - for(var i in corners) { - var corner = corners[i]; - - var acorners = corner.getAdjacentCorners(); - for(var j in acorners) { - if(acorners[j].canBuild()) { - list.push(acorners[j].getEntId()); - } - } - - var aedges = corner.getAdjacentEdges(); - for(var j in aedges) { - var aedge = aedges[j]; - if(aedge.canBuild(ply)) { - list.push(aedge.getEntId()); - } - - // TODO: Add adjacent roads - } - } - - ply.emit('PlayerStartBuild', { - available: list - }); + ply.emit('PlayerStartBuild'); }, @@ -520,15 +491,17 @@ CATAN.Game.prototype = { if(!ply.isTurn()) return; if(!ply.mustMoveRobber) return; - var hex = this.getEntById(data.id); - if(hex == null) return; - if(!hex.isTile()) return; - if(hex.hasRobber()) return; + var tile = this.getEntById(data.id); - hex.setRobber(); + if( (tile == null) || // Valid entity check + (!tile.isTile()) || // Must be a hex tile + (!tile.isDesert()) || // Can't place on desert + (tile.hasRobber())) return; // Can't move to same tile + + this.getBoard().getRobber().setTile(tile); this.emit('RobberMoved', { - id: hex.getEntId() + id: tile.getEntId() }) ply.mustMoveRobber = false; @@ -538,44 +511,7 @@ CATAN.Game.prototype = { syncGame: function(ply) { var game = ply.game, - board = game.board; - - // Send tiles - var tiles = []; - for(var i=0; i < board.hexTiles.length; i++) { - var tile = board.hexTiles[i]; - tiles.push({ - id: tile.getEntId(), - pos: tile.getPosition(), - resource: tile.getResource(), - token: tile.getToken(), - robber: tile.hasRobber() - }); - }; - ply.emit('boardEntities', { name: 'HexTile', ents: tiles }); - - // Send corners - var corners = []; - for(var i=0; i < board.hexCorners.length; i++) { - var corner = board.hexCorners[i]; - corners.push({ - id: corner.getEntId(), - pos: corner.getPosition() - }); - }; - ply.emit('boardEntities', { name: 'HexCorner', ents: corners }); - - // Send edges - var edges = []; - for(var i=0; i < board.hexEdges.length; i++) { - var edge = board.hexEdges[i]; - edges.push({ - id: edge.getEntId(), - pos: edge.getPosition(), - ang: edge.getAngle() - }); - }; - ply.emit('boardEntities', { name: 'HexEdge', ents: edges }); + board = game.getBoard(); // Send players var players = game.getPlayers(); @@ -591,6 +527,27 @@ CATAN.Game.prototype = { } } + // Send tiles + var tiles = []; + for(var i=0; i < board.hexTiles.length; i++) { + var tile = board.hexTiles[i]; + tiles.push({ + id: tile.getEntId(), + resource: tile.getResource(), + token: tile.getToken() + }); + }; + ply.emit('boardEntities', { ents: tiles }); + + // Send robber tile + var robber = this.getBoard().getRobber(); + ply.emit('boardEntities', { + ents: [{ + id: robber.getEntId(), + tileId: robber.getTile().getEntId() + }] + }); + } }; \ No newline at end of file diff --git a/server/turnmanager.js b/server/turnmanager.js index 2382ebc..4fee2bd 100644 --- a/server/turnmanager.js +++ b/server/turnmanager.js @@ -167,7 +167,7 @@ CATAN.TurnManager.prototype = { ply.emit('setupBuild', { building: this.setupBuildOrder[ply.SetupStep].Type, available: buildable - }) + }); }, diff --git a/server/board.js b/shared/board.js similarity index 76% rename from server/board.js rename to shared/board.js index ad29f2e..75de31b 100644 --- a/server/board.js +++ b/shared/board.js @@ -3,16 +3,16 @@ */ if(SERVER) { - THREE = require('./vector.js'); + THREE = require('../server/vector.js'); // Require base entity first - require('../shared/entities/BaseEntity.js'); + require('./entities/BaseEntity.js'); // Board entities - require('../shared/entities/HexTile.js'); - require('../shared/entities/HexCorner.js'); - require('../shared/entities/HexEdge.js'); - require('../shared/entities/Robber.js'); + require('./entities/HexTile.js'); + require('./entities/HexCorner.js'); + require('./entities/HexEdge.js'); + require('./entities/Robber.js'); } CATAN.Board = function(game) { @@ -30,6 +30,8 @@ CATAN.Board = function(game) { this.hexCorners = []; this.hexEdges = []; + this.robber = null; + this.setup(); } @@ -43,10 +45,15 @@ CATAN.Board = function(game) { CATAN.Board.prototype = { setup: function() { - this.Schema = this.game.getSchema(); - this.Grid = this.game.getSchema().getGrid(); - this.resourceCount = this.Schema.getResources(); - this.numTokens = this.Schema.getNumberTokens(); + if(SERVER) { + this.Schema = this.game.getSchema(); + this.Grid = this.game.getSchema().getGrid(); + this.resourceCount = this.Schema.getResources(); + this.numTokens = this.Schema.getNumberTokens(); + } else { + this.Schema = CATAN.getSchema(); + this.Grid = CATAN.getSchema().getGrid(); + } // Update later for possible expansions? this.cellHeight = this.Grid.length; @@ -61,16 +68,18 @@ CATAN.Board.prototype = { if (tile != 0) { // Check if grid coordinate is valid tile = this.setupHexTile(x,y); - this.setupResource(tile); - this.setupNumberToken(tile); - this.hexTiles.push( tile ); + + this.Grid[y][x] = tile; } } } + // Create robber entity + this.robber = CATAN.ents.create('Robber'); + // Create corner and edge entities var corners = []; var edges = []; @@ -98,12 +107,14 @@ CATAN.Board.prototype = { corner = CATAN.ents.create('HexCorner'); corner.setPosition(positions[j]); - corner.Id = this.hexCorners.length + 1; // unique Id corners[posStr] = corner; // set reference for later this.hexCorners.push(corner); - this.game.entities.push(corner); + + if(SERVER) { + this.game.entities.push(corner); + } } else { // duplicate corner @@ -124,7 +135,7 @@ CATAN.Board.prototype = { tile.getEdgePosAng(EDGE_B), tile.getEdgePosAng(EDGE_BL), tile.getEdgePosAng(EDGE_TL) - ] + ]; for(var j in orientations) { @@ -137,14 +148,16 @@ CATAN.Board.prototype = { if (!edges[posStr]) { // create new edge entity edge = CATAN.ents.create('HexEdge'); - edge.Id = this.hexEdges.length + 1; // unique Id edge.setPosition(orientations[j].pos); edge.setAngle(orientations[j].ang); edges[posStr] = edge; // set reference for later this.hexEdges.push(edge); - this.game.entities.push(edge); + + if(SERVER) { + this.game.entities.push(edge); + } } else { // duplicate edge @@ -167,21 +180,29 @@ CATAN.Board.prototype = { // Create docks + + + if(SERVER) { + var tiles = this.getTiles(); + for(var i in tiles) { + var tile = tiles[i]; + this.setupResource(tile); + this.setupNumberToken(tile); + } + } }, setupHexTile: function(x, y) { - // Create new HexGridCell object var offset = this.getWorldHexOffset() - var hexTile = CATAN.ents.create('HexTile'); - hexTile.Id = this.hexTiles.length + 1; - hexTile.setRadius(this.hexRadius); - hexTile.setGridIndex(x,y,offset); + var tile = CATAN.ents.create('HexTile'); + tile.setRadius(this.hexRadius); + tile.setGridIndex(x,y,offset); - // Replace value in grid - this.Grid[y][x] = hexTile; - this.game.entities.push(hexTile); + if(SERVER) { + this.game.entities.push(tile); + } - return hexTile; + return tile; }, setupResource: function(tile) { @@ -195,14 +216,7 @@ CATAN.Board.prototype = { // Setup robber if the resource is desert if (tile.Resource == RESOURCE_DESERT) { - - // TODO: Remove this check - if(this.robber) { - console.log("DUPLICATE ROBBER!"); - } - - tile.setRobber(this); - + this.robber.setTile(tile); } }, @@ -229,7 +243,7 @@ CATAN.Board.prototype = { var c2 = this.hexCorners[j]; // get corner to be compared - if(c.Id != c2.Id) { // check for same corner + if(c.getEntId() != c2.getEntId()) { // check for same corner var distance = Math.floor( c.position.distanceTo(c2.position) ); @@ -267,7 +281,7 @@ CATAN.Board.prototype = { var e2 = this.hexEdges[j]; // get edge to be compared - if(e.Id != e2.Id) { // check for same edge + if(e.getEntId() != e2.getEntId()) { // check for same edge var distance = Math.floor( e.position.distanceTo(e2.position) ); @@ -308,41 +322,6 @@ CATAN.Board.prototype = { return new THREE.Vector3( ( (this.cellWidth * w) - r ) / 2, 0, (this.cellHeight * h) / 2 ); }, - getBuildingByID: function(id, BUILDING_ENUM) { - if(BUILDING_ENUM == BUILDING_ROAD) { - return this.getEdgeByID(id); - } else if(BUILDING_ENUM == BUILDING_SETTLEMENT || BUILDING_ENUM == BUILDING_CITY) { - return this.getCornerByID(id); - } - }, - - getCornerByID: function(id) { - for(var i in this.hexCorners) { - var c = this.hexCorners[i]; - if (c.Id == id) return c; - } - }, - - getEdgeByID: function(id) { - for(var i in this.hexEdges) { - var e = this.hexEdges[i]; - if (e.Id == id) return e; - } - }, - - getAvailableBuildings: function(ply, BUILDING_ENUM) { - var entities = this.board.getBuildingsByType(BUILDING_ENUM); - - var buildable = []; - for(var i in entities) { - if(entities[i].canBuild(ply)) { - buildable.push(entities[i].getEntId()); - } - } - - return buildable; - }, - getTiles: function() { return this.hexTiles; }, @@ -353,6 +332,10 @@ CATAN.Board.prototype = { getEdges: function() { return this.hexEdges; + }, + + getRobber: function() { + return this.robber; } } \ No newline at end of file diff --git a/shared/entities/BaseEntity.js b/shared/entities/BaseEntity.js index 3dc942a..4426048 100644 --- a/shared/entities/BaseEntity.js +++ b/shared/entities/BaseEntity.js @@ -5,13 +5,15 @@ var BaseEntity = function() { this.entid = -1; + this.Mesh = null; + this.Owner = null; this.Building = -1; this.position = new THREE.Vector3(0,0,0); this.angle = -1; - this.visible = false; + this.bVisible = false; }; @@ -27,7 +29,7 @@ BaseEntity.prototype.setOwner = function(ply) { if(typeof ply === 'undefined') return; this.Owner = ply; - ply.setOwnership(this) + ply.setOwnership(this); if(CLIENT) { this.show(); @@ -42,27 +44,25 @@ BaseEntity.prototype.setOwner = function(ply) { BaseEntity.prototype.show = function(opacity) { opacity = (typeof opacity !== 'undefined') ? opacity : 1; - if(typeof this.getMesh() !== 'undefined') { // change to mesh later - this.getMesh().material.opacity = opacity; - if(opacity < 1) { - this.getMesh().material.transparent = true; - } + var mesh = this.getMesh(); + mesh.material.opacity = opacity; + if(opacity < 1) { + mesh.material.transparent = true; } - this.visible = true; + this.bVisible = true; }; BaseEntity.prototype.hide = function() { - if(typeof this.getMesh() !== 'undefined') { - this.getMesh().material.opacity = 0; - this.getMesh().material.transparent = false; - } + var mesh = this.getMesh(); + mesh.material.opacity = 0; + mesh.material.transparent = false; - this.visible = false; + this.bVisible = false; }; BaseEntity.prototype.isVisible = function() { - return this.visible; + return this.bVisible; } /* ----------------------------------------------- @@ -141,8 +141,7 @@ BaseEntity.prototype.getAdjacentEdges = function() { if(CLIENT) { BaseEntity.prototype._setup = function(data) { - this.entid = data.id; - this.setPosition(data.pos); + // this.entid = data.id; } BaseEntity.prototype.onHover = function() {} @@ -163,6 +162,15 @@ if(CLIENT) { transparent: true }); } + } else { + + if(this.getOwner() != CATAN.LocalPlayer) return; + + if(this.isSettlement()) { + $('body').css('cursor','pointer'); + // TODO: Change preview to city model + } + } } diff --git a/shared/entities/HexCorner.js b/shared/entities/HexCorner.js index 46556c0..50af3d7 100644 --- a/shared/entities/HexCorner.js +++ b/shared/entities/HexCorner.js @@ -33,14 +33,24 @@ HexCorner.prototype.setupMesh = function() { } HexCorner.prototype.canBuild = function() { - if(this.hasOwner()) return false; // Must build settlement at least two corners away - for(var i in this.AdjacentCorners) { - if(this.AdjacentCorners[i].hasOwner()) return false; + var adjCorners = this.getAdjacentCorners(); + for(var i in adjCorners) { + var corner = adjCorners[i]; + if(corner.hasOwner()) return false; } + // Must build next to road + /*var adjEdges = this.getAdjacentEdges(); + for(var j in adjEdges) { + var edge = adjEdges[i]; + if(edge.hasOwner() && edge.getOwner() == ply) { + return true; + } + }*/ + return true; } diff --git a/shared/entities/HexEdge.js b/shared/entities/HexEdge.js index 8eaa8c4..d213232 100644 --- a/shared/entities/HexEdge.js +++ b/shared/entities/HexEdge.js @@ -22,14 +22,18 @@ HexEdge.prototype.canBuild = function(ply) { var corners = this.getAdjacentCorners(); for(var i in corners) { var corner = corners[i]; - if(ply.isOwner(corner)) return true; + if(corner.hasOwner() && corner.getOwner() == ply) { + return true; + } } // Can build near adjacent edge var edges = this.getAdjacentEdges(); for(var i in edges) { var edge = edges[i]; - if(ply.isOwner(edge)) return true; + if(edge.hasOwner() && edge.getOwner() == ply) { + return true; + } } return false; @@ -52,7 +56,6 @@ HexEdge.prototype.setupMesh = function() { if(CLIENT) { HexEdge.prototype.setup = function(data) { this._setup(data); - this.setAngle(data.ang); this.setupMesh(); } } diff --git a/shared/entities/HexTile.js b/shared/entities/HexTile.js index 8b62709..090c094 100644 --- a/shared/entities/HexTile.js +++ b/shared/entities/HexTile.js @@ -9,7 +9,7 @@ var HexTile = function() { // Catan this.Resource = -1; this.NumberToken = -1; - this.Robber = false; + this.bHasRobber = false; this.AdjacentCorners = []; this.AdjacentEdges = []; @@ -23,15 +23,25 @@ var HexTile = function() { this.edgesX = []; this.edgesY = []; - this.visible = true; + this.bVisible = true; }; HexTile.prototype = CATAN.ents.create('BaseEntity'); +/* + Resources +*/ HexTile.prototype.getResource = function() { return this.Resource; }; HexTile.prototype.setResource = function(resource) { this.Resource = resource; }; +HexTile.prototype.isDesert = function() { + return this.getResource() == RESOURCE_DESERT; +} + +/* + Tokens +*/ HexTile.prototype.getToken = function() { return this.NumberToken; }; HexTile.prototype.setToken = function(num) { this.NumberToken = num; @@ -69,8 +79,15 @@ HexTile.prototype.setToken = function(num) { } }; -HexTile.prototype.hasRobber = function() { return this.Robber; }; +/* + Robber +*/ +HexTile.prototype.hasRobber = function() { return this.bHasRobber; }; +HexTile.prototype.setRobber = function(bRobber) { this.bHasRobber = bRobber; }; +/* + Hex Geometry +*/ HexTile.prototype.setRadius = function(r) { this.Radius = r; this.Width = r * 2; @@ -187,27 +204,6 @@ HexTile.prototype.getEdgePosAng = function(EDGE_ENUM) { return { pos: position, ang: angle }; } -/* ----------------------------------------------- - HexTile.setRobber - - Desc: Sets the robber on top of the tile -------------------------------------------------*/ -HexTile.prototype.setRobber = function(board) { - - this.Robber = true; - - var robber; - var ents = CATAN.ents.getByName('Robber'); - - if(ents.length < 1) { - robber = CATAN.ents.create('Robber'); - } else { - robber = ents[0]; - } - - robber.setTile(this); -} - if(CLIENT) { HexTile.prototype.setup = function(data) { this._setup(data); @@ -216,10 +212,6 @@ if(CLIENT) { this.setToken(data.token); this.setupMesh(); - - if(data.robber) { - this.setRobber(); - } } } diff --git a/shared/entities/Robber.js b/shared/entities/Robber.js index 3d54843..6074a36 100644 --- a/shared/entities/Robber.js +++ b/shared/entities/Robber.js @@ -7,7 +7,7 @@ var Robber = function() { this.create(); this.modelpath = "models/robber.js"; - this.tile = -1; + this.tile = null; }; @@ -22,10 +22,34 @@ Robber.prototype.setupMesh = function() { CATAN.Game.scene.add( this.Mesh ); } +Robber.prototype.getTile = function() { + return this.tile; +} + Robber.prototype.setTile = function(tile) { + if(this.getTile() != null) { + this.getTile().setRobber(false); + } + this.tile = tile; + this.tile.setRobber(true); + if(CLIENT) { - this.getMesh().position = new THREE.Vector3(tile.position.x, tile.position.y, tile.position.z); + this.getMesh().position = new THREE.Vector3( + tile.position.x, + tile.position.y, + tile.position.z + ); + } +} + +if(CLIENT) { + Robber.prototype.setup = function(data) { + this._setup(data); + this.setupMesh(); + + var tile = CATAN.ents.getById(data.tileId); + this.setTile(tile); } } diff --git a/shared/player.js b/shared/player.js index 3b01985..fa139a8 100644 --- a/shared/player.js +++ b/shared/player.js @@ -121,18 +121,11 @@ CATAN.Player.prototype = { }, setOwnership: function(building) { - building.Color = this.Color; this.buildings.push(building); }, isOwner: function(ent) { return ( ent.hasOwner() && ent.getOwner() == this ); - /*for(i in this.buildings) { - var b = this.buildings[i]; - if (b.getEntId() == building.getEntId() && b.Building == building.Building) { - return true; - }; - };*/ } } diff --git a/shared/schemas/classic.js b/shared/schemas/classic.js index 8f40fac..3010ca2 100644 --- a/shared/schemas/classic.js +++ b/shared/schemas/classic.js @@ -137,22 +137,22 @@ GAMEMODE.Special = [ ]; +// Default Catan Board Arrangement +// 0 = No tile +// 1 = Resource +// 2 = Dock? +GAMEMODE.getGrid = function() { + return [[0,1,1,1,0], + [1,1,1,1,1], + [1,1,1,1,1], + [1,1,1,1,1], + [0,0,1,0,0]] +} + if(SERVER) { GAMEMODE.MaxPlayers = 4; - // Default Catan Board Arrangement - // 0 = No tile - // 1 = Resource - // 2 = Dock? - GAMEMODE.getGrid = function() { - return [[0,1,1,1,0], - [1,1,1,1,1], - [1,1,1,1,1], - [1,1,1,1,1], - [0,0,1,0,0]] - } - GAMEMODE.getResources = function() { resources = [RESOURCE_DESERT]; @@ -223,21 +223,30 @@ if(SERVER) { }; // Player has built structure in the playing state. - GAMEMODE.onPlayerBuild = function(ply, building) { + GAMEMODE.onPlayerBuild = function(ply, ent) { // Remove resources - var cost = this.Buildings[building.getType()].cost; + var cost = this.Buildings[ent.getType()].cost; for(i in cost) { ply.removeResource(i, cost[i]); }; - if (building.isRoad()) { + if (ent.isRoad()) { + // check longest road - } else if (building.isSettlement() || building.isCity()) { + this.checkLongestRoad(ply); + + } else if (ent.isSettlement() || ent.isCity()) { + ply.addVictoryPoint(); this.onPlayerScore(ply); + } }; + GAMEMODE.checkLongestRoad = function(ply) { + + } + } CATAN.Schemas.register("Classic", GAMEMODE); \ No newline at end of file