Skip to content

Commit

Permalink
feat: Implement unique action ids (#3031)
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim authored Apr 27, 2024
1 parent 8e5f010 commit 74e1038
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

-
- `ex.Action` now requires a unique `id` property

### Deprecated

Expand All @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/engine/Actions/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/ActionSequence.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Blink.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/engine/Actions/Action/CallMethod.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Delay.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Die.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/EaseBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/EaseTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Fade.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Meet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/MoveBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/MoveTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/ParallelActions.ts
Original file line number Diff line number Diff line change
@@ -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[]) {
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/Repeat.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/RepeatForever.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/RotateBy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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';
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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/RotateTo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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';
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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/ScaleBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Actions/Action/ScaleTo.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/spec/ActionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 74e1038

Please sign in to comment.