-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
191 lines (165 loc) · 3.79 KB
/
sketch.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
// Constants
const SPACE_BAR = 32;
const MARGIN = 16;
// Variables
let backgroundColor;
let characterHeight;
let characterWidth;
let canvasHeight;
let canvasWidth;
let spikesTypes;
let floorHeight;
let floorColor;
let character;
let images;
let spikes;
let model;
// Preload
function preload() {
// Loads the images
images = {
character: {
standing: loadImage("character_standing.png"),
jumping: loadImage("character_jumping.png"),
},
spike: {
singleSpike: loadImage("single_spike.png"),
clusterSpike: loadImage("cluster_spike.png"),
},
};
// Creates the spikes array
spikes = [];
// Creates the spikesTypes array
spikesTypes = ["singleSpike", "clusterSpike"];
}
// Setup
function setup() {
// Canvas
canvasWidth = windowWidth - MARGIN;
canvasHeight = windowHeight - MARGIN;
backgroundColor = "#6789B1";
createCanvas(canvasWidth, canvasHeight);
noStroke();
// Background
background(backgroundColor);
// Floor
floorHeight = 32;
floorColor = "#63535B";
fill(floorColor);
rect(0, canvasHeight - floorHeight, canvasWidth, floorHeight);
// Character
characterWidth = images.character.standing.width;
characterHeight = images.character.standing.height;
character = new Character(
50,
canvasHeight - characterHeight - floorHeight,
characterWidth,
characterHeight,
0,
3 * characterHeight,
2.5 * characterHeight,
images.character
);
character.show();
// Initial spike
let spikeType = random(spikesTypes);
let spikeWidth = images.spike[spikeType].width;
let spikeHeight = images.spike[spikeType].height;
let spikeXPos = canvasWidth;
let spikeYPos = canvasHeight - spikeHeight - floorHeight;
spikes.push(
new Spike(
spikeXPos,
spikeYPos,
spikeWidth,
spikeHeight,
spikeType,
2.5,
images.spike
)
);
}
// Draw loop
function draw() {
// Background
background(backgroundColor);
// Floor
noStroke();
fill(floorColor);
rect(0, canvasHeight - floorHeight, canvasWidth, floorHeight);
// Updates the character
character.update(true);
// Generates a spike
generateSpike();
// Updates the spikes
for (let i = 0; i < spikes.length; i++) {
spikes[i].update(true);
if (spikes[i].isOffScreen()) {
spikes.splice(i, 1);
}
}
}
// Clears the keyCode if it has been released
function keyReleased() {
keyCode = 0;
}
// If the window is resized, resizes the canvas
function windowResized() {
canvasWidth = windowWidth - MARGIN;
canvasHeight = windowHeight - MARGIN;
resizeCanvas(canvasWidth, canvasHeight);
}
// If the window loses focus, pauses the game
window.onblur = () => {
// Stops the loop
noLoop();
// Clears the keyCode
keyCode = 0;
};
// If the window gain focus, unpauses the game
window.onfocus = () => {
// Resumes the loop
loop();
};
// If the context menu is opened, clears the keyCode
window.oncontextmenu = () => {
keyCode = 0;
};
// Generates a spike
function generateSpike() {
// 80% chance of generating a single spike, 20% chance of generating a cluster spike
let spikeType = "";
if (random() < 0.6) {
spikeType = "singleSpike";
} else {
spikeType = "clusterSpike";
}
// Generates the spikes positions and sizes
let spikeWidth = images.spike[spikeType].width;
let spikeHeight = images.spike[spikeType].height;
let spikeXPos = canvasWidth;
let spikeYPos = canvasHeight - spikeHeight - floorHeight;
// Returns nothing if the spikes can't be generated because a spikes is too close to it or if there are too many spikes
if (
spikes[spikes.length - 1].xPos > spikeXPos - 6 * spikeWidth ||
spikes.length > 8
) {
return;
}
// Returns nothing if the 5% chance of generating a spike is not met
if (random() > 0.05) {
return;
}
// Generates the spike
let spike = new Spike(
spikeXPos,
spikeYPos,
spikeWidth,
spikeHeight,
spikeType,
2.5,
images.spike
);
// Adds the spike to the array
spikes.push(spike);
}