canvasengine 1.3.0 → 2.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +0 -0
- package/.github/workflows/ci.yml +29 -0
- package/README.md +79 -0
- package/dist/compiler/vite.js +119 -0
- package/dist/compiler/vite.js.map +1 -0
- package/dist/index.d.ts +846 -0
- package/dist/index.js +3340 -0
- package/dist/index.js.map +1 -0
- package/index.d.ts +6 -0
- package/logo.png +0 -0
- package/package.json +84 -18
- package/src/compiler/grammar.pegjs +180 -0
- package/src/compiler/vite.ts +166 -0
- package/src/components/Canvas.ts +134 -0
- package/src/components/Container.ts +46 -0
- package/src/components/DisplayObject.ts +458 -0
- package/src/components/DrawMap/index.ts +65 -0
- package/src/components/Graphic.ts +147 -0
- package/src/components/NineSliceSprite.ts +46 -0
- package/src/components/ParticleEmitter.ts +39 -0
- package/src/components/Scene.ts +6 -0
- package/src/components/Sprite.ts +493 -0
- package/src/components/Text.ts +145 -0
- package/src/components/Tilemap/Tile.ts +79 -0
- package/src/components/Tilemap/TileGroup.ts +207 -0
- package/src/components/Tilemap/TileLayer.ts +163 -0
- package/src/components/Tilemap/TileSet.ts +41 -0
- package/src/components/Tilemap/index.ts +80 -0
- package/src/components/TilingSprite.ts +39 -0
- package/src/components/Viewport.ts +159 -0
- package/src/components/index.ts +12 -0
- package/src/components/types/DisplayObject.ts +68 -0
- package/src/components/types/MouseEvent.ts +3 -0
- package/src/components/types/Spritesheet.ts +389 -0
- package/src/components/types/index.ts +4 -0
- package/src/directives/Drag.ts +84 -0
- package/src/directives/KeyboardControls.ts +922 -0
- package/src/directives/Scheduler.ts +112 -0
- package/src/directives/Sound.ts +91 -0
- package/src/directives/Transition.ts +45 -0
- package/src/directives/ViewportCull.ts +40 -0
- package/src/directives/ViewportFollow.ts +26 -0
- package/src/directives/index.ts +7 -0
- package/src/engine/animation.ts +113 -0
- package/src/engine/bootstrap.ts +19 -0
- package/src/engine/directive.ts +23 -0
- package/src/engine/reactive.ts +379 -0
- package/src/engine/signal.ts +138 -0
- package/src/engine/trigger.ts +40 -0
- package/src/engine/utils.ts +135 -0
- package/src/hooks/addContext.ts +6 -0
- package/src/hooks/useProps.ts +155 -0
- package/src/hooks/useRef.ts +21 -0
- package/src/index.ts +14 -0
- package/src/presets/Bar.ts +89 -0
- package/src/presets/Button.ts +0 -0
- package/src/presets/Joystick.ts +286 -0
- package/src/presets/NightAmbiant.ts +122 -0
- package/src/presets/Particle.ts +53 -0
- package/src/utils/Ease.ts +33 -0
- package/src/utils/RadialGradient.ts +86 -0
- package/starter/assets/logo.png +0 -0
- package/starter/components/app.ce +18 -0
- package/starter/components/hello.ce +35 -0
- package/starter/index.html +21 -0
- package/starter/main.ts +6 -0
- package/starter/package.json +20 -0
- package/starter/tsconfig.json +32 -0
- package/starter/vite.config.ts +12 -0
- package/tsconfig.json +32 -0
- package/tsconfig.node.json +10 -0
- package/tsup.config.ts +28 -0
- package/vitest.config.ts +12 -0
- package/.gitattributes +0 -22
- package/.npmignore +0 -163
- package/canvasengine-1.3.0.all.min.js +0 -21
- package/canvasengine.js +0 -5802
- package/core/DB.js +0 -24
- package/core/ModelServer.js +0 -348
- package/core/Users.js +0 -190
- package/core/engine-common.js +0 -952
- package/doc/cocoonjs.md +0 -36
- package/doc/doc-lang.yml +0 -43
- package/doc/doc-router.yml +0 -14
- package/doc/doc-tuto.yml +0 -9
- package/doc/doc.yml +0 -39
- package/doc/element.md +0 -37
- package/doc/entity.md +0 -90
- package/doc/extend.md +0 -47
- package/doc/get_started.md +0 -19
- package/doc/images/entity.png +0 -0
- package/doc/multitouch.md +0 -58
- package/doc/nodejs.md +0 -142
- package/doc/scene.md +0 -44
- package/doc/text.md +0 -156
- package/examples/server/client.html +0 -31
- package/examples/server/server.js +0 -16
- package/examples/tiled_server/client.html +0 -52
- package/examples/tiled_server/images/tiles_spritesheet.png +0 -0
- package/examples/tiled_server/server/map.json +0 -50
- package/examples/tiled_server/server/map.tmx +0 -16
- package/examples/tiled_server/server/server.js +0 -16
- package/extends/Animation.js +0 -910
- package/extends/Effect.js +0 -252
- package/extends/Gleed2d.js +0 -252
- package/extends/Hit.js +0 -1509
- package/extends/Input.js +0 -699
- package/extends/Marshal.js +0 -716
- package/extends/Scrolling.js +0 -388
- package/extends/Soundmanager2.js +0 -5466
- package/extends/Spritesheet.js +0 -196
- package/extends/Text.js +0 -366
- package/extends/Tiled.js +0 -403
- package/extends/Window.js +0 -575
- package/extends/gamepad.js +0 -397
- package/extends/socket.io.min.js +0 -2
- package/extends/swf/soundmanager2.swf +0 -0
- package/extends/swf/soundmanager2_debug.swf +0 -0
- package/extends/swf/soundmanager2_flash9.swf +0 -0
- package/extends/swf/soundmanager2_flash9_debug.swf +0 -0
- package/extends/swf/soundmanager2_flash_xdomain.zip +0 -0
- package/extends/workers/transition.js +0 -43
- package/index.js +0 -46
- package/license.txt +0 -19
- package/readme.md +0 -483
package/extends/Hit.js
DELETED
|
@@ -1,1509 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (C) 2012 by WebCreative5, Samuel Ronce
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
in the Software without restriction, including without limitation the rights
|
|
7
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
furnished to do so, subject to the following conditions:
|
|
10
|
-
|
|
11
|
-
The above copyright notice and this permission notice shall be included in
|
|
12
|
-
all copies or substantial portions of the Software.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
-
THE SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (typeof exports != "undefined") {
|
|
25
|
-
var CE = require("canvasengine").listen(),
|
|
26
|
-
CanvasEngine = false,
|
|
27
|
-
Class = CE.Class;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/*
|
|
31
|
-
http://www.amphibian.com/blogstuff/collision.html
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
/*
|
|
36
|
-
* This is the Point constructor. Polygon uses this object, but it is
|
|
37
|
-
* just a really simple set of x and y coordinates.
|
|
38
|
-
*/
|
|
39
|
-
Class.create("Point", {
|
|
40
|
-
|
|
41
|
-
initialize: function(px, py) {
|
|
42
|
-
this.x = px;
|
|
43
|
-
this.y = py;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
/*
|
|
51
|
-
* This is the Polygon constructor. All points are center-relative.
|
|
52
|
-
*/
|
|
53
|
-
Class.create("Polygon", {
|
|
54
|
-
|
|
55
|
-
initialize: function(c) {
|
|
56
|
-
this.points = [];
|
|
57
|
-
this.center = c;
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
/*
|
|
61
|
-
* Point x and y values should be relative to the center.
|
|
62
|
-
*/
|
|
63
|
-
addPoint: function(p) {
|
|
64
|
-
this.points.push(p);
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
/*
|
|
68
|
-
* Point x and y values should be absolute coordinates.
|
|
69
|
-
*/
|
|
70
|
-
addAbsolutePoint: function(p) {
|
|
71
|
-
this.points.push( { "x": p.x - this.center.x, "y": p.y - this.center.y } );
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
/*
|
|
75
|
-
* Returns the number of sides. Equal to the number of vertices.
|
|
76
|
-
*/
|
|
77
|
-
getNumberOfSides: function() {
|
|
78
|
-
return this.points.length;
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
/*
|
|
82
|
-
* rotate the polygon by a number of radians
|
|
83
|
-
*/
|
|
84
|
-
rotate: function(rads) {
|
|
85
|
-
|
|
86
|
-
for (var i = 0; i < this.points.length; i++) {
|
|
87
|
-
var x = this.points[i].x;
|
|
88
|
-
var y = this.points[i].y;
|
|
89
|
-
this.points[i].x = Math.cos(rads) * x - Math.sin(rads) * y;
|
|
90
|
-
this.points[i].y = Math.sin(rads) * x + Math.cos(rads) * y;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
/*
|
|
96
|
-
* This function returns true if the given point is inside the polygon,
|
|
97
|
-
* and false otherwise.
|
|
98
|
-
*/
|
|
99
|
-
containsPoint: function(pnt) {
|
|
100
|
-
|
|
101
|
-
var nvert = this.points.length;
|
|
102
|
-
var testx = pnt.x;
|
|
103
|
-
var testy = pnt.y;
|
|
104
|
-
|
|
105
|
-
var vertx = new Array();
|
|
106
|
-
for (var q = 0; q < this.points.length; q++) {
|
|
107
|
-
vertx.push(this.points[q].x + this.center.x);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
var verty = new Array();
|
|
111
|
-
for (var w = 0; w < this.points.length; w++) {
|
|
112
|
-
verty.push(this.points[w].y + this.center.y);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
var i, j = 0;
|
|
116
|
-
var c = false;
|
|
117
|
-
for (i = 0, j = nvert - 1; i < nvert; j = i++) {
|
|
118
|
-
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
|
|
119
|
-
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
|
|
120
|
-
c = !c;
|
|
121
|
-
}
|
|
122
|
-
return c;
|
|
123
|
-
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
/*
|
|
127
|
-
* To detect intersection with another Polygon object, this
|
|
128
|
-
* function uses the Separating Axis Theorem. It returns false
|
|
129
|
-
* if there is no intersection, or an object if there is. The object
|
|
130
|
-
* contains 2 fields, overlap and axis. Moving the polygon by overlap
|
|
131
|
-
* on axis will get the polygons out of intersection.
|
|
132
|
-
*/
|
|
133
|
-
intersectsWith: function(other) {
|
|
134
|
-
|
|
135
|
-
var axis = Class.New("Point");
|
|
136
|
-
var tmp, minA, maxA, minB, maxB;
|
|
137
|
-
var side, i;
|
|
138
|
-
var smallest = null;
|
|
139
|
-
var overlap = 99999999;
|
|
140
|
-
|
|
141
|
-
/* test polygon A's sides */
|
|
142
|
-
for (side = 0; side < this.getNumberOfSides(); side++)
|
|
143
|
-
{
|
|
144
|
-
/* get the axis that we will project onto */
|
|
145
|
-
if (side == 0)
|
|
146
|
-
{
|
|
147
|
-
axis.x = this.points[this.getNumberOfSides() - 1].y - this.points[0].y;
|
|
148
|
-
axis.y = this.points[0].x - this.points[this.getNumberOfSides() - 1].x;
|
|
149
|
-
}
|
|
150
|
-
else
|
|
151
|
-
{
|
|
152
|
-
axis.x = this.points[side - 1].y - this.points[side].y;
|
|
153
|
-
axis.y = this.points[side].x - this.points[side - 1].x;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/* normalize the axis */
|
|
157
|
-
tmp = Math.sqrt(axis.x * axis.x + axis.y * axis.y);
|
|
158
|
-
axis.x /= tmp;
|
|
159
|
-
axis.y /= tmp;
|
|
160
|
-
|
|
161
|
-
/* project polygon A onto axis to determine the min/max */
|
|
162
|
-
minA = maxA = this.points[0].x * axis.x + this.points[0].y * axis.y;
|
|
163
|
-
for (i = 1; i < this.getNumberOfSides(); i++)
|
|
164
|
-
{
|
|
165
|
-
tmp = this.points[i].x * axis.x + this.points[i].y * axis.y;
|
|
166
|
-
if (tmp > maxA)
|
|
167
|
-
maxA = tmp;
|
|
168
|
-
else if (tmp < minA)
|
|
169
|
-
minA = tmp;
|
|
170
|
-
}
|
|
171
|
-
/* correct for offset */
|
|
172
|
-
tmp = this.center.x * axis.x + this.center.y * axis.y;
|
|
173
|
-
minA += tmp;
|
|
174
|
-
maxA += tmp;
|
|
175
|
-
|
|
176
|
-
/* project polygon B onto axis to determine the min/max */
|
|
177
|
-
minB = maxB = other.points[0].x * axis.x + other.points[0].y * axis.y;
|
|
178
|
-
for (i = 1; i < other.getNumberOfSides(); i++)
|
|
179
|
-
{
|
|
180
|
-
tmp = other.points[i].x * axis.x + other.points[i].y * axis.y;
|
|
181
|
-
if (tmp > maxB)
|
|
182
|
-
maxB = tmp;
|
|
183
|
-
else if (tmp < minB)
|
|
184
|
-
minB = tmp;
|
|
185
|
-
}
|
|
186
|
-
/* correct for offset */
|
|
187
|
-
tmp = other.center.x * axis.x + other.center.y * axis.y;
|
|
188
|
-
minB += tmp;
|
|
189
|
-
maxB += tmp;
|
|
190
|
-
|
|
191
|
-
/* test if lines intersect, if not, return false */
|
|
192
|
-
if (maxA < minB || minA > maxB) {
|
|
193
|
-
return false;
|
|
194
|
-
} else {
|
|
195
|
-
var o = (maxA > minB ? maxA - minB : maxB - minA);
|
|
196
|
-
if (o < overlap) {
|
|
197
|
-
overlap = o;
|
|
198
|
-
|
|
199
|
-
smallest = {x: axis.x, y: axis.y};
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/* test polygon B's sides */
|
|
205
|
-
for (side = 0; side < other.getNumberOfSides(); side++)
|
|
206
|
-
{
|
|
207
|
-
/* get the axis that we will project onto */
|
|
208
|
-
if (side == 0)
|
|
209
|
-
{
|
|
210
|
-
axis.x = other.points[other.getNumberOfSides() - 1].y - other.points[0].y;
|
|
211
|
-
axis.y = other.points[0].x - other.points[other.getNumberOfSides() - 1].x;
|
|
212
|
-
}
|
|
213
|
-
else
|
|
214
|
-
{
|
|
215
|
-
axis.x = other.points[side - 1].y - other.points[side].y;
|
|
216
|
-
axis.y = other.points[side].x - other.points[side - 1].x;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/* normalize the axis */
|
|
220
|
-
tmp = Math.sqrt(axis.x * axis.x + axis.y * axis.y);
|
|
221
|
-
axis.x /= tmp;
|
|
222
|
-
axis.y /= tmp;
|
|
223
|
-
|
|
224
|
-
/* project polygon A onto axis to determine the min/max */
|
|
225
|
-
minA = maxA = this.points[0].x * axis.x + this.points[0].y * axis.y;
|
|
226
|
-
for (i = 1; i < this.getNumberOfSides(); i++)
|
|
227
|
-
{
|
|
228
|
-
tmp = this.points[i].x * axis.x + this.points[i].y * axis.y;
|
|
229
|
-
if (tmp > maxA)
|
|
230
|
-
maxA = tmp;
|
|
231
|
-
else if (tmp < minA)
|
|
232
|
-
minA = tmp;
|
|
233
|
-
}
|
|
234
|
-
/* correct for offset */
|
|
235
|
-
tmp = this.center.x * axis.x + this.center.y * axis.y;
|
|
236
|
-
minA += tmp;
|
|
237
|
-
maxA += tmp;
|
|
238
|
-
|
|
239
|
-
/* project polygon B onto axis to determine the min/max */
|
|
240
|
-
minB = maxB = other.points[0].x * axis.x + other.points[0].y * axis.y;
|
|
241
|
-
for (i = 1; i < other.getNumberOfSides(); i++)
|
|
242
|
-
{
|
|
243
|
-
tmp = other.points[i].x * axis.x + other.points[i].y * axis.y;
|
|
244
|
-
if (tmp > maxB)
|
|
245
|
-
maxB = tmp;
|
|
246
|
-
else if (tmp < minB)
|
|
247
|
-
minB = tmp;
|
|
248
|
-
}
|
|
249
|
-
/* correct for offset */
|
|
250
|
-
tmp = other.center.x * axis.x + other.center.y * axis.y;
|
|
251
|
-
minB += tmp;
|
|
252
|
-
maxB += tmp;
|
|
253
|
-
|
|
254
|
-
/* test if lines intersect, if not, return false */
|
|
255
|
-
if (maxA < minB || minA > maxB) {
|
|
256
|
-
return false;
|
|
257
|
-
} else {
|
|
258
|
-
var o = (maxA > minB ? maxA - minB : maxB - minA);
|
|
259
|
-
if (o < overlap) {
|
|
260
|
-
overlap = o;
|
|
261
|
-
|
|
262
|
-
smallest = {x: axis.x, y: axis.y};
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function offset(poly, pt) {
|
|
268
|
-
return {
|
|
269
|
-
x: pt.x + poly.center.x,
|
|
270
|
-
y: pt.y + poly.center.y
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
var a1, a2, coincident_result = [], lines = [], k=0;
|
|
276
|
-
|
|
277
|
-
function testInteraction(type, a1, a2) {
|
|
278
|
-
var j, b1, b2, result, coincident = [];
|
|
279
|
-
|
|
280
|
-
for (j = 0; j < other.getNumberOfSides(); j++) {
|
|
281
|
-
b1 = offset(other, other.points[j]);
|
|
282
|
-
b2 = offset(other, other.points[j+1] ? other.points[j+1] : other.points[0]);
|
|
283
|
-
result = Polygon.intersectLineLine(a1, a2, b1, b2);
|
|
284
|
-
if (result == "Coincident") {
|
|
285
|
-
coincident.push({
|
|
286
|
-
sides: j
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
lines[k].push(result);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
k++;
|
|
294
|
-
return coincident;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
for (i = 0; i < this.getNumberOfSides(); i++) {
|
|
299
|
-
lines[k] = [];
|
|
300
|
-
a1 = offset(this, this.points[i]);
|
|
301
|
-
a2 = offset(this, this.points[i+1] ? this.points[i+1] : this.points[0]);
|
|
302
|
-
coincident_result.push(testInteraction(null, a1, a2));
|
|
303
|
-
}
|
|
304
|
-
return {
|
|
305
|
-
overlap: overlap + 0.001,
|
|
306
|
-
axis: smallest,
|
|
307
|
-
lines: lines,
|
|
308
|
-
coincident: coincident_result
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
var Polygon = {};
|
|
316
|
-
|
|
317
|
-
Polygon.intersectLineLine = function(a1, a2, b1, b2) {
|
|
318
|
-
|
|
319
|
-
var ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x);
|
|
320
|
-
var ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x);
|
|
321
|
-
var u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
|
|
322
|
-
|
|
323
|
-
if ( u_b != 0 ) {
|
|
324
|
-
var ua = ua_t / u_b;
|
|
325
|
-
var ub = ub_t / u_b;
|
|
326
|
-
|
|
327
|
-
if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
|
|
328
|
-
return {
|
|
329
|
-
x: a1.x + ua * (a2.x - a1.x),
|
|
330
|
-
y: a1.y + ua * (a2.y - a1.y)
|
|
331
|
-
};
|
|
332
|
-
} else {
|
|
333
|
-
return false;
|
|
334
|
-
}
|
|
335
|
-
} else {
|
|
336
|
-
if ( ua_t == 0 || ub_t == 0 ) {
|
|
337
|
-
return "Coincident";
|
|
338
|
-
} else {
|
|
339
|
-
return "Parallel";
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
@doc entity
|
|
349
|
-
@class EntityModel Model entities. Inherit a class to assign a polygon and positions your class
|
|
350
|
-
@example
|
|
351
|
-
|
|
352
|
-
Class.create("Character", {
|
|
353
|
-
|
|
354
|
-
initialize: function() {
|
|
355
|
-
|
|
356
|
-
},
|
|
357
|
-
|
|
358
|
-
passable: function(x, y, other_entity) {
|
|
359
|
-
var state;
|
|
360
|
-
|
|
361
|
-
this.savePosition(); // save positions
|
|
362
|
-
this.position(x, y); // test new positions
|
|
363
|
-
state = this.hit(other_entity); // test collision for new positions
|
|
364
|
-
|
|
365
|
-
if (state.over >= 1) {
|
|
366
|
-
this.restorePosition(); // If collision, restores the initial positions
|
|
367
|
-
return false;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
return true;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
}).extend("EntityModel");
|
|
374
|
-
|
|
375
|
-
*/
|
|
376
|
-
Class.create("EntityModel", {
|
|
377
|
-
/**
|
|
378
|
-
@doc entity/
|
|
379
|
-
@property x Position X
|
|
380
|
-
@type Integer
|
|
381
|
-
@default 0
|
|
382
|
-
*/
|
|
383
|
-
x: 0,
|
|
384
|
-
/**
|
|
385
|
-
@doc entity/
|
|
386
|
-
@property y Position Y
|
|
387
|
-
@type Integer
|
|
388
|
-
@default 0
|
|
389
|
-
*/
|
|
390
|
-
y: 0,
|
|
391
|
-
_memorize: {
|
|
392
|
-
x: 0,
|
|
393
|
-
y:0
|
|
394
|
-
},
|
|
395
|
-
hitState: {
|
|
396
|
-
over: 0,
|
|
397
|
-
out: 0
|
|
398
|
-
},
|
|
399
|
-
_polygon: {},
|
|
400
|
-
_frame: "0",
|
|
401
|
-
/**
|
|
402
|
-
@doc entity/
|
|
403
|
-
@method position Change the position of the entity and the element. Returns object {x: , y: }
|
|
404
|
-
@param {Integer} x Position X
|
|
405
|
-
@param {Integer} y Position Y
|
|
406
|
-
@return {Object}
|
|
407
|
-
*/
|
|
408
|
-
position: function(x, y) {
|
|
409
|
-
if (x !== undefined && y !== undefined) {
|
|
410
|
-
this.x = x;
|
|
411
|
-
this.y = y;
|
|
412
|
-
var poly = this._polygon[this._frame];
|
|
413
|
-
if (poly) {
|
|
414
|
-
poly.center.x = this.x;
|
|
415
|
-
poly.center.y = this.y;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
return {
|
|
419
|
-
x: this.x,
|
|
420
|
-
y: this.y
|
|
421
|
-
};
|
|
422
|
-
},
|
|
423
|
-
/**
|
|
424
|
-
@doc entity/
|
|
425
|
-
@method savePosition Saves the current positions
|
|
426
|
-
*/
|
|
427
|
-
savePosition: function() {
|
|
428
|
-
this._memorize.x = this.x;
|
|
429
|
-
this._memorize.y = this.y;
|
|
430
|
-
},
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
@doc entity/
|
|
434
|
-
@method restorePosition Restores the current positions
|
|
435
|
-
*/
|
|
436
|
-
restorePosition: function() {
|
|
437
|
-
this.x = this._memorize.x;
|
|
438
|
-
this.y = this._memorize.y;
|
|
439
|
-
},
|
|
440
|
-
/**
|
|
441
|
-
@doc entity/
|
|
442
|
-
@method polygon Polygons define the entity
|
|
443
|
-
@param {Array} array The array contains two tables including the positions of point polygons. The order of elements define the shape of polygons.
|
|
444
|
-
@example
|
|
445
|
-
|
|
446
|
-
View Hit class
|
|
447
|
-
*/
|
|
448
|
-
polygon: function(array) {
|
|
449
|
-
if (array instanceof Array) {
|
|
450
|
-
array = {"0": array};
|
|
451
|
-
}
|
|
452
|
-
for (var key in array) {
|
|
453
|
-
this._polygon[key] = Class.New("Polygon", [{x: array[key][0][0], y: array[key][0][1] }]);
|
|
454
|
-
for (var i=0 ; i < array[key].length ; i++) {
|
|
455
|
-
this._polygon[key].addPoint({x: array[key][i][0], y: array[key][i][1]});
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
}
|
|
459
|
-
},
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
@doc entity/
|
|
463
|
-
@method rect Define the polygon as a rectangle. The element will take up his new dimensions (`width` and `height` properties). If `x` and `y` are undefined, they will default 0. If only one parameter is defined, the shape is a square with the given width
|
|
464
|
-
@param {Integer} x Position X
|
|
465
|
-
@param {Integer} y (optional) Position Y
|
|
466
|
-
@param {Integer} w (optional) Width
|
|
467
|
-
@param {Integer} h (optional) Height
|
|
468
|
-
@example
|
|
469
|
-
|
|
470
|
-
View Hit class
|
|
471
|
-
*/
|
|
472
|
-
rect: function(x, y, w, h) {
|
|
473
|
-
if (!w && !h) {
|
|
474
|
-
w = x;
|
|
475
|
-
h = y;
|
|
476
|
-
x = 0;
|
|
477
|
-
y = 0;
|
|
478
|
-
}
|
|
479
|
-
if (!h) {
|
|
480
|
-
h = w;
|
|
481
|
-
}
|
|
482
|
-
this.polygon([
|
|
483
|
-
[x, y],
|
|
484
|
-
[x+w, y],
|
|
485
|
-
[x+w, y+h],
|
|
486
|
-
[x, y+h]
|
|
487
|
-
]);
|
|
488
|
-
|
|
489
|
-
},
|
|
490
|
-
|
|
491
|
-
/**
|
|
492
|
-
@doc entity/
|
|
493
|
-
@method hit Calls the function when two or more entities come into colision. Return this object :
|
|
494
|
-
|
|
495
|
-
{
|
|
496
|
-
over:
|
|
497
|
-
out:
|
|
498
|
-
result: {
|
|
499
|
-
coincident: []
|
|
500
|
-
lines: []
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
* over : The number of times that the entity hit the other entity
|
|
505
|
-
* out : it is 1 if the entity does not affect other entity
|
|
506
|
-
* result
|
|
507
|
-
* coincident : Array containing the lines coinciding with the lines of the other entity. 0 corresponds to the first line from the first point of the polygon.
|
|
508
|
-
* lines : Values in each line with other lines. Example
|
|
509
|
-
|
|
510
|
-
[
|
|
511
|
-
false,
|
|
512
|
-
false,
|
|
513
|
-
{x: 32, y: 32},
|
|
514
|
-
"Coincident"
|
|
515
|
-
]
|
|
516
|
-
|
|
517
|
-
Here, it is testing the first line of the polygon entity A. We test the line with 4 sides polygon entity B :
|
|
518
|
-
|
|
519
|
-
1. The first line of entity A does not intersect with the first line of entity B
|
|
520
|
-
2. The first line of entity A does not intersect with the second line of entity B
|
|
521
|
-
3. The first line of Entity A has an intersection at positions (32,32) with the third line entity B
|
|
522
|
-
4. The first line of entity A coincides with the fourth line entity B
|
|
523
|
-
|
|
524
|
-
> In this example, `coincident` object is :
|
|
525
|
-
|
|
526
|
-
coincident: [3]
|
|
527
|
-
|
|
528
|
-
The number of lines is equal to the order (n) of the polygon - 1.
|
|
529
|
-
|
|
530
|
-
@param {EntityModel} entity_model Other entity
|
|
531
|
-
@return {Object}
|
|
532
|
-
*/
|
|
533
|
-
hit: function(entity_model) {
|
|
534
|
-
var result = this._polygon[this._frame].intersectsWith(entity_model._polygon[entity_model._frame]);
|
|
535
|
-
this.hitState.result = result;
|
|
536
|
-
if (result) {
|
|
537
|
-
this.hitState.out = 0;
|
|
538
|
-
this.hitState.over++;
|
|
539
|
-
}
|
|
540
|
-
else if (this.hitState.over > 0) {
|
|
541
|
-
this.hitState.out = 1;
|
|
542
|
-
this.hitState.over = 0;
|
|
543
|
-
}
|
|
544
|
-
else {
|
|
545
|
-
this.hitState.out = 0;
|
|
546
|
-
this.hitState.over = 0;
|
|
547
|
-
}
|
|
548
|
-
return this.hitState;
|
|
549
|
-
},
|
|
550
|
-
/**
|
|
551
|
-
@doc entity/
|
|
552
|
-
@method getPoints Retrieves points of the polygon. Returns an array of objects :
|
|
553
|
-
|
|
554
|
-
[
|
|
555
|
-
{x: , y: },
|
|
556
|
-
{x: , y: },
|
|
557
|
-
{x: , y: }
|
|
558
|
-
...
|
|
559
|
-
]
|
|
560
|
-
|
|
561
|
-
@params {Integer} frame (optional) Get a polygon frame precise (for animations). If nothing is specified, the current frame is the entity
|
|
562
|
-
@return {Array}
|
|
563
|
-
*/
|
|
564
|
-
getPoints: function(frame) {
|
|
565
|
-
frame = frame || this._frame;
|
|
566
|
-
return this._polygon[frame].points;
|
|
567
|
-
},
|
|
568
|
-
|
|
569
|
-
/**
|
|
570
|
-
@doc entity/
|
|
571
|
-
@method getPolygonReg Find the origin point of the polygon. Returns an object :
|
|
572
|
-
|
|
573
|
-
{x: , y: }
|
|
574
|
-
|
|
575
|
-
@params {Integer} frame (optional) Get a polygon frame precise (for animations). If nothing is specified, the current frame is the entity
|
|
576
|
-
@return {Object}
|
|
577
|
-
*/
|
|
578
|
-
getPolygonReg: function(frame) {
|
|
579
|
-
frame = frame || this._frame;
|
|
580
|
-
return this._polygon[frame].center;
|
|
581
|
-
},
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
@doc entity/
|
|
585
|
-
@method getPolygon Retrieves the polygon
|
|
586
|
-
@params {Integer} frame (optional) Get a polygon frame precise (for animations). If nothing is specified, the current frame is the entity
|
|
587
|
-
@return {Polygon}
|
|
588
|
-
*/
|
|
589
|
-
getPolygon: function(frame) {
|
|
590
|
-
frame = frame || this._frame;
|
|
591
|
-
return this._polygon[frame];
|
|
592
|
-
},
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
@doc entity/
|
|
596
|
-
@method frame Sets or retrieves a frame
|
|
597
|
-
@params {Integer} frame (optional) An entity may be composed of several different polygons (eg for an animation). Each frame corresponds to a polygon. Change frame by assigning an integer. Notice that the first frame is 0
|
|
598
|
-
@return {Integer}
|
|
599
|
-
*/
|
|
600
|
-
frame: function(frame) {
|
|
601
|
-
if (frame) {
|
|
602
|
-
this._frame = frame;
|
|
603
|
-
}
|
|
604
|
-
return this._frame;
|
|
605
|
-
},
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
/**
|
|
609
|
-
@doc hit
|
|
610
|
-
@class Entity Create an entity. An entity is an abstract element. It allows to define its position, its hitbox without display on the screen. This class is linked to a model for the calculation of positions and an element (`el` property) to display
|
|
611
|
-
@param {CanvasEngine.Element} stage Stage element
|
|
612
|
-
@example
|
|
613
|
-
|
|
614
|
-
In `ready` method
|
|
615
|
-
|
|
616
|
-
this.entity = Class.New("Entity", [stage]);
|
|
617
|
-
this.entity.rect(100);
|
|
618
|
-
this.entity.position(10, 10);
|
|
619
|
-
this.entity.el.fillStyle = "red";
|
|
620
|
-
this.entity.el.fillRect(0, 0, 100, 100);
|
|
621
|
-
stage.append(this.entity.el);
|
|
622
|
-
|
|
623
|
-
In `render` method
|
|
624
|
-
|
|
625
|
-
this.entity.move(5, null);
|
|
626
|
-
this.entity.hit("over", [this.other_entity], function(el) { // other_entity is another instance of the Entity class
|
|
627
|
-
el.opacity = .5;
|
|
628
|
-
});
|
|
629
|
-
*/
|
|
630
|
-
Class.create("Entity", {
|
|
631
|
-
stage: null,
|
|
632
|
-
params: {},
|
|
633
|
-
el: null,
|
|
634
|
-
mode: null,
|
|
635
|
-
hit_entities: [],
|
|
636
|
-
initialize: function(stage, params, model) {
|
|
637
|
-
|
|
638
|
-
if (model === undefined) model = true;
|
|
639
|
-
|
|
640
|
-
this.stage = stage;
|
|
641
|
-
this.params = params;
|
|
642
|
-
this.el = this.stage.getScene().createElement();
|
|
643
|
-
if (model){
|
|
644
|
-
this.setModel(Class.New("EntityModel"));
|
|
645
|
-
}
|
|
646
|
-
this.testHit();
|
|
647
|
-
},
|
|
648
|
-
|
|
649
|
-
setModel: function(_class) {
|
|
650
|
-
this.model = _class;
|
|
651
|
-
},
|
|
652
|
-
/**
|
|
653
|
-
@doc hit/
|
|
654
|
-
@method position Change the position of the entity and the element. Returns object {x: , y: }
|
|
655
|
-
@param {Integer} x Position X
|
|
656
|
-
@param {Integer} y Position Y
|
|
657
|
-
@return {Object}
|
|
658
|
-
@example
|
|
659
|
-
|
|
660
|
-
In `ready` method
|
|
661
|
-
|
|
662
|
-
var entity = Class.New("Entity", [stage]);
|
|
663
|
-
entity.position(10, 50);
|
|
664
|
-
|
|
665
|
-
*/
|
|
666
|
-
position: function(x, y, move) {
|
|
667
|
-
var pos = this.model.position(x, y);
|
|
668
|
-
if (x !== undefined) {
|
|
669
|
-
this.el.x = pos.x;
|
|
670
|
-
this.el.y = pos.y;
|
|
671
|
-
|
|
672
|
-
}
|
|
673
|
-
return {x: pos.y, y: pos.y};
|
|
674
|
-
},
|
|
675
|
-
|
|
676
|
-
/**
|
|
677
|
-
@doc hit/
|
|
678
|
-
@method move Move the position of the entity and the element. Returns object {x: , y: }
|
|
679
|
-
@param {Integer} x Add position X
|
|
680
|
-
@param {Integer} y (optional) add position Y
|
|
681
|
-
@return {Object}
|
|
682
|
-
@example
|
|
683
|
-
|
|
684
|
-
In `ready` method
|
|
685
|
-
|
|
686
|
-
var entity = Class.New("Entity", [stage]);
|
|
687
|
-
entity.move(10, null); // Current position X + 10
|
|
688
|
-
entity.move(null, 10); // Current position Y + 10
|
|
689
|
-
|
|
690
|
-
*/
|
|
691
|
-
move: function(x, y) {
|
|
692
|
-
var pos = this.model.position();
|
|
693
|
-
if (!x) x = 0;
|
|
694
|
-
if (!y) y = 0;
|
|
695
|
-
return this.position(x + pos.x, y + pos.y, true);
|
|
696
|
-
},
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
/**
|
|
701
|
-
@doc hit/
|
|
702
|
-
@method polygon Polygons define the entity
|
|
703
|
-
@param {Array} array The array contains two tables including the positions of point polygons. The order of elements define the shape of polygons.
|
|
704
|
-
@example
|
|
705
|
-
|
|
706
|
-
In `ready` method
|
|
707
|
-
|
|
708
|
-
var entity = Class.New("Entity", [stage]);
|
|
709
|
-
entity.polygon([
|
|
710
|
-
[0, 0],
|
|
711
|
-
[50, 40],
|
|
712
|
-
[0, 40]
|
|
713
|
-
]);
|
|
714
|
-
|
|
715
|
-
*/
|
|
716
|
-
polygon: function(array) {
|
|
717
|
-
this.model.polygon(array);
|
|
718
|
-
},
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
@doc hit/
|
|
722
|
-
@method rect Define the polygon as a rectangle. The element will take up his new dimensions (`width` and `height` properties). If `x` and `y` are undefined, they will default 0. If only one parameter is defined, the shape is a square with the given width
|
|
723
|
-
@param {Integer} x Position X
|
|
724
|
-
@param {Integer} y (optional) Position Y
|
|
725
|
-
@param {Integer} w (optional) Width
|
|
726
|
-
@param {Integer} h (optional) Height
|
|
727
|
-
@example
|
|
728
|
-
|
|
729
|
-
In `ready` method
|
|
730
|
-
|
|
731
|
-
var entity = Class.New("Entity", [stage]);
|
|
732
|
-
entity.rect(0, 0, 100, 100);
|
|
733
|
-
// entity.rect(100, 100); => equivalent
|
|
734
|
-
// entity.rect(100); => equivalent
|
|
735
|
-
|
|
736
|
-
Other example
|
|
737
|
-
|
|
738
|
-
var entity = Class.New("Entity", [stage]);
|
|
739
|
-
entity.rect(100, 300);
|
|
740
|
-
// entity.rect(0, 0, 100, 300); => equivalent
|
|
741
|
-
*/
|
|
742
|
-
rect: function(x, y, w, h) {
|
|
743
|
-
this.model.rect(x, y, w, h);
|
|
744
|
-
this.el.width = w;
|
|
745
|
-
this.el.height = h;
|
|
746
|
-
},
|
|
747
|
-
onHit: function(event_name, entities, callback) {
|
|
748
|
-
this.hit_entities = this.hit_entities.concat(entities);
|
|
749
|
-
this.el.on("entity:hit:" + event_name, callback);
|
|
750
|
-
/*for (var i=0 ; i < this.hit_entities.length ; i++) {
|
|
751
|
-
this.hit_entities[i].hit(event_name, this, callback);
|
|
752
|
-
}
|
|
753
|
-
*/
|
|
754
|
-
},
|
|
755
|
-
testHit: function() {
|
|
756
|
-
var self = this;
|
|
757
|
-
this.el.attr("entity:testHit", function() {
|
|
758
|
-
self.hit(self.hit_entities);
|
|
759
|
-
});
|
|
760
|
-
},
|
|
761
|
-
testAnimHit: function() {
|
|
762
|
-
var self = this;
|
|
763
|
-
this.el.on("animation:draw", function(frame) {
|
|
764
|
-
|
|
765
|
-
});
|
|
766
|
-
},
|
|
767
|
-
/**
|
|
768
|
-
@doc hit/
|
|
769
|
-
@method hit Calls the function when two or more entities come into colision
|
|
770
|
-
@param {Array} entities Array containing elements of type `Entity`
|
|
771
|
-
@param {Function} callback (optional) Callback function when the collision occurs
|
|
772
|
-
@example
|
|
773
|
-
|
|
774
|
-
In `ready` method
|
|
775
|
-
|
|
776
|
-
this.entity1 = Class.New("Entity", [stage]);
|
|
777
|
-
this.entity2 = Class.New("Entity", [stage]);
|
|
778
|
-
this.entity1.rect(100);
|
|
779
|
-
this.entity2.rect(100);
|
|
780
|
-
|
|
781
|
-
In `render` method
|
|
782
|
-
|
|
783
|
-
this.entity1.hit([this.entity2], function(el) {
|
|
784
|
-
el.opacity = 0.5;
|
|
785
|
-
});
|
|
786
|
-
*/
|
|
787
|
-
hit: function(entities, callback) {
|
|
788
|
-
var state, self = this;
|
|
789
|
-
|
|
790
|
-
function _call(e) {
|
|
791
|
-
if (callback) callback.call(self, e, self.el);
|
|
792
|
-
self.el.trigger("entity:hit:" + e, [self.el]);
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
for (var i=0 ; i < entities.length ; i++) {
|
|
796
|
-
state = this.model.hit(entities[i].model);
|
|
797
|
-
if (state.over == 1) {
|
|
798
|
-
_call("over");
|
|
799
|
-
}
|
|
800
|
-
else if (state.out == 1) {
|
|
801
|
-
_call("out");
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
});
|
|
806
|
-
|
|
807
|
-
var Matrix = {
|
|
808
|
-
|
|
809
|
-
/*isometric: function() {
|
|
810
|
-
[a, b, c, d, e, f]
|
|
811
|
-
[scaleX, skewX, skewY, scaleY, translateX, translateY]
|
|
812
|
-
},
|
|
813
|
-
*/
|
|
814
|
-
};
|
|
815
|
-
|
|
816
|
-
/**
|
|
817
|
-
@doc grid
|
|
818
|
-
@class Grid reate a virtual grid and the location of an entity in the grid and properties of a cell
|
|
819
|
-
@param {Integer} rows Number of rows
|
|
820
|
-
@param {Integer} cols Number of columns
|
|
821
|
-
@example
|
|
822
|
-
|
|
823
|
-
In `ready` method
|
|
824
|
-
|
|
825
|
-
var entity = Class.New("Entity", [stage]),
|
|
826
|
-
pos = entity.position(),
|
|
827
|
-
grid = Class.New("Grid", [10, 5]);
|
|
828
|
-
|
|
829
|
-
grid.setCellSize(32, 32);
|
|
830
|
-
|
|
831
|
-
console.log(grid.getCellByPos(pos.x, pos.y)); // {col: 0, row: 0}
|
|
832
|
-
*/
|
|
833
|
-
Class.create("Grid", {
|
|
834
|
-
_rows: 0,
|
|
835
|
-
_cols: 0,
|
|
836
|
-
cell: {
|
|
837
|
-
width: 0,
|
|
838
|
-
height: 0,
|
|
839
|
-
prop: [],
|
|
840
|
-
},
|
|
841
|
-
_matrix: null,
|
|
842
|
-
_transform: null,
|
|
843
|
-
_func: null,
|
|
844
|
-
initialize: function(rows, cols) {
|
|
845
|
-
if (rows instanceof Array) {
|
|
846
|
-
this._matrix = rows;
|
|
847
|
-
this.cell.prop = rows;
|
|
848
|
-
cols = rows[0].length;
|
|
849
|
-
rows = rows.length;
|
|
850
|
-
}
|
|
851
|
-
this._rows = rows;
|
|
852
|
-
this._cols = cols;
|
|
853
|
-
},
|
|
854
|
-
|
|
855
|
-
// TODO
|
|
856
|
-
transform: function(func) {
|
|
857
|
-
this._func = func;
|
|
858
|
-
},
|
|
859
|
-
|
|
860
|
-
// TODO
|
|
861
|
-
convert: function(x, y) {
|
|
862
|
-
if (!this._func) {
|
|
863
|
-
return {x: x, y: y};
|
|
864
|
-
}
|
|
865
|
-
return this._func.call(this, x, y);
|
|
866
|
-
},
|
|
867
|
-
|
|
868
|
-
/**
|
|
869
|
-
@doc grid/
|
|
870
|
-
@method setPropertyCell Assign properties to the cells (two-dimensional arrays)
|
|
871
|
-
1. Abscissa
|
|
872
|
-
2. 0rdered
|
|
873
|
-
@param {Array} prop Two-dimensional arrays. The second array contains a value (integer, object, etc.)
|
|
874
|
-
@example
|
|
875
|
-
|
|
876
|
-
In `ready` method
|
|
877
|
-
|
|
878
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
879
|
-
grid.setPropertyCell(
|
|
880
|
-
[
|
|
881
|
-
[0, 0],
|
|
882
|
-
[1, 0]
|
|
883
|
-
]
|
|
884
|
-
);
|
|
885
|
-
*/
|
|
886
|
-
setPropertyCell: function(prop) {
|
|
887
|
-
if (typeof(PF) != "undefined") {
|
|
888
|
-
this._pf_grid = false;
|
|
889
|
-
this._pf_prop = (CanvasEngine || CE.Core).rotateMatrix(prop);
|
|
890
|
-
}
|
|
891
|
-
this.cell.prop = prop;
|
|
892
|
-
},
|
|
893
|
-
|
|
894
|
-
/**
|
|
895
|
-
@doc grid/
|
|
896
|
-
@method getPropertyByCell Gets the properties of a cell
|
|
897
|
-
@param {Integer} col Column
|
|
898
|
-
@param {Integer} row Row
|
|
899
|
-
@return {Object}
|
|
900
|
-
@example
|
|
901
|
-
|
|
902
|
-
In `ready` method
|
|
903
|
-
|
|
904
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
905
|
-
grid.setPropertyCell(
|
|
906
|
-
[
|
|
907
|
-
[0, 0],
|
|
908
|
-
[1, 0]
|
|
909
|
-
]
|
|
910
|
-
);
|
|
911
|
-
grid.getPropertyByCell(1, 0); // return 1
|
|
912
|
-
|
|
913
|
-
Returns `undefined` if column or row doesn't exist
|
|
914
|
-
*/
|
|
915
|
-
getPropertyByCell: function(col, row) {
|
|
916
|
-
if (!this.cell.prop[col]) {
|
|
917
|
-
return undefined;
|
|
918
|
-
}
|
|
919
|
-
return this.cell.prop[col][row];
|
|
920
|
-
},
|
|
921
|
-
|
|
922
|
-
/**
|
|
923
|
-
@doc grid/
|
|
924
|
-
@method setPropertyByCell `(>= 1.3.0)` Change a value in a cell
|
|
925
|
-
@param {Integer} col Column
|
|
926
|
-
@param {Integer} row Row
|
|
927
|
-
@param {Object} prop Value
|
|
928
|
-
@return {Class.Hit}
|
|
929
|
-
@example
|
|
930
|
-
|
|
931
|
-
In `ready` method
|
|
932
|
-
|
|
933
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
934
|
-
grid.setPropertyCell(
|
|
935
|
-
[
|
|
936
|
-
[0, 0],
|
|
937
|
-
[1, 0]
|
|
938
|
-
]
|
|
939
|
-
);
|
|
940
|
-
grid.setPropertyByCell(1, 0, 0); // grid is [[0, 0], [0, 0]] now
|
|
941
|
-
|
|
942
|
-
Returns `undefined` if column or row doesn't exist
|
|
943
|
-
*/
|
|
944
|
-
setPropertyByCell: function(col, row, prop) {
|
|
945
|
-
if (!this.cell.prop[col]) {
|
|
946
|
-
return undefined;
|
|
947
|
-
}
|
|
948
|
-
this.cell.prop[col][row] = prop;
|
|
949
|
-
if (typeof(PF) != "undefined") {
|
|
950
|
-
this._pf_grid = false;
|
|
951
|
-
this._pf_prop = (CanvasEngine || CE.Core).rotateMatrix(this.cell.prop);
|
|
952
|
-
}
|
|
953
|
-
return this;
|
|
954
|
-
},
|
|
955
|
-
|
|
956
|
-
/**
|
|
957
|
-
@doc grid/
|
|
958
|
-
@method getPropertyByPos Obtain the properties of a cell according to positions
|
|
959
|
-
@param {Integer} x Position X
|
|
960
|
-
@param {Integer} y Position Y
|
|
961
|
-
@return {Object}
|
|
962
|
-
@example
|
|
963
|
-
|
|
964
|
-
In `ready` method
|
|
965
|
-
|
|
966
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
967
|
-
grid.setPropertyCell(
|
|
968
|
-
[
|
|
969
|
-
[0, 0],
|
|
970
|
-
[1, 0]
|
|
971
|
-
]
|
|
972
|
-
);
|
|
973
|
-
grid.setCellSize(32, 32);
|
|
974
|
-
grid.getPropertyByPos(53, 24); // return 1
|
|
975
|
-
*/
|
|
976
|
-
getPropertyByPos: function(x, y) {
|
|
977
|
-
var cell = this.getCellByPos(x, y);
|
|
978
|
-
return this.getPropertyByCell(cell.col, cell.row);
|
|
979
|
-
},
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
/**
|
|
983
|
-
@doc grid/
|
|
984
|
-
@method testCell Test cell lines which coincide with the lines of the polygons of the entity
|
|
985
|
-
|
|
986
|
-
[
|
|
987
|
-
[
|
|
988
|
-
{
|
|
989
|
-
points: {
|
|
990
|
-
x: 5, y: 5
|
|
991
|
-
},
|
|
992
|
-
sides: 2
|
|
993
|
-
}
|
|
994
|
-
],
|
|
995
|
-
|
|
996
|
-
]
|
|
997
|
-
|
|
998
|
-
`sides` sends lines of polygons of the entity which coincide with the grid lines. 0 corresponds to the first line from the first point of the polygon. The number of lines is equal to the order (n) of the polygon - 1.
|
|
999
|
-
|
|
1000
|
-
@param {Object} cell The cell defined by the object {col: Integer, row: Integer}
|
|
1001
|
-
@param {Entity|EntityModel} entity
|
|
1002
|
-
@return {Array}
|
|
1003
|
-
@example
|
|
1004
|
-
|
|
1005
|
-
In `ready` method
|
|
1006
|
-
|
|
1007
|
-
var entity = Class.New("Entity", [stage]), pos;
|
|
1008
|
-
entity.rect(10);
|
|
1009
|
-
pos = entity.position();
|
|
1010
|
-
|
|
1011
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
1012
|
-
|
|
1013
|
-
grid.setCellSize(32, 32);
|
|
1014
|
-
|
|
1015
|
-
grid.testCell({
|
|
1016
|
-
col: 0,
|
|
1017
|
-
row: 0
|
|
1018
|
-
}, entity)
|
|
1019
|
-
|
|
1020
|
-
// => returns
|
|
1021
|
-
/*
|
|
1022
|
-
[
|
|
1023
|
-
[
|
|
1024
|
-
{
|
|
1025
|
-
points: {
|
|
1026
|
-
x: 0, y: 0
|
|
1027
|
-
},
|
|
1028
|
-
sides: 0
|
|
1029
|
-
}
|
|
1030
|
-
],
|
|
1031
|
-
[],
|
|
1032
|
-
[],
|
|
1033
|
-
[
|
|
1034
|
-
{
|
|
1035
|
-
points: {
|
|
1036
|
-
x: 0, y: 0
|
|
1037
|
-
},
|
|
1038
|
-
sides: 3
|
|
1039
|
-
}
|
|
1040
|
-
]
|
|
1041
|
-
]
|
|
1042
|
-
*/
|
|
1043
|
-
testCell: function(cell, entity, params) {
|
|
1044
|
-
params = params || {};
|
|
1045
|
-
|
|
1046
|
-
if (!entity.getPolygon) {
|
|
1047
|
-
entity = entity.model;
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
var result = [],
|
|
1051
|
-
poly = entity.getPolygon(),
|
|
1052
|
-
self = this;
|
|
1053
|
-
|
|
1054
|
-
function offset(poly, pt) {
|
|
1055
|
-
return {
|
|
1056
|
-
x: pt.x + poly.center.x,
|
|
1057
|
-
y: pt.y + poly.center.y
|
|
1058
|
-
};
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
function testInteraction(type, a1, a2, poly) {
|
|
1062
|
-
var j, b1, b2, lines = [], k=0, result, coincident = [], coincident_points = null, obj = {};
|
|
1063
|
-
|
|
1064
|
-
for (j = 0; j < poly.getNumberOfSides(); j++) {
|
|
1065
|
-
b1 = offset(poly, poly.points[j]);
|
|
1066
|
-
b2 = offset(poly, poly.points[j+1] ? poly.points[j+1] : poly.points[0]);
|
|
1067
|
-
result = Polygon.intersectLineLine(a1, a2, b1, b2);
|
|
1068
|
-
|
|
1069
|
-
if (result == "Coincident") {
|
|
1070
|
-
|
|
1071
|
-
if (a1.x == b1.x && a1.y == b1.y) {
|
|
1072
|
-
coincident_points = {
|
|
1073
|
-
x: a1.x,
|
|
1074
|
-
y: a1.y
|
|
1075
|
-
};
|
|
1076
|
-
}
|
|
1077
|
-
else if (a2.x == b2.x && a2.y == b2.y) {
|
|
1078
|
-
coincident_points = {
|
|
1079
|
-
x: a2.x,
|
|
1080
|
-
y: a2.y
|
|
1081
|
-
};
|
|
1082
|
-
}
|
|
1083
|
-
else if (a1.x == b2.x && a1.y == b2.y) {
|
|
1084
|
-
coincident_points = {
|
|
1085
|
-
x: a1.x,
|
|
1086
|
-
y: a1.y
|
|
1087
|
-
};
|
|
1088
|
-
}
|
|
1089
|
-
else if (a2.x == b1.x && a2.y == b1.y) {
|
|
1090
|
-
coincident_points = {
|
|
1091
|
-
x: a2.x,
|
|
1092
|
-
y: a2.y
|
|
1093
|
-
};
|
|
1094
|
-
}
|
|
1095
|
-
coincident.push({
|
|
1096
|
-
sides: j,
|
|
1097
|
-
points: coincident_points
|
|
1098
|
-
});
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
if (true /*params.ignoreTypeLine */) {
|
|
1102
|
-
return coincident;
|
|
1103
|
-
}
|
|
1104
|
-
else {
|
|
1105
|
-
obj[type] = coincident;
|
|
1106
|
-
return obj;
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
function real(cell) {
|
|
1113
|
-
return {
|
|
1114
|
-
x: cell.x * self.cell.width,
|
|
1115
|
-
y: cell.y * self.cell.height
|
|
1116
|
-
};
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
if (!cell.getPolygon) {
|
|
1120
|
-
entity = entity.model;
|
|
1121
|
-
}
|
|
1122
|
-
|
|
1123
|
-
var points_cell = [
|
|
1124
|
-
{y: cell.row, x: cell.col},
|
|
1125
|
-
{y: cell.row, x: cell.col+1},
|
|
1126
|
-
{y: cell.row+1, x: cell.col+1},
|
|
1127
|
-
{y: cell.row+1, x: cell.col}
|
|
1128
|
-
];
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
var a1, a2;
|
|
1132
|
-
for (j = 0; j < points_cell.length; j++) {
|
|
1133
|
-
a1 = real(points_cell[j]);
|
|
1134
|
-
a2 = real(points_cell[j+1] ? points_cell[j+1] : points_cell[0]);
|
|
1135
|
-
result.push(testInteraction(null, a1, a2, poly));
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
|
-
return result;
|
|
1139
|
-
|
|
1140
|
-
},
|
|
1141
|
-
|
|
1142
|
-
/**
|
|
1143
|
-
@doc grid/
|
|
1144
|
-
@method getEntityCells Obtain the cells where the entity. Return this object (example) :
|
|
1145
|
-
|
|
1146
|
-
{
|
|
1147
|
-
cells: [{
|
|
1148
|
-
col: 1,
|
|
1149
|
-
row: 1
|
|
1150
|
-
}]
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
|
-
@param {Entity|EntityModel} entity
|
|
1154
|
-
@return {Object}
|
|
1155
|
-
@example
|
|
1156
|
-
|
|
1157
|
-
In `ready` method
|
|
1158
|
-
|
|
1159
|
-
var entity = Class.New("Entity", [stage]),
|
|
1160
|
-
pos = entity.position();
|
|
1161
|
-
|
|
1162
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
1163
|
-
|
|
1164
|
-
grid.setCellSize(32, 32);
|
|
1165
|
-
|
|
1166
|
-
grid.getEntityCells(entity);
|
|
1167
|
-
|
|
1168
|
-
// => returns
|
|
1169
|
-
/*
|
|
1170
|
-
{
|
|
1171
|
-
cells: [{
|
|
1172
|
-
col: 0,
|
|
1173
|
-
row: 0
|
|
1174
|
-
}]
|
|
1175
|
-
}
|
|
1176
|
-
*/
|
|
1177
|
-
|
|
1178
|
-
getEntityCells: function(entity, params) {
|
|
1179
|
-
var i, j, p, points, reg, poly, px, py,
|
|
1180
|
-
_cells = [],
|
|
1181
|
-
ep = { // extreme points
|
|
1182
|
-
min_x: 99999999,
|
|
1183
|
-
max_x: 0,
|
|
1184
|
-
min_y: 99999999,
|
|
1185
|
-
max_y: 0
|
|
1186
|
-
};
|
|
1187
|
-
|
|
1188
|
-
params = params || {};
|
|
1189
|
-
|
|
1190
|
-
if (entity.model) {
|
|
1191
|
-
points = entity.model.getPoints();
|
|
1192
|
-
reg = entity.model.getPolygonReg();
|
|
1193
|
-
poly = entity.model.getPolygon();
|
|
1194
|
-
}
|
|
1195
|
-
else {
|
|
1196
|
-
points = entity.getPoints();
|
|
1197
|
-
reg = entity.getPolygonReg();
|
|
1198
|
-
poly = entity.getPolygon();
|
|
1199
|
-
}
|
|
1200
|
-
for (i=0 ; i < points.length ; i++) {
|
|
1201
|
-
p = points[i];
|
|
1202
|
-
px = p.x + reg.x;
|
|
1203
|
-
py = p.y + reg.y;
|
|
1204
|
-
if (px < ep.min_x) {
|
|
1205
|
-
ep.min_x = px;
|
|
1206
|
-
}
|
|
1207
|
-
if (px > ep.max_x) {
|
|
1208
|
-
ep.max_x = px;
|
|
1209
|
-
}
|
|
1210
|
-
if (py < ep.min_y) {
|
|
1211
|
-
ep.min_y = py;
|
|
1212
|
-
}
|
|
1213
|
-
if (py > ep.max_y) {
|
|
1214
|
-
ep.max_y = py;
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
var cell = [
|
|
1219
|
-
this.getCellByPos(ep.min_x, ep.min_y),
|
|
1220
|
-
this.getCellByPos(ep.max_x, ep.min_y),
|
|
1221
|
-
this.getCellByPos(ep.max_x, ep.max_y),
|
|
1222
|
-
this.getCellByPos(ep.min_x, ep.max_y)
|
|
1223
|
-
];
|
|
1224
|
-
|
|
1225
|
-
var nbrows = cell[2].row - cell[0].row,
|
|
1226
|
-
nbcols = cell[1].col - cell[0].col;
|
|
1227
|
-
for (i=0 ; i < nbcols-1 ; i++) {
|
|
1228
|
-
for (j=0 ; j < nbrows-1 ; j++) {
|
|
1229
|
-
cell.push({row: cell[0].row + j, col: cell[0].col + i});
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
return {
|
|
1234
|
-
cells: cell
|
|
1235
|
-
};
|
|
1236
|
-
},
|
|
1237
|
-
|
|
1238
|
-
/**
|
|
1239
|
-
@doc grid/
|
|
1240
|
-
@method getCellByPos Retrieve the column and row in the grid according to the positions. Returns this object : {col: , row: }
|
|
1241
|
-
@param {Integer} x Position X
|
|
1242
|
-
@param {Integer} y Position Y
|
|
1243
|
-
@return {Object}
|
|
1244
|
-
@example
|
|
1245
|
-
|
|
1246
|
-
In `ready` method
|
|
1247
|
-
|
|
1248
|
-
var grid = Class.New("Grid", [2, 2]);
|
|
1249
|
-
grid.setCellSize(32, 32);
|
|
1250
|
-
grid.getCellByPos(53, 35); // return {col: 1, row: 1}
|
|
1251
|
-
*/
|
|
1252
|
-
getCellByPos: function(x, y) {
|
|
1253
|
-
if (this.cell.width == 0 || this.cell.height == 0) {
|
|
1254
|
-
throw "Set the size of the cell prior with setCellSize method";
|
|
1255
|
-
}
|
|
1256
|
-
var col = Math.floor(this.convert(x, y).x / this.cell.width),
|
|
1257
|
-
row = Math.floor(this.convert(x, y).y / this.cell.height);
|
|
1258
|
-
return {col: col, row: row};
|
|
1259
|
-
},
|
|
1260
|
-
|
|
1261
|
-
/**
|
|
1262
|
-
@doc grid/
|
|
1263
|
-
@method setCellSize Set the size of a cell
|
|
1264
|
-
@param {Integer} w Width (pixels)
|
|
1265
|
-
@param {Integer} h Height (pixels)
|
|
1266
|
-
*/
|
|
1267
|
-
setCellSize: function(w, h) {
|
|
1268
|
-
this.cell.width = w;
|
|
1269
|
-
this.cell.height = h;
|
|
1270
|
-
},
|
|
1271
|
-
|
|
1272
|
-
/**
|
|
1273
|
-
@doc grid/
|
|
1274
|
-
@method getRows Get the number of rows
|
|
1275
|
-
@return {Integer}
|
|
1276
|
-
*/
|
|
1277
|
-
getRows: function() {
|
|
1278
|
-
return this._rows;
|
|
1279
|
-
},
|
|
1280
|
-
|
|
1281
|
-
/**
|
|
1282
|
-
@doc grid/
|
|
1283
|
-
@method getCols Get the number of columns
|
|
1284
|
-
@return {Integer}
|
|
1285
|
-
*/
|
|
1286
|
-
getCols: function() {
|
|
1287
|
-
return this._cols;
|
|
1288
|
-
},
|
|
1289
|
-
|
|
1290
|
-
/**
|
|
1291
|
-
@doc grid/
|
|
1292
|
-
@method getNbCell Get the total number of cell (rows * columns)
|
|
1293
|
-
@return {Integer}
|
|
1294
|
-
*/
|
|
1295
|
-
getNbCell: function() {
|
|
1296
|
-
return this.getRows() * this.getCols();
|
|
1297
|
-
},
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
/**
|
|
1301
|
-
@doc grid/
|
|
1302
|
-
@method passableCell `(>= 1.3.0`) Test tiles passable in an area. The array must contain only the value 0 or 1. Useful to know the movement of an entity as a part in a game of chess
|
|
1303
|
-
|
|
1304
|
-
- 0 : passable
|
|
1305
|
-
- 1 : not passable
|
|
1306
|
-
|
|
1307
|
-
Returns the columns and rows in grid passable
|
|
1308
|
-
|
|
1309
|
-
@param {Integer} row Start position (row)
|
|
1310
|
-
@param {Integer} col Start position (col)
|
|
1311
|
-
@param {Integer} distance Maximum search distance (number of tiles)
|
|
1312
|
-
@param {Array} array_exception (optional) Two-dimensional array with the positions not to test : `[[x0, y0], [x1, y1], ...]`
|
|
1313
|
-
@return {Array}
|
|
1314
|
-
@example
|
|
1315
|
-
|
|
1316
|
-
In `ready` method
|
|
1317
|
-
|
|
1318
|
-
var matrix = [
|
|
1319
|
-
[0, 0, 0, 1, 0],
|
|
1320
|
-
[1, 0, 0, 0, 1],
|
|
1321
|
-
[0, 0, 1, 0, 0],
|
|
1322
|
-
];
|
|
1323
|
-
|
|
1324
|
-
var grid = Class.New("Grid", [matrix]);
|
|
1325
|
-
grid.passableCell(0, 0, 2);
|
|
1326
|
-
|
|
1327
|
-
// returns : [[0, 1], [0, 2], [1, 1], [0, 0]]
|
|
1328
|
-
|
|
1329
|
-
*/
|
|
1330
|
-
passableCell: function(x, y, max, array_exception) {
|
|
1331
|
-
|
|
1332
|
-
array_exception = array_exception || [];
|
|
1333
|
-
|
|
1334
|
-
var H_MAP = this._cols;
|
|
1335
|
-
var W_MAP = this._rows;
|
|
1336
|
-
|
|
1337
|
-
var mapData = this.cell.prop;
|
|
1338
|
-
|
|
1339
|
-
var tab_move_passable = [];
|
|
1340
|
-
|
|
1341
|
-
function init_tab_move() {
|
|
1342
|
-
for (var i = 0 ; i < H_MAP * 2 + 1 ; i++) {
|
|
1343
|
-
tab_move_passable[i] = [];
|
|
1344
|
-
for (var j = 0 ; j < H_MAP * 2 + 1 ; j++) {
|
|
1345
|
-
tab_move_passable[i][j] = -1;
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
var middle = Math.floor(tab_move_passable.length / 2);
|
|
1349
|
-
tab_move_passable[middle][middle] = 0;
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
var pos_temporaire = [];
|
|
1353
|
-
var pos_semi_tempor = [[x, y]];
|
|
1354
|
-
var path = 0;
|
|
1355
|
-
var diff_x = pos_semi_tempor[0][0] - H_MAP;
|
|
1356
|
-
var diff_y = pos_semi_tempor[0][1] - H_MAP;
|
|
1357
|
-
var tab_move = [];
|
|
1358
|
-
var id = 0;
|
|
1359
|
-
init_tab_move();
|
|
1360
|
-
|
|
1361
|
-
function inArray(x, y) {
|
|
1362
|
-
|
|
1363
|
-
for (var j = 0 ; j < array_exception.length ; j++) {
|
|
1364
|
-
if (array_exception[j][0] == x && array_exception[j][1] == y) {
|
|
1365
|
-
return true;
|
|
1366
|
-
}
|
|
1367
|
-
}
|
|
1368
|
-
return false;
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
while (!pos_semi_tempor.length == 0 && path < max) {
|
|
1372
|
-
pos_temporaire = [];
|
|
1373
|
-
for (var i = 0 ; i < pos_semi_tempor.length ; i++) {
|
|
1374
|
-
var new_pos_x = pos_semi_tempor[i][0];
|
|
1375
|
-
var new_pos_y = pos_semi_tempor[i][1];
|
|
1376
|
-
var tab_x = new_pos_x;
|
|
1377
|
-
var tab_y = new_pos_y;
|
|
1378
|
-
|
|
1379
|
-
for (var j = 0 ; j < 4 ; j++) {
|
|
1380
|
-
switch (j) {
|
|
1381
|
-
case 0:
|
|
1382
|
-
|
|
1383
|
-
if (mapData[new_pos_x][new_pos_y + 1] != undefined && mapData[new_pos_x][new_pos_y + 1] == id && !inArray(new_pos_x, new_pos_y + 1) && tab_move_passable[tab_x][tab_y + 1] == -1) {
|
|
1384
|
-
pos_temporaire.push([new_pos_x,new_pos_y + 1]);
|
|
1385
|
-
tab_move.push([new_pos_x,new_pos_y + 1]);
|
|
1386
|
-
tab_move_passable[tab_x][tab_y + 1] = 0;
|
|
1387
|
-
}
|
|
1388
|
-
break;
|
|
1389
|
-
case 1:
|
|
1390
|
-
|
|
1391
|
-
if (mapData[new_pos_x + 1] != undefined && mapData[new_pos_x + 1][new_pos_y] != undefined && mapData[new_pos_x + 1][new_pos_y] == id && !inArray(new_pos_x + 1, new_pos_y) && tab_move_passable[tab_x + 1][tab_y] == -1) {
|
|
1392
|
-
pos_temporaire.push([new_pos_x + 1,new_pos_y]);
|
|
1393
|
-
tab_move.push([new_pos_x + 1,new_pos_y]);
|
|
1394
|
-
tab_move_passable[tab_x + 1][tab_y] = 0;
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
break;
|
|
1398
|
-
case 2:
|
|
1399
|
-
if (mapData[new_pos_x][new_pos_y - 1] != undefined && mapData[new_pos_x][new_pos_y - 1] == id && !inArray(new_pos_x, new_pos_y - 1) && tab_move_passable[tab_x][tab_y - 1] == -1) {
|
|
1400
|
-
pos_temporaire.push([new_pos_x,new_pos_y - 1]);
|
|
1401
|
-
tab_move.push([new_pos_x,new_pos_y - 1]);
|
|
1402
|
-
tab_move_passable[tab_x][tab_y - 1] = 0;
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
break;
|
|
1406
|
-
case 3:
|
|
1407
|
-
if (mapData[new_pos_x - 1] != undefined && mapData[new_pos_x - 1][new_pos_y] != undefined && mapData[new_pos_x - 1][new_pos_y] == id && !inArray(new_pos_x - 1, new_pos_y) && tab_move_passable[tab_x - 1][tab_y] == -1) {
|
|
1408
|
-
pos_temporaire.push([new_pos_x - 1,new_pos_y]);
|
|
1409
|
-
tab_move.push([new_pos_x - 1,new_pos_y]);
|
|
1410
|
-
tab_move_passable[tab_x - 1][tab_y] = 0;
|
|
1411
|
-
}
|
|
1412
|
-
break;
|
|
1413
|
-
}
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
}
|
|
1417
|
-
|
|
1418
|
-
pos_semi_tempor = pos_temporaire;
|
|
1419
|
-
path += 1;
|
|
1420
|
-
}
|
|
1421
|
-
return tab_move;
|
|
1422
|
-
},
|
|
1423
|
-
|
|
1424
|
-
/**
|
|
1425
|
-
@doc grid/
|
|
1426
|
-
@method pathfinding `(>= 1.3.0)` Find the shortest path. Using the library [Pathfinding.js](https://github.com/qiao/PathFinding.js). The array must contain only the value 0 or 1. Useful to know the movement of an entity as a part in a game of chess
|
|
1427
|
-
|
|
1428
|
-
- 0 : passable
|
|
1429
|
-
- 1 : not passable
|
|
1430
|
-
|
|
1431
|
-
> Before, Insert the script `pathfinding-browser.js` in header
|
|
1432
|
-
|
|
1433
|
-
> Attention ! The matrix is different that matrix of the library. The columns are the rows and rows are the columns.
|
|
1434
|
-
|
|
1435
|
-
Returns the columns and rows of path. If no arguments, returns the `PF.Grid`. You can use the API library.
|
|
1436
|
-
|
|
1437
|
-
var matrix = [
|
|
1438
|
-
[0, 0, 0, 1, 0],
|
|
1439
|
-
[1, 0, 0, 0, 1],
|
|
1440
|
-
[0, 0, 1, 0, 0],
|
|
1441
|
-
];
|
|
1442
|
-
var grid = Class.New("Grid", [matrix]);
|
|
1443
|
-
var pf_grid = grid.pathfinding();
|
|
1444
|
-
pf_grid.setWalkableAt(1, 2, false);
|
|
1445
|
-
|
|
1446
|
-
@param {Integer} row0 Start position (row)
|
|
1447
|
-
@param {Integer} col0 Start position (col)
|
|
1448
|
-
@param {Integer} row1 Final position (row)
|
|
1449
|
-
@param {Integer} col1 Final position (col)
|
|
1450
|
-
@param {String} type (optional) Type of the search algorithm
|
|
1451
|
-
|
|
1452
|
-
* `AStarFinder` (default)
|
|
1453
|
-
* `BreadthFirstFinder`
|
|
1454
|
-
* `BestFirstFinder`
|
|
1455
|
-
* `DijkstraFinder`
|
|
1456
|
-
* `BiAStarFinder`
|
|
1457
|
-
* `BiBestFirstFinder`
|
|
1458
|
-
* `BiDijkstraFinder`
|
|
1459
|
-
* `BiBreadthFirstFinder`
|
|
1460
|
-
* `JumpPointFinder`
|
|
1461
|
-
|
|
1462
|
-
@param {Object} options (optional) Library Options. See [https://github.com/qiao/PathFinding.js](https://github.com/qiao/PathFinding.js)
|
|
1463
|
-
@return {Array|PF.Grid}
|
|
1464
|
-
@example
|
|
1465
|
-
|
|
1466
|
-
In `ready` method
|
|
1467
|
-
|
|
1468
|
-
var matrix = [
|
|
1469
|
-
[0, 0, 0, 1, 0],
|
|
1470
|
-
[1, 0, 0, 0, 1],
|
|
1471
|
-
[0, 0, 1, 0, 0],
|
|
1472
|
-
];
|
|
1473
|
-
|
|
1474
|
-
var grid = Class.New("Grid", [matrix]);
|
|
1475
|
-
grid.pathfinding(0, 0, 2, 4);
|
|
1476
|
-
|
|
1477
|
-
// returns : [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4]]
|
|
1478
|
-
|
|
1479
|
-
grid.setPropertyByCell(0, 1, 1);
|
|
1480
|
-
grid.pathfinding(0, 0, 2, 4);
|
|
1481
|
-
|
|
1482
|
-
// returns [];
|
|
1483
|
-
|
|
1484
|
-
*/
|
|
1485
|
-
pathfinding: function(x0, y0, x1, y1, type, options) {
|
|
1486
|
-
type = type || "AStarFinder";
|
|
1487
|
-
if (!this._pf_grid) {
|
|
1488
|
-
this._pf_grid = new PF.Grid(this._rows, this._cols, this._pf_prop);
|
|
1489
|
-
}
|
|
1490
|
-
if (x0 === undefined) {
|
|
1491
|
-
return this._pf_grid;
|
|
1492
|
-
}
|
|
1493
|
-
return new PF[type](options).findPath(x0, y0, x1, y1, this._pf_grid.clone());
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
});
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
var Hit = {
|
|
1500
|
-
Grid: {
|
|
1501
|
-
"new": function(rows, cols) {
|
|
1502
|
-
return Class.New("Grid", [rows, cols]);
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
|
|
1507
|
-
if (typeof exports != "undefined") {
|
|
1508
|
-
exports.Class = Hit;
|
|
1509
|
-
}
|