-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderBackground.js
448 lines (365 loc) · 12.2 KB
/
renderBackground.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Background gradient for advanced sky colours
function setGradient(x, y, w, h, c1, c2)
{
for (var i = y; i <= y + h; i++)
{
var inter = map(i, y, y + h, 0, 1);
var c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
}
// Function to draw cloud objects.
function moveClouds()
{
for(var i = 0; i < clouds.length; i++)
{
//map clouds to player movement
if(clouds[i].y_pos > 300)
{
var moveClouds = map(gameChar_world_x, 0, 3000, 0, 500);
}
else if(clouds[i].y_pos > 150)
{
var moveClouds = map(gameChar_world_x, 0, 3000, 0, 250);
}
else
{
var moveClouds = map(gameChar_world_x, 0, 3000, 0, 125);
}
fill(178, 209, 163, 250);
ellipse(
clouds[i].x_pos+65 + moveClouds,
clouds[i].y_pos-27,
clouds[i].size+40,
clouds[i].size+40);
ellipse(
clouds[i].x_pos+25 + moveClouds,
clouds[i].y_pos-15,
clouds[i].size+30,
clouds[i].size+30);
ellipse(
clouds[i].x_pos-5 + moveClouds,
clouds[i].y_pos,
clouds[i].size+30,
clouds[i].size);
ellipse(
clouds[i].x_pos+85 + moveClouds,
clouds[i].y_pos,
clouds[i].size+30,
clouds[i].size);
ellipse(
clouds[i].x_pos+45 + moveClouds,
clouds[i].y_pos,
clouds[i].size+30,
clouds[i].size);
}
}
// big mountain constructor function
function BigMountain(x, y)
{
this.x = x;
this.y = y;
this.scale = scale;
this.drawBigMountain = function(){
stroke(25, 200, 102);
strokeWeight(4);
line(this.x+780, this.y-100,
this.x+770, this.y-110);
line(this.x+690, this.y-80,
this.x+550, this.y-20);
fill(201,163,64);
strokeWeight(0.5);
stroke(0,1);
beginShape();
vertex(
this.x+400,
this.y+32);
vertex(
this.x+800,
this.y+32);
vertex(
this.x+780,
this.y-100);
vertex(
this.x+770,
this.y-110);
vertex(
this.x+760,
this.y-350);
vertex(
this.x+700,
this.y-350);
vertex(
this.x+690,
this.y-80);
vertex(
this.x+550,
this.y-20);
vertex(
this.x+420,
this.y-18);
vertex(
this.x+410,
this.y-20);
endShape(CLOSE);
stroke(25, 200, 102);
strokeWeight(3);
line(this.x+760, this.y-350,
this.x+700, this.y-350);
line(this.x+700, this.y-25,
this.x+420, this.y-20);
noStroke();
}
}
// small mountain constructor function
function SmallMountain(x, y)
{
this.x = x;
this.y = y;
this.scale = scale;
this.drawSmallMountain = function(){
var perspective = map(gameChar_world_x, 0, 3000, 0, 50);
fill(201,150,64);
stroke(201,125,64);
strokeWeight(1);
beginShape();
vertex(
this.x+400 * 0.5 + perspective,
this.y+32);
vertex(
this.x+800* 0.5 + perspective,
this.y+32);
vertex(
this.x+780* 0.5 + perspective,
this.y-120);
vertex(
this.x+770* 0.5 + perspective,
this.y-130);
vertex(
this.x+760* 0.5 + perspective,
this.y-310);
vertex(
this.x+700* 0.5 + perspective,
this.y-320);
vertex(
this.x+690* 0.5 + perspective,
this.y-80);
vertex(
this.x+600* 0.5 + perspective,
this.y-20);
endShape(CLOSE);
stroke(25, 200, 102);
strokeWeight(4);
line(this.x+764* 0.5 + perspective,
this.y-310,
this.x+704* 0.5 + perspective,
this.y-320);
line(this.x+694* 0.5 + perspective,
this.y-80,
this.x+604* 0.5 + perspective,
this.y-20);
line(this.x+604* 0.5 + perspective,
this.y-20,
this.x+404 * 0.5 + perspective,
this.y+30);
noStroke();
}
}
function drawMountains()
{
for(var i = 0; i < bigMountains.length; i++)
{
bigMountains[i].drawBigMountain();
smallMountains[i].drawSmallMountain();
}
}
// Function to draw dynamic trees
function advancedTrees(leaves_x, leaves_y)
{
strokeWeight(0.2);
fill(154, 127, 94);
rect(leaves_x,floorPos_y,7,-100);
var points = [];
var size = 56;
for(var i = 0; i < 56; i++)
{
var v = createVector(1,random(0.4,0.65));
var r = (PI * 2 * i)/56;
v.rotate(r);
points.push(v);
}
fill(25, 170, 122);
beginShape();
for(var j = 0; j < points.length; j++)
{
var v = p5.Vector.mult(points[j], size);
curveVertex(v.x + leaves_x,v.y + leaves_y);
}
endShape();
}
//background trees
function drawTrees()
{
for (var i = 0; i < bg_trees.length; i++)
{
fill(163, 153, 88);
strokeWeight(0.2);
stroke(0);
rect(bg_trees[i].x_pos,
floorPos_y,
bg_trees[i].width,
bg_trees[i].height);
fill(26,155,150);
stroke(0);
strokeWeight(0.3);
ellipse(bg_trees[i].x_pos + 1,
bg_trees[i].height + bg_trees[i].y_pos,
bg_trees[i].size,
bg_trees[i].size);
}
}
// namek spacechip at flagpole location
function namekShip()
{
stroke(0);
strokeWeight(0.5);
fill(245,245,245);
ellipse(800 + 1800, 290, 200, 220); // main body
triangle(680 + 1800,450, 720 + 1800,347, 735 + 1800,360);// landing spikes
triangle(920 + 1800,450, 880 + 1800,347, 865 + 1800,360);
triangle(755 + 1800,240, 720 + 1800,160, 770 + 1800,225); //eyebrow spikes
triangle(845 + 1800,240, 880 + 1800,160, 830 + 1800,225);
triangle(670 + 1800,290, 740 + 1800,280, 740 + 1800,300); //whiskers
triangle(930 + 1800,290, 860 + 1800,280, 860 + 1800,300);
strokeWeight(3);
fill(125,0,255);
ellipse(800 + 1800, 290, 80, 80); //window
noStroke();
fill(125,95,255);
ellipse(780 + 1800, 280, 20, 20);
}
function sideFire(radius, alpha)
{
// loop through all the flame objects backwards to draw older ones on top
for(var i = sideFlames.length -1; i >= 0; i--)
{
sideFlames[i].move(-30, -1);
sideFlames[i].show();
sideFlames[i].shrink(0.1);
if(sideFlames[i].radius <= 0 )
{
//remove the burnt objects
sideFlames.splice(i, 1);
}
}
var x = random(3000,-1500);
var y = random(0, floorPos_y -10);
var alpha = alpha;
var c = new fire(x, y, radius, alpha);
sideFlames.push(c);
}
//draw canyon fire with radiuis relative to the canyon where it is used
function canyonFire(x_pos, y_pos, radius, alpha)
{
// loop through all the flame objects backwards to draw older ones on top
for(var i = flames.length -1; i >= 0; i--)
{
flames[i].move(random(-3, 3), random(1, 3));
flames[i].show();
flames[i].shrink(0.9);
if(flames[i].radius <= 0 )
{
//remove the burnt objects
flames.splice(i, 1);
}
}
var x = x_pos;
var y = y_pos +50;
var alpha = alpha;
var b = new fire(x, y, radius, alpha);
flames.push(b);
}
// create dynamic fire
// a class uses different syntax from constructor function, main principles remain the same
class fire
{
constructor(tempX, tempY, tempR, alpha)
{
this.x = tempX;
this.y = tempY;
this.radius = tempR;
this.alpha = alpha;
// generate random colours
this.color = color(255);
let r = random(3);
// set the colours
if(r < 1)
{
this.color = color(255,100,20, alpha);
}
else if(r >= 1 && r < 2 )
{
this.color = color(255, 200, 10, alpha);
}
else if(r >= 2 )
{
this.color = color(255, 80, 5, alpha);
}
}
show() {
noStroke();
fill(this.color);
ellipse(this.x, this.y, this.radius);
}
move(dir_x, dir_y) {
this.x += dir_x;
this.y -= dir_y;
}
shrink(scale){
// reduce size as fire rises
this.radius -= scale;
}
}
// ---------------------------------
// Canyon render and check functions
// ---------------------------------
// Function to draw canyon objects.
function drawCanyon(t_canyon)
{
stroke(255, 77, 0);
strokeWeight(10);
line(t_canyon.x_pos,437,t_canyon.x_pos,582);
line(t_canyon.x_pos + t_canyon.width,437,
t_canyon.x_pos + t_canyon.width,582);
noStroke();
fill(255,0,0, 150);
rect(t_canyon.x_pos,432, t_canyon.width,148,t_canyon.radius);
canyonFire(t_canyon.x_pos + t_canyon.width /2, 552, t_canyon.width, 50);
}
// Function to check character is over a canyon.
function checkCanyon(t_canyon)
{
if(gameChar_world_x > t_canyon.x_pos &&
gameChar_world_x < t_canyon.x_pos + t_canyon.width &&
gameChar_y >= 432)
{
// diable player lateral movement
isLeft = false;
isRight = false;
isPlummeting = true;
}
if(isPlummeting == true)
{
gameChar_y += 0.5; // rate of fall down the canyon
}
}
// loop through canyon array and draw all canyon objects while checking player position
function drawAndCheckCanyon()
{
for(var i = 0; i < canyons.length; i++)
{
drawCanyon(canyons[i]);
checkCanyon(canyons[i]);
}
}