forked from doublespeakgames/adarkroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.js
346 lines (296 loc) · 10.1 KB
/
path.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
var Path = {
DEFAULT_BAG_SPACE: 10,
_STORES_OFFSET: 0,
// Everything not in this list weighs 1
Weight: {
'bone spear': 2,
'iron sword': 3,
'steel sword': 5,
'rifle': 5,
'bullets': 0.1,
'energy cell': 0.2,
'laser rifle': 5,
'bolas': 0.5
},
name: 'Path',
options: {}, // Nuthin'
init: function(options) {
this.options = $.extend(
this.options,
options
);
// Init the World
World.init();
// Create the path tab
this.tab = Header.addLocation(_("A Dusty Path"), "path", Path);
// Create the Path panel
this.panel = $('<div>').attr('id', "pathPanel")
.addClass('location')
.appendTo('div#locationSlider');
// Add the outfitting area
var outfitting = $('<div>').attr({'id': 'outfitting', 'data-legend': _('supplies:')}).appendTo(this.panel);
$('<div>').attr('id', 'bagspace').appendTo(outfitting);
// Add the embark button
new Button.Button({
id: 'embarkButton',
text: _("embark"),
click: Path.embark,
width: '80px',
cooldown: World.DEATH_COOLDOWN
}).appendTo(this.panel);
Path.outfit = $SM.get('outfit');
Engine.updateSlider();
//subscribe to stateUpdates
$.Dispatch('stateUpdate').subscribe(Path.handleStateUpdates);
},
openPath: function() {
Path.init();
Engine.event('progress', 'path');
Notifications.notify(Room, _('the compass points ' + World.dir));
},
getWeight: function(thing) {
var w = Path.Weight[thing];
if(typeof w != 'number') w = 1;
return w;
},
getCapacity: function() {
if($SM.get('stores.convoy', true) > 0) {
return Path.DEFAULT_BAG_SPACE + 60;
} else if($SM.get('stores.wagon', true) > 0) {
return Path.DEFAULT_BAG_SPACE + 30;
} else if($SM.get('stores.rucksack', true) > 0) {
return Path.DEFAULT_BAG_SPACE + 10;
}
return Path.DEFAULT_BAG_SPACE;
},
getFreeSpace: function() {
var num = 0;
if(Path.outfit) {
for(var k in Path.outfit) {
var n = Path.outfit[k];
if(isNaN(n)) {
// No idea how this happens, but I will fix it here!
Path.outfit[k] = n = 0;
}
num += n * Path.getWeight(k);
}
}
return Path.getCapacity() - num;
},
updatePerks: function(ignoreStores) {
if($SM.get('character.perks')) {
var perks = $('#perks');
var needsAppend = false;
if(perks.length === 0) {
needsAppend = true;
perks = $('<div>').attr({'id': 'perks', 'data-legend': _('perks:')});
}
for(var k in $SM.get('character.perks')) {
var id = 'perk_' + k.replace(' ', '-');
var r = $('#' + id);
if($SM.get('character.perks["'+k+'"]') && r.length === 0) {
r = $('<div>').attr('id', id).addClass('perkRow').appendTo(perks);
$('<div>').addClass('row_key').text(_(k)).appendTo(r);
$('<div>').addClass('tooltip bottom right').text(Engine.Perks[k].desc).appendTo(r);
}
}
if(needsAppend && perks.children().length > 0) {
perks.prependTo(Path.panel);
}
if(!ignoreStores && Engine.activeModule === Path) {
$('#storesContainer').css({top: perks.height() + 26 + Path._STORES_OFFSET + 'px'});
}
}
},
updateOutfitting: function() {
var outfit = $('div#outfitting');
if(!Path.outfit) {
Path.outfit = {};
}
// Add the armour row
var armour = _("none");
if($SM.get('stores["s armour"]', true) > 0)
armour = _("steel");
else if($SM.get('stores["i armour"]', true) > 0)
armour = _("iron");
else if($SM.get('stores["l armour"]', true) > 0)
armour = _("leather");
var aRow = $('#armourRow');
if(aRow.length === 0) {
aRow = $('<div>').attr('id', 'armourRow').addClass('outfitRow').prependTo(outfit);
$('<div>').addClass('row_key').text(_('armour')).appendTo(aRow);
$('<div>').addClass('row_val').text(armour).appendTo(aRow);
$('<div>').addClass('clear').appendTo(aRow);
} else {
$('.row_val', aRow).text(armour);
}
// Add the water row
var wRow = $('#waterRow');
if(wRow.length === 0) {
wRow = $('<div>').attr('id', 'waterRow').addClass('outfitRow').insertAfter(aRow);
$('<div>').addClass('row_key').text(_('water')).appendTo(wRow);
$('<div>').addClass('row_val').text(World.getMaxWater()).appendTo(wRow);
$('<div>').addClass('clear').appendTo(wRow);
} else {
$('.row_val', wRow).text(World.getMaxWater());
}
var space = Path.getFreeSpace();
var total = 0;
// Add the non-craftables to the craftables
var carryable = $.extend({
'cured meat': { type: 'tool' },
'bullets': { type: 'tool' },
'grenade': {type: 'weapon' },
'bolas': {type: 'weapon' },
'laser rifle': {type: 'weapon' },
'energy cell': {type: 'tool' },
'bayonet': {type: 'weapon' },
'charm': {type: 'tool'},
'medicine': {type: 'tool'}
}, Room.Craftables);
for(var k in carryable) {
var lk = _(k);
var store = carryable[k];
var have = $SM.get('stores["'+k+'"]');
var num = Path.outfit[k];
num = typeof num == 'number' ? num : 0;
if (have < num) { num = have; }
var numAvailable = $SM.get('stores["'+k+'"]', true);
var row = $('div#outfit_row_' + k.replace(' ', '-'), outfit);
if((store.type == 'tool' || store.type == 'weapon') && have > 0) {
total += num * Path.getWeight(k);
if(row.length === 0) {
row = Path.createOutfittingRow(k, num, store.name);
var curPrev = null;
outfit.children().each(function(i) {
var child = $(this);
if(child.attr('id').indexOf('outfit_row_') === 0) {
var cName = child.children('.row_key').text();
if(cName < lk) {
curPrev = child.attr('id');
}
}
});
if(curPrev == null) {
row.insertAfter(wRow);
} else {
row.insertAfter(outfit.find('#' + curPrev));
}
} else {
$('div#' + row.attr('id') + ' > div.row_val > span', outfit).text(num);
$('div#' + row.attr('id') + ' .tooltip .numAvailable', outfit).text(numAvailable - num);
}
if(num === 0) {
$('.dnBtn', row).addClass('disabled');
$('.dnManyBtn', row).addClass('disabled');
} else {
$('.dnBtn', row).removeClass('disabled');
$('.dnManyBtn', row).removeClass('disabled');
}
if(num >= numAvailable || space < Path.getWeight(k)) {
$('.upBtn', row).addClass('disabled');
$('.upManyBtn', row).addClass('disabled');
} else if(space >= Path.getWeight(k)) {
$('.upBtn', row).removeClass('disabled');
$('.upManyBtn', row).removeClass('disabled');
}
} else if(have === 0 && row.length > 0) {
row.remove();
}
}
// Update bagspace
$('#bagspace').text(_('free {0}/{1}', Math.floor(Path.getCapacity() - total) , Path.getCapacity()));
if(Path.outfit['cured meat'] > 0) {
Button.setDisabled($('#embarkButton'), false);
} else {
Button.setDisabled($('#embarkButton'), true);
}
},
createOutfittingRow: function(key, num, name) {
if(!name) name = _(key);
var row = $('<div>').attr('id', 'outfit_row_' + key.replace(' ', '-')).addClass('outfitRow').attr('key',key);
$('<div>').addClass('row_key').text(name).appendTo(row);
var val = $('<div>').addClass('row_val').appendTo(row);
$('<span>').text(num).appendTo(val);
$('<div>').addClass('upBtn').appendTo(val).click([1], Path.increaseSupply);
$('<div>').addClass('dnBtn').appendTo(val).click([1], Path.decreaseSupply);
$('<div>').addClass('upManyBtn').appendTo(val).click([10], Path.increaseSupply);
$('<div>').addClass('dnManyBtn').appendTo(val).click([10], Path.decreaseSupply);
$('<div>').addClass('clear').appendTo(row);
var numAvailable = $SM.get('stores["'+key+'"]', true);
var tt = $('<div>').addClass('tooltip bottom right').appendTo(row);
$('<div>').addClass('row_key').text(_('weight')).appendTo(tt);
$('<div>').addClass('row_val').text(Path.getWeight(key)).appendTo(tt);
$('<div>').addClass('row_key').text(_('available')).appendTo(tt);
$('<div>').addClass('row_val').addClass('numAvailable').text(numAvailable).appendTo(tt);
return row;
},
increaseSupply: function(btn) {
var supply = $(this).closest('.outfitRow').attr('key');
Engine.log('increasing ' + supply + ' by up to ' + btn.data);
var cur = Path.outfit[supply];
cur = typeof cur == 'number' ? cur : 0;
if(Path.getFreeSpace() >= Path.getWeight(supply) && cur < $SM.get('stores["'+supply+'"]', true)) {
var maxExtraByWeight = Math.floor(Path.getFreeSpace() / Path.getWeight(supply));
var maxExtraByStore = $SM.get('stores["'+supply+'"]', true) - cur;
var maxExtraByBtn = btn.data;
Path.outfit[supply] = cur + Math.min(maxExtraByBtn, Math.min(maxExtraByWeight, maxExtraByStore));
$SM.set('outfit['+supply+']', Path.outfit[supply]);
Path.updateOutfitting();
}
},
decreaseSupply: function(btn) {
var supply = $(this).closest('.outfitRow').attr('key');
Engine.log('decreasing ' + supply + ' by up to ' + btn.data);
var cur = Path.outfit[supply];
cur = typeof cur == 'number' ? cur : 0;
if(cur > 0) {
Path.outfit[supply] = Math.max(0, cur - btn.data);
$SM.set('outfit['+supply+']', Path.outfit[supply]);
Path.updateOutfitting();
}
},
onArrival: function(transition_diff) {
Path.setTitle();
Path.updateOutfitting();
Path.updatePerks(true);
$SM.set('outfit', Path.outfit);
Engine.moveStoresView($('#perks'), transition_diff);
},
setTitle: function() {
document.title = _('A Dusty Path');
},
embark: function() {
for(var k in Path.outfit) {
$SM.add('stores["'+k+'"]', -Path.outfit[k]);
}
$SM.remove('outfit');
World.onArrival();
$('#outerSlider').animate({left: '-700px'}, 300);
Engine.activeModule = World;
},
handleStateUpdates: function(e){
if(e.category == 'character' && e.stateName.indexOf('character.perks') === 0 && Engine.activeModule == Path){
Path.updatePerks();
};
},
scrollSidebar: function(direction, reset){
if( typeof reset != "undefined" ){
$('#perks').css('top', '0px');
$('#storesContainer').css('top', '206px');
Path._STORES_OFFSET = 0;
return;
}
var momentum = 10;
if( direction == 'up' )
momentum = momentum * -1
if( direction == 'down' && inView( direction, $('#perks') ) ){
return false;
}else if( direction == 'up' && inView( direction, $('#storesContainer') ) ){
return false;
}
scrollByX( $('#perks'), momentum );
scrollByX( $('#storesContainer'), momentum );
Path._STORES_OFFSET += momentum;
}
};