forked from magwo/elevatorsaga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
86 lines (76 loc) · 2.59 KB
/
base.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
// Console shim
(function () {
var f = function () {};
if (!window.console) {
window.console = {
log:f, info:f, warn:f, debug:f, error:f
};
}
}());
var limitNumber = function(num, min, max) {
return Math.min(max, Math.max(num, min));
}
var epsilonEquals = function(a, b) {
return Math.abs(a-b) < 0.00000001;
}
// Polyfill from MDN
var sign = function(x) {
x = +x // convert to a number
if (x === 0 || isNaN(x))
return x
return x > 0 ? 1 : -1
}
if(typeof Math.sign === "undefined") {
Math.sign = sign;
}
var deprecationWarning = function(name) {
console.warn("You are using a deprecated feature scheduled for removal: " + name);
}
var createBoolPassthroughFunction = function(owner, obj, objPropertyName) {
return function(val) {
if(typeof val !== "undefined") {
obj[objPropertyName] = val ? true : false;
obj.trigger("change:" + objPropertyName, obj[objPropertyName]);
return owner;
} else {
return obj[objPropertyName];
}
};
};
// Simple Promise pattern using riot.observable
function Promise() {
var self = riot.observable(this);
self.resolution = null;
self.result = null;
$.map(['done', 'fail', 'always'], function(name) {
self[name] = function(arg) {
if($.isFunction(arg)) {
self.one(name, arg);
if(self.resolution === name || (self.resolution !== null && name === 'always')) {
self.trigger(name, self.result);
}
} else {
if(name === 'always') {
throw new Error('Invalid argument for "always" function: ' + arg);
}
if(self.resolution !== null) {
throw new Error('Can not resolve promise - already resolved to ' + self.resolution)
}
self.resolution = name;
self.result = arg;
self.trigger(name, self.result).trigger('always', self.result);
}
return self;
};
});
}
window.distanceNeededToAchieveSpeed = function(currentSpeed, targetSpeed, acceleration) {
// v² = u² + 2a * d
var requiredDistance = (Math.pow(targetSpeed, 2) - Math.pow(currentSpeed, 2)) / (2 * acceleration);
return requiredDistance;
}
window.accelerationNeededToAchieveChangeDistance = function(currentSpeed, targetSpeed, distance) {
// v² = u² + 2a * d
var requiredAcceleration = 0.5 * ((Math.pow(targetSpeed, 2) - Math.pow(currentSpeed, 2)) / distance);
return requiredAcceleration;
}