bard-legends-framework 0.10.9 → 0.11.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 (109) hide show
  1. package/dist/game-entities/controller/controller.d.ts +0 -1
  2. package/dist/game-entities/controller/controller.js +1 -0
  3. package/dist/game-entities/controller/controller.test.js +2 -2
  4. package/dist/game-entities/entity/entity.d.ts +8 -6
  5. package/dist/game-entities/entity/entity.js +5 -7
  6. package/dist/game-entities/entity/entity.test.js +40 -134
  7. package/dist/game-entities/entity/helpers/entity-store.helper.d.ts +6 -9
  8. package/dist/game-entities/entity/helpers/entity-store.helper.js +22 -57
  9. package/dist/game-entities/entity/helpers/entity-store.helper.test.d.ts +1 -0
  10. package/dist/game-entities/entity/helpers/entity-store.helper.test.js +136 -0
  11. package/dist/game-entities/entity/helpers/entity-views.helper.d.ts +1 -1
  12. package/dist/game-entities/entity/helpers/entity-views.helper.js +5 -3
  13. package/dist/game-entities/entity/singleton-entity.d.ts +3 -3
  14. package/dist/game-entities/entity/singleton-entity.js +1 -1
  15. package/dist/game-entities/entity/singleton-entity.test.js +2 -29
  16. package/dist/game-entities/index.d.ts +1 -3
  17. package/dist/game-entities/index.js +3 -7
  18. package/dist/game-entities/scene/scene.d.ts +3 -3
  19. package/dist/game-entities/scene/scene.js +31 -25
  20. package/dist/game-entities/scene/scene.test.js +4 -32
  21. package/dist/game-entities/service/service.js +5 -3
  22. package/dist/game-entities/service/service.test.js +2 -28
  23. package/dist/game-entities/unit-test.helper.d.ts +5 -0
  24. package/dist/game-entities/unit-test.helper.js +14 -0
  25. package/dist/game-entities/update-cycle.d.ts +15 -4
  26. package/dist/game-entities/update-cycle.js +14 -9
  27. package/dist/game-entities/view/view.d.ts +5 -6
  28. package/dist/game-entities/view/view.js +5 -17
  29. package/dist/game-entities/view/view.test.js +36 -244
  30. package/dist/physics/entity-types/immovable-physics-entity.d.ts +1 -3
  31. package/dist/physics/entity-types/immovable-physics-entity.js +1 -3
  32. package/dist/physics/entity-types/movable-entity.js +4 -4
  33. package/dist/physics/entity-types/movable-physics-entity.d.ts +2 -3
  34. package/dist/physics/entity-types/movable-physics-entity.js +5 -6
  35. package/dist/physics/entity-types/physics-entity.d.ts +2 -1
  36. package/dist/physics/entity-types/physics-entity.js +3 -2
  37. package/dist/physics/interfaces.d.ts +1 -1
  38. package/dist/physics/module//342/232/234/357/270/217gateways/controllers/physics-internal.controller.d.ts +3 -3
  39. package/dist/physics/module//342/232/234/357/270/217gateways/controllers/physics.controller.d.ts +1 -1
  40. package/dist/physics/module//342/232/234/357/270/217gateways/physics-internal.gateway.d.ts +3 -3
  41. package/dist/physics/module//342/232/234/357/270/217gateways/physics.gateway.d.ts +1 -1
  42. package/dist/physics/module//360/237/223/220services/creation/physics-world.service.js +2 -2
  43. package/dist/physics/module//360/237/223/220services/path-finding/availability-grid.service.js +1 -1
  44. package/dist/physics/module//360/237/223/220services/path-finding/path-finder.service.js +1 -1
  45. package/dist/physics/module//360/237/223/220services/query/physics-query.service.d.ts +3 -3
  46. package/dist/physics/module//360/237/223/220services/test-visuals/test-visuals.service.js +17 -7
  47. package/dist/physics/module//360/237/247/212entities/physics-world.entity.js +2 -2
  48. package/dist/pixi/components/mouse-wheel-listener.ui.d.ts +2 -1
  49. package/dist/pixi/components/mouse-wheel-listener.ui.js +4 -3
  50. package/dist/pixi/display-object/container-attributes.d.ts +2 -3
  51. package/dist/pixi/display-object/container-attributes.js +2 -3
  52. package/dist/pixi/display-object/container.d.ts +0 -3
  53. package/dist/pixi/display-object/container.js +0 -10
  54. package/dist/pixi/display-object/objects/sprite/glow-sprite-generator.js +5 -3
  55. package/dist/pixi/game.d.ts +6 -2
  56. package/dist/pixi/game.js +14 -5
  57. package/dist/pixi/modules/CAMERA//360/237/247/212entities/camera.entity.js +4 -4
  58. package/dist/pixi/modules/CAMERA//360/237/247/251views/camera.view.js +1 -1
  59. package/dist/pixi/services/keyboard/keyboard.d.ts +4 -2
  60. package/dist/pixi/services/keyboard/keyboard.js +6 -4
  61. package/dist/pixi/services/mouse/mouse-target-focus.service.d.ts +2 -1
  62. package/dist/pixi/services/mouse/mouse-target-focus.service.js +6 -5
  63. package/dist/pixi/services/mouse/mouse.service.d.ts +15 -5
  64. package/dist/pixi/services/mouse/mouse.service.js +38 -18
  65. package/dist/utilities/animator/animating-content/fade-in-content.d.ts +18 -0
  66. package/dist/utilities/animator/animating-content/fade-in-content.js +37 -0
  67. package/dist/utilities/animator/animating-content/slide-in-content.d.ts +37 -0
  68. package/dist/utilities/animator/animating-content/slide-in-content.js +99 -0
  69. package/dist/utilities/animator/animations.d.ts +37 -0
  70. package/dist/utilities/animator/animations.js +88 -0
  71. package/dist/utilities/animator/animator.d.ts +61 -0
  72. package/dist/utilities/animator/animator.js +315 -0
  73. package/dist/utilities/animator/animator.test.d.ts +1 -0
  74. package/dist/utilities/animator/animator.test.js +867 -0
  75. package/dist/utilities/animator/index.d.ts +7 -0
  76. package/dist/utilities/animator/index.js +24 -0
  77. package/dist/utilities/animator/state-animation/slide-state-animation.d.ts +24 -0
  78. package/dist/utilities/animator/state-animation/slide-state-animation.js +130 -0
  79. package/dist/utilities/animator/state-animation/slide-state-animation.test.d.ts +1 -0
  80. package/dist/utilities/animator/state-animation/slide-state-animation.test.js +301 -0
  81. package/dist/utilities/animator/state-animation/state-animation.d.ts +33 -0
  82. package/dist/utilities/animator/state-animation/state-animation.js +146 -0
  83. package/dist/utilities/animator/state-animation/state-animation.test.d.ts +1 -0
  84. package/dist/utilities/animator/state-animation/state-animation.test.js +335 -0
  85. package/dist/utilities/animator/state-animation/visit-disappear-state-animation.d.ts +32 -0
  86. package/dist/utilities/animator/state-animation/visit-disappear-state-animation.js +168 -0
  87. package/dist/utilities/animator/state-animation/visit-disappear-state-animation.test.d.ts +1 -0
  88. package/dist/utilities/animator/state-animation/visit-disappear-state-animation.test.js +431 -0
  89. package/dist/utilities/data-structures/vector-set/vector-set.d.ts +7 -0
  90. package/dist/utilities/data-structures/vector-set/vector-set.js +26 -0
  91. package/dist/utilities/delta-time/delta-time.d.ts +3 -0
  92. package/dist/utilities/delta-time/delta-time.js +10 -0
  93. package/dist/utilities/grid-algorithms/closest-available-space/closest-available-space.helper.d.ts +6 -0
  94. package/dist/utilities/grid-algorithms/closest-available-space/closest-available-space.helper.js +55 -0
  95. package/dist/utilities/grid-algorithms/closest-available-space/closest-available-space.helper.test.d.ts +1 -0
  96. package/dist/utilities/grid-algorithms/closest-available-space/closest-available-space.helper.test.js +100 -0
  97. package/dist/utilities/grid-algorithms/path-finder/path-finder.d.ts +8 -0
  98. package/dist/utilities/grid-algorithms/path-finder/path-finder.js +98 -0
  99. package/dist/utilities/grid-algorithms/path-finder/path-finder.test.d.ts +1 -0
  100. package/dist/utilities/grid-algorithms/path-finder/path-finder.test.js +155 -0
  101. package/dist/utilities/grid-algorithms/position-to-grid-position-converter.d.ts +6 -0
  102. package/dist/utilities/grid-algorithms/position-to-grid-position-converter.js +17 -0
  103. package/dist/utilities/grid-algorithms/vector-field-path-finder/vector-field-path-finder.d.ts +18 -0
  104. package/dist/utilities/grid-algorithms/vector-field-path-finder/vector-field-path-finder.js +129 -0
  105. package/dist/utilities/grid-algorithms/vector-field-path-finder/vector-field-path-finder.test.d.ts +1 -0
  106. package/dist/utilities/grid-algorithms/vector-field-path-finder/vector-field-path-finder.test.js +254 -0
  107. package/dist/utilities/index.d.ts +6 -6
  108. package/dist/utilities/index.js +6 -6
  109. package/package.json +3 -3
