• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 11652940474

03 Nov 2024 05:29PM UTC coverage: 90.175% (-0.01%) from 90.187%
11652940474

push

github

eonarheim
feat: Actor.actions.flash(...)

5864 of 7463 branches covered (78.57%)

5 of 7 new or added lines in 2 files covered. (71.43%)

1 existing line in 1 file now uncovered.

12849 of 14249 relevant lines covered (90.17%)

25259.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/src/engine/Actions/ActionsComponent.ts
1
import { ActionContext } from './ActionContext';
2
import { Component } from '../EntityComponentSystem/Component';
3
import { Entity } from '../EntityComponentSystem/Entity';
4
import { Actor } from '../Actor';
5
import { MotionComponent } from '../EntityComponentSystem/Components/MotionComponent';
6
import { TransformComponent } from '../EntityComponentSystem/Components/TransformComponent';
7
import { Vector } from '../Math/vector';
8
import { EasingFunction } from '../Util/EasingFunctions';
9
import { ActionQueue } from './ActionQueue';
10
import { RotationType } from './RotationType';
11
import { Action } from './Action';
12
import { Color } from '../Color';
13

14
export interface ActionContextMethods extends Pick<ActionContext, keyof ActionContext> {}
15

16
export class ActionsComponent extends Component implements ActionContextMethods {
17
  dependencies = [TransformComponent, MotionComponent];
848✔
18
  private _ctx: ActionContext | null = null;
848✔
19

20
  onAdd(entity: Entity) {
21
    this._ctx = new ActionContext(entity);
848✔
22
  }
23

24
  onRemove() {
25
    this._ctx = null;
1✔
26
  }
27

28
  private _getCtx() {
29
    if (!this._ctx) {
70!
30
      throw new Error('Actions component not attached to an entity, no context available');
×
31
    }
32
    return this._ctx;
70✔
33
  }
34

35
  /**
36
   * Returns the internal action queue
37
   * @returns action queue
38
   */
39
  public getQueue(): ActionQueue {
40
    if (!this._ctx) {
×
41
      throw new Error('Actions component not attached to an entity, no queue available');
×
42
    }
43
    return this._ctx.getQueue();
×
44
  }
45

46
  public runAction(action: Action): ActionContext {
47
    if (!this._ctx) {
13!
48
      throw new Error('Actions component not attached to an entity, cannot run action');
×
49
    }
50
    return this._ctx.runAction(action);
13✔
51
  }
52

53
  /**
54
   * Updates the internal action context, performing action and moving through the internal queue
55
   * @param elapsedMs
56
   */
57
  public update(elapsedMs: number): void {
58
    return this._ctx?.update(elapsedMs);
2,037!
59
  }
60

61
  /**
62
   * Clears all queued actions from the Actor
63
   */
64
  public clearActions(): void {
65
    this._ctx?.clearActions();
14!
66
  }
67

68
  /**
69
   * This method will move an actor to the specified `x` and `y` position over the
70
   * specified duration using a given {@apilink EasingFunctions} and return back the actor. This
71
   * method is part of the actor 'Action' fluent API allowing action chaining.
72
   * @param pos       The x,y vector location to move the actor to
73
   * @param duration  The time it should take the actor to move to the new location in milliseconds
74
   * @param easingFcn Use {@apilink EasingFunctions} or a custom function to use to calculate position, Default is {@apilink EasingFunctions.Linear}
75
   */
76
  public easeTo(pos: Vector, duration: number, easingFcn?: EasingFunction): ActionContext;
77
  /**
78
   * This method will move an actor to the specified `x` and `y` position over the
79
   * specified duration using a given {@apilink EasingFunctions} and return back the actor. This
80
   * method is part of the actor 'Action' fluent API allowing action chaining.
81
   * @param x         The x location to move the actor to
82
   * @param y         The y location to move the actor to
83
   * @param duration  The time it should take the actor to move to the new location in milliseconds
84
   * @param easingFcn Use {@apilink EasingFunctions} or a custom function to use to calculate position, Default is {@apilink EasingFunctions.Linear}
85
   */
86
  public easeTo(x: number, y: number, duration: number, easingFcn?: EasingFunction): ActionContext;
87
  public easeTo(...args: any[]): ActionContext {
88
    return this._getCtx().easeTo.apply(this._ctx, args as any);
4✔
89
  }
90

91
  public easeBy(offset: Vector, duration: number, easingFcn?: EasingFunction): ActionContext;
92
  public easeBy(offsetX: number, offsetY: number, duration: number, easingFcn?: EasingFunction): ActionContext;
93
  public easeBy(...args: any[]): ActionContext {
94
    return this._getCtx().easeBy.apply(this._ctx, args as any);
3✔
95
  }
96

97
  /**
98
   * This method will move an actor to the specified x and y position at the
99
   * speed specified (in pixels per second) and return back the actor. This
100
   * method is part of the actor 'Action' fluent API allowing action chaining.
101
   * @param pos    The x,y vector location to move the actor to
102
   * @param speed  The speed in pixels per second to move
103
   */
104
  public moveTo(pos: Vector, speed: number): ActionContext;
105
  /**
106
   * This method will move an actor to the specified x and y position at the
107
   * speed specified (in pixels per second) and return back the actor. This
108
   * method is part of the actor 'Action' fluent API allowing action chaining.
109
   * @param x      The x location to move the actor to
110
   * @param y      The y location to move the actor to
111
   * @param speed  The speed in pixels per second to move
112
   */
113
  public moveTo(x: number, y: number, speed: number): ActionContext;
114
  public moveTo(xOrPos: number | Vector, yOrSpeed: number, speedOrUndefined?: number): ActionContext {
115
    return this._getCtx().moveTo.apply(this._ctx, [xOrPos, yOrSpeed, speedOrUndefined] as any);
12✔
116
  }
117

118
  /**
119
   * This method will move an actor by the specified x offset and y offset from its current position, at a certain speed.
120
   * This method is part of the actor 'Action' fluent API allowing action chaining.
121
   * @param offset The (x, y) offset to apply to this actor
122
   * @param speed  The speed in pixels per second the actor should move
123
   */
124
  public moveBy(offset: Vector, speed: number): ActionContext;
125
  /**
126
   * This method will move an actor by the specified x offset and y offset from its current position, at a certain speed.
127
   * This method is part of the actor 'Action' fluent API allowing action chaining.
128
   * @param xOffset     The x offset to apply to this actor
129
   * @param yOffset     The y location to move the actor to
130
   * @param speed  The speed in pixels per second the actor should move
131
   */
132
  public moveBy(xOffset: number, yOffset: number, speed: number): ActionContext;
133
  public moveBy(xOffsetOrVector: number | Vector, yOffsetOrSpeed: number, speedOrUndefined?: number): ActionContext {
134
    return this._getCtx().moveBy.apply(this._ctx, [xOffsetOrVector, yOffsetOrSpeed, speedOrUndefined] as any);
6✔
135
  }
136

137
  /**
138
   * This method will rotate an actor to the specified angle at the speed
139
   * specified (in radians per second) and return back the actor. This
140
   * method is part of the actor 'Action' fluent API allowing action chaining.
141
   * @param angleRadians  The angle to rotate to in radians
142
   * @param speed         The angular velocity of the rotation specified in radians per second
143
   * @param rotationType  The {@apilink RotationType} to use for this rotation
144
   */
145
  public rotateTo(angleRadians: number, speed: number, rotationType?: RotationType): ActionContext {
146
    return this._getCtx().rotateTo(angleRadians, speed, rotationType);
6✔
147
  }
148

149
  /**
150
   * This method will rotate an actor by the specified angle offset, from it's current rotation given a certain speed
151
   * in radians/sec and return back the actor. This method is part
152
   * of the actor 'Action' fluent API allowing action chaining.
153
   * @param angleRadiansOffset  The angle to rotate to in radians relative to the current rotation
154
   * @param speed          The speed in radians/sec the actor should rotate at
155
   * @param rotationType  The {@apilink RotationType} to use for this rotation, default is shortest path
156
   */
157
  public rotateBy(angleRadiansOffset: number, speed: number, rotationType?: RotationType): ActionContext {
158
    return this._getCtx().rotateBy(angleRadiansOffset, speed, rotationType);
5✔
159
  }
160

161
  /**
162
   * This method will scale an actor to the specified size at the speed
163
   * specified (in magnitude increase per second) and return back the
164
   * actor. This method is part of the actor 'Action' fluent API allowing
165
   * action chaining.
166
   * @param size    The scale to adjust the actor to over time
167
   * @param speed   The speed of scaling specified in magnitude increase per second
168
   */
169
  public scaleTo(size: Vector, speed: Vector): ActionContext;
170
  /**
171
   * This method will scale an actor to the specified size at the speed
172
   * specified (in magnitude increase per second) and return back the
173
   * actor. This method is part of the actor 'Action' fluent API allowing
174
   * action chaining.
175
   * @param sizeX   The scaling factor to apply on X axis
176
   * @param sizeY   The scaling factor to apply on Y axis
177
   * @param speedX  The speed of scaling specified in magnitude increase per second on X axis
178
   * @param speedY  The speed of scaling specified in magnitude increase per second on Y axis
179
   */
180
  public scaleTo(sizeX: number, sizeY: number, speedX: number, speedY: number): ActionContext;
181
  public scaleTo(
182
    sizeXOrVector: number | Vector,
183
    sizeYOrSpeed: number | Vector,
184
    speedXOrUndefined?: number,
185
    speedYOrUndefined?: number
186
  ): ActionContext {
187
    return this._getCtx().scaleTo.apply(this._ctx, [sizeXOrVector, sizeYOrSpeed, speedXOrUndefined, speedYOrUndefined] as any);
4✔
188
  }
189

190
  /**
191
   * This method will scale an actor by an amount relative to the current scale at a certain speed in scale units/sec
192
   * and return back the actor. This method is part of the
193
   * actor 'Action' fluent API allowing action chaining.
194
   * @param offset   The scaling factor to apply to the actor
195
   * @param speed    The speed to scale at in scale units/sec
196
   */
197
  public scaleBy(offset: Vector, speed: number): ActionContext;
198
  /**
199
   * This method will scale an actor by an amount relative to the current scale at a certain speed in scale units/sec
200
   * and return back the actor. This method is part of the
201
   * actor 'Action' fluent API allowing action chaining.
202
   * @param sizeOffsetX   The scaling factor to apply on X axis
203
   * @param sizeOffsetY   The scaling factor to apply on Y axis
204
   * @param speed    The speed to scale at in scale units/sec
205
   */
206
  public scaleBy(sizeOffsetX: number, sizeOffsetY: number, speed: number): ActionContext;
207
  public scaleBy(sizeOffsetXOrVector: number | Vector, sizeOffsetYOrSpeed: number, speed?: number): ActionContext {
208
    return this._getCtx().scaleBy.apply(this._ctx, [sizeOffsetXOrVector, sizeOffsetYOrSpeed, speed] as any);
4✔
209
  }
210

211
  /**
212
   * This method will cause an actor to blink (become visible and not
213
   * visible). Optionally, you may specify the number of blinks. Specify the amount of time
214
   * the actor should be visible per blink, and the amount of time not visible.
215
   * This method is part of the actor 'Action' fluent API allowing action chaining.
216
   * @param timeVisible     The amount of time to stay visible per blink in milliseconds
217
   * @param timeNotVisible  The amount of time to stay not visible per blink in milliseconds
218
   * @param numBlinks       The number of times to blink
219
   */
220
  public blink(timeVisible: number, timeNotVisible: number, numBlinks?: number): ActionContext {
221
    return this._getCtx().blink(timeVisible, timeNotVisible, numBlinks);
2✔
222
  }
223

224
  /**
225
   * This method will cause an actor's opacity to change from its current value
226
   * to the provided value by a specified time (in milliseconds). This method is
227
   * part of the actor 'Action' fluent API allowing action chaining.
228
   * @param opacity  The ending opacity
229
   * @param duration     The time it should take to fade the actor (in milliseconds)
230
   */
231
  public fade(opacity: number, duration: number): ActionContext {
232
    return this._getCtx().fade(opacity, duration);
3✔
233
  }
234

235
  /**
236
   * This will cause an actor to flash a specific color for a period of time
237
   * @param color
238
   * @param duration The duration in milliseconds
239
   */
240
  public flash(color: Color, duration: number = 1000) {
×
NEW
241
    return this._getCtx().flash(color, duration);
×
242
  }
243

244
  /**
245
   * This method will delay the next action from executing for a certain
246
   * amount of time (in milliseconds). This method is part of the actor
247
   * 'Action' fluent API allowing action chaining.
248
   * @param duration  The amount of time to delay the next action in the queue from executing in milliseconds
249
   */
250
  public delay(duration: number): ActionContext {
251
    return this._getCtx().delay(duration);
3✔
252
  }
253

254
  /**
255
   * This method will add an action to the queue that will remove the actor from the
256
   * scene once it has completed its previous  Any actions on the
257
   * action queue after this action will not be executed.
258
   */
259
  public die(): ActionContext {
260
    return this._getCtx().die();
1✔
261
  }
262

263
  /**
264
   * This method allows you to call an arbitrary method as the next action in the
265
   * action queue. This is useful if you want to execute code in after a specific
266
   * action, i.e An actor arrives at a destination after traversing a path
267
   */
268
  public callMethod(method: () => any): ActionContext {
269
    return this._getCtx().callMethod(method);
1✔
270
  }
271

272
  /**
273
   * This method will cause the actor to repeat all of the actions built in
274
   * the `repeatBuilder` callback. If the number of repeats
275
   * is not specified it will repeat forever. This method is part of
276
   * the actor 'Action' fluent API allowing action chaining
277
   *
278
   * ```typescript
279
   * // Move up in a zig-zag by repeated moveBy's
280
   * actor.actions.repeat(repeatCtx => {
281
   * repeatCtx.moveBy(10, 0, 10);
282
   * repeatCtx.moveBy(0, 10, 10);
283
   * }, 5);
284
   * ```
285
   * @param repeatBuilder The builder to specify the repeatable list of actions
286
   * @param times  The number of times to repeat all the previous actions in the action queue. If nothing is specified the actions
287
   * will repeat forever
288
   */
289
  public repeat(repeatBuilder: (repeatContext: ActionContext) => any, times?: number): ActionContext {
290
    return this._getCtx().repeat(repeatBuilder, times);
8✔
291
  }
292

293
  /**
294
   * This method will cause the actor to repeat all of the actions built in
295
   * the `repeatBuilder` callback. If the number of repeats
296
   * is not specified it will repeat forever. This method is part of
297
   * the actor 'Action' fluent API allowing action chaining
298
   *
299
   * ```typescript
300
   * // Move up in a zig-zag by repeated moveBy's
301
   * actor.actions.repeat(repeatCtx => {
302
   * repeatCtx.moveBy(10, 0, 10);
303
   * repeatCtx.moveBy(0, 10, 10);
304
   * }, 5);
305
   * ```
306
   * @param repeatBuilder The builder to specify the repeatable list of actions
307
   */
308
  public repeatForever(repeatBuilder: (repeatContext: ActionContext) => any): ActionContext {
309
    return this._getCtx().repeatForever(repeatBuilder);
5✔
310
  }
311

312
  /**
313
   * This method will cause the entity to follow another at a specified distance
314
   * @param entity           The entity to follow
315
   * @param followDistance  The distance to maintain when following, if not specified the actor will follow at the current distance.
316
   */
317
  public follow(entity: Actor, followDistance?: number): ActionContext {
318
    return this._getCtx().follow(entity, followDistance);
1✔
319
  }
320

321
  /**
322
   * This method will cause the entity to move towards another until they
323
   * collide "meet" at a specified speed.
324
   * @param entity  The entity to meet
325
   * @param speed  The speed in pixels per second to move, if not specified it will match the speed of the other actor
326
   */
327
  public meet(entity: Actor, speed?: number): ActionContext {
328
    return this._getCtx().meet(entity, speed);
1✔
329
  }
330

331
  /**
332
   * Returns a promise that resolves when the current action queue up to now
333
   * is finished.
334
   */
335
  public toPromise(): Promise<void> {
336
    return this._getCtx().toPromise();
1✔
337
  }
338
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc