@antha/entity-2d 0.12.0 → 0.14.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.
@@ -1,4 +1,5 @@
1
1
  import { type AnthaAssetModState } from '@antha/asset';
2
+ import { type AnthaMod } from '@antha/engine';
2
3
  import { type AnthaGraphics2dModState } from '@antha/graphics-2d';
3
4
  import { type AnyObject, type PartialWithUndefined } from '@augment-vir/common';
4
5
  import { type EntityStore2d, type EntityStore2dConstructorParams } from './entity.js';
@@ -7,7 +8,7 @@ import { type EntityStore2d, type EntityStore2dConstructorParams } from './entit
7
8
  *
8
9
  * @category Internal
9
10
  */
10
- export type AnthaEntity2dModState<State extends AnyObject = any> = {
11
+ export type AnthaEntity2dModState<State extends AnyObject = AnyObject> = {
11
12
  entityStore: EntityStore2d<Partial<AnthaEntity2dModState<State>>>;
12
13
  /** If true, entity updates and collision checks are skipped. */
13
14
  disableEntityUpdates: boolean;
@@ -31,5 +32,5 @@ export declare function createAnthaEntityMod2d<ExtraState extends AnyObject>(opt
31
32
  defineEntity: import("./entity-suite.js").DefineViewEntity2d<AnthaEntity2dModState<ExtraState>>;
32
33
  defineLogicEntity: import("./entity-suite.js").DefineLogicEntity2d<AnthaEntity2dModState<ExtraState>>;
33
34
  entityKeys: Set<string>;
34
- mod: import("@antha/engine").AnthaMod<any>;
35
+ mod: AnthaMod<AnthaEntity2dModState<ExtraState>>;
35
36
  };
@@ -18,34 +18,32 @@ export function createAnthaEntityMod2d(options = {}) {
18
18
  cleanup({ state }) {
19
19
  state.entityStore?.destroy();
20
20
  },
21
- async execute({ state, engine, msSinceLastExecute }) {
21
+ async execute(executeParams) {
22
22
  /**
23
23
  * If we don't have a mod that is expected to create the asset loader, then we create
24
24
  * one ourself.
25
25
  */
26
- if (!state.assetLoader &&
27
- !engine.currentMods.some((mod) => mod.modName === anthaAssetModName)) {
28
- state.assetLoader = new AssetLoader();
26
+ if (!executeParams.state.assetLoader &&
27
+ !executeParams.engine.currentMods.some((mod) => mod.modName === anthaAssetModName)) {
28
+ executeParams.state.assetLoader = new AssetLoader();
29
29
  }
30
- const pixiApplication = state.pixi?.pixiApplication;
30
+ const pixiApplication = executeParams.state.pixi?.pixiApplication;
31
31
  if (!pixiApplication) {
32
32
  return SkipExecution;
33
33
  }
34
- if (state.entityStore) {
35
- if (!state.disableEntityUpdates) {
36
- await state.entityStore.updateAllEntities({
37
- msSinceLastUpdate: msSinceLastExecute,
38
- });
34
+ if (executeParams.state.entityStore) {
35
+ if (!executeParams.state.disableEntityUpdates) {
36
+ await executeParams.state.entityStore.updateAllEntities(executeParams);
39
37
  }
40
38
  }
41
- else if (state.assetLoader) {
42
- state.entityStore = new EntityStore(mergeDefinedProperties({
39
+ else if (executeParams.state.assetLoader) {
40
+ executeParams.state.entityStore = new EntityStore(mergeDefinedProperties({
43
41
  pixi: pixiApplication,
44
- state,
45
- assetLoader: state.assetLoader,
42
+ state: executeParams.state,
43
+ assetLoader: executeParams.state.assetLoader,
46
44
  }, options));
47
45
  }
48
- if (state.debugHitboxes) {
46
+ if (executeParams.state.debugHitboxes) {
49
47
  return html `
50
48
  <canvas class="hitbox-debug-canvas"></canvas>
51
49
  `;
@@ -6,7 +6,7 @@ import { BaseEntity2d, type BaseEntityAssetDefinitions, type Entity2dConstructor
6
6
  *
7
7
  * @category Internal
8
8
  */
9
- export type DefineEntity2dArgs<ParamsShape extends Shape<AnyObject> | undefined, EntityAssets extends BaseEntityAssetDefinitions | undefined> = {
9
+ export type DefineEntity2dArgs<ParamsShape extends Shape | undefined, EntityAssets extends BaseEntityAssetDefinitions | undefined> = {
10
10
  /** Entity classes this entity observes collisions with. Omit to observe none. */
11
11
  collidesWith?: EntityCollisionDefinition | undefined;
12
12
  /**
@@ -89,7 +89,7 @@ export type DefineEntity2dArgs<ParamsShape extends Shape<AnyObject> | undefined,
89
89
  *
90
90
  * @default undefined // no mapping
91
91
  */
92
- paramsMap?: ParamsMap<NoInfer<ParamsShape> extends Shape<AnyObject> ? NoInfer<ParamsShape>['runtimeType'] : undefined> | undefined;
92
+ paramsMap?: ParamsMap<NoInfer<ParamsShape> extends Shape ? NoInfer<ParamsShape>['runtimeType'] : undefined> | undefined;
93
93
  assets?: EntityAssets;
94
94
  };
95
95
  /**
@@ -97,7 +97,7 @@ export type DefineEntity2dArgs<ParamsShape extends Shape<AnyObject> | undefined,
97
97
  *
98
98
  * @category Internal
99
99
  */
100
- export type StaticEntity2dParts<State extends AnyObject = any, ParamsShape extends Shape<AnyObject> | undefined = any, EntityAssets extends BaseEntityAssetDefinitions | undefined = any> = {
100
+ export type StaticEntity2dParts<State extends AnyObject = any, ParamsShape extends Shape | undefined = any, EntityAssets extends BaseEntityAssetDefinitions | undefined = any> = {
101
101
  /** Entity classes this entity observes collisions with. Omit to observe none. */
102
102
  collidesWith: EntityCollisionDefinition | undefined;
103
103
  /** Cached entity classes this entity observes collisions with. */
@@ -117,11 +117,11 @@ export type StaticEntity2dParts<State extends AnyObject = any, ParamsShape exten
117
117
  * Defines which properties from {@link BaseEntity2d.params} will be mapped to hitbox and/or view
118
118
  * properties.
119
119
  */
120
- paramsMap: IsAny<ParamsShape> extends true ? any : ParamsMap<ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>;
120
+ paramsMap: IsAny<ParamsShape> extends true ? any : ParamsMap<ParamsShape extends Shape ? ParamsShape['runtimeType'] : undefined>;
121
121
  /** Parses the serialized params generated by {@link BaseEntity2d.serialize}. */
122
122
  deserialize(serialized: string | undefined): AnyObject | undefined;
123
123
  assets: MappedEntityAssets<EntityAssets>;
124
- ConstructorArgsType: Entity2dConstructorParams<State, ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>;
124
+ ConstructorArgsType: Entity2dConstructorParams<State, ParamsShape extends Shape ? ParamsShape['runtimeType'] : undefined>;
125
125
  };
126
126
  /**
127
127
  * ========================
@@ -143,13 +143,13 @@ export type DefinedViewEntity2dInstance<State extends AnyObject, ParamsShape ext
143
143
  *
144
144
  * @category Internal
145
145
  */
146
- 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>;
146
+ export type DefinedViewEntity2dConstructor<State extends AnyObject, ParamsShape extends Shape | undefined, EntityAssets extends BaseEntityAssetDefinitions | undefined> = Constructor<DefinedViewEntity2dInstance<State, ParamsShape, EntityAssets>, ConstructorParameters<typeof ViewEntity2d<State, ParamsShape extends Shape ? ParamsShape['runtimeType'] : undefined>>> & StaticEntity2dParts<State, ParamsShape>;
147
147
  /**
148
148
  * Type for {@link EntitySuite2d.defineEntity}.
149
149
  *
150
150
  * @category Internal
151
151
  */
152
- 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>>;
152
+ export type DefineViewEntity2d<State extends AnyObject> = <const ParamsShape extends Shape | undefined, const EntityAssets extends BaseEntityAssetDefinitions | undefined>(params: DefineEntity2dArgs<ParamsShape, EntityAssets>) => DefinedViewEntity2dConstructor<State, NoInfer<ParamsShape>, NoInfer<EntityAssets>> & StaticEntity2dParts<NoInfer<State>, NoInfer<ParamsShape>>;
153
153
  /**
154
154
  * ========================
155
155
  *
@@ -165,19 +165,19 @@ export type DefineViewEntity2d<State extends AnyObject> = <const ParamsShape ext
165
165
  *
166
166
  * @category Internal
167
167
  */
168
- export type DefinedLogicEntity2dInstance<State extends AnyObject, ParamsShape extends Shape<AnyObject> | undefined> = BaseEntity2d<State, ParamsShape extends Shape<AnyObject> ? ParamsShape['runtimeType'] : undefined>;
168
+ export type DefinedLogicEntity2dInstance<State extends AnyObject, ParamsShape extends Shape | undefined> = BaseEntity2d<State, ParamsShape extends Shape ? ParamsShape['runtimeType'] : undefined>;
169
169
  /**
170
170
  * Output of {@link DefineLogicEntity2d}.
171
171
  *
172
172
  * @category Internal
173
173
  */
174
- 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>;
174
+ export type DefinedLogicEntity2dConstructor<State extends AnyObject, ParamsShape extends Shape | undefined> = Constructor<DefinedLogicEntity2dInstance<State, ParamsShape>, ConstructorParameters<typeof BaseEntity2d<State, ParamsShape extends Shape ? ParamsShape['runtimeType'] : undefined>>> & StaticEntity2dParts<State, ParamsShape>;
175
175
  /**
176
176
  * Type for `EntitySuite.defineEntity`.
177
177
  *
178
178
  * @category Internal
179
179
  */
180
- 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>>;
180
+ export type DefineLogicEntity2d<State extends AnyObject> = <const ParamsShape extends Shape | undefined, const EntityAssets extends BaseEntityAssetDefinitions | undefined>(params: DefineEntity2dArgs<ParamsShape, EntityAssets>) => DefinedLogicEntity2dConstructor<NoInfer<State>, NoInfer<ParamsShape>>;
181
181
  /**
182
182
  * ========================
183
183
  *
package/dist/entity.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type Asset, type AssetLoader, type AssetValue } from '@antha/asset';
2
+ import { type ModExecuteParams } from '@antha/engine';
2
3
  import { type PixiApplication } from '@antha/graphics-2d';
3
4
  import { ConstructorInstanceMap, type AbstractConstructor, type AnyObject, type Constructor, type EmptyObject, type ExtractKeysWithMatchingValues, type IsEqual, type IsNever, type MaybePromise, type PartialWithUndefined, type WritableKeysOf } from '@augment-vir/common';
4
5
  import { System as HitboxSystem, type Response as Collision, type Body as Hitbox } from 'detect-collisions';
@@ -141,7 +142,7 @@ export declare class EntityStore2d<State extends AnyObject = any> {
141
142
  *
142
143
  * @returns All detected hitbox collisions (if any).
143
144
  */
144
- updateAllEntities(updateParams: Readonly<EntityUpdateParams>): Promise<void>;
145
+ updateAllEntities(updateParams: Readonly<ModExecuteParams<NoInfer<State>>>): Promise<void>;
145
146
  /** Get all current instances of the given entity class constructor. */
146
147
  getEntities<T>(entityClassConstructor: AbstractConstructor<T> | Constructor<T>): Set<T>;
147
148
  /** Remove an entity from the store. */
@@ -153,6 +154,8 @@ export declare class EntityStore2d<State extends AnyObject = any> {
153
154
  deserializeEntity(entityKey: string, serializedParams: string | undefined): Promise<BaseEntity2d>;
154
155
  /** Create a new instance of the given entity class and add it to this entity store. */
155
156
  addEntity<const NewEntityConstructor extends Entity2dConstructor>(entityClass: NewEntityConstructor, ...params: AddEntity2dParams<NoInfer<NewEntityConstructor>>): Promise<InstanceType<NewEntityConstructor>>;
157
+ /** Immediately destroys every current entity while leaving the entity store usable. */
158
+ destroyAllEntities(): void;
156
159
  /** Destroys the entity store and all entities contained inside it. */
157
160
  destroy(): void;
158
161
  }
@@ -296,14 +299,6 @@ export declare const position2dParamsMap: {
296
299
  * @category Internal
297
300
  */
298
301
  export type ReverseParamsMap = Record<string, Partial<Record<'hitbox' | 'view', string[]>>>;
299
- /**
300
- * The parameters given to entity update methods.
301
- *
302
- * @category Internal
303
- */
304
- export type EntityUpdateParams = {
305
- msSinceLastUpdate: number;
306
- };
307
302
  /**
308
303
  * Base entity class, types, and functionality.
309
304
  *
@@ -320,7 +315,7 @@ export declare abstract class BaseEntity2d<State extends AnyObject = any, Params
320
315
  /** Cached entity classes this entity observes collisions with. */
321
316
  static readonly collidesWithSet: ReadonlySet<Entity2dConstructor>;
322
317
  /** Shape definition of this entity's parameters. */
323
- static readonly paramsShape: Shape<AnyObject> | undefined;
318
+ static readonly paramsShape: Shape | undefined;
324
319
  static readonly assets: MappedEntityAssets<BaseEntityAssetDefinitions | undefined> | undefined;
325
320
  /**
326
321
  * Defines which properties from {@link BaseEntity2d.params} will be mapped to hitbox and/or view
@@ -361,7 +356,7 @@ export declare abstract class BaseEntity2d<State extends AnyObject = any, Params
361
356
  * Called every game tick. Run all entity updates in here. This should be overridden in all
362
357
  * entity definition classes.
363
358
  */
364
- abstract update(updateParams: Readonly<EntityUpdateParams>): MaybePromise<void>;
359
+ abstract update(updateParams: Readonly<ModExecuteParams<NoInfer<State>>>): MaybePromise<void>;
365
360
  /** Called after construction to perform async initialization (e.g. creating views). */
366
361
  initInstance(): MaybePromise<void>;
367
362
  /** The game's current state. */
package/dist/entity.js CHANGED
@@ -282,6 +282,13 @@ export class EntityStore2d {
282
282
  this.entityInstanceMap.add(child);
283
283
  return child;
284
284
  }
285
+ /** Immediately destroys every current entity while leaving the entity store usable. */
286
+ destroyAllEntities() {
287
+ if (this.isDestroyed) {
288
+ throw new Error('Cannot operate on a destroyed entity store.');
289
+ }
290
+ this.currentEntityInstances.forEach((entity) => entity.immediatelyDestroy());
291
+ }
285
292
  /** Destroys the entity store and all entities contained inside it. */
286
293
  destroy() {
287
294
  if (this.isDestroyed) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antha/entity-2d",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "An Antha mod for handling 2D entities.",
5
5
  "keywords": [
6
6
  "vir",
@@ -35,13 +35,13 @@
35
35
  "test:docs": "virmator docs check"
36
36
  },
37
37
  "dependencies": {
38
- "@augment-vir/assert": "^32.2.3",
39
- "@augment-vir/common": "^32.2.3"
38
+ "@augment-vir/assert": "^32.2.4",
39
+ "@augment-vir/common": "^32.2.4"
40
40
  },
41
41
  "devDependencies": {
42
- "@antha/engine": "^0.12.0",
43
- "@antha/web-test-runner-plugin-pixi": "^0.12.0",
44
- "@augment-vir/test": "^32.2.3",
42
+ "@antha/engine": "^0.14.0",
43
+ "@antha/web-test-runner-plugin-pixi": "^0.14.0",
44
+ "@augment-vir/test": "^32.2.4",
45
45
  "@web/dev-server-esbuild": "^2.0.0",
46
46
  "@web/test-runner": "^1.0.0",
47
47
  "@web/test-runner-playwright": "^1.0.0",
@@ -52,10 +52,10 @@
52
52
  "typed-event-target": "^4.3.3"
53
53
  },
54
54
  "peerDependencies": {
55
- "@antha/asset": "^0.12.0",
56
- "@antha/audio": "^0.12.0",
57
- "@antha/engine": "^0.12.0",
58
- "@antha/graphics-2d": "^0.12.0",
55
+ "@antha/asset": "^0.14.0",
56
+ "@antha/audio": "^0.14.0",
57
+ "@antha/engine": "^0.14.0",
58
+ "@antha/graphics-2d": "^0.14.0",
59
59
  "detect-collisions": ">=10",
60
60
  "element-vir": ">=26",
61
61
  "object-shape-tester": ">=6",