aboutsummaryrefslogtreecommitdiff
path: root/src/cosmic-cargo/static/draw.js
blob: abce8d72facde5bab197486fde4f05d23436c600 (plain)
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
var username = undefined; // for saving score
function draw() {
    switch(gameState){
        case "title":
            draw_title();
            break;
        case "main":
            draw_main();
            break;
        case "event":
            draw_event();
            break;
        case "shop":
            draw_shop();
            break;
        case "status":
            draw_status();
            break;
        case "event_result":
            draw_event_result();
            break;
        case "shop_result":
            draw_shop_result();
            break;
        case "options":
            draw_options();
            break;
        case "gameover":
            draw_gameover();
            break;
        case "win":
            draw_win();
            break;
        case "setup":
            draw_setup();
            break;
    }
}
function draw_setup(){
    color(3);
    ctx.fillRect(0, 0, width, height);
    var names = party.reduce( (acc, el) => {
        if(acc == ""){
            return el.name
        } else {
            return `${acc}, ${el.name}`
        }
    }, "")
    let text = `You are a space trucker in the distant year 2019. Your latest mission: transport essential cargo from Octilion to a new colony on Replaris. Without a shipment of goods within the next Earth-year, they won't survive. You assemble your most able crew: ${names}.`;
    font(8, text, 3, 7, true);
    font(8, ")press any key)", 30, height - 2);
}
function draw_win(){
    color(3);
    ctx.fillRect(0, 0, width, height);
    // game over text
    font(12, "You've arrived!", 3, 12);
    font(8, "Score:", 3, 24);
    font(8, `${getAliveMembers().length} alive members * 400`, 3, 34);
    font(8, `${ship.cargo} tons of cargo * 100`, 3, 44);
    font(8, `${Math.floor(ship.credits)} credits`, 3, 54);
    font(8, `Took ${ship.current_day} days )${10*(350-ship.current_day)})`, 3, 64);
    var total = getAliveMembers().length*400 + ship.cargo*100 + ship.credits + (10*(350-ship.current_day));
    font(8, `Total: ${Math.floor(total)}`, 3, 120);
    // set score
    if(!submittedScore){
        username = submitScore("cosmic cargo", total, username);
        submittedScore = true
    }
}
function draw_gameover(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    // game over text
    font(12, "Game over!", 3, 12);
    let allDead = getAliveMembers().length == 0;
    if(allDead){
        font(8, "Your crew all died", 3, 24);
    } else {
        font(8, "You ran out of fuel", 3, 24);
    }
    font(8, "Score:", 3, 44);
    font(8, `${Math.floor(ship.credits)} credits`, 3, 54);
    font(8, `Took ${ship.current_day} days`, 3, 64);
    var total = ship.credits + ship.current_day;
    font(8, `Total: ${Math.floor(total)}`, 3, 120);
    if(!submittedScore){
        username = submitScore("cosmic cargo", total, username);
        submittedScore = true
    }
}
function draw_stars(w, h){
    color(1);
    for(var x = 0; x < w; x++){
        for(var y = 0; y < h; y++){
            let chance = 1; // chance% of drawing a star
            if(random_chance(0.01)){
                ctx.fillRect(x, y, 1, 1);
            } 
        }
    }
}
function draw_title(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    // stars
    draw_stars(width, height);
    // title
    font(13, "COSMIC CARGO", 5, 20);
    font(8, "A space trucking game", 3, 30);
    // Only display this if the game is loaded
    if(imagesLoaded){
        font(8, ")press any key)", 27, height - 20);
    }
}
function draw_main(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    // ship rectangle
    draw_stars(width, height/2);
    ctx.beginPath();
    ctx.lineWidth = "2";
    ctx.rect(1, height/2, width-2, height/2 -1);
    ctx.stroke();
    // ship
    if(t % 6 < 3){
        ctx.drawImage(images["ship1"], 20, 28); 
    } else {
        ctx.drawImage(images["ship2"], 20, 28); 
    }
    // map
    ctx.drawImage(images["map"], 0, height/2); 
    // map rectangle
    color(1)
    // Day and distance notification
    font(8, `Day ${ship.current_day}`, 3, 82);
    font(8, `${Math.floor(ship.fuel)} Fuel`, 3, 129);
    font(8, `${ship.distance} lightyears`, 3, 139);
    // map progress
    color(0);
    ctx.lineWidth = "1";
    // Draw dashed
    ctx.setLineDash([2])
    ctx.beginPath();
    ctx.moveTo(9, 104)
    ctx.lineTo(43, 92)
    ctx.lineTo(76, 112)
    ctx.lineTo(98, 81)
    ctx.lineTo(147, 103)
    ctx.stroke();
    // Draw indicators
    ctx.beginPath();
    var coords = line_to_from_progress(.11, 43, 92, 76, 112);
    ctx.arc(coords[0], coords[1], 1.5, 0, 2 * Math.PI);
    ctx.fill();
    ctx.beginPath();
    coords = line_to_from_progress(.11, 98, 81, 147, 103);
    ctx.arc(coords[0], coords[1], 1.5, 0, 2 * Math.PI);
    ctx.fill();
    // Planet 1
    ctx.beginPath();
    ctx.arc(9, 104, 1.5, 0, 2 * Math.PI);
    ctx.fill();
    // Planet 2
    ctx.beginPath();
    ctx.arc(43, 92, 1.5, 0, 2 * Math.PI);
    ctx.fill();
    // Planet 3
    ctx.beginPath();
    ctx.arc(76, 112, 1.5, 0, 2 * Math.PI);
    ctx.fill();
    // Planet 4
    ctx.beginPath();
    ctx.arc(98, 81, 1.5, 0, 2 * Math.PI);
    ctx.fill();
    // Planet 5
    ctx.beginPath();
    ctx.arc(147, 103, 1.5, 0, 2 * Math.PI);
    ctx.fill();

    // Draw solid on top
    let progress = ship.distance / ship.end_distance;
    ctx.setLineDash([0])
    ctx.beginPath();
    ctx.moveTo(9, 104)
    var shipCoords = [-20, -20];
    if(progress < 0.25){
        shipCoords = line_to_from_progress(progress, 9, 104, 43, 92);
        ctx.lineTo(shipCoords[0], shipCoords[1])
    } else {
        ctx.lineTo(43, 92)
        if (progress < 0.5){
            shipCoords = line_to_from_progress(progress - .25, 43, 92, 76, 112);
            ctx.lineTo(shipCoords[0], shipCoords[1])
        }
        else {
            ctx.lineTo(76, 112)
            if(progress < 0.75){
                shipCoords = line_to_from_progress(progress - .5, 76, 112, 98, 81);
                ctx.lineTo(shipCoords[0], shipCoords[1])
            } else {
                ctx.lineTo(98, 81)
                shipCoords = line_to_from_progress(progress - .75, 98, 81, 147, 103);
                ctx.lineTo(shipCoords[0], shipCoords[1])
            }   
        }        
    }
    ctx.stroke();
    draw_ship_indicator(shipCoords);
}
function draw_ship_indicator(coords){
    ctx.beginPath();
    ctx.arc(coords[0], coords[1], 2, 0, 2 * Math.PI);
    ctx.fill();
}
function line_to_from_progress(norm_progress, x1, y1, x2, y2){
    var  newX = x1 + (x2-x1)*4*norm_progress
    var  newY = y1 + (y2-y1)*4*norm_progress
    return [newX, newY]
}
function draw_event(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    font(12, `${currentEvent.name}`, 3, 13);
    font(8, `${currentEvent.desc}`, 3, 25, true);
    let choices = get_choices(currentEvent)
    var i = 0;
    for(let choice of choices){
        font(8, choice, 10, 100 + 10*i);
        if(selectedChoice == i){
            font(8, `~`, 3, 100 + 10*i);
        }
        i++;
    }
}
function draw_options(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    font(12, `Adjust volume`, 5, 13);

    let music_volume = `${audio[music[0]].volume*100}`.replace(/\.\d*/,"")
    let sfx_volume = `${audio[sfx[0]].volume*100}`.replace(/\.\d*/,"")
    font(8, `MUSIC: ${(music_volume)}`, 10, 22);
    if(selectedChoice == 0){
        font(8, `~`, 4, 22);
    }
    font(8, `SFX: ${sfx_volume}`, 10, 32);
    if(selectedChoice == 1){
        font(8, `~`, 4, 32);
    }
}
function draw_status(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    // person list
    var i = 0;
    for(let person of party){
        var c = 2
        if(person.status == "Dead" || person.status == "Missing"){
            c = 0
        } 
        font(8, person.name, 3, 7 + 12*i, false, c);
        font(8, person.status, 100, 7 + 12*i, false, c);
        i++;
    }
    
    font(8, `Cargo: ${ship.cargo} tons`, 3, 106);
    font(8, `Credits: ${ship.credits}`, 3, 118);
    font(8, `Fuel: ${Math.floor(ship.fuel)}`, 3, 130);
    font(8, `Day ${ship.current_day}`, 3, 142);
    font(8, `${ship.distance} of ${ship.end_distance}`, 75, 142);
}
function draw_shop(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    font(12, "Shop", 3, 13);
    font(8, `Welcome to ${currentShop.name}! Enjoy your stay!`, 3, 25, true);
    let choices = shop_choices(currentShop);
    var i = 0;
    for(let choice of choices){
        font(8, choice, 10, 100 + 10*i);
        if(selectedChoice == i){
            font(8, `~`, 4, 100 + 10*i);
        }
        i++;
    }
}
function draw_event_result(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    font(8, `${eventResult}`, 3, 11, true);
    font(8, ")press any key)", 26, height - 8);
}
function draw_shop_result(){
    // background
    color(3);
    ctx.fillRect(0, 0, width, height);
    font(8, `${shopResult}`, 3, 11, true);
    font(8, ")press any key)", 26, height - 8);
}

