-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
246 lines (210 loc) · 5.99 KB
/
controller.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'use strict';
var app = angular.module('KerbalTech', []);
app.controller('KerbalTechController', ['$scope', '$http', function($scope,$http)
{
window.$scope = $scope;
$scope.techs = {};
$scope.activeTech = 'Start';
$scope.hoveringPart = null;
$scope.purchases = [
parseInt(window.location.hash.slice(-16,-8), 16) || 0,
parseInt(window.location.hash.slice(-8), 16) || 0
];
$http.get('techs.json').then(function(res)
{
$scope.techs = res.data;
for(var i in $scope.techs)
$scope.techs[i].value = [
parseInt($scope.techs[i].value.slice(-16,-8), 16) || 0,
parseInt($scope.techs[i].value.slice(-8), 16) || 0
];
});
$scope.$watchCollection('purchases', function(newval){
if(window.location.hash || newval[0] || newval[1]){
var part1 = ('00000000'+(newval[0]>>>16).toString(16)).slice(-4);
var part2 = ('00000000'+(newval[0]&0xffff).toString(16)).slice(-4);
var part3 = ('00000000'+(newval[1]>>>16).toString(16)).slice(-4);
var part4 = ('00000000'+(newval[1]&0xffff).toString(16)).slice(-4);
console.log(part1, part2, part3, part4);
window.location.hash = '#'+part1+part2+part3+part4;
}
});
$scope.sanitize = function(input){
return input.replace(/\./, '');
};
$scope.isPurchased = function(val){
if(val){
return $scope.purchases[0] & val[0] || $scope.purchases[1] & val[1];
}
else return false;
};
$scope.togglePurchase = function(val){
$scope.purchases[0] = $scope.purchases[0] ^ val[0];
$scope.purchases[1] = $scope.purchases[1] ^ val[1];
};
$scope.isAvailable = function(tech)
{
if(tech){
var dep1met = tech.dependencies[0] && $scope.isPurchased($scope.techs[tech.dependencies[0]].value);
var dep2met = tech.dependencies[1] && $scope.isPurchased($scope.techs[tech.dependencies[1]].value);
return !$scope.isPurchased(tech.value) && (
tech.dependencies.length === 0
|| dep1met
|| dep2met
|| tech.allDepsRequired && dep1met && dep2met
);
}
else return false;
};
$scope.totalSpent = function()
{
var total = 0;
for(var i in $scope.techs){
if($scope.isPurchased($scope.techs[i].value)){
total += $scope.techs[i].cost;
}
}
return total;
}
$scope.facilityRequired = function()
{
var max = 0;
for(var i in $scope.techs){
var tech = $scope.techs[i];
if($scope.isPurchased(tech.value) && tech.cost > max){
max = tech.cost;
}
}
return max > 500 ? 3 : max > 100 ? 2 : 1;
}
$scope.showPartPopup = function(part, evt)
{
$scope.hoveringPart = part;
var popup = document.getElementById('partTooltip');
popup.style.display = 'block';
popup.style.top = evt.clientY+'px';
}
$scope.hidePartPopup = function(evt)
{
var popup = document.getElementById('partTooltip');
popup.style.display = '';
}
}]);
app.directive('kspZoomPan', function(){
return {
link: function($scope, elem, attrs)
{
elem = elem[0];
var fieldCenter = [1700,1000];
var zoom = 1;
var offset = [(fieldCenter[0]-elem.clientWidth)/2,0];
function update()
{
var diff = [fieldCenter[0]-elem.clientWidth/2, fieldCenter[1]-elem.clientHeight/2];
// transforms applied top to bottom
elem.children[0].style.transform =
'translate('+-diff[0]+'px,'+-diff[1]+'px) '+
'scale('+zoom+') '+
'translate('+offset[0]+'px,'+offset[1]+'px) '+
'';
}
update();
var dragInit = null;
function dragStart(evt)
{
if(evt.preventDefault)
evt.preventDefault();
dragInit = {
x: evt.clientX,
y: evt.clientY
};
}
function dragEnd(evt){
dragInit = null;
}
function drag(evt){
if(dragInit){
offset[0] = Math.max(-1700, Math.min(1700, offset[0] + (evt.clientX - dragInit.x)/zoom));
offset[1] = Math.max(-1000, Math.min(1000, offset[1] + (evt.clientY - dragInit.y)/zoom));
update();
dragInit.x = evt.clientX;
dragInit.y = evt.clientY;
}
}
elem.addEventListener('mousedown', dragStart);
elem.addEventListener('mousemove', drag);
elem.addEventListener('mouseup', dragEnd);
elem.addEventListener('wheel', function(evt){
evt.preventDefault();
zoom = Math.max(0.3, Math.min(2, evt.deltaY > 0 ? zoom-0.05 : zoom+0.05));
update();
});
var pinchDist = 0, origZoom = 0;
elem.addEventListener('touchstart', function(evt){
if(evt.touches.length === 1)
dragStart(evt.touches[0]);
else if(evt.touches.length >= 2){
var t1 = evt.touches[0], t2 = evt.touches[1];
origZoom = zoom;
pinchDist = Math.sqrt( Math.pow(t1.clientX-t2.clientX,2) + Math.pow(t1.clientY-t2.clientY, 2) );
}
});
elem.addEventListener('touchend', dragEnd);
elem.addEventListener('touchcancel', dragEnd);
elem.addEventListener('touchmove', function(evt){
evt.preventDefault();
if(evt.touches.length === 1){
drag(evt.touches[0]);
}
else if(evt.touches.length >= 2){
var t1 = evt.touches[0], t2 = evt.touches[1];
var newPinchDist = Math.sqrt( Math.pow(t1.clientX-t2.clientX,2) + Math.pow(t1.clientY-t2.clientY, 2) );
zoom = Math.max(0.3, Math.min(2, origZoom * (500+newPinchDist-pinchDist)*0.002));
update();
}
});
}
}
});
app.directive('kspD', function(){
return {
link: function($scope, elem, attrs){
attrs.$observe('kspD', function(newval){
attrs.$set('d', newval);
});
}
}
});
var modal = null;
var animLength = 100;
function about()
{
if(!modal) modal = document.getElementById('modal');
modal.style.opacity = 0;
modal.style.display = '';
var starttime = Date.now();
var finished = setInterval(function(){
var curtime = Date.now();
if(curtime < starttime+animLength)
modal.style.opacity = (curtime-starttime)/animLength;
else {
modal.style.opacity = 1;
clearInterval(finished);
}
}, 0);
}
function okay()
{
modal.style.opacity = 1;
var starttime = Date.now();
var finished = setInterval(function(){
var curtime = Date.now();
if(curtime < starttime+animLength)
modal.style.opacity = 1-((curtime-starttime)/animLength);
else {
modal.style.opacity = 0;
modal.style.display = 'none';
clearInterval(finished);
}
}, 0);
}