@woosh/meep-engine 2.50.3 → 2.51.0

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.
Files changed (120) hide show
  1. package/editor/actions/concrete/WriteGridValueAction.js +13 -18
  2. package/editor/ecs/component/editors/Sampler2DEditor.js +2 -2
  3. package/editor/process/SymbolicDisplayProcess.js +1 -1
  4. package/editor/process/symbolic/buildThreeJSHelperEntity.js +1 -1
  5. package/editor/process/symbolic/makeCameraSymbolicDisplay.js +1 -1
  6. package/editor/process/symbolic/makeGridPositionSymbolDisplay.js +1 -1
  7. package/editor/process/symbolic/makeLightSymbolicDisplay.js +1 -1
  8. package/editor/process/symbolic/makePathSymbolicDisplay.js +1 -1
  9. package/editor/process/symbolic/makePositionedIconDisplaySymbol.js +1 -1
  10. package/editor/view/ecs/components/GridObstacleController.js +2 -2
  11. package/editor/view/ecs/components/TerrainController.js +2 -2
  12. package/package.json +1 -1
  13. package/samples/terrain/editor.js +2 -2
  14. package/src/core/UUID.spec.js +8 -0
  15. package/src/core/bvh2/bvh3/ExplicitBinaryBoundingVolumeHierarchy.d.ts +4 -0
  16. package/src/core/bvh2/bvh3/ExplicitBinaryBoundingVolumeHierarchy.js +57 -55
  17. package/src/core/bvh2/bvh3/ExplicitBinaryBoundingVolumeHierarchy.spec.js +54 -0
  18. package/src/core/bvh2/bvh3/ebvh_build_for_geometry_morton.js +3 -3
  19. package/src/core/bvh2/bvh3/ebvh_build_for_geometry_morton.spec.js +60 -0
  20. package/src/core/bvh2/visual/convert_bvh_to_dot_format_string.js +2 -2
  21. package/src/core/cache/Cache.js +3 -5
  22. package/src/core/cache/CacheElement.js +1 -0
  23. package/src/core/geom/2d/quad-tree/PointQuadTree.js +10 -0
  24. package/src/core/geom/3d/aabb/aabb3_array_combine.js +3 -3
  25. package/src/core/graph/{convertGraphToDotString.js → convert_graph_to_dot_string.js} +1 -1
  26. package/src/core/graph/convert_graph_to_dot_string.spec.js +28 -0
  27. package/src/core/math/noise/sdnoise.js +5 -7
  28. package/src/core/math/noise/sdnoise.spec.js +87 -0
  29. package/src/engine/{Platform.js → browserInfo.js} +1 -5
  30. package/src/engine/ecs/fow/FogOfWar.js +48 -54
  31. package/src/engine/ecs/{systems → renderable}/RenderSystem.d.ts +1 -1
  32. package/src/engine/ecs/{systems → renderable}/RenderSystem.js +10 -38
  33. package/src/engine/ecs/renderable/Renderable.d.ts +25 -0
  34. package/src/engine/ecs/{components → renderable}/Renderable.js +18 -83
  35. package/src/engine/ecs/renderable/Renderable.spec.js +10 -0
  36. package/src/engine/ecs/speaker/VoiceSystem.js +15 -9
  37. package/src/engine/ecs/systems/MeshColliderSystem.js +1 -1
  38. package/src/engine/ecs/systems/RangedAttackSystem.js +1 -1
  39. package/src/engine/ecs/systems/ViewportMeshProjectionSystem.js +1 -1
  40. package/src/engine/ecs/terrain/ecs/layers/TerrainLayers.js +2 -2
  41. package/src/engine/ecs/terrain/ecs/splat/SplatMapOptimizerDebugger.js +3 -3
  42. package/src/engine/ecs/terrain/ecs/splat/SplatMapping.js +2 -2
  43. package/src/engine/ecs/terrain/overlay/TerrainOverlay.js +2 -2
  44. package/src/engine/ecs/terrain/util/paintTerrainOverlayViaLookupTable.js +2 -2
  45. package/src/engine/graphics/copy_transform_to_threejs_object.js +12 -0
  46. package/src/engine/graphics/debug/VisualSymbolLine.js +1 -1
  47. package/src/engine/graphics/ecs/decal/DecalSystem.js +1 -1
  48. package/src/engine/graphics/ecs/highlight/renderer/makeGaussianBlurShader.js +1 -4
  49. package/src/engine/graphics/ecs/highlight/system/RenderableHighlightSystem.d.ts +1 -1
  50. package/src/engine/graphics/ecs/highlight/system/RenderableHighlightSystem.js +2 -2
  51. package/src/engine/graphics/ecs/mesh/applyTransformToThreeObject.js +2 -5
  52. package/src/engine/graphics/ecs/path/ribbon/RibbonPathBuilder.js +2 -2
  53. package/src/engine/graphics/geometry/optimization/merge/prototypeGeometryMerge.js +1 -1
  54. package/src/engine/graphics/particles/particular/engine/utils/volume/prototypeParticleVolume.js +1 -1
  55. package/src/engine/graphics/render/forward_plus/plugin/ptototypeFPPlugin.js +1 -1
  56. package/src/engine/graphics/sh3/path_tracer/prototypePathTracer.js +2 -2
  57. package/src/engine/graphics/shaders/SoftOutlineShader.js +2 -4
  58. package/src/engine/graphics/texture/atlas/CachingTextureAtlas.spec.js +24 -0
  59. package/src/engine/graphics/texture/atlas/TextureAtlas.spec.js +46 -0
  60. package/src/engine/graphics/texture/sampler/SampleTraverser.js +1 -1
  61. package/src/engine/graphics/texture/sampler/bicubic.spec.js +13 -0
  62. package/src/engine/graphics/texture/sampler/differenceSampler.js +1 -1
  63. package/src/engine/graphics/texture/sampler/distance/computeSignedDistanceField_NaiveFlood.js +5 -1
  64. package/src/engine/graphics/texture/sampler/distance/computeSignedDistanceField_NaiveFlood.spec.js +183 -0
  65. package/src/engine/graphics/texture/sampler/filter/filter_lanczos3.js +20 -0
  66. package/src/engine/graphics/texture/sampler/filter/mitchell.js +0 -21
  67. package/src/engine/graphics/texture/sampler/filter/mitchell_v1.js +21 -0
  68. package/src/engine/graphics/texture/sampler/filter/sampler2d_scale_down_generic.js +17 -16
  69. package/src/engine/graphics/texture/sampler/prototypeSamplerFiltering.js +11 -9
  70. package/src/engine/graphics/texture/sampler/{scaleSampler2D.js → resize/sampler2d_scale.js} +8 -8
  71. package/src/engine/graphics/texture/sampler/resize/sampler2d_scale.spec.js +73 -0
  72. package/src/engine/graphics/texture/sampler/{sampler2_d_scale_down_lanczos.js → resize/sampler2d_scale_down_lanczos.js} +3 -24
  73. package/src/engine/graphics/texture/sampler/resize/sampler2d_scale_down_lanczos.spec.js +29 -0
  74. package/src/engine/graphics/texture/sampler/{sampler2d_scale_down_linear.js → resize/sampler2d_scale_down_linear.js} +5 -5
  75. package/src/engine/graphics/texture/sampler/resize/sampler2d_scale_down_linear.spec.js +44 -0
  76. package/src/engine/graphics/texture/sampler/resize/{sampler2d_downsample_mipmap.js → sampler2d_scale_down_mipmap.js} +2 -2
  77. package/src/engine/graphics/texture/sampler/resize/{sampler2d_downsample_mipmap.spec.js → sampler2d_scale_down_mipmap.spec.js} +2 -2
  78. package/src/engine/graphics/texture/sampler/{genericResampleSampler2D.js → resize/sampler2d_scale_generic.js} +16 -12
  79. package/src/engine/graphics/texture/sampler/{upsampleSampler2D.js → resize/sampler2d_scale_up_linear.js} +5 -2
  80. package/src/engine/graphics/texture/sampler/resize/sampler2d_scale_up_linear.spec.js +14 -0
  81. package/src/engine/graphics/texture/sampler/sampler2d_channel_compute_min_indices.js +4 -2
  82. package/src/engine/graphics/texture/sampler/sampler2d_channel_compute_min_indices.spec.js +15 -0
  83. package/src/engine/graphics/texture/sampler/util/drawSamplerHTML.js +2 -2
  84. package/src/engine/graphics/three/expand_aabb_by_transformed_three_object.js +66 -0
  85. package/src/engine/graphics/three/expand_aabb_by_transformed_three_object.spec.js +108 -0
  86. package/src/engine/graphics/trail/TemporalPath.js +83 -78
  87. package/src/engine/graphics/trail/TemporalPath.spec.js +5 -0
  88. package/src/engine/graphics/trail/x/RibbonX.spec.js +5 -0
  89. package/src/engine/input/devices/InputDeviceSwitch.spec.js +5 -0
  90. package/src/engine/input/ecs/{InputBinding.js → components/InputBinding.js} +1 -1
  91. package/src/engine/input/ecs/components/InputController.js +2 -2
  92. package/src/engine/input/ecs/ism/InputBinding.js +2 -2
  93. package/src/engine/input/ecs/util/TerrainCameraTargetSampler.js +1 -1
  94. package/src/engine/input/ecs/util/TopDownCameraControllerHelper.js +1 -1
  95. package/src/engine/intelligence/behavior/behavior_to_dot.spec.js +25 -0
  96. package/src/engine/intelligence/behavior/util/DelayBehavior.js +6 -4
  97. package/src/engine/intelligence/blackboard/Blackboard.d.ts +2 -0
  98. package/src/engine/intelligence/blackboard/Blackboard.js +36 -2
  99. package/src/engine/intelligence/blackboard/Blackboard.spec.js +49 -0
  100. package/src/engine/intelligence/blackboard/BlackboardSerializationAdapter.spec.js +2 -0
  101. package/src/engine/navigation/grid/{AStar.js → find_path_on_grid_astar.js} +21 -18
  102. package/src/engine/navigation/grid/find_path_on_grid_astar.spec.js +7 -0
  103. package/src/engine/sound/ecs/SoundListenerSystem.js +1 -1
  104. package/src/generation/markers/debug/visualizeMarkers.js +1 -1
  105. package/src/engine/MeepSettings.js +0 -9
  106. package/src/engine/User.js +0 -28
  107. package/src/engine/UserController.js +0 -273
  108. package/src/engine/ecs/components/AABB.js +0 -33
  109. package/src/engine/ecs/components/AABBCollider.js +0 -15
  110. package/src/engine/ecs/components/Renderable.d.ts +0 -14
  111. package/src/engine/ecs/grid/Sampler2DDecoder.js +0 -57
  112. package/src/engine/ecs/grid/makeTerrainGeometry.js +0 -110
  113. package/src/engine/ecs/systems/AABBColliderSystem.js +0 -61
  114. package/src/engine/ecs/systems/AABBSystem.js +0 -89
  115. package/src/engine/graphics/texture/sampler/sampler2d_make_array_filler_function.js +0 -65
  116. package/src/engine/graphics/texture/sampler/sampler2d_scale_down_linear.spec.js +0 -17
  117. package/src/engine/graphics/trail/x/simulator/RibbonState.js +0 -10
  118. package/src/engine/grid/Grid.js +0 -131
  119. package/src/engine/navigation/grid/GridField.js +0 -328
  120. /package/src/engine/ecs/{components → renderable}/RenderableFlags.js +0 -0