function split_into_parts(what, size){
    var parts = [];
    var line = "";
    for(var word of what.split(" ")){
        if(line.length + word.length > size){
            parts.push(line.trim());
            line = "";
        }
        line += " " + word;
    }
    if(line){
        parts.push(line);
    }
    return parts;
}
function font(size, what, x, y, wrap = false, c = 2) {
    ctx.font = size + "px Gameboy";
    color(c);
    if(wrap){
        // Match up to 22 characters, making sure to not end a line mid word
        //var parts = what.match(/.{1,21}[\b\.'\?\)]/g); 
        var parts = split_into_parts(what, 21);
        parts.forEach((element, i) => {
            ctx.fillText(element.trim(), x, y + i*size);
        });
    } else {
        ctx.fillText(what, x, y);
    }
}
function color(c) {
    var color = "";
    switch (c){
        case 0:
            color = "#FA0000";
            break;
        case 1:
            color = "#b4b4b4";
            break;
        case 2:
            color = "#fafafa";
            break;
        case 3:
            color = "#000000";
            break;
    }
    ctx.fillStyle = color;
    ctx.strokeStyle = color;
}
function loadImages(imagefiles) {
    loadcount = 0;
    loadtotal = imagefiles.length;
    imagesLoaded = false;
 
    // Load the images
    var loadedimages = [];
    for (var i=0; i<imagefiles.length; i++) {
        // Create the image object
        var image = new Image();
 
        // Add onload event handler
        image.onload = function () {
            loadcount++;
            if (loadcount == loadtotal) {
                // Done loading
                imagesLoaded = true;
            }
        };
        image.src = imagefiles[i];
        loadedimages[i] = image;
    }
    return loadedimages;
}