-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgemooy.js
More file actions
121 lines (104 loc) · 3 KB
/
Copy pathgemooy.js
File metadata and controls
121 lines (104 loc) · 3 KB
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
/*
* requires yoob.Controller
* requires yoob.Playfield
* requires yoob.Cursor
*/
function GemooyPlayfield() {
this.setDefault(' ');
this.increment = function(x, y) {
var data = this.get(x, y);
if (data === ' ') {
data = '#';
} else if (data === '#') {
data = '@';
} else if (data === '@') {
data = ' ';
}
this.put(x, y, data);
};
this.decrement = function(x, y) {
var data = this.get(x, y);
if (data === ' ') {
data = '@';
} else if (data === '@') {
data = '#';
} else if (data === '#') {
data = ' ';
}
this.put(x, y, data);
};
};
GemooyPlayfield.prototype = new yoob.Playfield();
var proto = new yoob.Controller();
function GemooyController() {
var intervalId;
var p;
var ip;
var dp;
this.init = function(cfg) {
proto.init.apply(this, [cfg]);
p = new GemooyPlayfield();
ip = new yoob.Cursor({ x: 0, y: 0, dx: 1, dy: 1 });
ip.fillStyle = "#ff5080";
dp = new yoob.Cursor({ x: 0, y: 0, dx: 0, dy: 0 });
dp.fillStyle = "#50ff80";
p.setCursors([ip, dp]);
cfg.view.pf = p;
this.view = cfg.view;
cfg.view.drawCursor = function(ctx, cursor, canvasX, canvasY, cellWidth, cellHeight) {
ctx.fillStyle = cursor.fillStyle || "#50ff50";
ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight);
};
return this;
};
this.step = function() {
var instr = p.get(ip.x, ip.y);
if (instr === '@') {
var data = p.get(dp.x, dp.y);
if (data === ' ') {
ip.rotateClockwise();
} else if (data == '#') {
ip.rotateCounterclockwise();
}
} else if (instr === '#') {
if (ip.isHeaded(0, -1)) {
dp.y--;
ip.advance();
} else if (ip.isHeaded(0, 1)) {
dp.y++;
ip.advance();
} else if (ip.isHeaded(1, 0)) {
dp.x++;
ip.advance();
} else if (ip.isHeaded(-1, 0)) {
dp.x--;
ip.advance();
} else if (ip.isHeaded(-1, -1) || ip.isHeaded(1, -1)) {
p.increment(dp.x, dp.y);
} else if (ip.isHeaded(-1, 1) || ip.isHeaded(1, 1)) {
p.decrement(dp.x, dp.y);
}
}
ip.advance();
this.view.draw();
};
this.reset = function(text) {
p.clear();
p.load(0, 0, text);
p.foreach(function (x, y, value) {
if (value === '$') {
ip.x = x;
ip.y = y;
return ' ';
} else if (value === '%') {
dp.x = x;
dp.y = y;
return ' ';
}
});
ip.dx = 1;
ip.dy = 1;
this.view.draw();
};
};
GemooyController.prototype = proto;