frigame3 3.0.0 → 3.0.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.
Files changed (180) hide show
  1. package/dist/Animation.d.ts +42 -0
  2. package/dist/Animation.js +189 -0
  3. package/dist/BaseSprite.d.ts +116 -0
  4. package/dist/BaseSprite.js +518 -0
  5. package/dist/Gradient.d.ts +31 -0
  6. package/dist/Gradient.js +89 -0
  7. package/dist/Playground.d.ts +48 -0
  8. package/dist/Playground.js +167 -0
  9. package/dist/Rect.d.ts +63 -0
  10. package/dist/Rect.js +155 -0
  11. package/dist/Rectangle.d.ts +31 -0
  12. package/dist/Rectangle.js +69 -0
  13. package/dist/Renderer.d.ts +23 -0
  14. package/dist/Renderer.js +1 -0
  15. package/dist/ResourceManager.d.ts +21 -0
  16. package/dist/ResourceManager.js +100 -0
  17. package/dist/Sprite.d.ts +56 -0
  18. package/dist/Sprite.js +437 -0
  19. package/dist/SpriteGroup.d.ts +39 -0
  20. package/dist/SpriteGroup.js +176 -0
  21. package/dist/canvasRenderer/renderGroup.d.ts +15 -0
  22. package/dist/canvasRenderer/renderGroup.js +134 -0
  23. package/dist/canvasRenderer/renderPlayground.d.ts +9 -0
  24. package/dist/canvasRenderer/renderPlayground.js +67 -0
  25. package/dist/canvasRenderer/renderRectangle.d.ts +15 -0
  26. package/dist/canvasRenderer/renderRectangle.js +287 -0
  27. package/dist/canvasRenderer/renderSprite.d.ts +4 -0
  28. package/dist/canvasRenderer/renderSprite.js +93 -0
  29. package/dist/canvasRenderer.d.ts +2 -0
  30. package/dist/canvasRenderer.js +19 -0
  31. package/dist/dummyRenderer.d.ts +2 -0
  32. package/dist/dummyRenderer.js +41 -0
  33. package/dist/plugins/GamepadTracker.d.ts +15 -0
  34. package/dist/plugins/GamepadTracker.js +74 -0
  35. package/dist/plugins/Tilemap.d.ts +26 -0
  36. package/dist/plugins/Tilemap.js +85 -0
  37. package/dist/plugins/Vec2.d.ts +69 -0
  38. package/dist/plugins/Vec2.js +296 -0
  39. package/dist/plugins/fx/Shaker.d.ts +46 -0
  40. package/dist/plugins/fx/Shaker.js +190 -0
  41. package/dist/plugins/fx/Tweener.d.ts +42 -0
  42. package/dist/plugins/fx/Tweener.js +166 -0
  43. package/dist/plugins/fx/easing.d.ts +32 -0
  44. package/dist/plugins/fx/easing.js +192 -0
  45. package/dist/plugins/isometric/ISOMixin.d.ts +138 -0
  46. package/dist/plugins/isometric/ISOMixin.js +322 -0
  47. package/dist/plugins/isometric/ISORectangle.d.ts +151 -0
  48. package/dist/plugins/isometric/ISORectangle.js +127 -0
  49. package/dist/plugins/isometric/ISOSprite.d.ts +157 -0
  50. package/dist/plugins/isometric/ISOSprite.js +176 -0
  51. package/dist/plugins/isometric/ISOSpriteGroup.d.ts +151 -0
  52. package/dist/plugins/isometric/ISOSpriteGroup.js +116 -0
  53. package/dist/plugins/isometric/ISOTilemap.d.ts +24 -0
  54. package/dist/plugins/isometric/ISOTilemap.js +85 -0
  55. package/dist/plugins/isometric/utils.d.ts +2 -0
  56. package/dist/plugins/isometric/utils.js +10 -0
  57. package/dist/plugins/keyTracker.d.ts +3 -0
  58. package/dist/plugins/keyTracker.js +23 -0
  59. package/dist/plugins/mixer.d.ts +72 -0
  60. package/dist/plugins/mixer.js +396 -0
  61. package/dist/plugins/sorted/SortedAnimation.d.ts +13 -0
  62. package/dist/plugins/sorted/SortedAnimation.js +27 -0
  63. package/dist/plugins/sorted/SortedGroup.d.ts +137 -0
  64. package/dist/plugins/sorted/SortedGroup.js +61 -0
  65. package/dist/plugins/sorted/SortedMixin.d.ts +122 -0
  66. package/dist/plugins/sorted/SortedMixin.js +153 -0
  67. package/dist/plugins/sorted/SortedRectangle.d.ts +127 -0
  68. package/dist/plugins/sorted/SortedRectangle.js +17 -0
  69. package/dist/plugins/sorted/SortedSprite.d.ts +130 -0
  70. package/dist/plugins/sorted/SortedSprite.js +35 -0
  71. package/dist/utils.d.ts +11 -0
  72. package/dist/utils.js +33 -0
  73. package/package.json +6 -2
  74. package/CHANGELOG.md +0 -13
  75. package/biome.json +0 -93
  76. package/demo/package-lock.json +0 -515
  77. package/demo/package.json +0 -19
  78. package/demo/snake/index.html +0 -16
  79. package/demo/snake/index.ts +0 -110
  80. package/demo/tetris/index.html +0 -16
  81. package/demo/tetris/index.ts +0 -536
  82. package/demo/tsconfig.json +0 -53
  83. package/src/Animation.ts +0 -266
  84. package/src/BaseSprite.ts +0 -682
  85. package/src/Gradient.ts +0 -120
  86. package/src/Playground.ts +0 -247
  87. package/src/Rect.ts +0 -237
  88. package/src/Rectangle.ts +0 -108
  89. package/src/Renderer.ts +0 -28
  90. package/src/ResourceManager.ts +0 -145
  91. package/src/Sprite.ts +0 -484
  92. package/src/SpriteGroup.ts +0 -258
  93. package/src/canvasRenderer/renderGroup.ts +0 -194
  94. package/src/canvasRenderer/renderPlayground.ts +0 -103
  95. package/src/canvasRenderer/renderRectangle.ts +0 -421
  96. package/src/canvasRenderer/renderSprite.ts +0 -148
  97. package/src/canvasRenderer.ts +0 -38
  98. package/src/dummyRenderer.ts +0 -62
  99. package/src/plugins/GamepadTracker.ts +0 -97
  100. package/src/plugins/Tilemap.ts +0 -123
  101. package/src/plugins/Vec2.ts +0 -420
  102. package/src/plugins/fx/Shaker.ts +0 -293
  103. package/src/plugins/fx/Tweener.ts +0 -252
  104. package/src/plugins/fx/easing.ts +0 -276
  105. package/src/plugins/isometric/ISOMixin.ts +0 -444
  106. package/src/plugins/isometric/ISORectangle.ts +0 -183
  107. package/src/plugins/isometric/ISOSprite.ts +0 -228
  108. package/src/plugins/isometric/ISOSpriteGroup.ts +0 -155
  109. package/src/plugins/isometric/ISOTilemap.ts +0 -122
  110. package/src/plugins/isometric/utils.ts +0 -19
  111. package/src/plugins/keyTracker.ts +0 -31
  112. package/src/plugins/mixer.ts +0 -523
  113. package/src/plugins/sorted/SortedAnimation.ts +0 -53
  114. package/src/plugins/sorted/SortedGroup.ts +0 -94
  115. package/src/plugins/sorted/SortedMixin.ts +0 -203
  116. package/src/plugins/sorted/SortedRectangle.ts +0 -35
  117. package/src/plugins/sorted/SortedSprite.ts +0 -56
  118. package/src/utils.ts +0 -53
  119. package/test/animation/index.html +0 -20
  120. package/test/animation/index.ts +0 -253
  121. package/test/animation/mh.png +0 -0
  122. package/test/animation/moh.png +0 -0
  123. package/test/animation/mov.png +0 -0
  124. package/test/animation/mv.png +0 -0
  125. package/test/animation/rebound.png +0 -0
  126. package/test/animation/reboundm.png +0 -0
  127. package/test/animation/sh.png +0 -0
  128. package/test/animation/soh.png +0 -0
  129. package/test/animation/sov.png +0 -0
  130. package/test/animation/sv.png +0 -0
  131. package/test/isometric1/beach.png +0 -0
  132. package/test/isometric1/foglie.png +0 -0
  133. package/test/isometric1/grass.png +0 -0
  134. package/test/isometric1/index.html +0 -17
  135. package/test/isometric1/index.ts +0 -68
  136. package/test/isometric1/tronco.png +0 -0
  137. package/test/isometric1/untitled.tmx +0 -25
  138. package/test/isometric1/water.png +0 -0
  139. package/test/isometric2/banner.png +0 -0
  140. package/test/isometric2/books.png +0 -0
  141. package/test/isometric2/candlestick.png +0 -0
  142. package/test/isometric2/ice.png +0 -0
  143. package/test/isometric2/index.html +0 -16
  144. package/test/isometric2/index.ts +0 -129
  145. package/test/isometric2/knight_se.png +0 -0
  146. package/test/isometric2/tile.png +0 -0
  147. package/test/isometric2/untitled.tmx +0 -36
  148. package/test/isometric2/wall.png +0 -0
  149. package/test/layers/boy.png +0 -0
  150. package/test/layers/catgirl.png +0 -0
  151. package/test/layers/fri1.svg +0 -8
  152. package/test/layers/grassblock.png +0 -0
  153. package/test/layers/horngirl.png +0 -0
  154. package/test/layers/index.html +0 -17
  155. package/test/layers/index.ts +0 -191
  156. package/test/layers/pinkgirl.png +0 -0
  157. package/test/layers/render_ruler.py +0 -57
  158. package/test/layers/ruler.png +0 -0
  159. package/test/layers/sh.png +0 -0
  160. package/test/mixer/dieci.opus +0 -0
  161. package/test/mixer/dieci.wav +0 -0
  162. package/test/mixer/index.html +0 -33
  163. package/test/mixer/index.ts +0 -206
  164. package/test/mixer/news_intro.mp3 +0 -0
  165. package/test/mixer/news_intro.ogg +0 -0
  166. package/test/mixer/prova.mp3 +0 -0
  167. package/test/mixer/prova.ogg +0 -0
  168. package/test/package-lock.json +0 -515
  169. package/test/package.json +0 -19
  170. package/test/sorted/Brown Block.png +0 -0
  171. package/test/sorted/index.html +0 -16
  172. package/test/sorted/index.ts +0 -38
  173. package/test/transformation1/index.html +0 -17
  174. package/test/transformation1/index.ts +0 -51
  175. package/test/transformation1/sh.png +0 -0
  176. package/test/transformation2/index.html +0 -20
  177. package/test/transformation2/index.ts +0 -71
  178. package/test/transformation2/sh.png +0 -0
  179. package/test/tsconfig.json +0 -53
  180. package/tsconfig.json +0 -51
