-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelGenerator.html
210 lines (166 loc) · 5.12 KB
/
levelGenerator.html
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
<!DOCTYPE html>
<html>
<head>
<title>Roller Splat Level Generator</title>
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<style>
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
padding: 0;
margin: 0;
font-family: monospace;
background-color: #e4e4e4;
}
.container {
min-height: 100vh;
width: 100%;
display: flex;
justify-content: space-between;
padding: 2rem;
}
.row {
width: 100%;
}
.controllers {
display: flex;
gap: 2rem;
margin-bottom: 2rem;
}
.num {
font-weight: bold;
}
.tiles-holder {
display: grid;
width: fit-content;
height: fit-content;
border: solid 1px rgb(0, 0, 0);
}
.tile {
appearance: unset;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
width: 20px;
aspect-ratio: 1/1;
border: solid 1px rgb(0, 0, 0);
background-color: #fff;
}
.tile:checked { /*road*/
background-color: rgb(61, 61, 61);
}
.tile:disabled { /*border tile*/
cursor: not-allowed;
}
.tile:disabled::before { /*border tile*/
content: "×";
font-size: 1rem;
color: rgb(204, 204, 204);
}
.row:nth-child(2){
width: 80%;
}
.code {
width: 100%;
height: 100%;
padding: 1rem;
font-size: 0.6rem;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="controllers">
<div>
Rows <span class="num rows">5</span><br>
<input class="range rows-input" type="range" min="5" max="20" value="5">
</div>
<div>
Columns <span class="num columns">5</span><br>
<input class="range columns-input" type="range" min="5" max="20" value="5">
</div>
</div>
<div class="tiles-holder"></div>
</div>
<div class="row">
<textarea class="code"></textarea>
</div>
</div>
<script>
const tilesHolder = document.querySelector(".tiles-holder")
const inputRows = document.querySelector(".rows-input")
const inputColumns = document.querySelector(".columns-input")
const numRows = document.querySelector(".rows")
const numColumns = document.querySelector(".columns")
const codeTextarea = document.querySelector(".code")
inputRows.addEventListener("input", _=> numRows.innerText = inputRows.value)
inputColumns.addEventListener("input", _=> numColumns.innerText = inputColumns.value)
inputRows.addEventListener("change", drawTiles)
inputColumns.addEventListener("change", drawTiles)
let tiles
let rows, columns
drawTiles()
function drawTiles(){
rows = inputRows.value
columns = inputColumns.value
removeTiles();
tiles = []
for (let i=0; i<rows; i++){
let tilesRow = []
for (let j=0; j<columns; j++){
let tile = document.createElement("input")
tile.setAttribute("type","checkbox")
tile.setAttribute("class","tile")
tile.setAttribute("data-i",i)
tile.setAttribute("data-j",j)
if (isBorderTile(i,j)){
tile.disabled = true
tilesRow = [...tilesRow, 1]
}else{
tile.checked = true
tilesRow = [...tilesRow, 0]
}
tilesHolder.appendChild(tile)
}
tiles = [...tiles, tilesRow]
}
tilesHolder.style.gridTemplateRows = `repeat(${rows}, 1fr)`
tilesHolder.style.gridTemplateColumns = `repeat(${columns}, 1fr)`
}
function isBorderTile(i, j){
return i==0 || j==0 || i==rows-1 || j==columns-1
}
function removeTiles(){
while (tilesHolder.firstChild)
tilesHolder.removeChild(tilesHolder.firstChild)
}
tilesHolder.addEventListener("click", e=>{
if (e.target.classList.contains("tile")){
let i = parseInt(e.target.getAttribute("data-i"))
let j = parseInt(e.target.getAttribute("data-j"))
tiles[i][j] = (e.target.checked)?0:1
codeTextarea.value = generateCodeString()
}
})
function generateCodeString(){
let code = "[\n\t"
for (let i=0; i<rows; i++){
code += "["
for (let j=0; j<columns; j++){
code += tiles[i][j]
if (j<columns-1)
code += ","
}
code += (i<rows-1)?"],\n\t":"]\n"
}
code += "],"
return code
}
</script>
</body>
</html>