Skip to content

Commit

Permalink
Use diameter instead of width and height for bots and players
Browse files Browse the repository at this point in the history
  • Loading branch information
jondubois committed Jan 13, 2017
1 parent 52bfe79 commit ce0ba4d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
7 changes: 3 additions & 4 deletions bot-manager.js
Expand Up @@ -9,14 +9,14 @@ 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 {
this.botMoveSpeed = options.botMoveSpeed;
}
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},
Expand All @@ -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();

Expand All @@ -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: {}
};
Expand Down
29 changes: 14 additions & 15 deletions cell.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
};

Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion config.js
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions public/index.html
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions worker.js
Expand Up @@ -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
};
Expand Down

0 comments on commit ce0ba4d

Please sign in to comment.