forked from Code-Bullet/Jump-King
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch-LAPTOP-4GTCNHGO.js
327 lines (251 loc) · 7.62 KB
/
sketch-LAPTOP-4GTCNHGO.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
let width = 0;
let height = 0;
let canvas = null;
let player = null;
let lines = [];
let backgroundImage = null;
let creatingLines = false;
let idleImage = null;
let squatImage = null;
let jumpImage = null;
let oofImage = null;
let run1Image = null;
let run2Image = null;
let run3Image = null;
let fallenImage = null;
let fallImage = null;
let showingLines = false;
let levelImages = [];
let placingPlayer = false;
let playerPlaced = true;
let testingSinglePlayer = false;
let fallSound = null;
let jumpSound = null;
let bumpSound = null;
let landSound = null;
let snowImage = null;
let population = null;
let levelDrawn = false;
let startingPlayerActions = 5;
let increaseActionsByAmount = 5;
let increaseActionsEveryXGenerations = 20;
function preload() {
backgroundImage = loadImage('images/levelImages/1.png')
idleImage = loadImage('images/poses/idle.png')
squatImage = loadImage('images/poses/squat.png')
jumpImage = loadImage('images/poses/jump.png')
oofImage = loadImage('images/poses/oof.png')
run1Image = loadImage('images/poses/run1.png')
run2Image = loadImage('images/poses/run2.png')
run3Image = loadImage('images/poses/run3.png')
fallenImage = loadImage('images/poses/fallen.png')
fallImage = loadImage('images/poses/fall.png')
snowImage = loadImage('images/snow3.png')
for (let i = 1; i <= 43; i++) {
levelImages.push(loadImage('images/levelImages/' + i + '.png'))
}
jumpSound = loadSound('sounds/jump.mp3')
fallSound = loadSound('sounds/fall.mp3')
bumpSound = loadSound('sounds/bump.mp3')
landSound = loadSound('sounds/land.mp3')
}
function setup() {
setupCanvas();
player = new Player();
population = new Population(300);
setupLevels();
jumpSound.playMode('sustain');
fallSound.playMode('sustain');
bumpSound.playMode('sustain');
landSound.playMode('sustain');
// lines.push(new Line(200,height - 80,width - 200, height-80));
// lines.push(new Line(10,height - 500,200, height-500));
// lines.push(new Line(200,height - 100,200, height-500));
}
function drawMousePosition() {
let snappedX = mouseX - mouseX % 20;
let snappedY = mouseY - mouseY % 20;
push();
fill(255, 0, 0)
noStroke();
ellipse(snappedX, snappedY, 5);
if (mousePos1 != null) {
stroke(255, 0, 0)
strokeWeight(5)
line(mousePos1.x, mousePos1.y, snappedX, snappedY)
}
pop();
}
let levelNumber = 0;
function draw() {
background(10);
// if(frameCount % 5==0 ){
//
// levelNumber = (levelNumber +1)%43;
// }
// image(backgroundImage,0,0);
// if (!creatingLines) {
// if (!placingPlayer || playerPlaced) {
//
// player.Update();
// player.Show();
// }
// } else {
// image(levelImages[levelNumber], 0, 0)
// }
push()
translate(0,50);
if(testingSinglePlayer) {
image(levels[player.currentLevelNo].levelImage, 0, 0)
player.Update();
player.Show();
}else {
if(population.AllPlayersFinished()){
population.NaturalSelection();
if(population.gen % increaseActionsEveryXGenerations ===0){
population.IncreasePlayerMoves(increaseActionsByAmount);
}
}
population.Update()
population.Update()
population.Update()
population.Show();
}
if (showingLines || creatingLines)
showLines();
if (creatingLines)
drawMousePosition();
if (frameCount % 15 === 0) {
previousFrameRate = floor(getFrameRate())
}
pop();
fill(0);
noStroke();
rect(0,0,width,50);
textSize(32);
fill(255, 255, 255);
text('FPS: ' + previousFrameRate, width - 160, 35);
text('Gen: ' + population.gen, 30, 35);
text('Moves: ' + population.players[0].brain.instructions.length, 200, 35);
text('Best Height: ' + population.bestHeight, 400, 35);
}
let previousFrameRate = 60;
function showLevel(levelNumberToShow) {
// print(levelNumberToShow)
image(levels[levelNumberToShow].levelImage, 0, 0)
}
function showLines() {
if (creatingLines) {
for (let l of lines) {
l.Show();
}
} else {
for (let l of levels[player.currentLevelNo].lines) {
l.Show();
}
}
}
function setupCanvas() {
canvas = createCanvas(1200, 950);
canvas.parent('canvas');
width = canvas.width;
height = canvas.height-50;
}
function keyPressed() {
switch(key){
case ' ':
player.jumpHeld = true
break;
case 'R':
population.ResetAllPlayers()
break;
case 'S':
bumpSound.stop();
jumpSound.stop();
landSound.stop();
fallSound.stop();
break;
}
switch (keyCode) {
case LEFT_ARROW:
player.leftHeld = true;
break;
case RIGHT_ARROW:
player.rightHeld = true;
break;
}
}
function keyReleased() {
switch (key) {
case ' ':
if (!creatingLines) {
player.jumpHeld = false
player.Jump()
}
break;
case 'R':
if (creatingLines) {
lines = [];
linesString = "";
mousePos1 = null;
mousePos2 = null;
}
break;
case 'N':
if (creatingLines) {
levelNumber += 1;
linesString += '\nlevels.push(tempLevel);';
linesString += '\ntempLevel = new Level();';
print(linesString);
lines = [];
linesString = '';
mousePos1 = null;
mousePos2 = null;
} else {
player.currentLevelNo += 1;
print(player.currentLevelNo);
}
break;
case 'D':
if (creatingLines) {
mousePos1 = null;
mousePos2 = null;
}
}
switch (keyCode) {
case LEFT_ARROW:
player.leftHeld = false;
break;
case RIGHT_ARROW:
player.rightHeld = false;
break;
}
}
let mousePos1 = null;
let mousePos2 = null;
let linesString = "";
function mouseClicked() {
if (creatingLines) {
let snappedX = mouseX - mouseX % 20;
let snappedY = mouseY - mouseY % 20;
if (mousePos1 == null) {
mousePos1 = createVector(snappedX, snappedY);
} else {
mousePos2 = createVector(snappedX, snappedY);
// print('tempLevel.lines.push(new Line(' + mousePos1.x + ',' + mousePos1.y + ',' + mousePos2.x + ',' + mousePos2.y + '));');
lines.push(new Line(mousePos1.x, mousePos1.y, mousePos2.x, mousePos2.y));
linesString += '\ntempLevel.lines.push(new Line(' + mousePos1.x + ',' + mousePos1.y + ',' + mousePos2.x + ',' + mousePos2.y + '));';
mousePos1 = null;
mousePos2 = null;
}
} else if (placingPlayer && !playerPlaced) {
playerPlaced = true;
player.currentPos = createVector(mouseX, mouseY);
}
}
// things to do
// - when a player lands in a new level, record the game state and start the next evolution at that point
// - when a player falls into a previous level, end the players movements, and mutate that move which fucked them up with a 100% chance
// - add a player replay, we could also include a generation replay, thats probably it
// - maybe consider adding a goal system for really hard levels.
// - we might not need to tell the people about that.