-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbike.server.js
64 lines (60 loc) · 1.81 KB
/
bike.server.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
module.exports.Bike = function(data) {
this.corners = data.corners;
this.color = data.color;
this.speed = data.speed;
this.head = data.head;
this.turn = function (direction) {
if (this.speed > 0) {//yeap we are alive
switch (this.head.direction) {
case 'u':
if (('u' == direction) || ('d') == direction) {
return;
}
break;
case 'd':
if (('u' == direction) || ('d') == direction) {
return;
}
break;
case 'l':
if (('l' == direction) || ('r') == direction) {
return;
}
break;
case 'r':
if (('l' == direction) || ('r') == direction) {
return;
}
break;
}
this.corners.push({ x:this.head.x,
y:this.head.y});
this.head.direction = direction;
}
}
this.tick = function () {
if (this.speed > 0) {//yeap we are alive
switch (this.head.direction) {
case 'u':
this.head.y -= 1;
break;
case 'd':
this.head.y += 1;
break;
case 'l':
this.head.x -= 1;
break;
case 'r':
this.head.x += 1;
break;
}
}
}
this.die = function() {
console.log("bike with color " + this.color + " died");
this.speed = 0;
}
this.toString = function() {
return "color:" + this.color + " head at (" + this.head.x + ";" + this.head.y + ")";
}
}