-
Notifications
You must be signed in to change notification settings - Fork 2
/
melonham.js
86 lines (69 loc) · 1.93 KB
/
melonham.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
(function () {
var melonham = me.plugin.Base.extend({
"version" : "0.9.11",
"plot" : function (p0, p1, callback) {
var x0 = p0.x;
var y0 = p0.y;
var x1 = p1.x;
var y1 = p1.y;
var dx = Math.abs(x1 - x0);
var dy = Math.abs(y1 - y0);
var sx = (x0 < x1) ? 1 : -1;
var sy = (y0 < y1) ? 1 : -1;
var err = dx - dy;
var e2 = 0;
do {
if (callback(x0, y0)) {
return me.entityPool.newInstanceOf(
"me.Vector2d",
x0,
y0
);
}
if (x0 === x1 && y0 === y1) {
break;
}
e2 = err * 2;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (x0 === x1 && y0 === y1) {
if (callback(x0, y0)) {
return me.entityPool.newInstanceOf(
"me.Vector2d",
x0,
y0
);
}
break;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
} while (true);
return null;
},
"collide" : function (p0, p1) {
var tile = null;
var map = me.game.collisionMap;
function getTile(x, y) {
if (x < 0 || y < 0 || x > map.cols || y > map.rows) {
return null;
}
return map.layerData[x][y];
}
return this.plot(p0, p1, function (x, y) {
tile = getTile(x, y);
if (tile &&
tile.tileset.getTileProperties(tile.tileId).isCollidable) {
return true;
}
return false;
});
}
});
me.entityPool.add("me.Vector2d", me.Vector2d, true);
me.plugin.register(melonham, "melonham");
})();