-
Notifications
You must be signed in to change notification settings - Fork 320
/
movable.js
137 lines (122 loc) · 4.04 KB
/
movable.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var EPSILON = 0.00001;
var linearInterpolate = function(value0, value1, x) {
return value0 + (value1 - value0) * x;
};
var powInterpolate = function(value0, value1, x, a) {
return value0 + (value1 - value0) * Math.pow(x, a) / (Math.pow(x, a) + Math.pow(1-x, a));
};
var coolInterpolate = function(value0, value1, x) {
return powInterpolate(value0, value1, x, 1.3);
};
var DEFAULT_INTERPOLATOR = coolInterpolate;
var _tmpPosStorage = [0,0];
function Movable() {
newGuard(this, Movable);
unobservable.Observable.call(this);
var movable = this;
movable.x = 0.0;
movable.y = 0.0;
movable.parent = null;
movable.worldX = 0.0;
movable.worldY = 0.0;
movable.currentTask = null;
movable.trigger('new_state', movable);
}
Movable.prototype = Object.create(unobservable.Observable.prototype);
Movable.prototype.updateDisplayPosition = function(forceTrigger) {
this.getWorldPosition(_tmpPosStorage);
var oldX = this.worldX;
var oldY = this.worldY;
this.worldX = _tmpPosStorage[0];
this.worldY = _tmpPosStorage[1];
if(oldX !== this.worldX || oldY !== this.worldY || forceTrigger === true) {
this.trigger('new_display_state', this);
}
};
Movable.prototype.moveTo = function(newX, newY) {
if(newX !== null) { this.x = newX; }
if(newY !== null) { this.y = newY; }
this.trigger("new_state", this);
};
Movable.prototype.moveToFast = function(newX, newY) {
this.x = newX;
this.y = newY;
this.trigger("new_state", this);
}
Movable.prototype.isBusy = function() {
return this.currentTask !== null;
};
Movable.prototype.makeSureNotBusy = function() {
if(this.isBusy()) {
console.error("Attempt to use movable while it was busy", this);
throw({message: "Object is busy - you should use callback", obj: this});
}
};
Movable.prototype.wait = function(millis, cb) {
this.makeSureNotBusy();
var timeSpent = 0.0;
var self = this;
self.currentTask = function waitTask(dt) {
timeSpent += dt;
if(timeSpent > millis) {
self.currentTask = null;
if(cb) { cb(); }
}
};
};
Movable.prototype.moveToOverTime = function(newX, newY, timeToSpend, interpolator, cb) {
this.makeSureNotBusy();
this.currentTask = true;
if(newX === null) { newX = this.x; }
if(newY === null) { newY = this.y; }
if(typeof interpolator === "undefined") { interpolator = DEFAULT_INTERPOLATOR; }
var origX = this.x;
var origY = this.y;
var timeSpent = 0.0;
var self = this;
self.currentTask = function moveToOverTimeTask(dt) {
timeSpent = Math.min(timeToSpend, timeSpent + dt);
if(timeSpent === timeToSpend) { // Epsilon issues possibly?
self.moveToFast(newX, newY);
self.currentTask = null;
if(cb) { cb(); }
} else {
var factor = timeSpent / timeToSpend;
self.moveToFast(interpolator(origX, newX, factor), interpolator(origY, newY, factor));
}
};
};
Movable.prototype.update = function(dt) {
if(this.currentTask !== null) {
this.currentTask(dt);
}
};
Movable.prototype.getWorldPosition = function(storage) {
var resultX = this.x;
var resultY = this.y;
var currentParent = this.parent;
while(currentParent !== null) {
resultX += currentParent.x;
resultY += currentParent.y;
currentParent = currentParent.parent;
}
storage[0] = resultX;
storage[1] = resultY;
};
Movable.prototype.setParent = function(movableParent) {
var objWorld = [0,0];
if(movableParent === null) {
if(this.parent !== null) {
this.getWorldPosition(objWorld);
this.parent = null;
this.moveToFast(objWorld[0], objWorld[1]);
}
} else {
// Parent is being set a non-null movable
this.getWorldPosition(objWorld);
var parentWorld = [0,0];
movableParent.getWorldPosition(parentWorld);
this.parent = movableParent;
this.moveToFast(objWorld[0] - parentWorld[0], objWorld[1] - parentWorld[1]);
}
};