forked from youchenlee/robocode-js
-
Notifications
You must be signed in to change notification settings - Fork 8
/
base-robot.js
151 lines (151 loc) · 4.1 KB
/
base-robot.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Generated by LiveScript 1.2.0
(function(){
var BaseRobot;
importScripts('../log.js');
BaseRobot = (function(){
BaseRobot.displayName = 'BaseRobot';
var prototype = BaseRobot.prototype, constructor = BaseRobot;
prototype.me = {
id: 0,
x: 0,
y: 0,
hp: 0
};
prototype.enemySpot = [];
function BaseRobot(name){
var this$ = this;
this.name = name != null ? name : "base-robot";
this.event_counter = 0;
this.callbacks = {};
self.onmessage = function(e){
return this$.receive(e.data);
};
}
prototype.move_forwards = function(distance, callback){
callback == null && (callback = null);
this.send({
"action": "move_forwards",
"amount": distance
}, callback);
};
prototype.move_backwards = function(distance, callback){
callback == null && (callback = null);
this.send({
"action": "move_backwards",
"amount": distance
}, callback);
};
prototype.move_opposide = function(distance, callback){
callback == null && (callback = null);
this.send({
"action": "move_opposide",
"amount": distance
}, callback);
};
prototype.turn_left = function(angle, callback){
callback == null && (callback = null);
this.send({
"action": "turn_left",
"amount": angle
}, callback);
};
prototype.turn_right = function(angle, callback){
callback == null && (callback = null);
this.send({
"action": "turn_right",
"amount": angle
}, callback);
};
prototype.turn_turret_left = function(angle, callback){
callback == null && (callback = null);
this.send({
"action": "turn_turret_left",
"amount": angle
});
};
prototype.turn_turret_right = function(angle, callback){
callback == null && (callback = null);
this.send({
"action": "turn_turret_right",
"amount": angle
});
};
prototype.shoot = function(){
this.send({
"action": "shoot"
});
};
prototype.yell = function(msg){
this.send({
"action": "yell",
"msg": msg
});
};
prototype.receive = function(msg){
var msg_obj;
msg_obj = JSON.parse(msg);
if (msg_obj.me) {
this.me = msg_obj.me;
}
switch (msg_obj["action"]) {
case "run":
this._run();
break;
case "callback":
logger.log('callback');
logger.log(this.event_counter);
if (typeof this.callbacks[msg_obj["event_id"]] === "function") {
this.callbacks[msg_obj["event_id"]]();
}
this.event_counter--;
if (this.event_counter === 0) {
this._run();
}
break;
case "interruption":
logger.log('interruption');
logger.log(this.event_counter);
this.event_counter = 0;
if (msg_obj["status"].wallCollide) {
this.onWallCollide();
}
if (msg_obj["status"].isHit) {
this.onHit();
}
console.log('onhit-and-run');
this._run();
break;
case "enemy-spot":
logger.log('enemy-spot');
this.enemySpot = msg_obj["enemy-spot"];
this.onEnemySpot();
}
};
prototype._run = function(){
var this$ = this;
logger.log(this.event_counter);
console.log('run');
setTimeout(function(){
return this$.onIdle();
}, 0);
};
prototype.onIdle = function(){
throw "You need to implement the onIdle() method";
};
prototype.onWallCollide = function(){
throw "You need to implement the onWallCollide() method";
};
prototype.onHit = function(){};
prototype.onEnemySpot = function(){};
prototype.send = function(msg_obj, callback){
var event_id;
logger.log('send' + " " + msg_obj.action);
event_id = this.event_counter++;
this.callbacks[event_id] = callback;
msg_obj["event_id"] = event_id;
postMessage(JSON.stringify(msg_obj));
};
return BaseRobot;
}());
this.BaseRobot = BaseRobot;
}).call(this);