-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathScaleTo.ts
79 lines (71 loc) · 2.43 KB
/
ScaleTo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { vec } from '../../Math/vector';
import { MotionComponent } from '../../EntityComponentSystem/Components/MotionComponent';
import { TransformComponent } from '../../EntityComponentSystem/Components/TransformComponent';
import { Action } from '../Action';
import { Entity } from '../../EntityComponentSystem/Entity';
export class ScaleTo implements Action {
private _tx: TransformComponent;
private _motion: MotionComponent;
public x: number;
public y: number;
private _startX: number;
private _startY: number;
private _endX: number;
private _endY: number;
private _speedX: number;
private _speedY: number;
private _distanceX: number;
private _distanceY: number;
private _started = false;
private _stopped = false;
constructor(entity: Entity, scaleX: number, scaleY: number, speedX: number, speedY: number) {
this._tx = entity.get(TransformComponent);
this._motion = entity.get(MotionComponent);
this._endX = scaleX;
this._endY = scaleY;
this._speedX = speedX;
this._speedY = speedY;
}
public update(_delta: number): void {
if (!this._started) {
this._started = true;
this._startX = this._tx.scale.x;
this._startY = this._tx.scale.y;
this._distanceX = Math.abs(this._endX - this._startX);
this._distanceY = Math.abs(this._endY - this._startY);
}
if (!(Math.abs(this._tx.scale.x - this._startX) >= this._distanceX)) {
const directionX = this._endY < this._startY ? -1 : 1;
this._motion.scaleFactor.x = this._speedX * directionX;
} else {
this._motion.scaleFactor.x = 0;
}
if (!(Math.abs(this._tx.scale.y - this._startY) >= this._distanceY)) {
const directionY = this._endY < this._startY ? -1 : 1;
this._motion.scaleFactor.y = this._speedY * directionY;
} else {
this._motion.scaleFactor.y = 0;
}
if (this.isComplete()) {
this._tx.scale = vec(this._endX, this._endY);
this._motion.scaleFactor.x = 0;
this._motion.scaleFactor.y = 0;
}
}
public isComplete(): boolean {
return (
this._stopped ||
(Math.abs(this._tx.scale.y - this._startX) >= (this._distanceX - 0.01) &&
Math.abs(this._tx.scale.y - this._startY) >= (this._distanceY - 0.01))
);
}
public stop(): void {
this._motion.scaleFactor.x = 0;
this._motion.scaleFactor.y = 0;
this._stopped = true;
}
public reset(): void {
this._started = false;
this._stopped = false;
}
}