@@ -0,0 +1,37 @@
1
+ import { Vector } from 'helpers-lib';
2
+ import { Container } from '../../../pixi';
3
+ import { AnimatorAnimation } from '../animations';
4
+ export interface ScrollInContentOptions {
5
+ readonly duration: number;
6
+ readonly animation: AnimatorAnimation;
7
+ readonly size: Vector;
8
+ readonly margin: number;
9
+ readonly direction: 'horizontal' | 'vertical';
10
+ }
11
+ export declare class SlideInContentByIndex extends Container {
12
+ private createFunction;
13
+ private options;
14
+ private directionProperty;
15
+ private stateAnimation;
16
+ private contents;
17
+ constructor(createFunction: (value: {
18
+ index: number;
19
+ container: Container;
20
+ }) => void, partialOptions?: Partial<ScrollInContentOptions>);
21
+ setIndex(index: number, options?: {
22
+ instant?: boolean;
23
+ }): this;
24
+ private createMask;
25
+ }
26
+ export declare class SlideInContent<T> extends SlideInContentByIndex {
27
+ readonly itemToIndex: Map<T, number>;
28
+ readonly indexToItem: Map<number, T>;
29
+ constructor(items: T[], createFunction: (value: {
30
+ index: number;
31
+ item: T;
32
+ container: Container;
33
+ }) => void, partialOptions?: Partial<ScrollInContentOptions>);
34
+ setItem(item: T, options?: {
35
+ instant?: boolean;
36
+ }): this;
37
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SlideInContent = exports.SlideInContentByIndex = void 0;
4
+ const helpers_lib_1 = require("helpers-lib");
5
+ const pixi_1 = require("../../../pixi");
6
+ const animations_1 = require("../animations");
7
+ const slide_state_animation_1 = require("../state-animation/slide-state-animation");
8
+ class SlideInContentByIndex extends pixi_1.Container {
9
+ constructor(createFunction, partialOptions) {
10
+ super();
11
+ this.createFunction = createFunction;
12
+ this.directionProperty = 'x';
13
+ this.contents = new Map();
14
+ this.options = {
15
+ duration: 300,
16
+ animation: new animations_1.AnimationEaseInOutCubic(),
17
+ size: new helpers_lib_1.Vector(500, 500),
18
+ margin: 20,
19
+ direction: 'horizontal',
20
+ ...partialOptions
21
+ };
22
+ let fullSize = this.options.direction === 'horizontal'
23
+ ? new helpers_lib_1.Vector(this.options.size.x + this.options.margin * 2, this.options.size.y)
24
+ : new helpers_lib_1.Vector(this.options.size.x, this.options.size.y + this.options.margin * 2);
25
+ let wrapper = new pixi_1.Container()
26
+ .setPosition(this.options.direction === 'horizontal' ? new helpers_lib_1.Vector(-this.options.margin, 0) : new helpers_lib_1.Vector(0, -this.options.margin), { round: false })
27
+ .displayParent(this)
28
+ .attach(this);
29
+ this.createMask(fullSize, this.options.margin, this.options.direction, wrapper);
30
+ this.directionProperty = this.options.direction === 'horizontal' ? 'x' : 'y';
31
+ this.stateAnimation = new slide_state_animation_1.SlideStateAnimation({
32
+ duration: this.options.duration,
33
+ animation: this.options.animation
34
+ })
35
+ .onStateChange((state, index) => {
36
+ if (state === slide_state_animation_1.SlideStateAnimationState.appear) {
37
+ let container = new pixi_1.Container().displayParent(this).attach(this);
38
+ this.contents.set(index, container);
39
+ this.createFunction({ index, container });
40
+ }
41
+ else {
42
+ let container = this.contents.get(index);
43
+ if (container) {
44
+ container.destroy();
45
+ this.contents.delete(index);
46
+ }
47
+ }
48
+ })
49
+ .onValueChange((value, index) => {
50
+ let container = this.contents.get(index);
51
+ if (container) {
52
+ let vec = { x: 0, y: 0 };
53
+ vec[this.directionProperty] = fullSize[this.directionProperty] * value + this.options.margin;
54
+ container.setPosition(helpers_lib_1.Vector.fromVec2(vec), { round: false });
55
+ }
56
+ })
57
+ .attach(this);
58
+ this.addChildTo = wrapper;
59
+ }
60
+ setIndex(index, options) {
61
+ this.stateAnimation.setIndex(index, options);
62
+ return this;
63
+ }
64
+ createMask(size, padding, direction, container) {
65
+ new pixi_1.ScrollMaskUI(container, new helpers_lib_1.Vector(size.x, size.y), padding, direction).attach(this);
66
+ }
67
+ }
68
+ exports.SlideInContentByIndex = SlideInContentByIndex;
69
+ class SlideInContent extends SlideInContentByIndex {
70
+ constructor(items, createFunction, partialOptions) {
71
+ super((value) => {
72
+ let item = this.indexToItem.get(value.index);
73
+ if (item) {
74
+ createFunction({ index: value.index, item, container: value.container });
75
+ }
76
+ else {
77
+ throw new Error(`SlideInContent: The item with index '${value.index}' is not part of the items list`);
78
+ }
79
+ }, partialOptions);
80
+ this.itemToIndex = new Map();
81
+ this.indexToItem = new Map();
82
+ items.forEach((item, index) => {
83
+ this.itemToIndex.set(item, index);
84
+ this.indexToItem.set(index, item);
85
+ });
86
+ }
87
+ setItem(item, options) {
88
+ let index = this.itemToIndex.get(item);
89
+ if (index !== undefined) {
90
+ this.setIndex(index, options);
91
+ }
92
+ else {
93
+ throw new Error(`SlideInContent: The item '${item}' is not part of the items list`);
94
+ }
95
+ return this;
96
+ }
97
+ }
98
+ exports.SlideInContent = SlideInContent;
99
+ //# sourceMappingURL=slide-in-content.js.map
@@ -0,0 +1,37 @@
1
+ export interface AnimatorAnimation {
2
+ start(): void;
3
+ multiplierFunction(t: number): number;
4
+ }
5
+ export declare class AnimationInterpolationFunctions {
6
+ static boomerangTime(time: number): number;
7
+ static lineer(time: number): number;
8
+ static easeIn(time: number): number;
9
+ static easeOut(time: number): number;
10
+ static easeInOut(time: number): number;
11
+ static easeInOutCubic(t: number): number;
12
+ }
13
+ export declare class AnimationLineer implements AnimatorAnimation {
14
+ start(): void;
15
+ multiplierFunction(t: number): number;
16
+ }
17
+ export declare class AnimationEaseIn implements AnimatorAnimation {
18
+ start(): void;
19
+ multiplierFunction(t: number): number;
20
+ }
21
+ export declare class AnimationEaseOut implements AnimatorAnimation {
22
+ start(): void;
23
+ multiplierFunction(t: number): number;
24
+ }
25
+ export declare class AnimationEaseInOut implements AnimatorAnimation {
26
+ start(): void;
27
+ multiplierFunction(t: number): number;
28
+ }
29
+ export declare class AnimationEaseInOutCubic implements AnimatorAnimation {
30
+ start(): void;
31
+ multiplierFunction(t: number): number;
32
+ }
33
+ export declare class AnimationFlicker implements AnimatorAnimation {
34
+ private timeGrid;
35
+ start(): void;
36
+ multiplierFunction(t: number): number;
37
+ }
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnimationFlicker = exports.AnimationEaseInOutCubic = exports.AnimationEaseInOut = exports.AnimationEaseOut = exports.AnimationEaseIn = exports.AnimationLineer = exports.AnimationInterpolationFunctions = void 0;
4
+ const helpers_lib_1 = require("helpers-lib");
5
+ class AnimationInterpolationFunctions {
6
+ static boomerangTime(time) {
7
+ return 1 - Math.abs(time - 0.5) * 2;
8
+ }
9
+ static lineer(time) {
10
+ return time;
11
+ }
12
+ static easeIn(time) {
13
+ return Math.pow(time, 2);
14
+ }
15
+ static easeOut(time) {
16
+ return 1 - Math.pow(1 - time, 2);
17
+ }
18
+ static easeInOut(time) {
19
+ if (time < 0.5) {
20
+ return 2 * time * time;
21
+ }
22
+ else {
23
+ time -= 1;
24
+ return 1 - 2 * time * time;
25
+ }
26
+ }
27
+ static easeInOutCubic(t) {
28
+ let a = 2 * t - 2;
29
+ return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * Math.pow(a, 2) + 1;
30
+ }
31
+ }
32
+ exports.AnimationInterpolationFunctions = AnimationInterpolationFunctions;
33
+ class AnimationLineer {
34
+ start() { }
35
+ multiplierFunction(t) {
36
+ return AnimationInterpolationFunctions.lineer(t);
37
+ }
38
+ }
39
+ exports.AnimationLineer = AnimationLineer;
40
+ class AnimationEaseIn {
41
+ start() { }
42
+ multiplierFunction(t) {
43
+ return AnimationInterpolationFunctions.easeIn(t);
44
+ }
45
+ }
46
+ exports.AnimationEaseIn = AnimationEaseIn;
47
+ class AnimationEaseOut {
48
+ start() { }
49
+ multiplierFunction(t) {
50
+ return AnimationInterpolationFunctions.easeOut(t);
51
+ }
52
+ }
53
+ exports.AnimationEaseOut = AnimationEaseOut;
54
+ class AnimationEaseInOut {
55
+ start() { }
56
+ multiplierFunction(t) {
57
+ return AnimationInterpolationFunctions.easeInOut(t);
58
+ }
59
+ }
60
+ exports.AnimationEaseInOut = AnimationEaseInOut;
61
+ class AnimationEaseInOutCubic {
62
+ start() { }
63
+ multiplierFunction(t) {
64
+ return AnimationInterpolationFunctions.easeInOutCubic(t);
65
+ }
66
+ }
67
+ exports.AnimationEaseInOutCubic = AnimationEaseInOutCubic;
68
+ class AnimationFlicker {
69
+ start() {
70
+ this.timeGrid = new Array(10).fill(0);
71
+ helpers_lib_1.Random.integerBetween(0, 9);
72
+ this.timeGrid[helpers_lib_1.Random.integerBetween(0, 9)] = Math.random();
73
+ if (Math.random() < 0.3) {
74
+ helpers_lib_1.Random.integerBetween(0, 9);
75
+ this.timeGrid[helpers_lib_1.Random.integerBetween(0, 9)] = Math.random();
76
+ }
77
+ if (Math.random() < 0.2) {
78
+ helpers_lib_1.Random.integerBetween(0, 9);
79
+ this.timeGrid[helpers_lib_1.Random.integerBetween(0, 9)] = Math.random();
80
+ }
81
+ }
82
+ multiplierFunction(t) {
83
+ let index = Math.floor(t * 10);
84
+ return this.timeGrid[index];
85
+ }
86
+ }
87
+ exports.AnimationFlicker = AnimationFlicker;
88
+ //# sourceMappingURL=animations.js.map
@@ -0,0 +1,61 @@
1
+ import { Attachable } from 'actions-lib';
2
+ import { AnimatorAnimation } from './animations';
3
+ export interface AnimationState {
4
+ duration: number;
5
+ elapsedTime: number;
6
+ startingValue: number;
7
+ targetValue: number;
8
+ loop: boolean;
9
+ completeAnimationsHandling: CompleteAnimationsHandlingType;
10
+ }
11
+ export declare enum ReAnimateHandlingType {
12
+ throwError = "throwError",
13
+ ignoreNewAnimation = "ignoreNewAnimation",
14
+ completePrevious = "completePrevious"
15
+ }
16
+ export declare enum CompleteAnimationsHandlingType {
17
+ returnToOriginal = "returnToOriginal",
18
+ stayInCurrentState = "stayInCurrentState",
19
+ jumpToEnd = "jumpToEnd"
20
+ }
21
+ export interface AnimationOptions {
22
+ duration?: number;
23
+ animation?: AnimatorAnimation;
24
+ loop?: boolean;
25
+ reAnimateHandling?: ReAnimateHandlingType;
26
+ completeAnimationsHandling?: CompleteAnimationsHandlingType;
27
+ }
28
+ export declare class Animator<T> extends Attachable {
29
+ private target;
30
+ private static animatedObjects;
31
+ private effectOn;
32
+ private defaultDuration;
33
+ private defaultAnimation;
34
+ private defaultLoop;
35
+ private defaultReAnimateHandling;
36
+ private defaultCompleteAnimationsHandling;
37
+ private onChangeCallback?;
38
+ private onCompleteCallback?;
39
+ private running;
40
+ private propertyStates;
41
+ private updateCycleSubscription;
42
+ private allResolvers;
43
+ get isAnimating(): boolean;
44
+ constructor(target: T, effectOn: string | string[], options?: AnimationOptions);
45
+ onChange(callback: (target: T) => void): Animator<T>;
46
+ onComplete(callback: (key: string, value: number) => void): Animator<T>;
47
+ destroy(): void;
48
+ getAnimationStateOfKey(key: string): AnimationState | undefined;
49
+ pauseAnimations(): void;
50
+ resumeAnimations(): void;
51
+ completeAnimations(completeAnimationsHandling?: CompleteAnimationsHandlingType): void;
52
+ set(values: Record<string, any>): void;
53
+ animate(values: Record<string, any>, options?: AnimationOptions): Promise<void>;
54
+ private completeAnimationOfProperty;
55
+ private checkOngoingAnimations;
56
+ private update;
57
+ private subscribeUpdateCycle;
58
+ private unsubscribeUpdateCycle;
59
+ private checkPropertyValidity;
60
+ private triggerOnChange;
61
+ }
@@ -0,0 +1,315 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Animator = exports.CompleteAnimationsHandlingType = exports.ReAnimateHandlingType = void 0;
4
+ const actions_lib_1 = require("actions-lib");
5
+ const helpers_lib_1 = require("helpers-lib");
6
+ const update_cycle_1 = require("../../game-entities/update-cycle");
7
+ const animations_1 = require("./animations");
8
+ var ReAnimateHandlingType;
9
+ (function (ReAnimateHandlingType) {
10
+ ReAnimateHandlingType["throwError"] = "throwError";
11
+ ReAnimateHandlingType["ignoreNewAnimation"] = "ignoreNewAnimation";
12
+ ReAnimateHandlingType["completePrevious"] = "completePrevious";
13
+ })(ReAnimateHandlingType || (exports.ReAnimateHandlingType = ReAnimateHandlingType = {}));
14
+ var CompleteAnimationsHandlingType;
15
+ (function (CompleteAnimationsHandlingType) {
16
+ CompleteAnimationsHandlingType["returnToOriginal"] = "returnToOriginal";
17
+ CompleteAnimationsHandlingType["stayInCurrentState"] = "stayInCurrentState";
18
+ CompleteAnimationsHandlingType["jumpToEnd"] = "jumpToEnd";
19
+ })(CompleteAnimationsHandlingType || (exports.CompleteAnimationsHandlingType = CompleteAnimationsHandlingType = {}));
20
+ const DEFAULT_DURATION = 200;
21
+ const DEFAULT_ANIMATION = new animations_1.AnimationLineer();
22
+ const DEFAULT_LOOP = false;
23
+ const DEFAULT_RE_ANIMATE_HANDLING = ReAnimateHandlingType.throwError;
24
+ const DEFAULT_COMPLETE_ANIMATIONS_HANDLING = CompleteAnimationsHandlingType.stayInCurrentState;
25
+ class Animator extends actions_lib_1.Attachable {
26
+ static { this.animatedObjects = new Map(); }
27
+ get isAnimating() {
28
+ return this.propertyStates.size > 0;
29
+ }
30
+ constructor(target, effectOn, options = {}) {
31
+ super();
32
+ this.target = target;
33
+ this.effectOn = new Set();
34
+ this.running = true;
35
+ this.propertyStates = new Map();
36
+ this.allResolvers = new Set();
37
+ if (!helpers_lib_1.Comparator.isObject(target)) {
38
+ this.attachToRoot();
39
+ throw new Error(`Animator: target is not an object! Target: "${target}"`);
40
+ }
41
+ this.effectOn = helpers_lib_1.Comparator.isArray(effectOn) ? new Set(effectOn) : new Set([effectOn]);
42
+ let animatedObjectProperties = Animator.animatedObjects.get(target);
43
+ if (!animatedObjectProperties) {
44
+ animatedObjectProperties = new Set();
45
+ Animator.animatedObjects.set(target, animatedObjectProperties);
46
+ }
47
+ this.effectOn.forEach(key => {
48
+ if (animatedObjectProperties.has(key)) {
49
+ this.attachToRoot();
50
+ throw new Error(`Animator: target property is already animated! Target: "${JSON.stringify(target, undefined, 2)}", Property: "${key}"`);
51
+ }
52
+ animatedObjectProperties.add(key);
53
+ });
54
+ this.defaultDuration = options.duration ?? DEFAULT_DURATION;
55
+ this.defaultAnimation = options.animation ?? DEFAULT_ANIMATION;
56
+ this.defaultLoop = options.loop ?? DEFAULT_LOOP;
57
+ this.defaultReAnimateHandling = options.reAnimateHandling ?? DEFAULT_RE_ANIMATE_HANDLING;
58
+ this.defaultCompleteAnimationsHandling = options.completeAnimationsHandling ?? DEFAULT_COMPLETE_ANIMATIONS_HANDLING;
59
+ this.effectOn.forEach(key => {
60
+ if (!helpers_lib_1.Comparator.hasProperty(target, key)) {
61
+ this.attachToRoot();
62
+ throw new Error(`Animator: target does not have property. Target property: "${key}"`);
63
+ }
64
+ else if (!helpers_lib_1.Comparator.isNumber(target[key])) {
65
+ this.attachToRoot();
66
+ throw new Error(`Animator: target property is not a number. Target property: "${key}"`);
67
+ }
68
+ });
69
+ }
70
+ onChange(callback) {
71
+ if (this.onChangeCallback) {
72
+ this.attachToRoot();
73
+ throw new Error('Animator: onChangeCallback is already set!');
74
+ }
75
+ this.onChangeCallback = callback;
76
+ return this;
77
+ }
78
+ onComplete(callback) {
79
+ if (this.onCompleteCallback) {
80
+ this.attachToRoot();
81
+ throw new Error('Animator: onCompleteCallback is already set!');
82
+ }
83
+ this.onCompleteCallback = callback;
84
+ return this;
85
+ }
86
+ destroy() {
87
+ if (!this.destroyed) {
88
+ this.onChangeCallback = undefined;
89
+ this.onCompleteCallback = undefined;
90
+ this.defaultAnimation = DEFAULT_ANIMATION;
91
+ this.propertyStates.clear();
92
+ let animatedObjectProperties = Animator.animatedObjects.get(this.target);
93
+ this.effectOn.forEach(key => animatedObjectProperties.delete(key));
94
+ if (animatedObjectProperties.size <= 0) {
95
+ Animator.animatedObjects.delete(this.target);
96
+ }
97
+ this.target = undefined;
98
+ this.allResolvers.forEach(resolver => resolver());
99
+ this.allResolvers.clear();
100
+ super.destroy();
101
+ }
102
+ }
103
+ getAnimationStateOfKey(key) {
104
+ let propertyState = this.propertyStates.get(key);
105
+ if (propertyState) {
106
+ return {
107
+ duration: propertyState.duration,
108
+ elapsedTime: propertyState.elapsedTime,
109
+ startingValue: propertyState.startingValue,
110
+ targetValue: propertyState.targetValue,
111
+ loop: propertyState.loop,
112
+ completeAnimationsHandling: propertyState.completeAnimationsHandling
113
+ };
114
+ }
115
+ }
116
+ pauseAnimations() {
117
+ this.running = false;
118
+ }
119
+ resumeAnimations() {
120
+ this.running = true;
121
+ }
122
+ completeAnimations(completeAnimationsHandling) {
123
+ let triggerOnChange = false;
124
+ this.propertyStates.forEach((value, key) => {
125
+ let completeAnimationsHandlingFinal = completeAnimationsHandling ?? value.completeAnimationsHandling;
126
+ if (completeAnimationsHandlingFinal === CompleteAnimationsHandlingType.returnToOriginal) {
127
+ if (this.target[key] !== value.startingValue) {
128
+ triggerOnChange = true;
129
+ this.target[key] = value.startingValue;
130
+ }
131
+ }
132
+ else if (completeAnimationsHandlingFinal === CompleteAnimationsHandlingType.jumpToEnd) {
133
+ if (this.target[key] !== value.targetValue) {
134
+ triggerOnChange = true;
135
+ this.target[key] = value.targetValue;
136
+ }
137
+ }
138
+ this.completeAnimationOfProperty(value);
139
+ });
140
+ triggerOnChange && this.triggerOnChange();
141
+ }
142
+ set(values) {
143
+ Object.keys(values).forEach(key => {
144
+ this.checkPropertyValidity(key);
145
+ });
146
+ this.checkOngoingAnimations(values);
147
+ let triggerOnChange = false;
148
+ Object.keys(values).forEach(key => {
149
+ triggerOnChange = triggerOnChange || this.target[key] !== values[key];
150
+ this.target[key] = values[key];
151
+ });
152
+ triggerOnChange && this.triggerOnChange();
153
+ }
154
+ async animate(values, options = {}) {
155
+ if (this.destroyed) {
156
+ return;
157
+ }
158
+ let valueKeys = Object.keys(values);
159
+ valueKeys.forEach(key => {
160
+ this.checkPropertyValidity(key);
161
+ });
162
+ this.checkOngoingAnimations(values, options.reAnimateHandling);
163
+ return new Promise(resolveAll => {
164
+ this.allResolvers.add(resolveAll);
165
+ Promise.all(Object.keys(values).map(key => new Promise(resolve => {
166
+ let duration = options.duration ?? this.defaultDuration;
167
+ let animation = options.animation ?? this.defaultAnimation;
168
+ let loop = options.loop ?? this.defaultLoop;
169
+ let completeAnimationsHandling = options.completeAnimationsHandling ?? this.defaultCompleteAnimationsHandling;
170
+ let startingValue = this.target[key];
171
+ let targetValue = values[key];
172
+ if (startingValue === targetValue && !loop) {
173
+ resolve(undefined);
174
+ return;
175
+ }
176
+ animation.start();
177
+ try {
178
+ this.propertyStates.set(key, {
179
+ propertyName: key,
180
+ duration,
181
+ animation,
182
+ elapsedTime: 0,
183
+ startingValue,
184
+ targetValue,
185
+ loop,
186
+ completeAnimationsHandling,
187
+ completionCallback: () => {
188
+ resolve(undefined);
189
+ }
190
+ });
191
+ }
192
+ catch {
193
+ resolveAll();
194
+ this.allResolvers.delete(resolveAll);
195
+ }
196
+ }))).then(() => {
197
+ resolveAll();
198
+ this.allResolvers.delete(resolveAll);
199
+ });
200
+ this.subscribeUpdateCycle();
201
+ });
202
+ }
203
+ completeAnimationOfProperty(propertyState) {
204
+ let currentState = this.target[propertyState.propertyName];
205
+ this.propertyStates.delete(propertyState.propertyName);
206
+ try {
207
+ propertyState.completionCallback();
208
+ this.onCompleteCallback?.(propertyState.propertyName, currentState);
209
+ }
210
+ catch (e) {
211
+ console.error('Animator:', e);
212
+ }
213
+ }
214
+ checkOngoingAnimations(values, reAnimateHandling) {
215
+ reAnimateHandling = reAnimateHandling ?? this.defaultReAnimateHandling;
216
+ Object.keys(values).forEach(key => {
217
+ let previousState = this.propertyStates.get(key);
218
+ if (previousState) {
219
+ if (reAnimateHandling === ReAnimateHandlingType.throwError) {
220
+ this.attachToRoot();
221
+ throw new Error(`Animator: property is already being animated. Property: "${key}"`);
222
+ }
223
+ else if (reAnimateHandling === ReAnimateHandlingType.ignoreNewAnimation) {
224
+ delete values[key];
225
+ }
226
+ else if (reAnimateHandling === ReAnimateHandlingType.completePrevious) {
227
+ this.completeAnimationOfProperty(previousState);
228
+ }
229
+ }
230
+ });
231
+ }
232
+ update(delta) {
233
+ if (this.running && !this.destroyed) {
234
+ let triggerOnChange = this.propertyStates.size;
235
+ let propertyStatesToComplete = [];
236
+ this.propertyStates.forEach((value, key) => {
237
+ value.elapsedTime += delta;
238
+ let animationIsCompete = value.elapsedTime >= value.duration;
239
+ if (animationIsCompete) {
240
+ if (value.loop) {
241
+ value.animation.start();
242
+ value.elapsedTime -= value.duration;
243
+ animationIsCompete = false;
244
+ }
245
+ else {
246
+ if (value.completeAnimationsHandling === CompleteAnimationsHandlingType.returnToOriginal) {
247
+ this.target[key] = value.startingValue;
248
+ }
249
+ else {
250
+ this.target[key] = value.targetValue;
251
+ }
252
+ // Do not complete inside the forEach loop, completion can start a new animation which will put forEach into infinite loop
253
+ propertyStatesToComplete.push(value);
254
+ }
255
+ }
256
+ if (!animationIsCompete) {
257
+ let multiplier = value.animation.multiplierFunction(value.elapsedTime / value.duration);
258
+ let relativeTarget = value.targetValue - value.startingValue;
259
+ this.target[key] = relativeTarget * multiplier + value.startingValue;
260
+ }
261
+ });
262
+ propertyStatesToComplete.forEach(value => {
263
+ this.completeAnimationOfProperty(value);
264
+ });
265
+ triggerOnChange && this.triggerOnChange();
266
+ if (this.propertyStates.size <= 0) {
267
+ this.unsubscribeUpdateCycle();
268
+ }
269
+ }
270
+ }
271
+ subscribeUpdateCycle() {
272
+ if (!this.updateCycleSubscription && !this.destroyed) {
273
+ this.updateCycleSubscription = update_cycle_1.UpdateCycle.beforeSceneUpdateAction
274
+ .subscribe(update => {
275
+ try {
276
+ this.update(update.delta);
277
+ }
278
+ catch (e) {
279
+ console.error('Animator:', e);
280
+ this.propertyStates.forEach(value => {
281
+ try {
282
+ value.completionCallback();
283
+ }
284
+ catch (err) {
285
+ console.error('Animator:', err);
286
+ }
287
+ });
288
+ this.propertyStates = new Map();
289
+ this.unsubscribeUpdateCycle();
290
+ }
291
+ })
292
+ .attachToRoot();
293
+ }
294
+ }
295
+ unsubscribeUpdateCycle() {
296
+ this.updateCycleSubscription?.destroy();
297
+ this.updateCycleSubscription = undefined;
298
+ }
299
+ checkPropertyValidity(key) {
300
+ if (!this.effectOn.has(key)) {
301
+ this.attachToRoot();
302
+ throw new Error(`Animator: not effected property is tried to update. Key: "${key}", EffectOn: "${this.effectOn}"`);
303
+ }
304
+ }
305
+ triggerOnChange() {
306
+ try {
307
+ this.onChangeCallback?.(this.target);
308
+ }
309
+ catch (e) {
310
+ console.error('Animator:', e);
311
+ }
312
+ }
313
+ }
314
+ exports.Animator = Animator;
315
+ //# sourceMappingURL=animator.js.map
@@ -0,0 +1 @@
1
+ export {};