-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
102 lines (82 loc) · 2.34 KB
/
index.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
var hxDaedalus = require("hxdaedalus-js").hxDaedalus;
var RectMesh = hxDaedalus.factories.RectMesh;
var EntityAI = hxDaedalus.ai.EntityAI;
var PathFinder = hxDaedalus.ai.PathFinder;
var DaedalusObject = hxDaedalus.data.Object;
function Walkable(width, height) {
var mesh = RectMesh.buildRectangle(width, height);
var entity = new EntityAI();
var pathFinder = new PathFinder();
pathFinder.entity = entity;
pathFinder.set_mesh(mesh);
this.mesh = mesh;
this.entity = entity;
this.pathFinder = pathFinder;
this.path = [];
}
Walkable.prototype.addRect = function (w, h, x, y) {
var obj = new DaedalusObject();
obj.set_coordinates([0, 0, 0, h, 0, h, w, h, w, h, w, 0, w, 0, 0, 0]);
obj.set_x(x);
obj.set_y(y);
this.mesh.insertObject(obj);
return obj;
};
Walkable.prototype.addPolygon = function (vertices, x, y) {
if (vertices.length < 6) {
console.log("Polygons must contain at least 3 points!");
return;
}
var obj = new DaedalusObject();
var coords = [];
var prevX = vertices[vertices.length - 2],
prevY = vertices[vertices.length - 1];
for (var i = 0; i < vertices.length; i += 2) {
var currX = vertices[i],
currY = vertices[i + 1];
coords.push(prevX, prevY, currX, currY);
prevX = currX;
prevY = currY;
}
obj.set_coordinates(coords);
obj.set_x(x || 0);
obj.set_y(y || 0);
this.mesh.insertObject(obj);
return obj;
};
Walkable.prototype.addPolyline = function (vertices, x, y) {
if (vertices.length < 4) {
console.log("Polylines must contain at least 2 points!");
return;
}
var obj = new DaedalusObject();
var coords = [];
var prevX = 0,
prevY = 0;
for (var i = 0; i < vertices.length; i += 2) {
var currX = vertices[i],
currY = vertices[i + 1];
if (i > 0) {
coords.push(prevX, prevY, currX, currY);
}
prevX = currX;
prevY = currY;
}
obj.set_coordinates(coords);
obj.set_x(x || 0);
obj.set_y(y || 0);
this.mesh.insertObject(obj);
return obj;
};
Walkable.prototype.deleteObstacle = function (obj) {
this.mesh.deleteObject(obj);
};
Walkable.prototype.findPath = function (fromX, fromY, toX, toY, radius, path) {
if (path === undefined) path = this.path;
this.entity.set_radius(radius);
this.entity.x = fromX;
this.entity.y = fromY;
this.pathFinder.findPath(toX, toY, path);
return path;
};
module.exports = Walkable;