-
Notifications
You must be signed in to change notification settings - Fork 0
/
jack.js
76 lines (65 loc) · 2.01 KB
/
jack.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
const Jack = function (url) {
const _self = this;
$.getJSON(url, function(json) { _self.init(json); })
}
Jack.prototype.init = function(raw) {
this.x = raw.x;
this.y = raw.y;
this.width = raw.image[0].length;
this.height = raw.image.length;
this.image = raw.image;
}
Jack.prototype.getPixels = function () {
const board = document.getElementById('board');
const ctx = board.getContext('2d');
const imageData = ctx.getImageData(this.x, this.y, this.width, this.height);
this.pixels = [];
for(let y = 0; y < this.height; y++) {
for(let x = 0; x < this.width; x++) {
const base = y * this.width * 4 + x * 4;
let color = '#FFFFFF';
if (jack.image[y][x] !== -1) {
color = App.palette[jack.image[y][x]];
const R = parseInt('0x'+color[1]+color[2]);
const G = parseInt('0x'+color[3]+color[4]);
const B = parseInt('0x'+color[5]+color[6]);
if (imageData.data[base] !== R || imageData.data[base + 1] !== G || imageData.data[base + 2] !== B) {
this.pixels.push({
x: jack.x + x,
y: jack.y + y,
color: jack.image[y][x]
})
}
}
}
}
}
Jack.prototype.draw = function () {
const board = document.getElementById('board');
const ctx = board.getContext('2d');
for(let i = 0; i < this.pixels.length; i++) {
const pixel = this.pixels[i];
ctx.fillStyle = App.palette[pixel.color];
ctx.fillRect(pixel.x, pixel.y, 1, 1);
}
}
Jack.prototype.autofill = function () {
const _self = this;
this.pid = setInterval(function() {
if (new Date() > App.cooldown) {
_self.getPixels();
if (_self.pixels.length > 0) {
const rand = Math.floor(Math.random() * _self.pixels.length);
const pixel = _self.pixels[rand];
App.switchColor(pixel.color);
App.attemptPlace(pixel.x, pixel.y);
console.log('fill: ', pixel);
} else {
_self.stop();
}
}
}, 10000);
}
Jack.prototype.stop = function () {
clearInterval(this.pid);
}