-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
411 lines (362 loc) · 10.1 KB
/
game.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Entry point for Ludum Dare 38
// It's going to be an incremental game in the solar system
// GPL3 for whole project
var focusedBody = "solar-system";
var fallbackImg = "fallback";
var attacking = false;
// Called when incrementals update
function update()
{
bodies(function(body) {
if (body.hasOwnProperty("owner")) {
addResources(body.resources, body.owner, 1, body.built);
}
});
ai();
var updateAI = 0.05; // Once every 50 secons
if (Math.random() < updateAI)
{
//
}
if (attacking) {
attackFrame();
}
drawUpdate();
}
function build(name, team, toBody)
{
if (!team) {
team = "player";
}
if (!toBody) {
toBody = focusedBody;
}
var canBuild = true;
$.each(buildable[name], function(resource, count) {
if (count > teams[team].resources[resource]) {
canBuild = false;
if (team == "player") {
// Add a little marker of what's insufficient
// Don't break the loop because we list everything that's insufficient
$("#owned-menu").append(
$("<p>").append(
"Insufficient " + resource
).addClass("no-build").fadeOut(3000, function() {
$(this).remove();
})
);
}
}
});
if (canBuild) {
addResources(buildable[name], team, -1);
var body = getBody(toBody);
if (!body.hasOwnProperty("built")) {
body.built = {};
}
incrementOrOne(body.built, name);
//if (name in buildMultipliers) {
//$.each(buildMultipliers[name], function(resource, multiplier) {
//var yields = body.resources;
//if (yields) {
//yields[resource] = Math.ceil(multiplier * yields[resource]);
//}
//});
//}
draw();
return true;
}
else {
return false;
}
}
// Attacking happens in one-second frames for d r a m a t i c e f f e c t
function attackFrame()
{
var enemy = getBody(focusedBody);
var toDelete = {};
var options = {};
if (!$.isEmptyObject(enemy.built)) {
$.each(ships, function(type, ship) {
if (type in enemy.built) {
options[type] = 0; // Value irrelevant, just need it in key format
}
});
}
if (!$.isEmptyObject(fleet) && !$.isEmptyObject(options)) {
$.each(fleet, function(type, count) {
for (var i=0; i<count; i++) {
if (Math.random() < ships[type].killChance) {
var attackingType = randFromList(Object.keys(options));
if (Math.random() > ships[attackingType].saveChance) { // Is not less than, it WASN'T saved
incrementOrOne(toDelete, attackingType);
if (toDelete[attackingType] >= enemy.built[attackingType]) {
delete options[attackingType];
}
}
}
}
});
// Enemies' returning fire TODO: Refactor with attacking fire
$.each(enemy.built, function(type, count) {
if (type in ships) {
// Enemy ship
for (var i=0; i<count; i++) {
if (Math.random() < ships[type].killChance) {
if (Object.keys(fleet).length) {
var attackingType = randFromList(Object.keys(fleet));
if (Math.random() > ships[attackingType].saveChance) { // Is not less than, it WASN'T saved
fleet[attackingType] -= 1;
if (fleet[attackingType] == 0) {
delete fleet[attackingType];
}
}
}
else {
return false; // Break out of jQuery loop
}
}
}
}
});
}
$.each(toDelete, function(type, count) {
enemy.built[type] -= count;
if (enemy.built[type] == 0) {
delete enemy.built[type];
delete options[type];
}
});
if ($.isEmptyObject(fleet)) {
// We died! :(
attacking = false;
$("#lost").show();
}
else if ($.isEmptyObject(options)) {
// We killed them and we're still alive!
attacking = false;
enemy.owner = "player";
}
draw();
}
function attack()
{
attacking = true;
attackFrame();
}
function drawOnce()
{
// Initialize build menu based on data.js, to be hidden and shown whenever
$.each(buildable, function(item, cost) {
var tooltip = $("<ul>").addClass("tooltip").append($("<p>").append(buildDescriptions[item]));
$("#build-menu").append(tooltip);
$.each(cost, function(resource, count) {
// IDs used to later color things we can't afford red. We can't have spaces.
tooltip.append($("<li>").append(names[resource] + ": " + count).attr("id", item.replace(" ", "-") + resource));
});
$("#build-menu").append($("<li>").append(
$("<a>").append(item).click(function() {
build(item);
}).hover(function(e) {
tooltip.show().css( { top: e.pageY + 5, left: e.pageX + 5 } );
}, function(e) {
tooltip.fadeOut(100)
})
));
});
// Similarly, initialize the interact menu
// (TODO)
}
// I noticed a pattern of drawing a list from a planet object. DRY.
function drawList(obj, field, formatFunc, overrideList)
{
if (overrideList) {
obj = {};
obj[field] = overrideList;
}
$("#l-" + field + " ul").empty();
if (obj && obj.hasOwnProperty(field) && !$.isEmptyObject(obj[field])) {
$("#l-" + field).show();
$.each(obj[field], function(name, count) {
$("#l-" + field + " ul").append($("<li>").append(formatFunc(name, count)));
});
}
else {
$("#l-" + field).hide();
}
}
function draw()
{
// Update the graphics and image map to the new focus
var imgSrc = focusedBody;
if (!(focusedBody in availImgs)) {
imgSrc = fallbackImg;
}
$("#focused-body").attr("src", "assets/" + imgSrc + ".png");
$("#focused-body").attr("usemap", "#" + focusedBody + "-map");
$("#focused-label").html(focusedBody.capitalize());
// Make sure the owned bodies is up to date TODO: Move?
$("#owned").empty();
bodies(function(body, name) {
if (body.owner == "player") {
$("#owned").append($("<li>").append(
$("<a>").attr("href", "#" + name).append(name.capitalize())
));
}
});
// Display appropriate interaction menu
var focusedBodyObj = getBody(focusedBody);
if (focusedBodyObj) {
if (focusedBodyObj.hasOwnProperty("owner") && focusedBodyObj.owner == "player") {
$("#owned-menu").show();
$("#not-owned-menu").hide();
}
else {
$("#not-owned-menu").show();
$("#owned-menu").hide();
}
$("#team-label").show();
var label = "Unclaimed";
if (focusedBodyObj.hasOwnProperty("owner")) {
label = teamNames[focusedBodyObj.owner];
}
if (attacking) {
label += " (Contested!)";
}
$("#team-label").html(label);
}
else {
$("#owned-menu").hide();
$("#not-owned-menu").hide();
$("#team-label").hide();
}
drawList(focusedBodyObj, "moons", function(name, moon) {
return $("<a>").attr("href", "#" + name).append(name.capitalize());
});
drawList(focusedBodyObj, "built", function(name, count) {
var entry = count + " " + name + (count > 1 ? "s" : "");
if (focusedBodyObj.owner == "player" && name in ships) { // TODO: Do this programatically (mabye when we have a fighting system?)
entry = $("<a>").click(function() {
focusedBodyObj.built[name] -= 1;
if (focusedBodyObj.built[name] == 0) {
delete focusedBodyObj.built[name];
}
incrementOrOne(fleet, name);
$("#fleet-tooltip").hide();
draw();
}).hover(function(e) {
$("#fleet-tooltip").show().css( { top: e.pageY + 5, left: e.pageX + 5 } );
}, function(e) {
$("#fleet-tooltip").fadeOut(100)
}).append(entry);
}
return entry;
});
drawList(focusedBodyObj, "resources", function(resource, count) {
return getMultiplied(focusedBodyObj.built, resource, count) + " " + names[resource] + "/s";
});
drawList(null, "fleet", function(name, count) {
return $("<a>").click(function() {
if (focusedBodyObj.owner && focusedBodyObj.owner == "player") {
fleet[name] -= 1;
if (fleet[name] == 0) {
delete fleet[name];
}
incrementOrOne(focusedBodyObj.built, name);
$("#fleet-tooltip-back").hide();
draw();
}
}).hover(function(e) {
$("#fleet-tooltip-back").show().css( { top: e.pageY + 5, left: e.pageX + 5 } );
}, function(e) {
$("#fleet-tooltip-back").fadeOut(100)
}).append(count + " " + name + (count > 1 ? "s" : ""));
}, fleet);
drawUpdate();
}
function drawUpdate()
{
$("#resources").empty();
$.each(teams["player"].resources, function(resource, count) {
$("#resources").append(
$("<li>").attr("title", descriptions[resource]).append(
names[resource] + ": " + count
));
});
// Color things we can't afford red in the tooltips
$.each(buildable, function(item, cost) {
$.each(cost, function(resource, count) {
if (count > teams["player"].resources[resource]) {
$("#" + item.replace(" ", "-") + resource).addClass("no-build");
}
else {
// Don't remember it
$("#" + item.replace(" ", "-") + resource).removeClass("no-build");
}
});
});
// Factions might change while viewing one planet
var sortedByPower = Object.keys(teams).sort(function(a, b) {
return totalResources(teams[b]) - totalResources(teams[a]);
});
drawList(null, "factions", function(index, name) {
return teamNames[name] + " (" + totalResources(teams[name]) + ")";
}, sortedByPower);
}
function hashChange()
{
while (attacking) {
attackFrame();
}
$("#lost").hide();
var lastBody = focusedBody;
focusedBody = window.location.hash.substr(1);
var parentBody = null;
if (!focusedBody || focusedBody == "solar-system") {
focusedBody = "solar-system";
parentBody = lastBody;
}
else {
$.each(planets, function(name, planet) {
if (planet.hasOwnProperty("moons") && planet.moons.hasOwnProperty(focusedBody)) {
parentBody = name;
}
});
if (!parentBody) {
parentBody = "solar-system";
}
}
// Update the stars so that it looks like we've moved, not a static background
document.body.style.backgroundPosition = Math.random() * 800 + "px " + Math.random() * 800 + "px";
$("#back").attr("href", "#" + parentBody);
if (!(parentBody in availImgs)) {
$("#back-fallback").show().html(parentBody.capitalize());
$("#back-img").hide();
}
else {
$("#back-img").show().attr("src", "assets/" + parentBody + ".png");
$("#back-fallback").hide();
}
draw();
}
function init()
{
// TODO: Decide fun interval
setInterval(update, 1000);
drawOnce();
draw();
//imageMapResize();
$("map").imageMapResize();
// This way must be slower! Why do you do it this way?
// The answer is so that the back button functions appropriately.
$(window).on("hashchange", hashChange);
if (window.location.hash) {
hashChange();
}
else {
window.location.hash = "#luna";
hashChange();
}
draw();
}
$(init);