diff --git a/bot-manager.js b/bot-manager.js index a32a40a..ec78c6f 100644 --- a/bot-manager.js +++ b/bot-manager.js @@ -9,7 +9,6 @@ var BOT_DEFAULT_CHANGE_DIRECTION_PROBABILITY = 0.01; var BotManager = function (options) { this.worldWidth = options.worldWidth; this.worldHeight = options.worldHeight; - this.botDiameter = options.botDiameter || BOT_DEFAULT_DIAMETER; if (options.botMoveSpeed == null) { this.botMoveSpeed = BOT_DEFAULT_SPEED; } else { @@ -17,6 +16,7 @@ var BotManager = function (options) { } this.botMass = options.botMass || BOT_DEFAULT_MASS; this.botChangeDirectionProbability = options.botChangeDirectionProbability || BOT_DEFAULT_CHANGE_DIRECTION_PROBABILITY; + this.botDefaultDiameter = options.botDefaultDiameter || BOT_DEFAULT_DIAMETER; this.botMoves = [ {u: 1}, @@ -39,7 +39,7 @@ BotManager.prototype.addBot = function (options) { if (!options) { options = {}; } - var diameter = this.botDiameter; + var diameter = options.diam || this.botDefaultDiameter; var radius = Math.round(diameter / 2); var botId = uuid.v4(); @@ -51,8 +51,7 @@ BotManager.prototype.addBot = function (options) { score: options.score || 0, speed: options.speed == null ? this.botMoveSpeed : options.speed, mass: options.mass || this.botMass, - width: diameter, - height: diameter, + diam: diameter, changeDirProb: this.botChangeDirectionProbability, op: {} }; diff --git a/cell.js b/cell.js index 6e08003..9f38617 100644 --- a/cell.js +++ b/cell.js @@ -67,7 +67,7 @@ var CellController = function (options, util) { this.botManager = new BotManager({ worldWidth: config.WORLD_WIDTH, worldHeight: config.WORLD_HEIGHT, - botDiameter: config.BOT_DIAMETER, + botDefaultDiameter: config.BOT_DEFAULT_DIAMETER, botMoveSpeed: config.BOT_MOVE_SPEED, botMass: config.BOT_MASS, botChangeDirectionProbability: config.BOT_CHANGE_DIRECTION_PROBABILITY @@ -194,7 +194,7 @@ CellController.prototype.generateBotOps = function (playerIds, players, coins) { // states are not saved unless they are grouped with one or more internal states from the current cell. // See util.groupStates() method near the bottom of this file for details. if (player.subtype == 'bot' && !player.external) { - var radius = Math.round(player.width / 2); + var radius = Math.round(player.diam / 2); var isBotOnEdge = player.x <= radius || player.x >= config.WORLD_WIDTH - radius || player.y <= radius || player.y >= config.WORLD_HEIGHT - radius; @@ -210,23 +210,22 @@ CellController.prototype.generateBotOps = function (playerIds, players, coins) { }; CellController.prototype.keepPlayerOnGrid = function (player) { - var halfWidth = Math.round(player.width / 2); - var halfHeight = Math.round(player.height / 2); + var radius = Math.round(player.diam / 2); - var leftX = player.x - halfWidth; - var rightX = player.x + halfWidth; - var topY = player.y - halfHeight; - var bottomY = player.y + halfHeight; + var leftX = player.x - radius; + var rightX = player.x + radius; + var topY = player.y - radius; + var bottomY = player.y + radius; if (leftX < 0) { - player.x = halfWidth; + player.x = radius; } else if (rightX > config.WORLD_WIDTH) { - player.x = config.WORLD_WIDTH - halfWidth; + player.x = config.WORLD_WIDTH - radius; } if (topY < 0) { - player.y = halfHeight; + player.y = radius; } else if (bottomY > config.WORLD_HEIGHT) { - player.y = config.WORLD_HEIGHT - halfHeight; + player.y = config.WORLD_HEIGHT - radius; } }; @@ -353,7 +352,7 @@ CellController.prototype.findPlayerOverlaps = function (playerIds, players, coin }; CellController.prototype.generateHitArea = function (target) { - var targetRadius = target.r || Math.round(target.width / 2); + var targetRadius = target.r || Math.round(target.diam / 2); return { target: target, minX: target.x - targetRadius, @@ -364,8 +363,8 @@ CellController.prototype.generateHitArea = function (target) { }; CellController.prototype.testCircleCollision = function (a, b) { - var radiusA = a.r || Math.round(a.width / 2); - var radiusB = b.r || Math.round(b.width / 2); + var radiusA = a.r || Math.round(a.diam / 2); + var radiusB = b.r || Math.round(b.diam / 2); var circleA = new SAT.Circle(new SAT.Vector(a.x, a.y), radiusA); var circleB = new SAT.Circle(new SAT.Vector(b.x, b.y), radiusB); diff --git a/config.js b/config.js index 9c104d8..5065781 100644 --- a/config.js +++ b/config.js @@ -47,7 +47,7 @@ module.exports = { BOT_COUNT: 10, BOT_MOVE_SPEED: 5, BOT_MASS: 10, - BOT_DIAMETER: 45, + BOT_DEFAULT_DIAMETER: 45, BOT_CHANGE_DIRECTION_PROBABILITY: 0.01, COIN_UPDATE_INTERVAL: 1000, diff --git a/public/index.html b/public/index.html index dcea401..532ffbb 100644 --- a/public/index.html +++ b/public/index.html @@ -92,7 +92,7 @@ WORLD_ROWS = data.rows; WORLD_CELL_WIDTH = data.cellWidth; WORLD_CELL_HEIGHT = data.cellHeight; - WORLD_CELL_OVERLAP_DISTANCE = data.cellOverlapDistance + WORLD_CELL_OVERLAP_DISTANCE = data.cellOverlapDistance; SERVER_WORKER_ID = data.serverWorkerId; ENVIRONMENT = data.environment; @@ -251,8 +251,9 @@ user.score = userData.score; user.sprite = sprite; - user.width = user.sprite.width; - user.height = user.sprite.height; + user.sprite.width = Math.round(userData.diam * 0.75); + user.sprite.height = userData.diam; + user.diam = user.sprite.width; moveUser(userData.id, userData.x, userData.y); diff --git a/worker.js b/worker.js index 379efe7..634c8d5 100644 --- a/worker.js +++ b/worker.js @@ -715,8 +715,7 @@ module.exports.run = function (worker) { name: playerOptions.name, x: startingPos.x, y: startingPos.y, - width: PLAYER_DIAMETER, - height: PLAYER_DIAMETER, + diam: PLAYER_DIAMETER, mass: PLAYER_MASS, score: 0 };