@@ -1,97 +0,0 @@
1
- import type { Playground } from "../Playground.js";
2
-
3
- export class GamepadTracker extends EventTarget {
4
- controllers: Map<number, Gamepad> = new Map<number, Gamepad>();
5
-
6
- _playground: WeakRef<Playground>;
7
- _callbackId: number | null = null;
8
-
9
- constructor(playground: Playground) {
10
- super();
11
-
12
- this._playground = new WeakRef(playground);
13
-
14
- this.registerCallback();
15
-
16
- playground.addEventListener("clearCallbacks", this._onClearCallbacks);
17
- }
18
-
19
- _scanGamepads = (): void => {
20
- const controllers = this.controllers;
21
- const gamepads = navigator.getGamepads();
22
-
23
- // Step 1: Update controller status
24
- for (const gamepad of gamepads) {
25
- if (gamepad !== null) {
26
- controllers.set(gamepad.index, gamepad);
27
- }
28
- }
29
-
30
- // Step 2: Remove disconnected controllers
31
- const indexes = new Set(
32
- gamepads
33
- .filter((gamepad: Gamepad | null): boolean => gamepad !== null)
34
- .map((gamepad: Gamepad | null): number => gamepad!.index),
35
- );
36
- for (const index of controllers.keys()) {
37
- if (!indexes.has(index)) {
38
- controllers.delete(index);
39
- }
40
- }
41
- };
42
-
43
- registerCallback(options?: { suppressWarning?: boolean }): this {
44
- if (this._callbackId === null) {
45
- const playground = this._playground.deref();
46
-
47
- if (playground !== undefined) {
48
- this._callbackId = playground.registerCallback(this._scanGamepads);
49
- } else {
50
- if (
51
- typeof console !== "undefined" &&
52
- options?.suppressWarning === false
53
- ) {
54
- console.warn("playground has been garbage collected");
55
- console.trace();
56
- }
57
- }
58
- } else {
59
- if (
60
- typeof console !== "undefined" &&
61
- options?.suppressWarning === false
62
- ) {
63
- console.warn("Callback already registered");
64
- console.trace();
65
- }
66
- }
67
-
68
- return this;
69
- }
70
-
71
- removeCallback(options?: { suppressWarning?: boolean }): this {
72
- const playground = this._playground.deref();
73
-
74
- if (playground !== undefined) {
75
- playground.removeCallback(this._callbackId, options);
76
- } else {
77
- if (
78
- typeof console !== "undefined" &&
79
- options?.suppressWarning === false
80
- ) {
81
- console.warn("playground has been garbage collected");
82
- console.trace();
83
- }
84
- }
85
-
86
- this._callbackId = null;
87
-
88
- return this;
89
- }
90
-
91
- _onClearCallbacks = (): void => {
92
- if (this._callbackId !== null) {
93
- this._callbackId = null;
94
- this.registerCallback();
95
- }
96
- };
97
- }
@@ -1,123 +0,0 @@
1
- import type { BaseSprite } from "../BaseSprite.js";
2
- import type { RectangleOptions } from "../Rectangle.js";
3
- import { Rectangle } from "../Rectangle.js";
4
- import type { SpriteOptions } from "../Sprite.js";
5
- import { Sprite } from "../Sprite.js";
6
- import type { SpriteGroupOptions } from "../SpriteGroup.js";
7
- import { SpriteGroup } from "../SpriteGroup.js";
8
- import type { SpriteRef } from "../utils.js";
9
-
10
- // animationList MUST have at least `animation` or `background`
11
- export type TileOptions =
12
- | (Pick<SpriteOptions, "animation"> & Partial<SpriteOptions>)
13
- | (Pick<RectangleOptions, "background"> & Partial<RectangleOptions>);
14
-
15
- export interface TilemapOptions {
16
- sizex: number; // Num tiles
17
- sizey: number; // Num tiles
18
- tileWidth: number; // Pixel
19
- tileHeight: number; // Pixel
20
- data: number[]; // (sizex * sizey) members, indices of animationList
21
- animationList: Record<number, TileOptions>;
22
- }
23
-
24
- export class Tilemap extends SpriteGroup {
25
- sizex = 0;
26
- sizey = 0;
27
- tileWidth = 0;
28
- tileHeight = 0;
29
-
30
- _locations: Uint32Array;
31
-
32
- constructor(
33
- options: TilemapOptions &
34
- Partial<SpriteGroupOptions> & { ref?: SpriteRef<Tilemap> },
35
- ) {
36
- super(options);
37
-
38
- const { sizex, sizey, tileWidth, tileHeight } = options;
39
-
40
- this.sizex = sizex;
41
- this.sizey = sizey;
42
- this.tileWidth = tileWidth;
43
- this.tileHeight = tileHeight;
44
-
45
- const locations = new Uint32Array(sizex * sizey).fill(0xffffffff);
46
- this._locations = locations;
47
- const layers = this._layers;
48
-
49
- if (
50
- options.width === undefined &&
51
- options.halfWidth === undefined &&
52
- options.radius === undefined
53
- ) {
54
- this.width = sizex * tileWidth;
55
- }
56
-
57
- if (
58
- options.height === undefined &&
59
- options.halfHeight === undefined &&
60
- options.radius === undefined
61
- ) {
62
- this.height = sizey * tileHeight;
63
- }
64
-
65
- const animationList = options.animationList;
66
-
67
- let i_location = 0;
68
- let row = 0;
69
- let col = 0;
70
- let left = 0;
71
- let top = 0;
72
- for (const data of options.data) {
73
- const animation_options = animationList[data];
74
- if (animation_options !== undefined && "animation" in animation_options) {
75
- // Sprite
76
- const sprite_options = Object.assign({}, animation_options, {
77
- left,
78
- top,
79
- });
80
- this.addChild(new Sprite(sprite_options));
81
- locations[i_location] = layers.length - 1;
82
- } else if (
83
- animation_options !== undefined &&
84
- "background" in animation_options
85
- ) {
86
- // Rectangle
87
- const rectangle_options = Object.assign({}, animation_options, {
88
- left,
89
- top,
90
- width: tileWidth,
91
- height: tileHeight,
92
- });
93
- this.addChild(new Rectangle(rectangle_options));
94
- locations[i_location] = layers.length - 1;
95
- } else {
96
- // TODO: Invalid
97
- }
98
-
99
- i_location += 1;
100
- left += tileWidth;
101
- col += 1;
102
- if (col >= sizex) {
103
- col = 0;
104
- left = 0;
105
- row += 1;
106
- top += tileHeight;
107
- if (row >= sizey) {
108
- break;
109
- }
110
- }
111
- }
112
- }
113
-
114
- getAt(row: number, col: number): BaseSprite | undefined {
115
- const i = row * this.sizex + col;
116
-
117
- if (i < this._locations.length) {
118
- return this._layers[this._locations[i]!];
119
- } else {
120
- return undefined;
121
- }
122
- }
123
- }
@@ -1,420 +0,0 @@
1
- export interface IVec2 {
2
- x: number;
3
- y: number;
4
- }
5
-
6
- export class Vec2 implements IVec2 {
7
- x: number;
8
- y: number;
9
-
10
- constructor(x: number, y: number) {
11
- this.x = x;
12
- this.y = y;
13
- }
14
-
15
- static clone(a: IVec2): Vec2 {
16
- return new Vec2(a.x, a.y);
17
- }
18
-
19
- clone(b: IVec2): this {
20
- this.x = b.x;
21
- this.y = b.y;
22
-
23
- return this;
24
- }
25
-
26
- static fromValues(x: number, y: number): Vec2 {
27
- return new Vec2(x, y);
28
- }
29
-
30
- fromValues(x: number, y: number): this {
31
- this.x = x;
32
- this.y = y;
33
-
34
- return this;
35
- }
36
-
37
- static fromPolar(mag: number, angle: number): Vec2 {
38
- return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
39
- }
40
-
41
- fromPolar(mag: number, angle: number): this {
42
- this.x = Math.cos(angle) * mag;
43
- this.y = Math.sin(angle) * mag;
44
-
45
- return this;
46
- }
47
-
48
- static randomUnit(): Vec2 {
49
- const angle = Math.random() * Math.PI * 2;
50
-
51
- return new Vec2(Math.cos(angle), Math.sin(angle));
52
- }
53
-
54
- randomUnit(): this {
55
- const angle = Math.random() * Math.PI * 2;
56
-
57
- this.x = Math.cos(angle);
58
- this.y = Math.sin(angle);
59
-
60
- return this;
61
- }
62
-
63
- static random(scalar = 1): Vec2 {
64
- const mag = Math.random() * scalar;
65
- const angle = Math.random() * Math.PI * 2;
66
-
67
- return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
68
- }
69
-
70
- random(scalar = 1): this {
71
- const mag = Math.random() * scalar;
72
- const angle = Math.random() * Math.PI * 2;
73
-
74
- this.x = Math.cos(angle) * mag;
75
- this.y = Math.sin(angle) * mag;
76
-
77
- return this;
78
- }
79
-
80
- static add(a: IVec2, b: IVec2): Vec2 {
81
- return new Vec2(a.x + b.x, a.y + b.y);
82
- }
83
-
84
- add(b: IVec2): this {
85
- this.x += b.x;
86
- this.y += b.y;
87
-
88
- return this;
89
- }
90
-
91
- static sub(a: IVec2, b: IVec2): Vec2 {
92
- return new Vec2(a.x - b.x, a.y - b.y);
93
- }
94
-
95
- sub(b: IVec2): this {
96
- this.x -= b.x;
97
- this.y -= b.y;
98
-
99
- return this;
100
- }
101
-
102
- static scale(a: IVec2, sx: number, sy: number = sx): Vec2 {
103
- return new Vec2(a.x * sx, a.y * sy);
104
- }
105
-
106
- scale(sx: number, sy: number = sx): this {
107
- this.x *= sx;
108
- this.y *= sy;
109
-
110
- return this;
111
- }
112
-
113
- static invert(a: IVec2): Vec2 {
114
- return new Vec2(-a.x, -a.y);
115
- }
116
-
117
- invert(): this {
118
- this.x *= -1;
119
- this.y *= -1;
120
-
121
- return this;
122
- }
123
-
124
- static normalize(a: IVec2): Vec2 {
125
- let mag = Math.hypot(a.x, a.y);
126
- if (mag !== 0) {
127
- mag = 1 / mag;
128
- }
129
-
130
- return new Vec2(a.x * mag, a.y * mag);
131
- }
132
-
133
- normalize(): this {
134
- let mag = Math.hypot(this.x, this.y);
135
- if (mag !== 0) {
136
- mag = 1 / mag;
137
- }
138
-
139
- this.x *= mag;
140
- this.y *= mag;
141
-
142
- return this;
143
- }
144
-
145
- static rotate(a: IVec2, angle: number): Vec2 {
146
- const x = a.x;
147
- const y = a.y;
148
- const ct = Math.cos(angle);
149
- const st = Math.sin(angle);
150
-
151
- return new Vec2(x * ct - y * st, x * st + y * ct);
152
- }
153
-
154
- rotate(angle: number): this {
155
- const x = this.x;
156
- const y = this.y;
157
- const ct = Math.cos(angle);
158
- const st = Math.sin(angle);
159
-
160
- this.x = x * ct - y * st;
161
- this.y = x * st + y * ct;
162
-
163
- return this;
164
- }
165
-
166
- static rotateAroundPoint(a: IVec2, axisPoint: IVec2, angle: number): Vec2 {
167
- return Vec2.clone(a).sub(axisPoint).rotate(angle).add(axisPoint);
168
- }
169
-
170
- rotateAroundPoint(axisPoint: IVec2, angle: number): this {
171
- return this.sub(axisPoint).rotate(angle).add(axisPoint);
172
- }
173
-
174
- static scaleAndRotate(
175
- a: IVec2,
176
- axisPoint: IVec2,
177
- angle: number,
178
- sx: number,
179
- sy: number = sx,
180
- ): Vec2 {
181
- return Vec2.clone(a)
182
- .sub(axisPoint)
183
- .scale(sx, sy)
184
- .rotate(angle)
185
- .add(axisPoint);
186
- }
187
-
188
- scaleAndRotate(
189
- axisPoint: IVec2,
190
- angle: number,
191
- sx: number,
192
- sy: number = sx,
193
- ): this {
194
- return this.sub(axisPoint).scale(sx, sy).rotate(angle).add(axisPoint);
195
- }
196
-
197
- static lerp(a: IVec2, b: IVec2, t: number): Vec2 {
198
- const ax = a.x;
199
- const ay = a.y;
200
-
201
- return new Vec2(ax + t * (b.x - ax), ay + t * (b.y - ay));
202
- }
203
-
204
- lerp(b: IVec2, t: number): this {
205
- const x = this.x;
206
- const y = this.y;
207
-
208
- this.x = x + t * (b.x - x);
209
- this.y = y + t * (b.y - y);
210
-
211
- return this;
212
- }
213
-
214
- static perp(a: IVec2): Vec2 {
215
- return new Vec2(-a.y, a.x);
216
- }
217
-
218
- perp(): this {
219
- const x = this.x;
220
- const y = this.y;
221
-
222
- this.x = -y;
223
- this.y = x;
224
-
225
- return this;
226
- }
227
-
228
- static magnitude(a: IVec2): number {
229
- return Math.hypot(a.x, a.y);
230
- }
231
-
232
- magnitude(): number {
233
- return Math.hypot(this.x, this.y);
234
- }
235
-
236
- static squaredMagnitude(a: IVec2): number {
237
- const x = a.x;
238
- const y = a.y;
239
-
240
- return x * x + y * y;
241
- }
242
-
243
- squaredMagnitude(): number {
244
- const x = this.x;
245
- const y = this.y;
246
-
247
- return x * x + y * y;
248
- }
249
-
250
- static azimuth(a: IVec2): number {
251
- return Math.atan2(a.y, a.x);
252
- }
253
-
254
- azimuth(): number {
255
- return Math.atan2(this.y, this.x);
256
- }
257
-
258
- static dot(a: IVec2, b: IVec2): number {
259
- return a.x * b.x + a.y * b.y;
260
- }
261
-
262
- dot(b: IVec2): number {
263
- return this.x * b.x + this.y * b.y;
264
- }
265
-
266
- static cross(a: IVec2, b: IVec2): number {
267
- return a.x * b.y - a.y * b.x;
268
- }
269
-
270
- cross(b: IVec2): number {
271
- return this.x * b.y - this.y * b.x;
272
- }
273
-
274
- static distance(a: IVec2, b: IVec2): number {
275
- return Math.hypot(a.x - b.x, a.y - b.y);
276
- }
277
-
278
- distance(b: IVec2): number {
279
- return Math.hypot(this.x - b.x, this.y - b.y);
280
- }
281
-
282
- static squaredDistance(a: IVec2, b: IVec2): number {
283
- const dx = a.x - b.x;
284
- const dy = a.y - b.y;
285
-
286
- return dx * dx + dy * dy;
287
- }
288
-
289
- squaredDistance(b: IVec2): number {
290
- const dx = this.x - b.x;
291
- const dy = this.y - b.y;
292
-
293
- return dx * dx + dy * dy;
294
- }
295
-
296
- static direction(a: IVec2, b: IVec2): Vec2 {
297
- const dx = b.x - a.x;
298
- const dy = b.y - a.y;
299
-
300
- let mag = Math.hypot(dx, dy);
301
- if (mag !== 0) {
302
- mag = 1 / mag;
303
- }
304
-
305
- return new Vec2(dx * mag, dy * mag);
306
- }
307
-
308
- direction(b: IVec2): this {
309
- const dx = b.x - this.x;
310
- const dy = b.y - this.y;
311
-
312
- let mag = Math.hypot(dx, dy);
313
- if (mag !== 0) {
314
- mag = 1 / mag;
315
- }
316
-
317
- this.x = dx * mag;
318
- this.y = dy * mag;
319
-
320
- return this;
321
- }
322
-
323
- static project(a: IVec2, b: IVec2): Vec2 {
324
- const bx = b.x;
325
- const by = b.y;
326
-
327
- const dotAB = a.x * bx + a.y * by;
328
-
329
- let squaredMagB = bx * bx + by * by;
330
- if (squaredMagB !== 0) {
331
- squaredMagB = 1 / squaredMagB;
332
- }
333
-
334
- const scalar = dotAB * squaredMagB;
335
-
336
- return new Vec2(bx * scalar, by * scalar);
337
- }
338
-
339
- project(b: IVec2): this {
340
- const bx = b.x;
341
- const by = b.y;
342
-
343
- const dotAB = this.x * bx + this.y * by;
344
-
345
- let squaredMagB = bx * bx + by * by;
346
- if (squaredMagB !== 0) {
347
- squaredMagB = 1 / squaredMagB;
348
- }
349
-
350
- const scalar = dotAB * squaredMagB;
351
-
352
- this.x *= scalar;
353
- this.y *= scalar;
354
-
355
- return this;
356
- }
357
-
358
- static abs(a: IVec2): Vec2 {
359
- return new Vec2(Math.abs(a.x), Math.abs(a.y));
360
- }
361
-
362
- abs(): this {
363
- this.x = Math.abs(this.x);
364
- this.y = Math.abs(this.y);
365
-
366
- return this;
367
- }
368
-
369
- static ceil(a: IVec2): Vec2 {
370
- return new Vec2(Math.ceil(a.x), Math.ceil(a.y));
371
- }
372
-
373
- ceil(): this {
374
- this.x = Math.ceil(this.x);
375
- this.y = Math.ceil(this.y);
376
-
377
- return this;
378
- }
379
-
380
- static floor(a: IVec2): Vec2 {
381
- return new Vec2(Math.floor(a.x), Math.floor(a.y));
382
- }
383
-
384
- floor(): this {
385
- this.x = Math.floor(this.x);
386
- this.y = Math.floor(this.y);
387
-
388
- return this;
389
- }
390
-
391
- static round(a: IVec2): Vec2 {
392
- return new Vec2(Math.round(a.x), Math.round(a.y));
393
- }
394
-
395
- round(): this {
396
- this.x = Math.round(this.x);
397
- this.y = Math.round(this.y);
398
-
399
- return this;
400
- }
401
-
402
- static trunc(a: IVec2): Vec2 {
403
- return new Vec2(Math.trunc(a.x), Math.trunc(a.y));
404
- }
405
-
406
- trunc(): this {
407
- this.x = Math.trunc(this.x);
408
- this.y = Math.trunc(this.y);
409
-
410
- return this;
411
- }
412
-
413
- static equals(a: IVec2, b: IVec2): boolean {
414
- return a.x === b.x && a.y === b.y;
415
- }
416
-
417
- equals(b: IVec2): boolean {
418
- return this.x === b.x && this.y === b.y;
419
- }
420
- }