@@ -1,65 +0,0 @@
1
- /**
2
- *
3
- * @param {Sampler2D} sampler
4
- * @param {number} scale
5
- * @param {number} offset
6
- * @return {function(index:int, array:ArrayLike, x:int, y:int)}
7
- */
8
- export function sampler2d_make_array_filler_function(sampler, scale=255, offset=0){
9
-
10
- const v4 = [1 / scale, 1 / scale, 1 / scale, 1 / scale];
11
-
12
- //
13
- function fillDD1(index, array, x, y) {
14
- const val = (sampler.sampleChannelBilinear(x, y, 0) + offset) * scale | 0;
15
- array[index] = val;
16
- array[index + 1] = val;
17
- array[index + 2] = val;
18
- array[index + 3] = 255;
19
- }
20
-
21
- function fillDD2(index, array, x, y) {
22
- sampler.sampleBilinear(x, y, v4, 0);
23
- const val = (v4[0] + offset) * scale | 0;
24
- array.fill(val, index, index + 3);
25
- array[index + 3] = (v4[1] + offset) * scale | 0;
26
- }
27
-
28
- function fillDD3(index, array, x, y) {
29
-
30
- sampler.sampleBilinear(x, y, v4, 0);
31
-
32
- array[index] = (v4[0] + offset) * scale | 0;
33
- array[index + 1] = (v4[1] + offset) * scale | 0;
34
- array[index + 2] = (v4[2] + offset) * scale | 0;
35
- array[index + 3] = 255;
36
- }
37
-
38
- function fillDD4(index, array, x, y) {
39
- sampler.sampleBilinear(x, y, v4, 0);
40
- array[index] = (v4[0] + offset) * scale | 0;
41
- array[index + 1] = (v4[1] + offset) * scale | 0;
42
- array[index + 2] = (v4[2] + offset) * scale | 0;
43
- array[index + 3] = (v4[3] + offset) * scale | 0;
44
- }
45
-
46
- let fillDD;
47
- switch (sampler.itemSize) {
48
- case 1:
49
- fillDD = fillDD1;
50
- break;
51
- case 2:
52
- fillDD = fillDD2;
53
- break;
54
- case 3:
55
- fillDD = fillDD3;
56
- break;
57
- case 4:
58
- fillDD = fillDD4;
59
- break;
60
- default :
61
- throw new Error("unsupported item size");
62
- break;
63
- }
64
- return fillDD;
65
- }
@@ -1,17 +0,0 @@
1
- import { Sampler2D } from "./Sampler2D.js";
2
- import { sampler2d_scale_down_linear } from "./sampler2d_scale_down_linear.js";
3
-
4
- test('2x2 -> 1x1 2x channel', () => {
5
- const input = Sampler2D.float32(2, 2, 2);
6
- const output = Sampler2D.float32(2, 1, 1);
7
-
8
- input.set(0, 0, [1, 3]);
9
- input.set(1, 0, [5, 7]);
10
- input.set(0, 1, [11, 13]);
11
- input.set(1, 1, [17, 19]);
12
-
13
- sampler2d_scale_down_linear(input, output);
14
-
15
- expect(output.data[0]).toEqual(8.5);
16
- expect(output.data[1]).toEqual(10.5);
17
- });
@@ -1,10 +0,0 @@
1
- export class RibbonState {
2
- constructor() {
3
-
4
- /**
5
- *
6
- * @type {RibbonX}
7
- */
8
- this.ribbon = null;
9
- }
10
- }
@@ -1,131 +0,0 @@
1
- /**
2
- * Created by Alex on 13/10/2014.
3
- */
4
- import GridField from '../navigation/grid/GridField.js';
5
- import Vector2 from '../../core/geom/Vector2.js';
6
- import Vector3 from "../../core/geom/Vector3.js";
7
-
8
- const Grid = function () {
9
- let gridScale = 2;
10
- let gridScaleX = gridScale;
11
- let gridScaleY = gridScale;
12
-
13
- this.size = new Vector2();
14
- this.field = null;
15
-
16
- this.init = function (width, height, worldScale) {
17
- if (worldScale === undefined) {
18
- worldScale = 2;
19
- }
20
- gridScale = worldScale;
21
- this.field = new GridField(width, height);
22
- this.size.set(width, height);
23
-
24
- updateGridScale(width, height);
25
- };
26
-
27
- function updateGridScale(width, height) {
28
- //scale is not exact as number of points in the mesh will be greater by 1 than number of tiles, for obvious reasons.
29
- gridScaleX = gridScale * (width / (width - 1));
30
- gridScaleY = gridScale * (height / (height - 1));
31
- }
32
-
33
- this.resize = function (width, height) {
34
- const oldSize = this.size;
35
-
36
- if (oldSize.x !== width || oldSize.y !== height) {
37
- const newField = new GridField(width, height);
38
- updateGridScale(width, height);
39
- //copy values from old field to new one
40
- if (this.field !== null) {
41
- this.field.rectTraverse(0, 0, oldSize.x, oldSize.y, function (x, y, value) {
42
- newField.pointSet(x, y, value);
43
- });
44
- }
45
-
46
- this.field = newField;
47
- this.size.set(width, height);
48
- }
49
- };
50
-
51
- /**
52
- *
53
- * @param {number} x
54
- * @param {number} y
55
- * @param {Vector3} resultV3
56
- */
57
- function pointGrid2World2(x, y, resultV3) {
58
- resultV3.x = (x) * gridScaleX;
59
- resultV3.z = (y) * gridScaleY;
60
- }
61
-
62
- /**
63
- *
64
- * @param {Vector3} v3
65
- * @param {Vector2} resultV2
66
- */
67
- function pointWorld2Grid(v3, resultV2) {
68
- resultV2.x = v3.x / gridScaleX;
69
- resultV2.y = v3.z / gridScaleY;
70
- }
71
-
72
- this.pointWorld2Grid = pointWorld2Grid;
73
- this.pointGrid2World = pointGrid2World2;
74
- const point2 = new Vector2();
75
-
76
- function mapHeightToPath(path, terrain, width, height, callback) {
77
- let mappedCount = 0;
78
- const l = path.length;
79
-
80
- function mapPoint(p) {
81
- terrain.sampleHeight(p.x, p.z, function (y) {
82
- p.y = y;
83
- if (++mappedCount >= l) {
84
- callback(path);
85
- }
86
- });
87
- }
88
-
89
- for (let i = 0; i < l; i++) {
90
- const p = path[i];
91
- mapPoint(p);
92
- }
93
- }
94
-
95
- const self = this;
96
- this.findPath = (function () {
97
- const start = new Vector2(0, 0);
98
- const goal = new Vector2(0, 0);
99
-
100
- /**
101
- *
102
- * @param {Vector3} from
103
- * @param {Vector3} to
104
- * @param {number} clearance
105
- * @param callback
106
- */
107
- function findPath(from, to, clearance, callback) {
108
- pointWorld2Grid(from, start);
109
- start.ceil();
110
- pointWorld2Grid(to, goal);
111
- goal.ceil();
112
- const field = self.field;
113
- const indexPath = field.findPath(start, goal, 10000, 0);
114
- const path = [];
115
- let i, point;
116
- for (i = 0; i < indexPath.length; i++) {
117
- const index = indexPath[i];
118
- field.index2point(index, point2);
119
- point = new Vector3(0, 0, 0);
120
- pointGrid2World2(point2.x, point2.y, point);
121
- path.push(point);
122
- }
123
- const terrain = self.terrain;
124
-
125
- mapHeightToPath(path, terrain, self.size.x * gridScaleX, self.size.y * gridScaleY, callback);
126
- }
127
-
128
- return findPath;
129
- })();
130
- };
131
- export default Grid;
@@ -1,328 +0,0 @@
1
- import Vector2 from '../../../core/geom/Vector2.js';
2
- import { clamp } from "../../../core/math/clamp.js";
3
- import BinaryHeap from '../../../core/collection/heap/BinaryHeap.js';
4
- import { assert } from "../../../core/assert.js";
5
- import { gridAStarSearch } from "./AStar.js";
6
-
7
- const blockValue = 255;
8
-
9
- /**
10
- * A kind of 2D data texture
11
- * @deprecated prefer using {@link Sampler2D} instead, as it's more versatile and has wider feature set
12
- *
13
- * @param {Number} width
14
- * @param {Number} height
15
- * @constructor
16
- *
17
- * @property {Number} width
18
- * @property {Number} height
19
- * @property {Uint8Array} data
20
- */
21
- function GridField(width, height) {
22
- this.width = width;
23
- this.height = height;
24
- this.data = null;
25
-
26
- if (height !== 0 && height !== 0) {
27
- const byteSize = height * width;
28
- this.data = new Uint8Array(byteSize);
29
- }
30
-
31
- const self = this;
32
-
33
- Object.defineProperties(this, {
34
- raw: {
35
- set: function (val) {
36
- console.warn("Deprecated, use .data accessor instead");
37
- self.data = val;
38
- },
39
- get: function () {
40
- console.warn("Deprecated, use .data accessor instead");
41
- return self.data;
42
- }
43
- }
44
- });
45
- }
46
-
47
- /**
48
- *
49
- * @param {Number} value
50
- */
51
- GridField.prototype.fill = function (value) {
52
- this.data.fill(value);
53
- };
54
-
55
- /**
56
- *
57
- * @param {Number} x0
58
- * @param {Number} y0
59
- * @param {Number} x1
60
- * @param {Number} y1
61
- * @param {function(x:Number,y:Number,value:Number)} callback
62
- */
63
- GridField.prototype.rectTraverse = function (x0, y0, x1, y1, callback) {
64
- const width = this.width;
65
- const height = this.height;
66
-
67
- let x, y;
68
-
69
- x0 = clamp(x0, 0, width - 1);
70
- y0 = clamp(y0, 0, height - 1);
71
- x1 = clamp(x1, x0, width - 1);
72
- y1 = clamp(y1, y0, height - 1);
73
-
74
- for (x = x0; x <= x1; x++) {
75
- for (y = y0; y <= y1; y++) {
76
- const index = this.point2index(x, y);
77
- const value = this.data[index];
78
- callback(x, y, value);
79
- }
80
- }
81
- };
82
-
83
- /**
84
- *
85
- * @param {Number} x0
86
- * @param {Number} y0
87
- * @param {Number} x1
88
- * @param {Number} y1
89
- * @param {Number} value
90
- */
91
- GridField.prototype.rectSet = function (x0, y0, x1, y1, value) {
92
- let x, y;
93
- for (x = x0; x <= x1; x++) {
94
- for (y = y0; y <= y1; y++) {
95
- this.pointSet(x, y, value);
96
- }
97
- }
98
- };
99
-
100
- /**
101
- *
102
- * @param {Vector2} pos
103
- */
104
- GridField.prototype.snapToNearestFreeCell = function (pos) {
105
- const self = this;
106
- const p = new Vector2().copy(pos).floor();
107
- const target = p.clone();
108
- const closed = [];
109
-
110
- function score(index) {
111
- self.index2point(index, p);
112
- return p.distanceSqrTo(target);
113
- }
114
-
115
- const open = new BinaryHeap(score);
116
- let current = this.point2index(p.x, p.y);
117
- open.push(current);
118
- const neighbors = [];
119
- while (open.size() > 0) {
120
- current = open.pop();
121
- closed[current] = true;
122
- if (this.data[current] !== blockValue) {
123
- break;
124
- }
125
- //add neighbours
126
- neighbors.length = 0; //reset
127
- this.calcNeighbors(current, neighbors);
128
- const numNeighbors = neighbors.length;
129
- for (let i = 0; i < numNeighbors; i++) {
130
- const n = neighbors[i];
131
- if (closed[n] !== void 0) {
132
- continue;
133
- }
134
- open.push(n);
135
- }
136
- }
137
- this.index2point(current, pos);
138
- };
139
-
140
- /**
141
- *
142
- * @param {Number} index
143
- * @param {function(index:Number, x:Number, y:Number)} visitor
144
- */
145
- GridField.prototype.traverseNeighbors = function traverseNeighbors(index, visitor) {
146
- const width = this.width;
147
- const height = this.height;
148
-
149
- const x = index % width;
150
- const y = (index / width) | 0;
151
-
152
- if (x > 0) {
153
- visitor(index - 1, x, y);
154
- }
155
- if (x < width - 1) {
156
- visitor(index + 1, x, y);
157
- }
158
- if (y > 0) {
159
- visitor(index - width, x, y);
160
- }
161
- if (y < height - 1) {
162
- visitor(index + width, x, y);
163
- }
164
- };
165
-
166
- /**
167
- *
168
- * @param {Number} x
169
- * @param {Number} y
170
- * @returns {Number}
171
- */
172
- GridField.prototype.read = function (x, y) {
173
- const index = this.point2index(x, y);
174
- return this.data[index];
175
- };
176
-
177
- /**
178
- *
179
- * @param {Number} x
180
- * @param {Number} y
181
- * @param {Number} value
182
- */
183
- GridField.prototype.pointAdd = function (x, y, value) {
184
- const index = this.point2index(x, y);
185
- this.data[index] += value;
186
- };
187
-
188
- /**
189
- *
190
- * @param {Number} x
191
- * @param {Number} y
192
- * @param {Number} value
193
- */
194
- GridField.prototype.pointSet = function (x, y, value) {
195
- const index = this.point2index(x, y);
196
- this.data[index] = value;
197
- };
198
-
199
-
200
- /**
201
- *
202
- * @param {Number} x
203
- * @param {Number} y
204
- * @returns {Number}
205
- */
206
- GridField.prototype.point2index = function (x, y) {
207
- return x + y * this.width;
208
- };
209
-
210
- /**
211
- * Paints with additive brush
212
- * @param {Array.<Number>} bendPointIndexList
213
- * @param {Number} value
214
- */
215
- GridField.prototype.pathAdd = function pathAdd(bendPointIndexList, value) {
216
- const indices = bendPointIndexList;
217
- const length = indices.length;
218
-
219
- //
220
- if (length > 0) {
221
- const pCurrent = new Vector2();
222
- const pTarget = new Vector2();
223
- let dx, dy;
224
- this.index2point(indices[0], pCurrent);
225
- this.pointAdd(pCurrent.x, pCurrent.y, value);
226
- for (let i = 1; i < length; i++) {
227
- this.index2point(indices[i], pTarget);
228
- dx = pTarget.x - pCurrent.x;
229
- dy = pTarget.y - pCurrent.y;
230
- if (dx !== 0) {
231
- //drop values to multiple of 1
232
- dx /= Math.abs(dx);
233
- while (pCurrent.x !== pTarget.x) {
234
- pCurrent.x += dx;
235
- this.pointAdd(pCurrent.x, pCurrent.y, value);
236
- }
237
- }
238
- if (dy !== 0) {
239
- //drop values to multiple of 1
240
- dy /= Math.abs(dy);
241
- while (pCurrent.y !== pTarget.y) {
242
- pCurrent.y += dy;
243
- this.pointAdd(pCurrent.x, pCurrent.y, value);
244
- }
245
- }
246
- }
247
- }
248
- };
249
-
250
- /**
251
- *
252
- * @param {Number} index
253
- * @param {Vector2} point
254
- */
255
- GridField.prototype.index2point = function (index, point) {
256
- const width = this.width;
257
-
258
- point.set(index % width, Math.floor(index / width));
259
- };
260
-
261
- /**
262
- *
263
- * @param {Number} index
264
- * @param {Array.<Number>} result
265
- */
266
- GridField.prototype.calcNeighbors = function (index, result) {
267
- const width = this.width;
268
- const height = this.height;
269
-
270
- const x = index % width;
271
- const y = (index / width) | 0;
272
- if (x > 0) {
273
- result.push(index - 1);
274
- }
275
- if (x < width - 1) {
276
- result.push(index + 1);
277
- }
278
- if (y > 0) {
279
- result.push(index - width);
280
- }
281
- if (y < height - 1) {
282
- result.push(index + width);
283
- }
284
- };
285
-
286
-
287
- /**
288
- *
289
- * @param {Number} index1
290
- * @param {Number} index2
291
- * @returns {Number}
292
- */
293
- GridField.prototype.manhattanDistanceByIndices = function (index1, index2) {
294
- const width = this.width;
295
-
296
- const x1 = index1 % width;
297
- const y1 = Math.floor(index1 / width);
298
- //
299
- const x2 = index2 % width;
300
- const y2 = Math.floor(index2 / width);
301
- //
302
- const dx = Math.abs(x1 - x2);
303
- const dy = Math.abs(y1 - y2);
304
- return dx + dy;
305
- };
306
-
307
- /**
308
- *
309
- * @param {Vector2} start
310
- * @param {Vector2} goal
311
- * @param {Number} crossingPenalty
312
- * @param {Number} bendPenalty
313
- * @returns {Number[]} path of indices
314
- */
315
- GridField.prototype.findPath = function (start, goal, crossingPenalty, bendPenalty) {
316
- assert.notEqual(start, null, 'start is null');
317
- assert.notEqual(start, undefined, 'start is undefined');
318
-
319
- assert.notEqual(goal, null, 'goal is null');
320
- assert.notEqual(goal, undefined, 'goal is undefined');
321
-
322
- const startIndex = this.point2index(start.x, start.y);
323
- const goalIndex = this.point2index(goal.x, goal.y);
324
-
325
- return gridAStarSearch(this.field, this.width, this.height, startIndex, goalIndex, crossingPenalty, bendPenalty, 255);
326
- };
327
-
328
- export default GridField;