dacha-game-systems 0.0.2

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 (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -0
  3. package/build/components/active-effects/index.d.ts +12 -0
  4. package/build/components/active-effects/index.js +17 -0
  5. package/build/components/effect/index.d.ts +19 -0
  6. package/build/components/effect/index.js +28 -0
  7. package/build/components/effect/types.d.ts +12 -0
  8. package/build/components/effect/types.js +1 -0
  9. package/build/components/index.d.ts +4 -0
  10. package/build/components/index.js +3 -0
  11. package/build/components/parallax/index.d.ts +12 -0
  12. package/build/components/parallax/index.js +15 -0
  13. package/build/events/index.d.ts +21 -0
  14. package/build/events/index.js +4 -0
  15. package/build/index.d.ts +2 -0
  16. package/build/index.js +2 -0
  17. package/build/schema/components/effect/index.d.ts +2 -0
  18. package/build/schema/components/effect/index.js +73 -0
  19. package/build/schema/components/effect/view.d.ts +3 -0
  20. package/build/schema/components/effect/view.js +37 -0
  21. package/build/schema/components/index.d.ts +2 -0
  22. package/build/schema/components/index.js +2 -0
  23. package/build/schema/components/parallax/index.d.ts +2 -0
  24. package/build/schema/components/parallax/index.js +11 -0
  25. package/build/schema/index.d.ts +22 -0
  26. package/build/schema/index.js +6 -0
  27. package/build/schema/locales/en.d.ts +18 -0
  28. package/build/schema/locales/en.js +18 -0
  29. package/build/schema/systems/effect-system/index.d.ts +3 -0
  30. package/build/schema/systems/effect-system/index.js +3 -0
  31. package/build/schema/systems/index.d.ts +2 -0
  32. package/build/schema/systems/index.js +2 -0
  33. package/build/schema/systems/parallax-system/index.d.ts +2 -0
  34. package/build/schema/systems/parallax-system/index.js +3 -0
  35. package/build/systems/effects-system/effect-applicators/continuous-effect-applicator.d.ts +10 -0
  36. package/build/systems/effects-system/effect-applicators/continuous-effect-applicator.js +23 -0
  37. package/build/systems/effects-system/effect-applicators/delayed-effect-applicator.d.ts +9 -0
  38. package/build/systems/effects-system/effect-applicators/delayed-effect-applicator.js +23 -0
  39. package/build/systems/effects-system/effect-applicators/effect-applicator.d.ts +12 -0
  40. package/build/systems/effects-system/effect-applicators/effect-applicator.js +13 -0
  41. package/build/systems/effects-system/effect-applicators/index.d.ts +12 -0
  42. package/build/systems/effects-system/effect-applicators/index.js +12 -0
  43. package/build/systems/effects-system/effect-applicators/instant-effect-applicator.d.ts +9 -0
  44. package/build/systems/effects-system/effect-applicators/instant-effect-applicator.js +18 -0
  45. package/build/systems/effects-system/effect-applicators/periodical-effect-applicator.d.ts +9 -0
  46. package/build/systems/effects-system/effect-applicators/periodical-effect-applicator.js +29 -0
  47. package/build/systems/effects-system/effect-applicators/time-limited-effect-applicator.d.ts +10 -0
  48. package/build/systems/effects-system/effect-applicators/time-limited-effect-applicator.js +33 -0
  49. package/build/systems/effects-system/effect-script.d.ts +4 -0
  50. package/build/systems/effects-system/effect-script.js +1 -0
  51. package/build/systems/effects-system/effects-system.d.ts +19 -0
  52. package/build/systems/effects-system/effects-system.js +128 -0
  53. package/build/systems/effects-system/index.d.ts +2 -0
  54. package/build/systems/effects-system/index.js +1 -0
  55. package/build/systems/index.d.ts +3 -0
  56. package/build/systems/index.js +2 -0
  57. package/build/systems/parallax-system/index.d.ts +12 -0
  58. package/build/systems/parallax-system/index.js +45 -0
  59. package/build/types/utils.d.ts +1 -0
  60. package/build/types/utils.js +1 -0
  61. package/package.json +79 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Mikhail Remmele
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # Dacha Game Systems
2
+
3
+ ## Description
4
+ Library with common reusable game systems.
@@ -0,0 +1,12 @@
1
+ import { Component } from 'dacha';
2
+ import type { Actor } from 'dacha';
3
+ export interface ActiveEffectsConfig {
4
+ list: Array<string>;
5
+ map: Record<string, Actor>;
6
+ }
7
+ export declare class ActiveEffects extends Component {
8
+ list: Array<string>;
9
+ map: Record<string, Actor>;
10
+ constructor(config?: ActiveEffectsConfig);
11
+ clone(): ActiveEffects;
12
+ }
@@ -0,0 +1,17 @@
1
+ import { Component } from 'dacha';
2
+ export class ActiveEffects extends Component {
3
+ constructor(config) {
4
+ super();
5
+ this.list = config ? [...config.list] : [];
6
+ this.map = config ? {
7
+ ...config.map
8
+ } : {};
9
+ }
10
+ clone() {
11
+ return new ActiveEffects({
12
+ list: this.list,
13
+ map: this.map
14
+ });
15
+ }
16
+ }
17
+ ActiveEffects.componentName = 'ActiveEffects';
@@ -0,0 +1,19 @@
1
+ import { Component } from 'dacha';
2
+ import type { EffectType, DelayedEffectOptions, PeriodicalEffectOptions, TimeLimitedEffectOptions } from './types';
3
+ interface EffectConfig {
4
+ script: string;
5
+ type: EffectType;
6
+ options: Record<string, unknown>;
7
+ applicatorOptions?: DelayedEffectOptions | PeriodicalEffectOptions | TimeLimitedEffectOptions;
8
+ }
9
+ export declare class Effect extends Component {
10
+ script: string;
11
+ type: EffectType;
12
+ options: Record<string, unknown>;
13
+ applicatorOptions?: DelayedEffectOptions | PeriodicalEffectOptions | TimeLimitedEffectOptions;
14
+ isCancelled: boolean;
15
+ removeTimeout: number;
16
+ constructor(config: EffectConfig);
17
+ clone(): Effect;
18
+ }
19
+ export {};
@@ -0,0 +1,28 @@
1
+ import { Component } from 'dacha';
2
+ const REMOVE_TIMEOUT = 5000;
3
+ export class Effect extends Component {
4
+ constructor(config) {
5
+ super();
6
+ this.script = config.script;
7
+ this.type = config.type;
8
+ this.options = {
9
+ ...config.options
10
+ };
11
+ if (config.applicatorOptions) {
12
+ this.applicatorOptions = {
13
+ ...config.applicatorOptions
14
+ };
15
+ }
16
+ this.isCancelled = false;
17
+ this.removeTimeout = REMOVE_TIMEOUT;
18
+ }
19
+ clone() {
20
+ return new Effect({
21
+ script: this.script,
22
+ type: this.type,
23
+ options: this.options,
24
+ applicatorOptions: this.applicatorOptions
25
+ });
26
+ }
27
+ }
28
+ Effect.componentName = 'Effect';
@@ -0,0 +1,12 @@
1
+ export type EffectType = 'instant' | 'delayed' | 'periodical' | 'continuous' | 'timeLimited';
2
+ export interface DelayedEffectOptions {
3
+ timer: number;
4
+ }
5
+ export interface PeriodicalEffectOptions {
6
+ frequency: number;
7
+ duration: number;
8
+ cooldown: number;
9
+ }
10
+ export interface TimeLimitedEffectOptions {
11
+ duration: number;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export { ActiveEffects } from './active-effects';
2
+ export { Effect } from './effect';
3
+ export type { EffectType, DelayedEffectOptions, PeriodicalEffectOptions, TimeLimitedEffectOptions, } from './effect/types';
4
+ export { Parallax } from './parallax';
@@ -0,0 +1,3 @@
1
+ export { ActiveEffects } from './active-effects';
2
+ export { Effect } from './effect';
3
+ export { Parallax } from './parallax';
@@ -0,0 +1,12 @@
1
+ import { Component } from 'dacha';
2
+ interface ParallaxConfig {
3
+ distance: number;
4
+ }
5
+ export declare class Parallax extends Component {
6
+ distance: number;
7
+ startX: number;
8
+ startY: number;
9
+ constructor(config: ParallaxConfig);
10
+ clone(): Parallax;
11
+ }
12
+ export {};
@@ -0,0 +1,15 @@
1
+ import { Component } from 'dacha';
2
+ export class Parallax extends Component {
3
+ constructor(config) {
4
+ super();
5
+ this.distance = config.distance;
6
+ this.startX = 0;
7
+ this.startY = 0;
8
+ }
9
+ clone() {
10
+ return new Parallax({
11
+ distance: this.distance
12
+ });
13
+ }
14
+ }
15
+ Parallax.componentName = 'Parallax';
@@ -0,0 +1,21 @@
1
+ import type { ActorEvent } from 'dacha';
2
+ export declare const AddEffect = "AddEffect";
3
+ export declare const RemoveEffect = "RemoveEffect";
4
+ export declare const ApplyEffect = "ApplyEffect";
5
+ export declare const CancelEffect = "CancelEffect";
6
+ export type AddEffectEvent = ActorEvent<{
7
+ id: string;
8
+ options: Record<string, unknown>;
9
+ }>;
10
+ export type RemoveEffectEvent = ActorEvent<{
11
+ id: string;
12
+ options: Record<string, unknown>;
13
+ }>;
14
+ declare module 'dacha' {
15
+ interface ActorEventMap {
16
+ [AddEffect]: AddEffectEvent;
17
+ [RemoveEffect]: RemoveEffectEvent;
18
+ [ApplyEffect]: ActorEvent;
19
+ [CancelEffect]: ActorEvent;
20
+ }
21
+ }
@@ -0,0 +1,4 @@
1
+ export const AddEffect = 'AddEffect';
2
+ export const RemoveEffect = 'RemoveEffect';
3
+ export const ApplyEffect = 'ApplyEffect';
4
+ export const CancelEffect = 'CancelEffect';
@@ -0,0 +1,2 @@
1
+ export * from './systems';
2
+ export * from './components';
package/build/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './systems';
2
+ export * from './components';
@@ -0,0 +1,2 @@
1
+ import type { WidgetSchema } from 'dacha-workbench';
2
+ export declare const effect: WidgetSchema;
@@ -0,0 +1,73 @@
1
+ import { EffectWidget } from './view';
2
+ export const effect = {
3
+ title: 'components.effect.title',
4
+ fields: [{
5
+ name: 'type',
6
+ title: 'components.effect.type.title',
7
+ type: 'select',
8
+ referenceId: 'types'
9
+ }, {
10
+ name: 'applicatorOptions.timer',
11
+ title: 'components.applicatorOptions.timer.title',
12
+ type: 'number',
13
+ dependency: {
14
+ name: 'type',
15
+ value: 'delayed'
16
+ }
17
+ }, {
18
+ name: 'applicatorOptions.frequency',
19
+ title: 'components.applicatorOptions.frequency.title',
20
+ type: 'number',
21
+ dependency: {
22
+ name: 'type',
23
+ value: 'periodical'
24
+ }
25
+ }, {
26
+ name: 'applicatorOptions.duration',
27
+ title: 'components.applicatorOptions.duration.title',
28
+ type: 'number',
29
+ dependency: {
30
+ name: 'type',
31
+ value: 'periodical|timeLimited'
32
+ }
33
+ }, {
34
+ name: 'applicatorOptions.cooldown',
35
+ title: 'components.applicatorOptions.cooldown.title',
36
+ type: 'number',
37
+ dependency: {
38
+ name: 'type',
39
+ value: 'periodical'
40
+ }
41
+ }, {
42
+ name: 'script',
43
+ title: 'components.effect.script.title',
44
+ type: 'select',
45
+ referenceId: 'scripts'
46
+ }],
47
+ references: {
48
+ types: {
49
+ items: [{
50
+ title: 'components.effect.types.instant.title',
51
+ value: 'instant'
52
+ }, {
53
+ title: 'components.effect.types.delayed.title',
54
+ value: 'delayed'
55
+ }, {
56
+ title: 'components.effect.types.periodical.title',
57
+ value: 'periodical'
58
+ }, {
59
+ title: 'components.effect.types.continuous.title',
60
+ value: 'continuous'
61
+ }, {
62
+ title: 'components.effect.types.timeLimited.title',
63
+ value: 'timeLimited'
64
+ }]
65
+ }
66
+ },
67
+ view: EffectWidget,
68
+ getInitialState: () => ({
69
+ type: 'instant',
70
+ applicatorOptions: {},
71
+ script: ''
72
+ })
73
+ };
@@ -0,0 +1,3 @@
1
+ import type { FC } from 'react';
2
+ import type { WidgetProps } from 'dacha-workbench';
3
+ export declare const EffectWidget: FC<WidgetProps>;
@@ -0,0 +1,37 @@
1
+ import { useMemo } from 'react';
2
+ import { Widget, useExtension, useConfig } from 'dacha-workbench';
3
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
4
+ const SYSTEM_NAME = 'EffectsSystem';
5
+ export const EffectWidget = ({
6
+ fields,
7
+ path,
8
+ references
9
+ }) => {
10
+ const {
11
+ resourcesSchema
12
+ } = useExtension();
13
+ const scriptPath = useMemo(() => path.concat('script'), [path]);
14
+ const scriptName = useConfig(scriptPath);
15
+ const extendedReferences = useMemo(() => ({
16
+ ...references,
17
+ scripts: {
18
+ items: Object.keys(resourcesSchema[SYSTEM_NAME] || {}).map(key => ({
19
+ title: key,
20
+ value: key
21
+ }))
22
+ }
23
+ }), [references]);
24
+ const partFields = resourcesSchema[SYSTEM_NAME]?.[scriptName]?.fields;
25
+ const partReferences = resourcesSchema[SYSTEM_NAME]?.[scriptName]?.references;
26
+ return /*#__PURE__*/_jsxs(_Fragment, {
27
+ children: [/*#__PURE__*/_jsx(Widget, {
28
+ path: path,
29
+ fields: fields,
30
+ references: extendedReferences
31
+ }), partFields && /*#__PURE__*/_jsx(Widget, {
32
+ path: path,
33
+ fields: partFields,
34
+ references: partReferences
35
+ })]
36
+ });
37
+ };
@@ -0,0 +1,2 @@
1
+ export { effect } from './effect';
2
+ export { parallax } from './parallax';
@@ -0,0 +1,2 @@
1
+ export { effect } from './effect';
2
+ export { parallax } from './parallax';
@@ -0,0 +1,2 @@
1
+ import type { WidgetSchema } from 'dacha-workbench';
2
+ export declare const parallax: WidgetSchema;
@@ -0,0 +1,11 @@
1
+ export const parallax = {
2
+ title: 'components.parallax.title',
3
+ fields: [{
4
+ name: 'distance',
5
+ title: 'components.parallax.distance.title',
6
+ type: 'number'
7
+ }],
8
+ getInitialState: () => ({
9
+ distance: 0
10
+ })
11
+ };
@@ -0,0 +1,22 @@
1
+ export { effectsSystem, parallaxSystem } from './systems';
2
+ export { effect, parallax } from './components';
3
+ export declare const locales: {
4
+ en: {
5
+ 'components.effect.title': string;
6
+ 'components.effect.script.title': string;
7
+ 'components.effect.type.title': string;
8
+ 'components.effect.types.instant.title': string;
9
+ 'components.effect.types.delayed.title': string;
10
+ 'components.effect.types.periodical.title': string;
11
+ 'components.effect.types.continuous.title': string;
12
+ 'components.effect.types.timeLimited.title': string;
13
+ 'components.applicatorOptions.timer.title': string;
14
+ 'components.applicatorOptions.frequency.title': string;
15
+ 'components.applicatorOptions.duration.title': string;
16
+ 'components.applicatorOptions.cooldown.title': string;
17
+ 'components.parallax.title': string;
18
+ 'components.parallax.distance.title': string;
19
+ 'systems.effectsSystem.title': string;
20
+ 'systems.parallaxSystem.title': string;
21
+ };
22
+ };
@@ -0,0 +1,6 @@
1
+ import { en } from './locales/en';
2
+ export { effectsSystem, parallaxSystem } from './systems';
3
+ export { effect, parallax } from './components';
4
+ export const locales = {
5
+ en
6
+ };
@@ -0,0 +1,18 @@
1
+ export const en: {
2
+ 'components.effect.title': string;
3
+ 'components.effect.script.title': string;
4
+ 'components.effect.type.title': string;
5
+ 'components.effect.types.instant.title': string;
6
+ 'components.effect.types.delayed.title': string;
7
+ 'components.effect.types.periodical.title': string;
8
+ 'components.effect.types.continuous.title': string;
9
+ 'components.effect.types.timeLimited.title': string;
10
+ 'components.applicatorOptions.timer.title': string;
11
+ 'components.applicatorOptions.frequency.title': string;
12
+ 'components.applicatorOptions.duration.title': string;
13
+ 'components.applicatorOptions.cooldown.title': string;
14
+ 'components.parallax.title': string;
15
+ 'components.parallax.distance.title': string;
16
+ 'systems.effectsSystem.title': string;
17
+ 'systems.parallaxSystem.title': string;
18
+ };
@@ -0,0 +1,18 @@
1
+ export const en = {
2
+ 'components.effect.title': 'Effect',
3
+ 'components.effect.script.title': 'Script',
4
+ 'components.effect.type.title': 'Type',
5
+ 'components.effect.types.instant.title': 'Instant',
6
+ 'components.effect.types.delayed.title': 'Delayed',
7
+ 'components.effect.types.periodical.title': 'Periodical',
8
+ 'components.effect.types.continuous.title': 'Continuous',
9
+ 'components.effect.types.timeLimited.title': 'Time Limited',
10
+ 'components.applicatorOptions.timer.title': 'Timer',
11
+ 'components.applicatorOptions.frequency.title': 'Frequency',
12
+ 'components.applicatorOptions.duration.title': 'Duration',
13
+ 'components.applicatorOptions.cooldown.title': 'Cooldown',
14
+ 'components.parallax.title': 'Parallax',
15
+ 'components.parallax.distance.title': 'Distance',
16
+ 'systems.effectsSystem.title': 'Effects System',
17
+ 'systems.parallaxSystem.title': 'Parallax System'
18
+ };
@@ -0,0 +1,3 @@
1
+ export declare const effectsSystem: {
2
+ title: string;
3
+ };
@@ -0,0 +1,3 @@
1
+ export const effectsSystem = {
2
+ title: 'systems.effectsSystem.title'
3
+ };
@@ -0,0 +1,2 @@
1
+ export { effectsSystem } from './effect-system';
2
+ export { parallaxSystem } from './parallax-system';
@@ -0,0 +1,2 @@
1
+ export { effectsSystem } from './effect-system';
2
+ export { parallaxSystem } from './parallax-system';
@@ -0,0 +1,2 @@
1
+ import type { WidgetSchema } from 'dacha-workbench';
2
+ export declare const parallaxSystem: WidgetSchema;
@@ -0,0 +1,3 @@
1
+ export const parallaxSystem = {
2
+ title: 'systems.parallaxSystem.title'
3
+ };
@@ -0,0 +1,10 @@
1
+ import type { Actor } from 'dacha';
2
+ import type { EffectScript } from '../effect-script';
3
+ import { EffectApplicator } from './effect-applicator';
4
+ export declare class ContinuousEffectApplicator extends EffectApplicator {
5
+ isFinished: boolean;
6
+ isApplied: boolean;
7
+ constructor(script: EffectScript, actor: Actor);
8
+ update(): void;
9
+ cancel(): void;
10
+ }
@@ -0,0 +1,23 @@
1
+ import { EffectApplicator } from './effect-applicator';
2
+ export class ContinuousEffectApplicator extends EffectApplicator {
3
+ constructor(script, actor) {
4
+ super(script, actor);
5
+ this.isApplied = false;
6
+ this.isFinished = false;
7
+ }
8
+ update() {
9
+ if (this.isApplied) {
10
+ return;
11
+ }
12
+ this.script.apply();
13
+ this.handleApply();
14
+ this.isApplied = true;
15
+ }
16
+ cancel() {
17
+ if (!this.isApplied) {
18
+ return;
19
+ }
20
+ this.script.onCancel();
21
+ this.handleCancel();
22
+ }
23
+ }
@@ -0,0 +1,9 @@
1
+ import type { Actor } from 'dacha';
2
+ import type { EffectScript } from '../effect-script';
3
+ import { EffectApplicator } from './effect-applicator';
4
+ export declare class DelayedEffectApplicator extends EffectApplicator {
5
+ isFinished: boolean;
6
+ constructor(script: EffectScript, actor: Actor);
7
+ update(deltaTime: number): void;
8
+ cancel(): void;
9
+ }
@@ -0,0 +1,23 @@
1
+ import { Effect } from '../../../components';
2
+ import { EffectApplicator } from './effect-applicator';
3
+ export class DelayedEffectApplicator extends EffectApplicator {
4
+ constructor(script, actor) {
5
+ super(script, actor);
6
+ this.isFinished = false;
7
+ }
8
+ update(deltaTime) {
9
+ if (this.isFinished) {
10
+ return;
11
+ }
12
+ const applicatorOptions = this.actor.getComponent(Effect).applicatorOptions;
13
+ applicatorOptions.timer -= deltaTime;
14
+ if (applicatorOptions.timer <= 0) {
15
+ this.script.apply();
16
+ this.handleApply();
17
+ this.isFinished = true;
18
+ }
19
+ }
20
+ cancel() {
21
+ this.handleCancel();
22
+ }
23
+ }
@@ -0,0 +1,12 @@
1
+ import type { Actor } from 'dacha';
2
+ import type { EffectScript } from '../effect-script';
3
+ export declare abstract class EffectApplicator {
4
+ protected script: EffectScript;
5
+ protected actor: Actor;
6
+ abstract isFinished: boolean;
7
+ constructor(script: EffectScript, actor: Actor);
8
+ protected handleCancel(): void;
9
+ protected handleApply(): void;
10
+ abstract update(deltaTime: number): void;
11
+ abstract cancel(): void;
12
+ }
@@ -0,0 +1,13 @@
1
+ import * as EventType from '../../../events';
2
+ export class EffectApplicator {
3
+ constructor(script, actor) {
4
+ this.script = script;
5
+ this.actor = actor;
6
+ }
7
+ handleCancel() {
8
+ this.actor.dispatchEvent(EventType.CancelEffect);
9
+ }
10
+ handleApply() {
11
+ this.actor.dispatchEvent(EventType.ApplyEffect);
12
+ }
13
+ }
@@ -0,0 +1,12 @@
1
+ import { InstantEffectApplicator } from './instant-effect-applicator';
2
+ import { DelayedEffectApplicator } from './delayed-effect-applicator';
3
+ import { PeriodicalEffectApplicator } from './periodical-effect-applicator';
4
+ import { ContinuousEffectApplicator } from './continuous-effect-applicator';
5
+ import { TimeLimitedEffectApplicator } from './time-limited-effect-applicator';
6
+ export declare const effectApplicators: {
7
+ instant: typeof InstantEffectApplicator;
8
+ delayed: typeof DelayedEffectApplicator;
9
+ periodical: typeof PeriodicalEffectApplicator;
10
+ continuous: typeof ContinuousEffectApplicator;
11
+ timeLimited: typeof TimeLimitedEffectApplicator;
12
+ };
@@ -0,0 +1,12 @@
1
+ import { InstantEffectApplicator } from './instant-effect-applicator';
2
+ import { DelayedEffectApplicator } from './delayed-effect-applicator';
3
+ import { PeriodicalEffectApplicator } from './periodical-effect-applicator';
4
+ import { ContinuousEffectApplicator } from './continuous-effect-applicator';
5
+ import { TimeLimitedEffectApplicator } from './time-limited-effect-applicator';
6
+ export const effectApplicators = {
7
+ instant: InstantEffectApplicator,
8
+ delayed: DelayedEffectApplicator,
9
+ periodical: PeriodicalEffectApplicator,
10
+ continuous: ContinuousEffectApplicator,
11
+ timeLimited: TimeLimitedEffectApplicator
12
+ };
@@ -0,0 +1,9 @@
1
+ import type { Actor } from 'dacha';
2
+ import type { EffectScript } from '../effect-script';
3
+ import { EffectApplicator } from './effect-applicator';
4
+ export declare class InstantEffectApplicator extends EffectApplicator {
5
+ isFinished: boolean;
6
+ constructor(script: EffectScript, actor: Actor);
7
+ update(): void;
8
+ cancel(): void;
9
+ }
@@ -0,0 +1,18 @@
1
+ import { EffectApplicator } from './effect-applicator';
2
+ export class InstantEffectApplicator extends EffectApplicator {
3
+ constructor(script, actor) {
4
+ super(script, actor);
5
+ this.isFinished = false;
6
+ }
7
+ update() {
8
+ if (this.isFinished) {
9
+ return;
10
+ }
11
+ this.script.apply();
12
+ this.handleApply();
13
+ this.isFinished = true;
14
+ }
15
+ cancel() {
16
+ this.handleCancel();
17
+ }
18
+ }
@@ -0,0 +1,9 @@
1
+ import type { Actor } from 'dacha';
2
+ import type { EffectScript } from '../effect-script';
3
+ import { EffectApplicator } from './effect-applicator';
4
+ export declare class PeriodicalEffectApplicator extends EffectApplicator {
5
+ isFinished: boolean;
6
+ constructor(script: EffectScript, actor: Actor);
7
+ update(deltaTime: number): void;
8
+ cancel(): void;
9
+ }
@@ -0,0 +1,29 @@
1
+ import { Effect } from '../../../components';
2
+ import { EffectApplicator } from './effect-applicator';
3
+ export class PeriodicalEffectApplicator extends EffectApplicator {
4
+ constructor(script, actor) {
5
+ super(script, actor);
6
+ this.isFinished = false;
7
+ }
8
+ update(deltaTime) {
9
+ if (this.isFinished) {
10
+ return;
11
+ }
12
+ const applicatorOptions = this.actor.getComponent(Effect).applicatorOptions;
13
+ applicatorOptions.cooldown -= deltaTime;
14
+ while (applicatorOptions.cooldown <= 0) {
15
+ this.script.apply();
16
+ this.handleApply();
17
+ applicatorOptions.cooldown += applicatorOptions.frequency;
18
+ }
19
+ if (applicatorOptions.duration) {
20
+ applicatorOptions.duration -= deltaTime;
21
+ if (applicatorOptions.duration <= 0) {
22
+ this.isFinished = true;
23
+ }
24
+ }
25
+ }
26
+ cancel() {
27
+ this.handleCancel();
28
+ }
29
+ }
@@ -0,0 +1,10 @@
1
+ import type { Actor } from 'dacha';
2
+ import type { EffectScript } from '../effect-script';
3
+ import { EffectApplicator } from './effect-applicator';
4
+ export declare class TimeLimitedEffectApplicator extends EffectApplicator {
5
+ isFinished: boolean;
6
+ isApplied: boolean;
7
+ constructor(script: EffectScript, actor: Actor);
8
+ update(deltaTime: number): void;
9
+ cancel(): void;
10
+ }
@@ -0,0 +1,33 @@
1
+ import { Effect } from '../../../components';
2
+ import { EffectApplicator } from './effect-applicator';
3
+ export class TimeLimitedEffectApplicator extends EffectApplicator {
4
+ constructor(script, actor) {
5
+ super(script, actor);
6
+ this.isApplied = false;
7
+ this.isFinished = false;
8
+ }
9
+ update(deltaTime) {
10
+ if (this.isFinished) {
11
+ return;
12
+ }
13
+ const applicatorOptions = this.actor.getComponent(Effect).applicatorOptions;
14
+ applicatorOptions.duration -= deltaTime;
15
+ if (applicatorOptions.duration <= 0) {
16
+ this.isFinished = true;
17
+ return;
18
+ }
19
+ if (this.isApplied) {
20
+ return;
21
+ }
22
+ this.script.apply();
23
+ this.handleApply();
24
+ this.isApplied = true;
25
+ }
26
+ cancel() {
27
+ if (!this.isApplied) {
28
+ return;
29
+ }
30
+ this.script.onCancel();
31
+ this.handleCancel();
32
+ }
33
+ }
@@ -0,0 +1,4 @@
1
+ export interface EffectScript {
2
+ apply(): void;
3
+ onCancel(): void;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import { System } from 'dacha';
2
+ import type { SystemOptions, UpdateOptions } from 'dacha';
3
+ export declare class EffectsSystem extends System {
4
+ private scene;
5
+ private activeEffectsCollection;
6
+ private effectsCollection;
7
+ private actorSpawner;
8
+ private scripts;
9
+ private applicatorsMap;
10
+ constructor(options: SystemOptions);
11
+ mount(): void;
12
+ unmount(): void;
13
+ private handleActorRemove;
14
+ private handleAddEffect;
15
+ private handleRemoveEffect;
16
+ private cancelEffect;
17
+ private addEffect;
18
+ update(options: UpdateOptions): void;
19
+ }
@@ -0,0 +1,128 @@
1
+ import { ActorCollection, System } from 'dacha';
2
+ import { RemoveActor } from 'dacha/events';
3
+ import * as EventType from '../../events';
4
+ import { ActiveEffects, Effect } from '../../components';
5
+ import { effectApplicators } from './effect-applicators';
6
+ export class EffectsSystem extends System {
7
+ constructor(options) {
8
+ super();
9
+ const {
10
+ actorSpawner,
11
+ resources = {},
12
+ scene
13
+ } = options;
14
+ this.scene = scene;
15
+ this.activeEffectsCollection = new ActorCollection(scene, {
16
+ components: [ActiveEffects]
17
+ });
18
+ this.effectsCollection = new ActorCollection(scene, {
19
+ components: [Effect]
20
+ });
21
+ this.actorSpawner = actorSpawner;
22
+ this.scripts = resources;
23
+ this.applicatorsMap = {};
24
+ }
25
+ mount() {
26
+ this.scene.addEventListener(EventType.AddEffect, this.handleAddEffect);
27
+ this.scene.addEventListener(EventType.RemoveEffect, this.handleRemoveEffect);
28
+ this.activeEffectsCollection.addEventListener(RemoveActor, this.handleActorRemove);
29
+ }
30
+ unmount() {
31
+ this.scene.removeEventListener(EventType.AddEffect, this.handleAddEffect);
32
+ this.scene.removeEventListener(EventType.RemoveEffect, this.handleRemoveEffect);
33
+ this.activeEffectsCollection.removeEventListener(RemoveActor, this.handleActorRemove);
34
+ }
35
+ handleActorRemove = event => {
36
+ const {
37
+ actor
38
+ } = event;
39
+ Object.keys(this.applicatorsMap[actor.id])?.forEach(name => {
40
+ delete this.applicatorsMap[actor.id][name];
41
+ });
42
+ };
43
+ handleAddEffect = event => {
44
+ const {
45
+ id,
46
+ options,
47
+ target
48
+ } = event;
49
+ this.cancelEffect(id, target);
50
+ this.addEffect(id, target, options);
51
+ };
52
+ handleRemoveEffect = event => {
53
+ const {
54
+ id,
55
+ target
56
+ } = event;
57
+ this.cancelEffect(id, target);
58
+ };
59
+ cancelEffect(id, actor) {
60
+ if (!this.applicatorsMap[actor.id] || !this.applicatorsMap[actor.id][id]) {
61
+ return;
62
+ }
63
+ this.applicatorsMap[actor.id][id].cancel();
64
+ delete this.applicatorsMap[actor.id][id];
65
+ const activeEffects = actor.getComponent(ActiveEffects);
66
+ const effectActor = activeEffects.map[id];
67
+ delete activeEffects.map[id];
68
+ activeEffects.list = activeEffects.list.filter(activeEffectId => id !== activeEffectId);
69
+ const effect = effectActor.getComponent(Effect);
70
+ effect.isCancelled = true;
71
+ }
72
+ addEffect(id, actor, options) {
73
+ const effectActor = this.actorSpawner.spawn(id);
74
+ actor.appendChild(effectActor);
75
+ const {
76
+ script,
77
+ type,
78
+ options: constOptions
79
+ } = effectActor.getComponent(Effect);
80
+ if (!actor.getComponent(ActiveEffects)) {
81
+ actor.setComponent(new ActiveEffects());
82
+ }
83
+ const activeEffects = actor.getComponent(ActiveEffects);
84
+ activeEffects.list.push(id);
85
+ activeEffects.map[id] = effectActor;
86
+ const EffectScript = this.scripts[script];
87
+ const EffectApplicator = effectApplicators[type];
88
+ const effectApplicator = new EffectApplicator(new EffectScript(actor, {
89
+ ...constOptions,
90
+ ...options
91
+ }), effectActor);
92
+ this.applicatorsMap[actor.id] ??= {};
93
+ this.applicatorsMap[actor.id][id] = effectApplicator;
94
+ }
95
+ update(options) {
96
+ const {
97
+ deltaTime
98
+ } = options;
99
+ this.effectsCollection.forEach(actor => {
100
+ const effect = actor.getComponent(Effect);
101
+ if (!effect.isCancelled) {
102
+ return;
103
+ }
104
+ effect.removeTimeout -= deltaTime;
105
+ if (effect.removeTimeout <= 0) {
106
+ actor.remove();
107
+ }
108
+ });
109
+ this.activeEffectsCollection.forEach(actor => {
110
+ const activeEffects = actor.getComponent(ActiveEffects);
111
+ activeEffects.list = activeEffects.list.filter(id => {
112
+ const effectApplicator = this.applicatorsMap[actor.id][id];
113
+ const effectActor = activeEffects.map[id];
114
+ const effect = effectActor.getComponent(Effect);
115
+ effectApplicator.update(deltaTime);
116
+ if (effectApplicator.isFinished) {
117
+ effectApplicator.cancel();
118
+ delete this.applicatorsMap[actor.id][id];
119
+ delete activeEffects.map[id];
120
+ effect.isCancelled = true;
121
+ return false;
122
+ }
123
+ return true;
124
+ });
125
+ });
126
+ }
127
+ }
128
+ EffectsSystem.systemName = 'EffectsSystem';
@@ -0,0 +1,2 @@
1
+ export { EffectsSystem } from './effects-system';
2
+ export type { EffectScript } from './effect-script';
@@ -0,0 +1 @@
1
+ export { EffectsSystem } from './effects-system';
@@ -0,0 +1,3 @@
1
+ export { EffectsSystem } from './effects-system';
2
+ export type { EffectScript } from './effects-system';
3
+ export { ParallaxSystem } from './parallax-system';
@@ -0,0 +1,2 @@
1
+ export { EffectsSystem } from './effects-system';
2
+ export { ParallaxSystem } from './parallax-system';
@@ -0,0 +1,12 @@
1
+ import { System } from 'dacha';
2
+ import type { SystemOptions } from 'dacha';
3
+ export declare class ParallaxSystem extends System {
4
+ private actorCollection;
5
+ private cameraService;
6
+ constructor(options: SystemOptions);
7
+ mount(): void;
8
+ unmount(): void;
9
+ private handleAddActor;
10
+ private setStartPosition;
11
+ update(): void;
12
+ }
@@ -0,0 +1,45 @@
1
+ import { ActorCollection, System, CameraService, Transform } from 'dacha';
2
+ import { AddActor } from 'dacha/events';
3
+ import { Parallax } from '../../components';
4
+ export class ParallaxSystem extends System {
5
+ constructor(options) {
6
+ super();
7
+ this.actorCollection = new ActorCollection(options.scene, {
8
+ components: [Transform, Parallax]
9
+ });
10
+ this.cameraService = options.scene.getService(CameraService);
11
+ }
12
+ mount() {
13
+ this.actorCollection.forEach(actor => this.setStartPosition(actor));
14
+ this.actorCollection.addEventListener(AddActor, this.handleAddActor);
15
+ }
16
+ unmount() {
17
+ this.actorCollection.removeEventListener(AddActor, this.handleAddActor);
18
+ }
19
+ handleAddActor = event => {
20
+ this.setStartPosition(event.actor);
21
+ };
22
+ setStartPosition(actor) {
23
+ const transform = actor.getComponent(Transform);
24
+ const parallax = actor.getComponent(Parallax);
25
+ parallax.startX = transform.offsetX;
26
+ parallax.startY = transform.offsetY;
27
+ }
28
+ update() {
29
+ const currentCamera = this.cameraService.getCurrentCamera();
30
+ if (!currentCamera) {
31
+ return;
32
+ }
33
+ const {
34
+ offsetX: cameraOffsetX,
35
+ offsetY: cameraOffsetY
36
+ } = currentCamera.getComponent(Transform);
37
+ this.actorCollection.forEach(actor => {
38
+ const transform = actor.getComponent(Transform);
39
+ const parallax = actor.getComponent(Parallax);
40
+ transform.offsetX = parallax.startX + (cameraOffsetX - parallax.startX) * parallax.distance;
41
+ transform.offsetY = parallax.startY + (cameraOffsetY - parallax.startY) * parallax.distance;
42
+ });
43
+ }
44
+ }
45
+ ParallaxSystem.systemName = 'ParallaxSystem';
@@ -0,0 +1 @@
1
+ export type Constructor<T> = new (...args: any[]) => T;
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "dacha-game-systems",
3
+ "version": "0.0.2",
4
+ "exports": {
5
+ ".": {
6
+ "require": "./build/index.js",
7
+ "import": "./build/index.js",
8
+ "types": "./build/index.d.ts"
9
+ },
10
+ "./events": {
11
+ "require": "./build/events/index.js",
12
+ "import": "./build/events/index.js",
13
+ "types": "./build/events/index.d.ts"
14
+ },
15
+ "./schema": {
16
+ "require": "./build/schema/index.js",
17
+ "import": "./build/schema/index.js",
18
+ "types": "./build/schema/index.d.ts"
19
+ }
20
+ },
21
+ "devDependencies": {
22
+ "@babel/cli": "7.16.8",
23
+ "@babel/core": "7.16.0",
24
+ "@babel/eslint-parser": "7.16.5",
25
+ "@babel/preset-typescript": "7.23.3",
26
+ "@babel/preset-react": "7.24.1",
27
+ "@types/jest": "29.5.11",
28
+ "@types/three": "0.150.2",
29
+ "@types/react": "^18.2.73",
30
+ "@types/react-dom": "^18.2.23",
31
+ "eslint": "8.57.0",
32
+ "eslint-config-airbnb": "19.0.4",
33
+ "eslint-config-airbnb-typescript": "18.0.0",
34
+ "eslint-import-resolver-typescript": "3.6.1",
35
+ "eslint-plugin-import": "2.29.1",
36
+ "husky": "7.0.4",
37
+ "jest": "29.7.0",
38
+ "ts-jest": "29.1.1",
39
+ "typescript": "5.3.3",
40
+ "dacha": "0.13.0",
41
+ "dacha-workbench": "0.0.22",
42
+ "react": "18.1.0",
43
+ "react-dom": "18.1.0"
44
+ },
45
+ "peerDependencies": {
46
+ "dacha": ">=0.13.0",
47
+ "dacha-workbench": ">=0.0.22",
48
+ "react": ">=18.1.0",
49
+ "react-dom": ">=18.1.0"
50
+ },
51
+ "sideEffects": false,
52
+ "files": [
53
+ "build",
54
+ "package.json",
55
+ "README.md",
56
+ "LICENSE"
57
+ ],
58
+ "scripts": {
59
+ "build": "npm run build:clean && npm run build:esm && npm run build:types",
60
+ "build:clean": "rm -rf build",
61
+ "build:esm": "npx babel src --out-dir build --extensions \".ts,.js,.tsx,.jsx\" --ignore \"src/**/*.test.ts,src/__mocks__\"",
62
+ "build:types": "tsc --project tsconfig.types.json",
63
+ "lint": "eslint .",
64
+ "test": "jest",
65
+ "prepare": "husky install",
66
+ "prepublishOnly": "npm run build"
67
+ },
68
+ "description": "Dacha is a JavaScript game engine for building games in browser.",
69
+ "repository": {
70
+ "type": "git",
71
+ "url": "git+https://github.com/michailRemmele/dacha-game-systems.git"
72
+ },
73
+ "author": "Mikhail Remmele",
74
+ "license": "MIT",
75
+ "bugs": {
76
+ "url": "https://github.com/michailRemmele/dacha-game-systems/issues"
77
+ },
78
+ "homepage": "https://github.com/michailRemmele/dacha-game-systems#readme"
79
+ }