forked from doublespeakgames/adarkroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabricator.js
244 lines (220 loc) · 6.99 KB
/
fabricator.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
/**
* Module that registers the fabricator functionality
*/
const Fabricator = {
_STORES_OFFSET: 0,
name: _('Fabricator'),
Craftables: {
'energy blade': {
name: _('energy blade'),
type: 'weapon',
buildMsg: _("the blade hums, charged particles sparking and fizzing."),
cost: () => ({
'alien alloy': 1
})
},
'fluid recycler': {
name: _('fluid recycler'),
type: 'upgrade',
maximum: 1,
buildMsg: _('water out, water in. waste not, want not.'),
cost: () => ({
'alien alloy': 2
})
},
'cargo drone': {
name: _('cargo drone'),
type: 'upgrade',
maximum: 1,
buildMsg: _('the workhorse of the wanderer fleet.'),
cost: () => ({
'alien alloy': 2
})
},
'kinetic armour': {
name: _('kinetic armour'),
type: 'upgrade',
maximum: 1,
blueprintRequired: true,
buildMsg: _('wanderer soldiers succeed by subverting the enemy\'s rage.'),
cost: () => ({
'alien alloy': 2
})
},
'disruptor': {
name: _('disruptor'),
type: 'weapon',
blueprintRequired: true,
buildMsg: _("somtimes it is best not to fight."),
cost: () => ({
'alien alloy': 1
})
},
'hypo': {
name: _('hypo'),
type: 'tool',
blueprintRequired: true,
buildMsg: _('a handful of hypos. life in a vial.'),
cost: () => ({
'alien alloy': 1
}),
quantity: 5
},
'stim': {
name: _('stim'),
type: 'tool',
blueprintRequired: true,
buildMsg: _('sometimes it is best to fight without restraint.'),
cost: () => ({
'alien alloy': 1
})
},
'plasma rifle': {
name: _('plasma rifle'),
type: 'weapon',
blueprintRequired: true,
buildMsg: _("the peak of wanderer weapons technology, sleek and deadly."),
cost: () => ({
'alien alloy': 1
})
},
'glowstone': {
name: _('glow stone'),
type: 'tool',
blueprintRequired: true,
buildMsg: _('a smooth, perfect sphere. its light is inextinguishable.'),
cost: () => ({
'alien alloy': 1
})
}
},
init: () => {
if (!$SM.get('features.location.fabricator')) {
$SM.set('features.location.fabricator', true);
}
// Create the Fabricator tab
Fabricator.tab = Header.addLocation(_("A Whirring Fabricator"), "fabricator", Fabricator, 'ship');
// Create the Fabricator panel
Fabricator.panel = $('<div>').attr('id', "fabricatorPanel")
.addClass('location');
if (Ship.panel) {
Fabricator.panel.insertBefore(Ship.panel);
}
else {
Fabricator.panel.appendTo('div#locationSlider');
}
$.Dispatch('stateUpdate').subscribe(() => {
Fabricator.updateBuildButtons();
Fabricator.updateBlueprints();
});
Engine.updateSlider();
Fabricator.updateBuildButtons();
},
onArrival: transition_diff => {
Fabricator.setTitle();
Fabricator.updateBlueprints(true);
if(!$SM.get('game.fabricator.seen')) {
Notifications.notify(Fabricator, _('the familiar hum of wanderer machinery coming to life. finally, real tools.'));
$SM.set('game.fabricator.seen', true);
}
AudioEngine.playBackgroundMusic(AudioLibrary.MUSIC_SHIP);
Engine.moveStoresView(null, transition_diff);
},
setTitle: () => {
if(Engine.activeModule == Fabricator) {
document.title = _("A Whirring Fabricator");
}
},
updateBuildButtons: () => {
let section = $('#fabricateButtons');
let needsAppend = false;
if (section.length === 0) {
section = $('<div>').attr({ 'id': 'fabricateButtons', 'data-legend': _('fabricate:') }).css('opacity', 0);
needsAppend = true;
}
for (const [ key, value ] of Object.entries(Fabricator.Craftables)) {
const max = $SM.num(key, value) + 1 > value.maximum;
if (!value.button) {
if (Fabricator.canFabricate(key)) {
const name = _(value.name) + ((value.quantity ?? 1) > 1 ? ` (x${value.quantity})` : '');
value.button = new Button.Button({
id: 'fabricate_' + key,
cost: value.cost(),
text: name,
click: Fabricator.fabricate,
width: '150px',
ttPos: section.children().length > 10 ? 'top right' : 'bottom right'
}).css('opacity', 0).attr('fabricateThing', key).appendTo(section).animate({ opacity: 1 }, 300, 'linear');
}
} else {
// refresh the tooltip
const costTooltip = $('.tooltip', value.button);
costTooltip.empty();
const cost = value.cost();
for (const [ resource, num ] of Object.entries(cost)) {
$("<div>").addClass('row_key').text(_(resource)).appendTo(costTooltip);
$("<div>").addClass('row_val').text(num).appendTo(costTooltip);
}
if (max && value.maxMsg && !value.button.hasClass('disabled')) {
Notifications.notify(Fabricator, value.maxMsg);
}
}
if (max) {
Button.setDisabled(value.button, true);
} else {
Button.setDisabled(value.button, false);
}
}
if (needsAppend && section.children().length > 0) {
section.appendTo(Fabricator.panel).animate({ opacity: 1 }, 300, 'linear');
}
},
updateBlueprints: ignoreStores => {
if(!$SM.get('character.blueprints')) {
return;
}
let blueprints = $('#blueprints');
let needsAppend = false;
if(blueprints.length === 0) {
needsAppend = true;
blueprints = $('<div>').attr({'id': 'blueprints', 'data-legend': _('blueprints')});
}
for (const k in $SM.get('character.blueprints')) {
const id = 'blueprint_' + k.replace(/ /g, '-');
let r = $('#' + id);
if($SM.get(`character.blueprints["${k}"]`) && r.length === 0) {
r = $('<div>').attr('id', id).addClass('blueprintRow').appendTo(blueprints);
$('<div>').addClass('row_key').text(_(k)).appendTo(r);
}
}
if(needsAppend && blueprints.children().length > 0) {
blueprints.prependTo(Fabricator.panel);
}
},
canFabricate: itemKey =>
!Fabricator.Craftables[itemKey].blueprintRequired ||
$SM.get(`character.blueprints['${itemKey}']`),
fabricate: button => {
const thing = $(button).attr('fabricateThing');
const craftable = Fabricator.Craftables[thing];
const numThings = Math.min(0, $SM.get(`stores['${thing}']`, true));
if (craftable.maximum <= numThings) {
return;
}
const storeMod = {};
const cost = craftable.cost();
for (const [ key, value ] of Object.entries(cost)) {
const have = $SM.get(`stores['${key}']`, true);
if (have < value) {
Notifications.notify(Fabricator, _(`not enough ${key}`));
return false;
} else {
storeMod[key] = have - value;
}
}
$SM.setM('stores', storeMod);
$SM.add(`stores['${thing}']`, craftable.quantity ?? 1);
Notifications.notify(Fabricator, craftable.buildMsg);
AudioEngine.playSound(AudioLibrary.CRAFT);
}
};