@gg-web-engine/core 0.0.4 → 0.0.5

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.
@@ -104,7 +104,6 @@ class Gg2dEntity extends gg_positionable_2d_entity_1.GgPositionable2dEntity {
104
104
  }
105
105
  dispose() {
106
106
  super.dispose();
107
- this.tick$.unsubscribe();
108
107
  if (this.object2D) {
109
108
  this.object2D.dispose();
110
109
  }
@@ -104,7 +104,6 @@ class Gg3dEntity extends gg_positionable_3d_entity_1.GgPositionable3dEntity {
104
104
  }
105
105
  dispose() {
106
106
  super.dispose();
107
- this.tick$.unsubscribe();
108
107
  if (this.object3D) {
109
108
  this.object3D.dispose();
110
109
  }
@@ -13,7 +13,23 @@ export declare type MapGraphNodeType = {
13
13
  loadOptions: Partial<Omit<LoadOptions, 'position' | 'rotation'>>;
14
14
  };
15
15
  export declare class MapGraph extends Graph<MapGraphNodeType> {
16
- static fromMapArray(array: MapGraphNodeType[]): MapGraph;
16
+ /**
17
+ * Creates a new MapGraph instance from an array of elements, where each element in the array is a node in the graph.
18
+ * The first element of the array is used as the root node of the graph.
19
+ * @param array The array of elements to create the graph from.
20
+ * @param closed An optional boolean indicating whether the graph is closed, meaning that the last node is adjacent to the first node.
21
+ * @returns A new Graph instance created from the array.
22
+ * @typeparam T The type of elements in the array and nodes in the graph.
23
+ */
24
+ static fromMapArray(array: MapGraphNodeType[], closed?: boolean): MapGraph;
25
+ /**
26
+ * Creates a new MapGraph instance from a two-dimensional square grid of elements, where each element in the grid is a node in the graph.
27
+ * The top-left element of the grid is used as the root node of the graph.
28
+ * The nodes in the graph are created in the same order as the elements in the grid, from left to right and then from top to bottom.
29
+ * @param grid The two-dimensional square grid of elements to create the graph from.
30
+ * @returns A new Graph instance created from the square grid.
31
+ * @typeparam T The type of elements in the square grid and nodes in the graph.
32
+ */
17
33
  static fromMapSquareGrid(grid: MapGraphNodeType[][]): MapGraph;
18
34
  getNearestDummy(thisNodes: Graph<MapGraphNodeType>[], cursor: Point3): Graph<MapGraphNodeType>;
19
35
  nodes(): MapGraph[];
@@ -15,7 +15,15 @@ const operators_1 = require("rxjs/operators");
15
15
  const graph_1 = require("../../base/data-structures/graph");
16
16
  const gg_entity_1 = require("../../base/entities/gg-entity");
17
17
  class MapGraph extends graph_1.Graph {
18
- static fromMapArray(array) {
18
+ /**
19
+ * Creates a new MapGraph instance from an array of elements, where each element in the array is a node in the graph.
20
+ * The first element of the array is used as the root node of the graph.
21
+ * @param array The array of elements to create the graph from.
22
+ * @param closed An optional boolean indicating whether the graph is closed, meaning that the last node is adjacent to the first node.
23
+ * @returns A new Graph instance created from the array.
24
+ * @typeparam T The type of elements in the array and nodes in the graph.
25
+ */
26
+ static fromMapArray(array, closed = false) {
19
27
  const root = new MapGraph(array[0]);
20
28
  let tail = root;
21
29
  for (let i = 1; i < array.length; i++) {
@@ -23,8 +31,19 @@ class MapGraph extends graph_1.Graph {
23
31
  tail.addAdjacent(newTail);
24
32
  tail = newTail;
25
33
  }
34
+ if (closed) {
35
+ tail.addAdjacent(root);
36
+ }
26
37
  return root;
27
38
  }
39
+ /**
40
+ * Creates a new MapGraph instance from a two-dimensional square grid of elements, where each element in the grid is a node in the graph.
41
+ * The top-left element of the grid is used as the root node of the graph.
42
+ * The nodes in the graph are created in the same order as the elements in the grid, from left to right and then from top to bottom.
43
+ * @param grid The two-dimensional square grid of elements to create the graph from.
44
+ * @returns A new Graph instance created from the square grid.
45
+ * @typeparam T The type of elements in the square grid and nodes in the graph.
46
+ */
28
47
  static fromMapSquareGrid(grid) {
29
48
  const nodes = grid.map(sgrid => sgrid.map(item => new MapGraph(item)));
30
49
  // bind them
@@ -1,5 +1,21 @@
1
1
  export declare class Graph<T> {
2
- static fromArray<T>(array: T[]): Graph<T>;
2
+ /**
3
+ * Creates a new Graph instance from an array of elements, where each element in the array is a node in the graph.
4
+ * The first element of the array is used as the root node of the graph.
5
+ * @param array The array of elements to create the graph from.
6
+ * @param closed An optional boolean indicating whether the graph is closed, meaning that the last node is adjacent to the first node.
7
+ * @returns A new Graph instance created from the array.
8
+ * @typeparam T The type of elements in the array and nodes in the graph.
9
+ */
10
+ static fromArray<T>(array: T[], closed?: boolean): Graph<T>;
11
+ /**
12
+ * Creates a new Graph instance from a two-dimensional square grid of elements, where each element in the grid is a node in the graph.
13
+ * The top-left element of the grid is used as the root node of the graph.
14
+ * The nodes in the graph are created in the same order as the elements in the grid, from left to right and then from top to bottom.
15
+ * @param grid The two-dimensional square grid of elements to create the graph from.
16
+ * @returns A new Graph instance created from the square grid.
17
+ * @typeparam T The type of elements in the square grid and nodes in the graph.
18
+ */
3
19
  static fromSquareGrid<T>(grid: T[][]): Graph<T>;
4
20
  data: T;
5
21
  private adjacent;
@@ -6,7 +6,15 @@ class Graph {
6
6
  this.data = data;
7
7
  this.adjacent = new Set();
8
8
  }
9
- static fromArray(array) {
9
+ /**
10
+ * Creates a new Graph instance from an array of elements, where each element in the array is a node in the graph.
11
+ * The first element of the array is used as the root node of the graph.
12
+ * @param array The array of elements to create the graph from.
13
+ * @param closed An optional boolean indicating whether the graph is closed, meaning that the last node is adjacent to the first node.
14
+ * @returns A new Graph instance created from the array.
15
+ * @typeparam T The type of elements in the array and nodes in the graph.
16
+ */
17
+ static fromArray(array, closed = false) {
10
18
  const root = new Graph(array[0]);
11
19
  let tail = root;
12
20
  for (let i = 1; i < array.length; i++) {
@@ -14,8 +22,19 @@ class Graph {
14
22
  tail.addAdjacent(newTail);
15
23
  tail = newTail;
16
24
  }
25
+ if (closed) {
26
+ tail.addAdjacent(root);
27
+ }
17
28
  return root;
18
29
  }
30
+ /**
31
+ * Creates a new Graph instance from a two-dimensional square grid of elements, where each element in the grid is a node in the graph.
32
+ * The top-left element of the grid is used as the root node of the graph.
33
+ * The nodes in the graph are created in the same order as the elements in the grid, from left to right and then from top to bottom.
34
+ * @param grid The two-dimensional square grid of elements to create the graph from.
35
+ * @returns A new Graph instance created from the square grid.
36
+ * @typeparam T The type of elements in the square grid and nodes in the graph.
37
+ */
19
38
  static fromSquareGrid(grid) {
20
39
  const nodes = grid.map(sgrid => sgrid.map(item => new Graph(item)));
21
40
  // bind them
@@ -1,5 +1,6 @@
1
1
  import { GgEntity, GGTickOrder } from './gg-entity';
2
2
  import { Point2 } from '../models/points';
3
+ import { GgWorld } from '../gg-world';
3
4
  export declare type RendererOptions = {
4
5
  transparent: boolean;
5
6
  background: number;
@@ -8,9 +9,12 @@ export declare type RendererOptions = {
8
9
  antialias: boolean;
9
10
  };
10
11
  export declare abstract class BaseGgRenderer extends GgEntity {
12
+ protected readonly canvas?: HTMLCanvasElement | undefined;
11
13
  readonly tickOrder = GGTickOrder.RENDERING;
12
14
  readonly rendererOptions: RendererOptions;
13
- protected constructor(canvas?: HTMLCanvasElement, options?: Partial<RendererOptions>);
15
+ protected constructor(canvas?: HTMLCanvasElement | undefined, options?: Partial<RendererOptions>);
14
16
  abstract render(): void;
15
17
  abstract resize(newSize: Point2): void;
18
+ onSpawned(world: GgWorld<any, any>): void;
19
+ onRemoved(): void;
16
20
  }
@@ -6,16 +6,28 @@ const gg_viewport_manager_1 = require("../gg-viewport-manager");
6
6
  class BaseGgRenderer extends gg_entity_1.GgEntity {
7
7
  constructor(canvas, options = {}) {
8
8
  super();
9
+ this.canvas = canvas;
9
10
  this.tickOrder = gg_entity_1.GGTickOrder.RENDERING;
10
11
  this.rendererOptions = Object.assign({ transparent: false, background: 0x000000, antialias: true }, (options || {}));
11
- if (canvas) {
12
- setTimeout(() => {
13
- gg_viewport_manager_1.GgViewportManager.instance.assignRendererToCanvas(this, canvas).then();
14
- }, 0);
15
- }
16
12
  this.tick$.subscribe(() => {
17
13
  this.render();
18
14
  });
19
15
  }
16
+ onSpawned(world) {
17
+ super.onSpawned(world);
18
+ if (this.canvas) {
19
+ setTimeout(() => {
20
+ gg_viewport_manager_1.GgViewportManager.instance.assignRendererToCanvas(this, this.canvas).then();
21
+ }, 0);
22
+ }
23
+ }
24
+ onRemoved() {
25
+ super.onRemoved();
26
+ if (this.canvas) {
27
+ setTimeout(() => {
28
+ gg_viewport_manager_1.GgViewportManager.instance.deregisterCanvas(this.canvas).then();
29
+ }, 0);
30
+ }
31
+ }
20
32
  }
21
33
  exports.BaseGgRenderer = BaseGgRenderer;
@@ -13,6 +13,7 @@ export declare enum GGTickOrder {
13
13
  POST_RENDERING = 1200
14
14
  }
15
15
  export declare abstract class GgEntity {
16
+ private static default_name_counter;
16
17
  /**
17
18
  * will receive [elapsed time, delta] of each world clock tick
18
19
  */
@@ -25,7 +25,7 @@ class GgEntity {
25
25
  * a world reference, where this entity was added to
26
26
  */
27
27
  this._world = null;
28
- this._name = '';
28
+ this._name = '0x' + (GgEntity.default_name_counter++).toString(16);
29
29
  /**
30
30
  * The flag whether entity should listen to ticks. If set to false, ticks will not be propagated to this entity
31
31
  * */
@@ -94,3 +94,4 @@ class GgEntity {
94
94
  }
95
95
  }
96
96
  exports.GgEntity = GgEntity;
97
+ GgEntity.default_name_counter = 0;
@@ -14,5 +14,6 @@ export declare class GgViewportManager {
14
14
  private getStageAsync;
15
15
  createCanvas(zIndex: number): Promise<HTMLCanvasElement>;
16
16
  registerCanvas(canvas: HTMLCanvasElement, zIndex: number): Promise<void>;
17
+ deregisterCanvas(canvas: HTMLCanvasElement): Promise<void>;
17
18
  assignRendererToCanvas(renderer: BaseGgRenderer, canvas: HTMLCanvasElement): Promise<void>;
18
19
  }
@@ -97,6 +97,16 @@ class GgViewportManager {
97
97
  canvas.height = size.y;
98
98
  });
99
99
  }
100
+ deregisterCanvas(canvas) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ for (const index in this.canvases) {
103
+ if (this.canvases[index].canvas === canvas) {
104
+ delete this.canvases[index];
105
+ break;
106
+ }
107
+ }
108
+ });
109
+ }
100
110
  assignRendererToCanvas(renderer, canvas) {
101
111
  return __awaiter(this, void 0, void 0, function* () {
102
112
  if (!gg_viewport_1.GgViewport.instance.isActive) {
@@ -123,6 +123,7 @@ class GgWorld {
123
123
  }
124
124
  dispose() {
125
125
  this.worldClock.stop();
126
+ this.keyboardInput.stop().then();
126
127
  for (let i = 0; i < this.children.length; i++) {
127
128
  this.children[i].onRemoved();
128
129
  this.children[i].dispose();
@@ -151,9 +152,6 @@ class GgWorld {
151
152
  this.tickListeners.splice(this.tickListeners.findIndex(x => x === entity), 1);
152
153
  entity.onRemoved();
153
154
  }
154
- else {
155
- console.warn('Trying to remove entity, which is not spawned');
156
- }
157
155
  if (dispose) {
158
156
  entity.dispose();
159
157
  }
@@ -12,4 +12,6 @@ export declare class Pnt2 {
12
12
  static scalarMult(p: Point2, m: number): Point2;
13
13
  /** linear interpolation */
14
14
  static lerp(a: Point2, b: Point2, t: number): Point2;
15
+ /** angle between vectors in radians */
16
+ static angle(a: Point2, b: Point2): number;
15
17
  }
@@ -36,5 +36,11 @@ class Pnt2 {
36
36
  y: a.y + t * (b.y - a.y),
37
37
  };
38
38
  }
39
+ /** angle between vectors in radians */
40
+ static angle(a, b) {
41
+ const dotProduct = a.x * b.x + a.y * b.y;
42
+ const magnitudeProduct = Math.sqrt(a.x ** 2 + a.y ** 2) * Math.sqrt(b.x ** 2 + b.y ** 2);
43
+ return Math.acos(dotProduct / magnitudeProduct);
44
+ }
39
45
  }
40
46
  exports.Pnt2 = Pnt2;
@@ -18,6 +18,8 @@ export declare class Pnt3 {
18
18
  static scalarMult(p: Point3, m: number): Point3;
19
19
  /** linear interpolation */
20
20
  static lerp(a: Point3, b: Point3, t: number): Point3;
21
+ /** angle between vectors in radians */
22
+ static angle(a: Point3, b: Point3): number;
21
23
  /** rotate point a with quaternion q */
22
24
  static rot(p: Point3, q: Point4): Point3;
23
25
  /** rotate point around axis a (normalized vector) */
@@ -61,6 +61,12 @@ class Pnt3 {
61
61
  z: a.z + t * (b.z - a.z),
62
62
  };
63
63
  }
64
+ /** angle between vectors in radians */
65
+ static angle(a, b) {
66
+ const dotProduct = a.x * b.x + a.y * b.y + a.z * b.z;
67
+ const magnitudeProduct = Math.sqrt(a.x ** 2 + a.y ** 2 + a.z ** 2) * Math.sqrt(b.x ** 2 + b.y ** 2 + b.z ** 2);
68
+ return Math.acos(dotProduct / magnitudeProduct);
69
+ }
64
70
  /** rotate point a with quaternion q */
65
71
  static rot(p, q) {
66
72
  // faster version of:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gg-web-engine/core",
3
- "version": "0.0.04",
3
+ "version": "0.0.05",
4
4
  "description": "An attempt to create open source game engine for browser",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",