-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbot-manager.js
96 lines (85 loc) · 2.37 KB
/
bot-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var uuid = require('uuid');
var SAT = require('sat');
var config = require('./config');
var BOT_DEFAULT_DIAMETER = 80;
var BOT_DEFAULT_SPEED = 1;
var BOT_DEFAULT_MASS = 10;
var BOT_DEFAULT_CHANGE_DIRECTION_PROBABILITY = 0.01;
var BotManager = function (options) {
this.worldWidth = options.worldWidth;
this.worldHeight = options.worldHeight;
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},
{d: 1},
{r: 1},
{l: 1}
];
};
BotManager.prototype.generateRandomPosition = function (botRadius) {
var botDiameter = botRadius * 2;
var position = {
x: Math.round(Math.random() * (this.worldWidth - botDiameter) + botRadius),
y: Math.round(Math.random() * (this.worldHeight - botDiameter) + botRadius)
};
return position;
};
BotManager.prototype.addBot = function (options) {
if (!options) {
options = {};
}
var botId = uuid.v4();
var bot = {
id: botId,
type: 'player',
// bots with heroId = 0
heroId: 0,
lastAttackDelay: -5,
subtype: 'bot',
health: config.HEROS_OPTIONS[0].baseHealth,
name: options.name || 'bot-' + Math.round(Math.random() * 10000),
score: options.score || 0,
speed: options.speed == null ? this.botMoveSpeed : options.speed,
mass: options.mass || this.botMass,
diam: config.HEROS_OPTIONS[0].diameter,
direction: "down1",
currentSkill: "",
delay: 5,
walkerStep: 1,
attackStep: -1,
auxAttackStep: -1,
auxWalkerStep: 10,
iddle: 0,
changeDirProb: this.botChangeDirectionProbability,
op: {}
};
if (options.x && options.y) {
bot.x = options.x;
bot.y = options.y;
} else {
var radius = Math.round(bot.diam / 2);
var position = this.generateRandomPosition(radius);
if (options.x) {
bot.x = options.x;
} else {
bot.x = position.x;
}
if (options.y) {
bot.y = options.y;
} else {
bot.y = position.y;
}
}
return bot;
};
BotManager.prototype.removeBot = function (bot) {
bot.delete = 1;
};
module.exports.BotManager = BotManager;