-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
147 lines (134 loc) · 3.94 KB
/
index.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
const canvasX = 550;
const canvasY = 550;
var currentLvl;
var counter = 0;
var boxNumber = 0;
var levelNumber = 0;
var levelSize = 0;
var active = false;
var win = false;
var inGame = false;
var swap = false;
//load assets
function preload() {
loadAllImages();
}
//setup canvas
function setup() {
canvas = createCanvas(canvasX, canvasY);
canvas.parent('canvas1');
document.getElementById("optimizeButton").style.visibility = "hidden";
displayHtmlValues();
background(0);
preventArrowKeyScroll();
drawOnlyWalls();
}
//start new game
function startGame() {
levelNumber = 0;
inGame = true;
newLevel();
}
//generate random level
function randomLevel() {
levelNumber = 0;
inGame = false;
newLevel();
//document.getElementById("startButton").style.visibility = "visible";
//document.getElementById("startButton").disabled = false;
}
//optimize the level
function optimize(iterations) {
optimizeLvl(currentLvl, iterations);
drawAll();
}
//generate a new level
function newLevel() {
//read level size and box number values
inGame ? randomValues() : readHtmlValues();
setHtmlValues();
currentLvl = new Level(levelSize, levelSize, boxNumber);
//randomly remove walls from the level
currentLvl.rip(randomInt(-2, 5));
//generate level
generatePaths(currentLvl);
//if the level is unsolvable, generate a new level
if (currentLvl.trash) {
newLevel();
console.log("trashlevel");
} else {
levelNumber++;
activeSpots = [];
document.getElementById("optimizeButton").style.visibility = "visible";
//randomly optimize the level if in game
if (inGame && boxNumber < 6) {
optimize(randomInt(-1000, 1000));
}
active = true;
win = false;
drawAll();
}
}
//p5 function, called every frame
function draw() {
if (active) {
//draw active spots, that were visited by the player
drawActiveSpots();
//lerp player sprite towards player position
px = (px + currentLvl.playerX) * 0.5;
py = (py + currentLvl.playerY) * 0.5;
drawPlayer(currentLvl);
//check for a win, else swap player sprite every 30 frames
if (win && counter >= 30) {
active = false;
} else if (counter >= 30) {
counter = 0;
swap = !swap;
}
counter++;
} else if (win) {
drawWin();
}
}
//read slider values
function readHtmlValues() {
levelSize = Math.floor(document.getElementById("sizeSlider").value);
boxNumber = Math.floor(document.getElementById("numSlider").value);
if (boxNumber * 2 > (levelSize - 4) * (levelSize - 4) - 2) {
boxNumber = Math.floor(((levelSize - 4) * (levelSize - 4) - 2) / 2);
}
}
//display slider values
function displayHtmlValues() {
document.getElementById("sizeTxt").innerHTML = "level size: " + String(document.getElementById("sizeSlider").value);
document.getElementById("boxTxt").innerHTML = "number of boxes: " + String(document.getElementById("numSlider").value);
}
//set slider values
function setHtmlValues() {
document.getElementById("sizeSlider").value = levelSize;
document.getElementById("numSlider").value = boxNumber;
displayHtmlValues();
}
//set levelSize and boxNumber randomly
function randomValues() {
var random = Math.random();
if (random <= 0.1) {
levelSize = randomInt(7, 9);
boxNumber = randomInt(2, 4);
} else if (random <= 0.3) {
levelSize = randomInt(8, 12);
boxNumber = 3;
} else if (random <= 0.7) {
levelSize = randomInt(9, 13);
boxNumber = 4;
} else if (random <= 0.9) {
levelSize = randomInt(9, 14);
boxNumber = 5;
} else if (random <= 0.96) {
levelSize = randomInt(16, 20);
boxNumber = randomInt(4, 8);
} else if (random <= 1) {
levelSize = randomInt(9, 16);
boxNumber = randomInt(6, 14);
}
}