Skip to content

Commit

Permalink
fix: Rename delta to elapsedMs (#3200)
Browse files Browse the repository at this point in the history
Switches `delta` to `elapsedMs` for greater readability and communication of units
  • Loading branch information
eonarheim authored Sep 3, 2024
1 parent 23cfb6d commit 167577f
Show file tree
Hide file tree
Showing 54 changed files with 284 additions and 278 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

- `PreDrawEvent`, `PostDrawEvent`, `PreTransformDrawEvent`, `PostTransformDrawEvent`, `PreUpdateEvent`, `PostUpdateEvent` now use `elapsedMs` instead of `delta` for the elapsed milliseconds between the last frame.460696
- `Trigger` API has been slightly changed:
- `action` now returns the triggering entity: `(entity: Entity) => void`
- `target` now works in conjunction with `filter` instead of overwriting it.
Expand Down Expand Up @@ -119,6 +120,7 @@ are doing mtv adjustments during precollision.

### Updates

- Non-breaking parameters that reference `delta` to `elapsedMs` to better communicate intent and units
- Perf improvements to `ex.ParticleEmitter`
* Use the same integrator as the MotionSystem in the tight loop
* Leverage object pools to increase performance and reduce allocations
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Entity } from '../EntityComponentSystem/Entity';
*/
export interface Action {
id: number;
update(delta: number): void;
update(elapsedMs: number): void;
isComplete(entity: Entity): boolean;
reset(): void;
stop(): void;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/ActionSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class ActionSequence implements Action {
this._sequenceBuilder(this._sequenceContext);
}

public update(delta: number): void {
this._actionQueue.update(delta);
public update(elapsedMs: number): void {
this._actionQueue.update(elapsedMs);
}

public isComplete(): boolean {
Expand Down
6 changes: 3 additions & 3 deletions src/engine/Actions/Action/Blink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Blink implements Action {
this._duration = (timeVisible + timeNotVisible) * numBlinks;
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._elapsedTime = 0;
Expand All @@ -29,8 +29,8 @@ export class Blink implements Action {
return;
}

this._elapsedTime += delta;
this._totalTime += delta;
this._elapsedTime += elapsedMs;
this._totalTime += elapsedMs;
if (this._graphics.visible && this._elapsedTime >= this._timeVisible) {
this._graphics.visible = false;
this._elapsedTime = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/CallMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CallMethod implements Action {
this._method = method;
}

public update(_delta: number) {
public update(elapsedMs: number) {
this._method();
this._hasBeenCalled = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/Delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export class Delay implements Action {
this._delay = delay;
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
}

this._elapsedTime += delta;
this._elapsedTime += elapsedMs;
}

isComplete(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/Die.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Die implements Action {
this._entity = entity;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
this._entity.get(ActionsComponent).clearActions();
this._entity.kill();
this._stopped = true;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/Actions/Action/EaseBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export class EaseBy implements Action {
this._lerpEnd = this._lerpStart.add(this._offset);
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (!this._initialized) {
this._initialize();
this._initialized = true;
}

// Need to update lerp time first, otherwise the first update will always be zero
this._currentLerpTime += delta;
this._currentLerpTime += elapsedMs;
let newX = this._tx.pos.x;
let newY = this._tx.pos.y;
if (this._currentLerpTime < this._lerpDuration) {
Expand All @@ -60,7 +60,7 @@ export class EaseBy implements Action {
newY = this.easingFcn(this._currentLerpTime, this._lerpStart.y, this._lerpEnd.y, this._lerpDuration);
}
// Given the lerp position figure out the velocity in pixels per second
this._motion.vel = vec((newX - this._tx.pos.x) / (delta / 1000), (newY - this._tx.pos.y) / (delta / 1000));
this._motion.vel = vec((newX - this._tx.pos.x) / (elapsedMs / 1000), (newY - this._tx.pos.y) / (elapsedMs / 1000));
} else {
this._tx.pos = vec(this._lerpEnd.x, this._lerpEnd.y);
this._motion.vel = Vector.Zero;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/Actions/Action/EaseTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export class EaseTo implements Action {
this._currentLerpTime = 0;
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (!this._initialized) {
this._initialize();
this._initialized = true;
}

// Need to update lerp time first, otherwise the first update will always be zero
this._currentLerpTime += delta;
this._currentLerpTime += elapsedMs;
let newX = this._tx.pos.x;
let newY = this._tx.pos.y;
if (this._currentLerpTime < this._lerpDuration) {
Expand All @@ -58,7 +58,7 @@ export class EaseTo implements Action {
newY = this.easingFcn(this._currentLerpTime, this._lerpStart.y, this._lerpEnd.y, this._lerpDuration);
}
// Given the lerp position figure out the velocity in pixels per second
this._motion.vel = vec((newX - this._tx.pos.x) / (delta / 1000), (newY - this._tx.pos.y) / (delta / 1000));
this._motion.vel = vec((newX - this._tx.pos.x) / (elapsedMs / 1000), (newY - this._tx.pos.y) / (elapsedMs / 1000));
} else {
this._tx.pos = vec(this._lerpEnd.x, this._lerpEnd.y);
this._motion.vel = Vector.Zero;
Expand Down
12 changes: 6 additions & 6 deletions src/engine/Actions/Action/Fade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ export class Fade implements Action {

private _endOpacity: number;
private _speed: number;
private _ogspeed: number;
private _originalSpeed: number;
private _multiplier: number = 1;
private _started = false;
private _stopped = false;

constructor(entity: Entity, endOpacity: number, speed: number) {
this._graphics = entity.get(GraphicsComponent);
this._endOpacity = endOpacity;
this._speed = this._ogspeed = speed;
this._speed = this._originalSpeed = speed;
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (!this._graphics) {
return;
}

if (!this._started) {
this._started = true;
this._speed = this._ogspeed;
this._speed = this._originalSpeed;

// determine direction when we start
if (this._endOpacity < this._graphics.opacity) {
Expand All @@ -40,10 +40,10 @@ export class Fade implements Action {
}

if (this._speed > 0) {
this._graphics.opacity += (this._multiplier * (Math.abs(this._graphics.opacity - this._endOpacity) * delta)) / this._speed;
this._graphics.opacity += (this._multiplier * (Math.abs(this._graphics.opacity - this._endOpacity) * elapsedMs)) / this._speed;
}

this._speed -= delta;
this._speed -= elapsedMs;

if (this.isComplete()) {
this._graphics.opacity = this._endOpacity;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/Flash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Flash implements Action {
this._total = duration;
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._total = this._duration;
Expand All @@ -53,7 +53,7 @@ export class Flash implements Action {
return;
}

this._currentDuration -= delta;
this._currentDuration -= elapsedMs;

if (this._graphics) {
this._material?.update((shader: Shader) => {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/Follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Follow implements Action {
this._speed = 0;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._distanceBetween = this._current.distance(this._end);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/Meet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Meet implements Action {
}
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._distanceBetween = this._current.distance(this._end);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/MoveBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class MoveBy implements Action {
}
}

public update(_delta: number) {
public update(elapsedMs: number) {
if (!this._started) {
this._started = true;
this._start = new Vector(this._tx.pos.x, this._tx.pos.y);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/MoveTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MoveTo implements Action {
this._speed = speed;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._start = new Vector(this._tx.pos.x, this._tx.pos.y);
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/ParallelActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class ParallelActions implements Action {
this._actions = parallelActions;
}

update(delta: number): void {
update(elapsedMs: number): void {
for (let i = 0; i < this._actions.length; i++) {
this._actions[i].update(delta);
this._actions[i].update(elapsedMs);
}
}
isComplete(entity: Entity): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/Repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export class Repeat implements Action {
this._repeat--; // current execution is the first repeat
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (this._actionQueue.isComplete()) {
this._actionQueue.clearActions();
this._repeatBuilder(this._repeatContext);
this._repeat--;
}
this._actionQueue.update(delta);
this._actionQueue.update(elapsedMs);
}

public isComplete(): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/RepeatForever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RepeatForever implements Action {
this._repeatBuilder(this._repeatContext);
}

public update(delta: number): void {
public update(elapsedMs: number): void {
if (this._stopped) {
return;
}
Expand All @@ -33,7 +33,7 @@ export class RepeatForever implements Action {
this._repeatBuilder(this._repeatContext);
}

this._actionQueue.update(delta);
this._actionQueue.update(elapsedMs);
}

public isComplete(): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/RotateBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class RotateBy implements Action {
this._rotationType = rotationType || RotationType.ShortestPath;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._start = this._tx.rotation;
Expand Down Expand Up @@ -89,7 +89,7 @@ export class RotateBy implements Action {
}

this._motion.angularVelocity = this._direction * this._speed;
this._currentNonCannonAngle += this._direction * this._speed * (_delta / 1000);
this._currentNonCannonAngle += this._direction * this._speed * (elapsedMs / 1000);

if (this.isComplete()) {
this._tx.rotation = this._end;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/Action/RotateTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RotateTo implements Action {
this._rotationType = rotationType || RotationType.ShortestPath;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._start = this._tx.rotation;
Expand Down Expand Up @@ -85,7 +85,7 @@ export class RotateTo implements Action {
}

this._motion.angularVelocity = this._direction * this._speed;
this._currentNonCannonAngle += this._direction * this._speed * (_delta / 1000);
this._currentNonCannonAngle += this._direction * this._speed * (elapsedMs / 1000);

if (this.isComplete()) {
this._tx.rotation = this._end;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/ScaleBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ScaleBy implements Action {
this._speedX = this._speedY = speed;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._startScale = this._tx.scale.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Actions/Action/ScaleTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ScaleTo implements Action {
this._speedY = speedY;
}

public update(_delta: number): void {
public update(elapsedMs: number): void {
if (!this._started) {
this._started = true;
this._startX = this._tx.scale.x;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/Actions/ActionsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export class ActionsSystem extends System {
}
});
}
update(delta: number): void {
update(elapsedMs: number): void {
for (let i = 0; i < this._actions.length; i++) {
const action = this._actions[i];
action.update(delta);
action.update(elapsedMs);
}
}
}
Loading

0 comments on commit 167577f

Please sign in to comment.