forked from Schnark/js13kgames-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.js
109 lines (101 loc) · 2.56 KB
/
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
/*global sound*/
function Robot (map) {
var start = map.getStart();
this.x = start[0];
this.y = start[1];
this.dir = start[2];
this.map = map;
}
Robot.prototype.getPos = function () {
return [this.x, this.y, this.dir];
};
Robot.prototype.setCode = function (code) {
this.origCode = code;
this.code = code;
this.codePos = 0;
this.stack = [];
};
Robot.prototype.getNextPos = function () {
switch (this.dir) {
case 0: return [this.x, this.y - 1];
case 1: return [this.x + 1, this.y];
case 2: return [this.x, this.y + 1];
case 3: return [this.x - 1, this.y];
}
};
Robot.prototype.move = function () {
var newPos = this.getNextPos(), type;
type = this.map.getType(newPos[0], newPos[1]);
if (type !== '_') {
if (/[A-EG]/.test(type)) {
type = 'target';
} else if (/[a-e]/.test(type)) {
type = 'item';
} else {
type = 'wall';
}
throw 'Crashed into ' + type + '!';
}
this.x = newPos[0];
this.y = newPos[1];
sound.play('move');
};
Robot.prototype.turn = function (dir) {
this.dir = (this.dir + dir + 4) % 4;
sound.play('turn');
};
Robot.prototype.take = function () {
var pos, type;
if (this.item) {
throw 'Can’t take two items!';
}
pos = this.getNextPos();
type = this.map.getType(pos[0], pos[1]);
if (!/[a-e]/.test(type)) {
throw 'No item to take!';
}
this.item = type;
this.map.setType(pos[0], pos[1], '_');
sound.play('take');
};
Robot.prototype.drop = function () {
var pos, type;
if (!this.item) {
throw 'No item to drop!';
}
pos = this.getNextPos();
type = this.map.getType(pos[0], pos[1]);
if (type !== '_' && type !== this.item.toUpperCase()) {
throw 'Can’t drop item here!';
}
this.map.setType(pos[0], pos[1], type === '_' ? this.item : 'G');
this.item = '';
sound.play(type === '_' ? 'drop' : 'drop-final');
};
Robot.prototype.step = function () {
var token, oldPos;
token = this.code[this.codePos];
switch (token) {
case '<': this.turn(-1); break;
case '>': this.turn(1); break;
case '_': this.move(); break;
case ',': this.take(); break;
case '.': this.drop(); break;
case ')':
oldPos = this.stack.pop();
if (oldPos !== -1) {
this.codePos = oldPos - 1; //codePos will be incremented below, so subtrcat 1
}
break;
case '1':
//restore original number of repetitions
this.code = this.code.slice(0, this.codePos) + this.origCode.charAt(this.codePos) + this.code.slice(this.codePos + 1);
this.stack.push(-1);
break;
default:
this.code = this.code.slice(0, this.codePos) + String(Number(token) - 1) + this.code.slice(this.codePos + 1);
this.stack.push(this.codePos);
}
this.codePos++;
return this.codePos;
};