diff --git a/CHANGELOG.md b/CHANGELOG.md index 17aa17a12..59b3cb670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Breaking Changes -- +- `ex.Action` now requires a unique `id` property ### Deprecated @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - `actor.oldGlobalPos` returns the globalPosition from the previous frame +- Built in actions now have a unique `id` property ### Fixed diff --git a/src/engine/Actions/Action.ts b/src/engine/Actions/Action.ts index b43fc3d46..4d2e7e944 100644 --- a/src/engine/Actions/Action.ts +++ b/src/engine/Actions/Action.ts @@ -4,8 +4,17 @@ import { Entity } from '../EntityComponentSystem/Entity'; * Used for implementing actions for the [[ActionContext|Action API]]. */ export interface Action { + id: number; update(delta: number): void; isComplete(entity: Entity): boolean; reset(): void; stop(): void; } + +let _ACTION_ID = 0; +/** + * + */ +export function nextActionId(): number { + return _ACTION_ID++; +} diff --git a/src/engine/Actions/Action/ActionSequence.ts b/src/engine/Actions/Action/ActionSequence.ts index c52f923f1..cee3657db 100644 --- a/src/engine/Actions/Action/ActionSequence.ts +++ b/src/engine/Actions/Action/ActionSequence.ts @@ -1,5 +1,5 @@ import { Entity } from '../../EntityComponentSystem'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { ActionContext } from '../ActionContext'; import { ActionQueue } from '../ActionQueue'; @@ -8,6 +8,7 @@ import { ActionQueue } from '../ActionQueue'; * [[ParallelActions]] to run multiple sequences in parallel. */ export class ActionSequence implements Action { + id = nextActionId(); private _actionQueue: ActionQueue; private _stopped: boolean = false; private _sequenceContext: ActionContext; diff --git a/src/engine/Actions/Action/Blink.ts b/src/engine/Actions/Action/Blink.ts index d350a09ed..2bcaa0ac1 100644 --- a/src/engine/Actions/Action/Blink.ts +++ b/src/engine/Actions/Action/Blink.ts @@ -1,8 +1,9 @@ import { GraphicsComponent } from '../../Graphics/GraphicsComponent'; import { Entity } from '../../EntityComponentSystem/Entity'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class Blink implements Action { + id = nextActionId(); private _graphics: GraphicsComponent; private _timeVisible: number = 0; private _timeNotVisible: number = 0; diff --git a/src/engine/Actions/Action/CallMethod.ts b/src/engine/Actions/Action/CallMethod.ts index 6b99eb00a..0943adae1 100644 --- a/src/engine/Actions/Action/CallMethod.ts +++ b/src/engine/Actions/Action/CallMethod.ts @@ -1,7 +1,8 @@ -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class CallMethod implements Action { - private _method: () => any = null; + id = nextActionId(); + private _method: () => any; private _hasBeenCalled: boolean = false; constructor(method: () => any) { this._method = method; diff --git a/src/engine/Actions/Action/Delay.ts b/src/engine/Actions/Action/Delay.ts index 56234987e..2c497d057 100644 --- a/src/engine/Actions/Action/Delay.ts +++ b/src/engine/Actions/Action/Delay.ts @@ -1,6 +1,7 @@ -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class Delay implements Action { + id = nextActionId(); private _elapsedTime: number = 0; private _delay: number; private _started: boolean = false; diff --git a/src/engine/Actions/Action/Die.ts b/src/engine/Actions/Action/Die.ts index decf4ea79..82b735e9b 100644 --- a/src/engine/Actions/Action/Die.ts +++ b/src/engine/Actions/Action/Die.ts @@ -1,8 +1,9 @@ import { Entity } from '../../EntityComponentSystem/Entity'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { ActionsComponent } from '../ActionsComponent'; export class Die implements Action { + id = nextActionId(); private _entity: Entity; private _stopped = false; diff --git a/src/engine/Actions/Action/EaseBy.ts b/src/engine/Actions/Action/EaseBy.ts index e82c2ab77..aafd778fc 100644 --- a/src/engine/Actions/Action/EaseBy.ts +++ b/src/engine/Actions/Action/EaseBy.ts @@ -2,9 +2,10 @@ import { MotionComponent } from '../../EntityComponentSystem/Components/MotionCo import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { Entity } from '../../EntityComponentSystem/Entity'; import { vec, Vector } from '../../Math/vector'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class EaseBy implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; private _currentLerpTime: number = 0; diff --git a/src/engine/Actions/Action/EaseTo.ts b/src/engine/Actions/Action/EaseTo.ts index 958e949fc..26896b166 100644 --- a/src/engine/Actions/Action/EaseTo.ts +++ b/src/engine/Actions/Action/EaseTo.ts @@ -2,9 +2,10 @@ import { Entity } from '../../EntityComponentSystem/Entity'; import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { MotionComponent } from '../../EntityComponentSystem/Components/MotionComponent'; import { vec, Vector } from '../../Math/vector'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class EaseTo implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; private _currentLerpTime: number = 0; diff --git a/src/engine/Actions/Action/Fade.ts b/src/engine/Actions/Action/Fade.ts index c5aacf6b6..b281191dd 100644 --- a/src/engine/Actions/Action/Fade.ts +++ b/src/engine/Actions/Action/Fade.ts @@ -1,9 +1,10 @@ import { Entity } from '../../EntityComponentSystem/Entity'; import { GraphicsComponent } from '../../Graphics/GraphicsComponent'; import { Logger } from '../../Util/Log'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class Fade implements Action { + id = nextActionId(); private _graphics: GraphicsComponent; public x: number; public y: number; diff --git a/src/engine/Actions/Action/Follow.ts b/src/engine/Actions/Action/Follow.ts index abd80c640..9c962ba68 100644 --- a/src/engine/Actions/Action/Follow.ts +++ b/src/engine/Actions/Action/Follow.ts @@ -2,9 +2,10 @@ import { MotionComponent } from '../../EntityComponentSystem/Components/MotionCo import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { Entity } from '../../EntityComponentSystem/Entity'; import { vec, Vector } from '../../Math/vector'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class Follow implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; private _followTx: TransformComponent; diff --git a/src/engine/Actions/Action/Meet.ts b/src/engine/Actions/Action/Meet.ts index 73f90f4ce..9304c44be 100644 --- a/src/engine/Actions/Action/Meet.ts +++ b/src/engine/Actions/Action/Meet.ts @@ -2,9 +2,10 @@ import { MotionComponent } from '../../EntityComponentSystem/Components/MotionCo import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { Entity } from '../../EntityComponentSystem/Entity'; import { Vector, vec } from '../../Math/vector'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class Meet implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; private _meetTx: TransformComponent; diff --git a/src/engine/Actions/Action/MoveBy.ts b/src/engine/Actions/Action/MoveBy.ts index 996228864..55fb90a97 100644 --- a/src/engine/Actions/Action/MoveBy.ts +++ b/src/engine/Actions/Action/MoveBy.ts @@ -3,9 +3,10 @@ import { TransformComponent } from '../../EntityComponentSystem/Components/Trans import { Entity } from '../../EntityComponentSystem/Entity'; import { Vector, vec } from '../../Math/vector'; import { Logger } from '../../Util/Log'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class MoveBy implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; private _entity: Entity; diff --git a/src/engine/Actions/Action/MoveTo.ts b/src/engine/Actions/Action/MoveTo.ts index 9ec7a0bbc..1381a0d99 100644 --- a/src/engine/Actions/Action/MoveTo.ts +++ b/src/engine/Actions/Action/MoveTo.ts @@ -2,9 +2,10 @@ import { MotionComponent } from '../../EntityComponentSystem/Components/MotionCo import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { Entity } from '../../EntityComponentSystem/Entity'; import { Vector, vec } from '../../Math/vector'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class MoveTo implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; public x: number; diff --git a/src/engine/Actions/Action/ParallelActions.ts b/src/engine/Actions/Action/ParallelActions.ts index d1e4bd067..19c9907cb 100644 --- a/src/engine/Actions/Action/ParallelActions.ts +++ b/src/engine/Actions/Action/ParallelActions.ts @@ -1,10 +1,11 @@ import { Entity } from '../../EntityComponentSystem'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; /** * Action that can run multiple [[Action]]s or [[ActionSequence]]s at the same time */ export class ParallelActions implements Action { + id = nextActionId(); private _actions: Action[]; constructor(parallelActions: Action[]) { diff --git a/src/engine/Actions/Action/Repeat.ts b/src/engine/Actions/Action/Repeat.ts index 9b4c087df..501426375 100644 --- a/src/engine/Actions/Action/Repeat.ts +++ b/src/engine/Actions/Action/Repeat.ts @@ -1,9 +1,10 @@ import { Entity } from '../../EntityComponentSystem/Entity'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { ActionContext } from '../ActionContext'; import { ActionQueue } from '../ActionQueue'; export class Repeat implements Action { + id = nextActionId(); private _actionQueue: ActionQueue; private _repeat: number; private _originalRepeat: number; diff --git a/src/engine/Actions/Action/RepeatForever.ts b/src/engine/Actions/Action/RepeatForever.ts index 911df4a37..2c1baf6a4 100644 --- a/src/engine/Actions/Action/RepeatForever.ts +++ b/src/engine/Actions/Action/RepeatForever.ts @@ -1,5 +1,5 @@ import { Entity } from '../../EntityComponentSystem/Entity'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { ActionContext } from '../ActionContext'; import { ActionQueue } from '../ActionQueue'; @@ -10,6 +10,7 @@ import { ActionQueue } from '../ActionQueue'; * */ export class RepeatForever implements Action { + id = nextActionId(); private _actionQueue: ActionQueue; private _stopped: boolean = false; private _repeatContext: ActionContext; diff --git a/src/engine/Actions/Action/RotateBy.ts b/src/engine/Actions/Action/RotateBy.ts index daa237634..caf22db2e 100644 --- a/src/engine/Actions/Action/RotateBy.ts +++ b/src/engine/Actions/Action/RotateBy.ts @@ -1,4 +1,4 @@ -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { RotationType } from '../RotationType'; import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { MotionComponent } from '../../EntityComponentSystem/Components/MotionComponent'; @@ -6,6 +6,7 @@ import { Entity } from '../../EntityComponentSystem/Entity'; import { TwoPI } from '../../Math/util'; export class RotateBy implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; public x: number; diff --git a/src/engine/Actions/Action/RotateTo.ts b/src/engine/Actions/Action/RotateTo.ts index fe18d4304..17520bd89 100644 --- a/src/engine/Actions/Action/RotateTo.ts +++ b/src/engine/Actions/Action/RotateTo.ts @@ -1,4 +1,4 @@ -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { RotationType } from '../RotationType'; import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; import { MotionComponent } from '../../EntityComponentSystem/Components/MotionComponent'; @@ -6,6 +6,7 @@ import { Entity } from '../../EntityComponentSystem/Entity'; import { TwoPI } from '../../Math/util'; export class RotateTo implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; public x: number; diff --git a/src/engine/Actions/Action/ScaleBy.ts b/src/engine/Actions/Action/ScaleBy.ts index cf539ee90..cd6e02e19 100644 --- a/src/engine/Actions/Action/ScaleBy.ts +++ b/src/engine/Actions/Action/ScaleBy.ts @@ -2,9 +2,10 @@ import { Vector } from '../../Math/vector'; import { Entity } from '../../EntityComponentSystem/Entity'; import { MotionComponent } from '../../EntityComponentSystem/Components/MotionComponent'; import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; export class ScaleBy implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; public x: number; diff --git a/src/engine/Actions/Action/ScaleTo.ts b/src/engine/Actions/Action/ScaleTo.ts index de36ff22f..4e21c96cc 100644 --- a/src/engine/Actions/Action/ScaleTo.ts +++ b/src/engine/Actions/Action/ScaleTo.ts @@ -1,10 +1,11 @@ import { vec } from '../../Math/vector'; import { MotionComponent } from '../../EntityComponentSystem/Components/MotionComponent'; import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent'; -import { Action } from '../Action'; +import { Action, nextActionId } from '../Action'; import { Entity } from '../../EntityComponentSystem/Entity'; export class ScaleTo implements Action { + id = nextActionId(); private _tx: TransformComponent; private _motion: MotionComponent; public x: number; diff --git a/src/spec/ActionSpec.ts b/src/spec/ActionSpec.ts index 8db9cd8d4..849f0286c 100644 --- a/src/spec/ActionSpec.ts +++ b/src/spec/ActionSpec.ts @@ -1261,6 +1261,22 @@ describe('Action', () => { expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({ target: actor, action: jasmine.any(ex.MoveTo) })); }); + it('emits actioncomplete with an action with ids', () => { + const spy = jasmine.createSpy(); + // actor.actions.moveTo(20, 0, 20); + const moveTo = new ex.MoveTo(actor, 20, 0, 20); + const moveTo2 = new ex.MoveTo(actor, 20, 0, 20); + actor.actions.runAction(moveTo); + actor.on('actioncomplete', spy); + for (let i = 0; i < 10; i++) { + scene.update(engine, 200); + } + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({ target: actor, action: moveTo })); + expect(moveTo.id).not.toEqual(moveTo2.id); + expect(moveTo2.id).toEqual(moveTo.id + 1); + }); + it('emits actionstart and actioncomplete events for each action in a repeat', () => { const startSpy = jasmine.createSpy(); const completeSpy = jasmine.createSpy();