@antha/entity-2d 0.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.
- package/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +11 -0
- package/dist/antha-entity.mod.d.ts +33 -0
- package/dist/antha-entity.mod.js +60 -0
- package/dist/entity-suite.d.ts +223 -0
- package/dist/entity-suite.js +77 -0
- package/dist/entity.d.ts +436 -0
- package/dist/entity.js +464 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/package.json +71 -0
package/LICENSE-CC0
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
CC0 1.0 Universal
|
|
2
|
+
|
|
3
|
+
Creative Commons Legal Code
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 electrovir
|
|
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,11 @@
|
|
|
1
|
+
# @antha/entity-2d
|
|
2
|
+
|
|
3
|
+
A pre-built [Antha](https://www.npmjs.com/package/antha) mod for rendering and handling collision detection for 2D entities. This requires a mod that has setup PixiJS in a manner compatible with [@antha/graphics-2d](https://npmjs.com/package/@antha/graphics-2d)
|
|
4
|
+
|
|
5
|
+
Demo: https://electrovir.github.io/antha/demo/basic-entities
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i @antha/entity-2d
|
|
11
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type AnthaAssetModState } from '@antha/asset';
|
|
2
|
+
import { type AnthaGraphics2dModState } from '@antha/graphics-2d';
|
|
3
|
+
import { type AnyObject, type PartialWithUndefined } from '@augment-vir/common';
|
|
4
|
+
import { type EntityStore2d, type EntityStore2dConstructorParams } from './entity.js';
|
|
5
|
+
/**
|
|
6
|
+
* State for {@link createAnthaEntityMod2d}.
|
|
7
|
+
*
|
|
8
|
+
* @category Internal
|
|
9
|
+
*/
|
|
10
|
+
export type AnthaEntity2dModState<State extends AnyObject = any> = {
|
|
11
|
+
entityStore: EntityStore2d<Partial<AnthaEntity2dModState<State>>>;
|
|
12
|
+
/** If `true`, hit boxes are visually rendered for debugging purposes. */
|
|
13
|
+
debugHitboxes: boolean;
|
|
14
|
+
} & State & AnthaGraphics2dModState & AnthaAssetModState;
|
|
15
|
+
/**
|
|
16
|
+
* Options for {@link createAnthaEntityMod2d}.
|
|
17
|
+
*
|
|
18
|
+
* @category Internal
|
|
19
|
+
*/
|
|
20
|
+
export type AnthaEntity2dModOptions = PartialWithUndefined<EntityStore2dConstructorParams & {
|
|
21
|
+
debug: boolean;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* A mod for rendering entities and handling collisions between them.
|
|
25
|
+
*
|
|
26
|
+
* @category Pre-built Mods
|
|
27
|
+
*/
|
|
28
|
+
export declare function createAnthaEntityMod2d<ExtraState extends AnyObject>(options?: Readonly<AnthaEntity2dModOptions>): {
|
|
29
|
+
defineEntity: import("./entity-suite.js").DefineViewEntity2d<AnthaEntity2dModState<ExtraState>>;
|
|
30
|
+
defineLogicEntity: import("./entity-suite.js").DefineLogicEntity2d<AnthaEntity2dModState<ExtraState>>;
|
|
31
|
+
entityKeys: Set<string>;
|
|
32
|
+
mod: import("@antha/engine").AnthaMod<any>;
|
|
33
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { anthaAssetModName, AssetLoader } from '@antha/asset';
|
|
2
|
+
import { defineAnthaMod, SkipExecution } from '@antha/engine';
|
|
3
|
+
import { mergeDefinedProperties, } from '@augment-vir/common';
|
|
4
|
+
import { html } from 'element-vir';
|
|
5
|
+
import { defineEntitySuite2d } from './entity-suite.js';
|
|
6
|
+
/**
|
|
7
|
+
* A mod for rendering entities and handling collisions between them.
|
|
8
|
+
*
|
|
9
|
+
* @category Pre-built Mods
|
|
10
|
+
*/
|
|
11
|
+
export function createAnthaEntityMod2d(options = {}) {
|
|
12
|
+
const { EntityStore, ...entitySuite } = defineEntitySuite2d();
|
|
13
|
+
const mod = defineAnthaMod({
|
|
14
|
+
modName: 'antha-entity-2d',
|
|
15
|
+
initState: {
|
|
16
|
+
debugHitboxes: !!options.debug,
|
|
17
|
+
},
|
|
18
|
+
cleanup({ state }) {
|
|
19
|
+
state.entityStore?.destroy();
|
|
20
|
+
},
|
|
21
|
+
async execute({ state, engine, msSinceLastExecute }) {
|
|
22
|
+
/**
|
|
23
|
+
* If we don't have a mod that is expected to create the asset loader, then we create
|
|
24
|
+
* one ourself.
|
|
25
|
+
*/
|
|
26
|
+
if (!state.assetLoader &&
|
|
27
|
+
!engine.currentMods.some((mod) => mod.modName === anthaAssetModName)) {
|
|
28
|
+
state.assetLoader = new AssetLoader();
|
|
29
|
+
}
|
|
30
|
+
const pixiApplication = state.pixi?.pixiApplication;
|
|
31
|
+
if (!pixiApplication) {
|
|
32
|
+
return SkipExecution;
|
|
33
|
+
}
|
|
34
|
+
if (state.entityStore) {
|
|
35
|
+
await state.entityStore.updateAllEntities({
|
|
36
|
+
msSinceLastUpdate: msSinceLastExecute,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
else if (state.assetLoader) {
|
|
40
|
+
state.entityStore = new EntityStore(mergeDefinedProperties({
|
|
41
|
+
pixi: pixiApplication,
|
|
42
|
+
state,
|
|
43
|
+
assetLoader: state.assetLoader,
|
|
44
|
+
}, options));
|
|
45
|
+
}
|
|
46
|
+
if (state.debugHitboxes) {
|
|
47
|
+
return html `
|
|
48
|
+
<canvas class="hitbox-debug-canvas"></canvas>
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
mod,
|
|
58
|
+
...entitySuite,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { type AnyObject } from '@augment-vir/common';
|
|
2
|
+
import { type Shape } from 'object-shape-tester';
|
|
3
|
+
import { type Constructor, type IsAny } from 'type-fest';
|
|
4
|
+
import { BaseEntity2d, type BaseEntityAssetDefinitions, type Entity2dConstructorParams, EntityStore2d, type EntityStore2dConstructorParams, type MappedEntityAssets, type ParamsMap, type ReverseParamsMap, ViewEntity2d } from './entity.js';
|
|
5
|
+
/**
|
|
6
|
+
* Params for both {@link EntitySuite2d.defineEntity} and {@link EntitySuite2d.defineLogicEntity}.
|
|
7
|
+
*
|
|
8
|
+
* @category Internal
|
|
9
|
+
*/
|
|
10
|
+
export type DefineEntity2dArgs<ParamsShape extends Shape<AnyObject> | undefined, EntityAssets extends BaseEntityAssetDefinitions | undefined> = {
|
|
11
|
+
/**
|
|
12
|
+
* This key is used for deserialization of entities to track which class needs to be
|
|
13
|
+
* constructed. Do not use duplicate key strings across multiple entity classes.
|
|
14
|
+
*/
|
|
15
|
+
key: string;
|
|
16
|
+
/**
|
|
17
|
+
* This should contain all parameters necessary to reconstruct this entity from scratch so it
|
|
18
|
+
* can be serialized, sent across the network in JSON format, then reconstructed on another
|
|
19
|
+
* device (for multiplayer support).
|
|
20
|
+
*
|
|
21
|
+
* Make sure to include {@link entityPositionParamsShape} as part of the shape if you want to
|
|
22
|
+
* include entity position parameters.
|
|
23
|
+
*/
|
|
24
|
+
paramsShape?: ParamsShape;
|
|
25
|
+
/**
|
|
26
|
+
* A mapping of the entity's params object (defined by {@link DefineEntity2dArgs.paramsShape})
|
|
27
|
+
* keys to hitbox and/or view properties.
|
|
28
|
+
*
|
|
29
|
+
* Use `standardParamsMap` to automatically map the params `x` and `y` in `paramsShape` to both
|
|
30
|
+
* the entity's hitbox x/y and the entity's view x/y.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const customMapping = {
|
|
36
|
+
* paramsShape: defineShape({
|
|
37
|
+
* left: -1,
|
|
38
|
+
* top: -1,
|
|
39
|
+
* }),
|
|
40
|
+
* paramsMap: {
|
|
41
|
+
* hitbox: {
|
|
42
|
+
* x: 'left', // maps `left` from `paramsShape` to the entity's hitbox.x
|
|
43
|
+
* y: 'top', // maps `top` from `paramsShape` to the entity's hitbox.y
|
|
44
|
+
* },
|
|
45
|
+
* view: {
|
|
46
|
+
* x: 'left', // maps `left` from `paramsShape` to the entity's view.x
|
|
47
|
+
* y: 'top', // maps `top` from `paramsShape` to the entity's view.y
|
|
48
|
+
* },
|
|
49
|
+
* },
|
|
50
|
+
* };
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* const standardMapping = {
|
|
57
|
+
* paramsShape: defineShape({
|
|
58
|
+
* left: -1,
|
|
59
|
+
* top: -1,
|
|
60
|
+
* }),
|
|
61
|
+
* paramsMap: standardParamsMap, // use the standard x/y mapping
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* const undefinedMapping = {
|
|
69
|
+
* paramsShape: defineShape({
|
|
70
|
+
* left: -1,
|
|
71
|
+
* top: -1,
|
|
72
|
+
* }),
|
|
73
|
+
* paramsMap: undefined, // no mapping at all
|
|
74
|
+
* };
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
*
|
|
79
|
+
* ```ts
|
|
80
|
+
* const omittedMapping = {
|
|
81
|
+
* paramsShape: defineShape({
|
|
82
|
+
* left: -1,
|
|
83
|
+
* top: -1,
|
|
84
|
+
* }),
|
|
85
|
+
* // no mapping at all
|
|
86
|
+
* };
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @default undefined // no mapping
|
|
90
|
+
*/
|
|
91
|
+
paramsMap?: ParamsMap<NoInfer<ParamsShape> extends Shape<AnyObject> ? NoInfer<ParamsShape>['runtimeType'] : undefined> | undefined;
|
|
92
|
+
assets?: EntityAssets;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Static members of both view and logic entity constructors.
|
|
96
|
+
*
|
|
97
|
+
* @category Internal
|
|
98
|
+
*/
|
|
99
|
+
export type StaticEntity2dParts<State extends AnyObject = any, ParamsShape extends Shape<AnyObject> | undefined = any, EntityAssets extends BaseEntityAssetDefinitions | undefined = any> = {
|
|
100
|
+
/**
|
|
101
|
+
* This key is used for deserialization of entities to track which class needs to be
|
|
102
|
+
* constructed. You cannot have duplicate keys loaded at the same time.
|
|
103
|
+
*
|
|
104
|
+
* This is used instead of inferring the entity key from the class name so that you can still
|
|
105
|
+
* minify your class names without making debugging nigh impossible (you'll still know which
|
|
106
|
+
* entities are being serialized and deserialized even if your class names are minified).
|
|
107
|
+
*/
|
|
108
|
+
entityKey: string;
|
|
109
|
+
/** Shape definition of this entity's parameters. */
|
|
110
|
+
paramsShape: ParamsShape;
|
|
111
|
+
/**
|
|
112
|
+
* Defines which properties from {@link BaseEntity2d.params} will be mapped to hitbox and/or view
|
|
113
|
+
* properties.
|
|
114
|
+
*/
|
|
115
|
+
paramsMap: IsAny<ParamsShape> extends true ? any : ParamsMap<ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>;
|
|
116
|
+
/** Parses the serialized params generated by {@link BaseEntity2d.serialize}. */
|
|
117
|
+
deserialize(serialized: string | undefined): AnyObject | undefined;
|
|
118
|
+
assets: MappedEntityAssets<EntityAssets>;
|
|
119
|
+
ConstructorArgsType: Entity2dConstructorParams<State, ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* ========================
|
|
123
|
+
*
|
|
124
|
+
* # View Entity
|
|
125
|
+
*
|
|
126
|
+
* Types for entity definitions that have a view.
|
|
127
|
+
*
|
|
128
|
+
* ========================
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* The constructor output of {@link DefinedViewEntity2dConstructor}.
|
|
132
|
+
*
|
|
133
|
+
* @category Internal
|
|
134
|
+
*/
|
|
135
|
+
export type DefinedViewEntity2dInstance<State extends AnyObject, ParamsShape extends Shape<Record<string, any>> | undefined, EntityAssets extends BaseEntityAssetDefinitions | undefined> = ViewEntity2d<State, ParamsShape extends Shape ? ParamsShape['runtimeType'] : undefined, EntityAssets>;
|
|
136
|
+
/**
|
|
137
|
+
* Output of {@link DefineViewEntity2d}.
|
|
138
|
+
*
|
|
139
|
+
* @category Internal
|
|
140
|
+
*/
|
|
141
|
+
export type DefinedViewEntity2dConstructor<State extends AnyObject, ParamsShape extends Shape<AnyObject> | undefined, EntityAssets extends BaseEntityAssetDefinitions | undefined> = Constructor<DefinedViewEntity2dInstance<State, ParamsShape, EntityAssets>, ConstructorParameters<typeof ViewEntity2d<State, ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>>> & StaticEntity2dParts<State, ParamsShape>;
|
|
142
|
+
/**
|
|
143
|
+
* Type for {@link EntitySuite2d.defineEntity}.
|
|
144
|
+
*
|
|
145
|
+
* @category Internal
|
|
146
|
+
*/
|
|
147
|
+
export type DefineViewEntity2d<State extends AnyObject> = <const ParamsShape extends Shape<AnyObject> | undefined, const EntityAssets extends BaseEntityAssetDefinitions | undefined>(params: DefineEntity2dArgs<ParamsShape, EntityAssets>) => DefinedViewEntity2dConstructor<State, NoInfer<ParamsShape>, NoInfer<EntityAssets>> & StaticEntity2dParts<NoInfer<State>, NoInfer<ParamsShape>>;
|
|
148
|
+
/**
|
|
149
|
+
* ========================
|
|
150
|
+
*
|
|
151
|
+
* # Logic Entity
|
|
152
|
+
*
|
|
153
|
+
* Types for entity definitions that don't have a view. The only difference between these types and
|
|
154
|
+
* the view types are that this uses `BaseEntity2d` instead of `ViewEntity2d`.
|
|
155
|
+
*
|
|
156
|
+
* ========================
|
|
157
|
+
*/
|
|
158
|
+
/**
|
|
159
|
+
* The constructor output of {@link DefinedLogicEntity2dConstructor}.
|
|
160
|
+
*
|
|
161
|
+
* @category Internal
|
|
162
|
+
*/
|
|
163
|
+
export type DefinedLogicEntity2dInstance<State extends AnyObject, ParamsShape extends Shape<AnyObject> | undefined> = BaseEntity2d<State, ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>;
|
|
164
|
+
/**
|
|
165
|
+
* Output of {@link DefineLogicEntity2d}.
|
|
166
|
+
*
|
|
167
|
+
* @category Internal
|
|
168
|
+
*/
|
|
169
|
+
export type DefinedLogicEntity2dConstructor<State extends AnyObject, ParamsShape extends Shape<AnyObject> | undefined> = Constructor<DefinedLogicEntity2dInstance<State, ParamsShape>, ConstructorParameters<typeof BaseEntity2d<State, ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>>> & StaticEntity2dParts<State, ParamsShape>;
|
|
170
|
+
/**
|
|
171
|
+
* Type for `EntitySuite.defineEntity`.
|
|
172
|
+
*
|
|
173
|
+
* @category Internal
|
|
174
|
+
*/
|
|
175
|
+
export type DefineLogicEntity2d<State extends AnyObject> = <const ParamsShape extends Shape<AnyObject> | undefined, const EntityAssets extends BaseEntityAssetDefinitions | undefined>(params: DefineEntity2dArgs<ParamsShape, EntityAssets>) => DefinedLogicEntity2dConstructor<NoInfer<State>, NoInfer<ParamsShape>>;
|
|
176
|
+
/**
|
|
177
|
+
* ========================
|
|
178
|
+
*
|
|
179
|
+
* # Entity Suite
|
|
180
|
+
*
|
|
181
|
+
* ========================
|
|
182
|
+
*/
|
|
183
|
+
/**
|
|
184
|
+
* Output of {@link defineEntitySuite2d}, used to defining and creating entities.
|
|
185
|
+
*
|
|
186
|
+
* @category Internal
|
|
187
|
+
*/
|
|
188
|
+
export type EntitySuite2d<State extends AnyObject> = {
|
|
189
|
+
/**
|
|
190
|
+
* The suite's entity store constructor. Instantiate this and to add your first entities.
|
|
191
|
+
*
|
|
192
|
+
* All defined entities will also have a reference to this store so they can add additional
|
|
193
|
+
* entities by themselves.
|
|
194
|
+
*/
|
|
195
|
+
EntityStore: new (params: Readonly<EntityStore2dConstructorParams>) => EntityStore2d<State>;
|
|
196
|
+
/**
|
|
197
|
+
* Define a standard entity (with a view). This is intended to be extended from your entity
|
|
198
|
+
* class.
|
|
199
|
+
*/
|
|
200
|
+
defineEntity: DefineViewEntity2d<State>;
|
|
201
|
+
/** Define an entity that doesn't have an attached view. These are likely to be rare. */
|
|
202
|
+
defineLogicEntity: DefineLogicEntity2d<State>;
|
|
203
|
+
/**
|
|
204
|
+
* A set of entity keys used within this entity suite. This will only be populated by all
|
|
205
|
+
* classes that are defined with `defineEntity` or `defineLogicEntity` (so this will miss any
|
|
206
|
+
* not-yet-resolved dynamic imports). This will be populated even before the classes are ever
|
|
207
|
+
* instantiated.
|
|
208
|
+
*/
|
|
209
|
+
entityKeys: Set<string>;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* This is the starting point of the @game-vir/entity package. Call this to produce the function
|
|
213
|
+
* needed to define new entities and the store needed to add entity instances.
|
|
214
|
+
*
|
|
215
|
+
* @category Main
|
|
216
|
+
*/
|
|
217
|
+
export declare function defineEntitySuite2d<State extends AnyObject>(): EntitySuite2d<State>;
|
|
218
|
+
/**
|
|
219
|
+
* Converts {@link ParamsMap} to {@link ReverseParamsMap}.
|
|
220
|
+
*
|
|
221
|
+
* @category Internal
|
|
222
|
+
*/
|
|
223
|
+
export declare function reverseParamsMap(paramsMap: ParamsMap | undefined): ReverseParamsMap | undefined;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
import { getObjectTypedEntries, getOrSet } from '@augment-vir/common';
|
|
3
|
+
import { BaseEntity2d, EntityStore2d, ViewEntity2d, } from './entity.js';
|
|
4
|
+
/**
|
|
5
|
+
* This is the starting point of the @game-vir/entity package. Call this to produce the function
|
|
6
|
+
* needed to define new entities and the store needed to add entity instances.
|
|
7
|
+
*
|
|
8
|
+
* @category Main
|
|
9
|
+
*/
|
|
10
|
+
export function defineEntitySuite2d() {
|
|
11
|
+
const entityKeys = new Set();
|
|
12
|
+
function createDefiner(entityParent) {
|
|
13
|
+
return (params) => {
|
|
14
|
+
if (params.assets) {
|
|
15
|
+
getObjectTypedEntries(params.assets).forEach(([key, rawAsset,]) => {
|
|
16
|
+
rawAsset.name = [
|
|
17
|
+
params.key,
|
|
18
|
+
key,
|
|
19
|
+
].join(':');
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return defineEntity(entityParent, params);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function defineEntity(entityParent, { key, paramsShape, paramsMap, assets }) {
|
|
26
|
+
if (entityKeys.has(key)) {
|
|
27
|
+
throw new Error(`Entity key '${key}' has already been attached to an entity class.`);
|
|
28
|
+
}
|
|
29
|
+
entityKeys.add(key);
|
|
30
|
+
const classWrapper = {
|
|
31
|
+
// @ts-expect-error: abstract methods are intentionally not implemented here
|
|
32
|
+
[key]: class extends entityParent {
|
|
33
|
+
static entityKey = key;
|
|
34
|
+
static paramsShape = paramsShape;
|
|
35
|
+
static assets = assets;
|
|
36
|
+
static paramsMap = paramsMap;
|
|
37
|
+
static reverseParamsMap = reverseParamsMap(paramsMap);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
41
|
+
return classWrapper[key];
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
EntityStore: EntityStore2d,
|
|
45
|
+
defineEntity: createDefiner(ViewEntity2d),
|
|
46
|
+
defineLogicEntity: createDefiner(BaseEntity2d),
|
|
47
|
+
entityKeys,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Converts {@link ParamsMap} to {@link ReverseParamsMap}.
|
|
52
|
+
*
|
|
53
|
+
* @category Internal
|
|
54
|
+
*/
|
|
55
|
+
export function reverseParamsMap(paramsMap) {
|
|
56
|
+
if (!paramsMap) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const reverseParamsMap = {};
|
|
60
|
+
getObjectTypedEntries(paramsMap).forEach(([topKey, mappings,]) => {
|
|
61
|
+
getObjectTypedEntries(mappings).forEach(([mapToKey, mapFromKey,]) => {
|
|
62
|
+
if (!mapFromKey) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const paramMapping = getOrSet(reverseParamsMap, check.isString(mapFromKey) ? mapFromKey : mapToKey, () => {
|
|
66
|
+
return {};
|
|
67
|
+
});
|
|
68
|
+
const mapToArray = getOrSet(paramMapping, topKey, () => {
|
|
69
|
+
return [];
|
|
70
|
+
});
|
|
71
|
+
if (!mapToArray.includes(mapToKey)) {
|
|
72
|
+
mapToArray.push(mapToKey);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return reverseParamsMap;
|
|
77
|
+
}
|