-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
50 lines (48 loc) · 1.32 KB
/
bot.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
var Bot = function(direction) {
this.direction = direction;
};
Bot.right = 0;
Bot.left = Math.PI;
Bot.up = Math.PI*1.5;
Bot.down = Math.PI*0.5;
$.extend(Bot.prototype, CanvasHelper, {
setPosition: function(x, y) {
this.position = {x:x,y:y};
},
left: function() {
this.direction -= Math.PI * 0.5;
},
right: function() {
this.direction += Math.PI * 0.5;
},
move: function() {
var currentLocation = this.field.location(this.position.x, this.position.y);
var newY = this.position.y + Math.floor(Math.sin(this.direction));
var newX = this.position.x + Math.floor(Math.cos(this.direction));
var newLocation = this.field.location(newX, newY);
if (newLocation) {
this.position = {x:newX, y:newY};
newLocation.bot = this;
currentLocation.bot = null;
newLocation.star = null;
}
},
draw: function(c) {
var self = this;
var size = this.blockSize - 20;
this.protect(function() {
c.translate(self.blockSize / 2, self.blockSize / 2);
c.rotate(self.direction);
c.fillStyle = '#e00';
$.each([1, -1], function() {
c.beginPath();
c.moveTo(0, 0);
c.lineTo(size, 0);
c.lineTo(-(size * 0.7), this * (size * 0.7));
c.lineTo(-(size * 0.3), 0);
c.closePath();
c.fill();
});
});
}
});