-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleveleditor.js
More file actions
300 lines (279 loc) · 10 KB
/
leveleditor.js
File metadata and controls
300 lines (279 loc) · 10 KB
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
if(document.getElementById('yomama').innerHTML == 'Level Editor!'){document.addEventListener('DOMContentLoaded', generateEditorDivs)}
//define variables
var movingArray = []
var direction;
var blocksToMove;
var blockName;
var speedvalue;
var usercreatedseed = ''
var selectedPlacement = 0
function generateEditorDivs(){
//make game container
const gamecontainercontainer = document.getElementById('gamecontainercontainer');
const gamecontainer = document.createElement('div')
gamecontainer.setAttribute('id','gamecontainer');
gamecontainercontainer.appendChild(gamecontainer)
//define div size
//define number of rows and columnsb
const rownumber = 12;
const colnumber = 16;
//define values for container
gamecontainer.style.backgroundColor = 'white';
gamecontainer.setAttribute("draggable", false);
gamecontainer.style.display = 'flex';
gamecontainer.style.flexWrap = 'wrap';
gamecontainer.style.width = colnumber * 50 + 'px';
gamecontainer.style.height = rownumber * 50 + 'px';
gamecontainer.style.border = '5px solid black';
gamecontainer.addEventListener('mouseleave', WallHover);
gamecontainer.addEventListener('contextmenu', AntiCheat);
//make counter for ids
let idcounter = 0
//loop for creating divs
for (let row = 0; row < rownumber; row++) {
for (let col = 0; col < colnumber; col++) {
//increase id counter
idcounter++
//to make divs
const makediv = document.createElement('div');
//define size of divs
makediv.style.width = '50px';
makediv.style.height = '50px';
makediv.style.float = 'left';
makediv.style.boxSizing = 'border-box';
makediv.style.position = 'relative'
makediv.setAttribute("draggable", false);
makediv.style.backgroundColor = 'white';
//make unique id and class for the divs
const divID = idcounter
const divClass = '0'
//add id and classes to div
makediv.id = divID
makediv.classList.add(divClass);
makediv.style.outline = '1px solid red';
makediv.addEventListener('mousedown', function() {
editorClick(divID);
});
makediv.addEventListener('mousemove', function() {
editorClick(divID);
});
//add the div to the container
gamecontainer.appendChild(makediv);
}
//clear float after each row
const clearDiv = document.createElement('div');
clearDiv.style.clear = 'both';
gamecontainer.appendChild(clearDiv);
}
const body = document.querySelector('body');
body.addEventListener('dragstart', function(event) {
event.preventDefault();
});
}
function setSelect(block){
if(block == 'blank'){
selectedPlacement = 0
}
if(block == 'wall'){
selectedPlacement = 1
}
else if(block == 'coin'){
selectedPlacement = 2
}
else if(block == 'end'){
selectedPlacement = 3
}
else if(block == 'start'){
selectedPlacement = 4
}
else if(block == 'power'){
selectedPlacement = 5
}
else if(block == 'moving'){
selectedPlacement = 6
}
else{console.log('error')}
console.log('selectedPlacement is ' + selectedPlacement + block)
}
function editorClick(id) {
var grid = document.getElementById(id);
if (event.type === 'mousemove' && event.buttons === 1 || event.type === 'mousedown') {
grid.className = selectedPlacement;
if (selectedPlacement === 0) {
grid.style.backgroundColor = 'white';
} else if (selectedPlacement === 1) {
grid.style.backgroundColor = 'black';
} else if (selectedPlacement === 2) {
grid.style.backgroundColor = 'yellow';
} else if (selectedPlacement === 3) {
grid.style.backgroundColor = 'green';
} else if (selectedPlacement === 4) {
grid.style.backgroundColor = 'blue';
} else if (selectedPlacement === 5) {
grid.style.backgroundColor = 'pink';
} else if (selectedPlacement === 6) {
if (document.getElementById(blockName) === null) {
if (blocksToMove > 0) {
if (speedvalue > 0) {
if (direction === 'horizontal') {
makeXMovingDivs(id, blocksToMove, speedvalue, blockName);
movingArray.push('2', id, blocksToMove, speedvalue, blockName);
console.log(movingArray);
} else if (direction === 'vertical') {
makeYMovingDivs(id, blocksToMove, speedvalue, blockName);
movingArray.push('1', id, blocksToMove, speedvalue, blockName);
console.log(movingArray);
} else {
alert('You must choose a direction.');
return;
}
} else {
alert('You must choose a valid speed.');
return;
}
} else {
alert('You must choose a valid amount of blocks to move.');
return;
}
} else if (document.getElementById(blockName).id == blockName){
alert('You may have not chosen a unique name. If a block is placed, ignore this message');
return;
}
}
}
}
function nextStep(){
selectedPlacement = 6
newseed = '0'
for(i = 1; i<193; i++){
let currentspace = document.getElementById(i.toString())
newseed = newseed + currentspace.className
}
usercreatedseed = newseed
//clear info column
let infocolumn = document.getElementById('infocolumn')
infocolumn.innerHTML = '<p>moving div direction</p><input type="radio" id="horizontal" name="fav_language" value="horizontal"><label for="horizontal">right</label><input type="radio" id="vertical" name="fav_language" value="vertical"><label for="vertical">down</label> </p><br><label for="blocksmove">How many blocks to move:</label><br><input type="text" id="blocksmove" name="blocksmove"><br><br> <label for="blockname">Block name(make sure it is UNIQUE and includes NO SPECIAL CHARACTERS):</label><br><input type="text" id="blockname" name="blockname"><br><br><p>Moving block speed in miliseconds (higher is slower)'
//make speed slider
const slider = document.createElement('input')
slider.type = 'range'
slider.id = 'slider'
slider.min = '50'
slider.max = '3000'
slider.step = '50'
//make display element
const valueDisplay = document.createElement('p')
valueDisplay.id = 'value'
valueDisplay.textContent = '0'
//append
infocolumn.appendChild(slider)
infocolumn.appendChild(valueDisplay)
//add js for slider
slider.addEventListener('input', function(){
var myValue = parseInt(this.value)
valueDisplay.textContent = myValue
})
//make button
const applybutton = document.createElement('button')
applybutton.id = 'apply'
applybutton.classList.add('cssbutton')
applybutton.addEventListener('click', applyDetails)
applybutton.textContent = 'Apply settings'
infocolumn.appendChild(applybutton)
//make new button
const clearbutton = document.createElement('button')
clearbutton.id = 'clear'
clearbutton.classList.add('cssbutton')
clearbutton.addEventListener('click', clearMoving)
clearbutton.textContent = 'CLEAR DIVS'
infocolumn.appendChild(clearbutton)
//make export button
const exporta = document.createElement('button')
exporta.id = 'export'
exporta.classList.add('cssbutton')
exporta.addEventListener('click', exportAll)
exporta.textContent = 'Export!'
infocolumn.appendChild(exporta)
}
function applyDetails(){
//check direction
const horizontalRadio = document.getElementById('horizontal')
const verticalRadio = document.getElementById('vertical')
if (horizontalRadio.checked) {
direction = 'horizontal'
} else if (verticalRadio.checked) {
direction = 'vertical'
} else {
//end function
alert('Please select a direction.')
return;
}
//get blocks to move
const blocksMoveInput = document.getElementById('blocksmove');
blocksToMove = parseInt(blocksMoveInput.value);
if (isNaN(blocksToMove) || blocksToMove <= 0) {
alert('Please enter a valid number of blocks to move.');
return;
}
// Get the block name
const blockNameInput = document.getElementById('blockname');
blockName = blockNameInput.value.trim();
if (blockName === '') {
alert('Please enter a block name.');
return;
}
//speed
const slider = document.getElementById('slider')
speedvalue = parseInt(slider.value)
//finalization
console.log('Direction:', direction);
console.log('Blocks to move:', blocksToMove);
console.log('Block name:', blockName);
console.log('Speed:', speedvalue);
alert('Settings applied successfully. Click on a cell to place your moving blocks');
}
function clearMoving(){
const result = confirm("Are you sure you want to DELETE ALL MOVING DIVS?");
if (result === true) {
movingArray.splice(0);
const elements = document.querySelectorAll(".movingdiv");
// Remove each element
elements.forEach(element => {
element.remove();
});}
else{
return;
}
}
function exportAll(){
alert('Your final seed will appear at the bottom of the page. Copy it and paste it in the level player to play your level!')
alert('[' + usercreatedseed + '],[' + movingArray + ']' )
let seedbox = document.getElementById('seedbox')
seedbox.innerHTML = '[' + usercreatedseed + '],[' + movingArray + ']'
}
function playCustomLevel() {
var mycustomseed = prompt('Enter the custom seed of the level (format: [manynumbers],[otherstuff])');
//remove all spaces
mycustomseed = mycustomseed.replace(/\s/g, "");
//split seed into 2 parts
var delimiterIndex = mycustomseed.indexOf(",");
//if comma is in a valid index
if (delimiterIndex !== -1) {
//slice at the index of the comma
var firstpartseed = mycustomseed.slice(1, delimiterIndex).trim();
//slice again but for the second half
var movingdivsettings = mycustomseed.slice(delimiterIndex + 1, -1).trim();
//remove square brackets
movingdivsettings = movingdivsettings.replace(/\[|\]/g, "");
} else {
//if comma is not there, return error
console.log('Invalid string format');
return;
}
seedsArray = [firstpartseed, '0'];
movingBlockSettings = [movingdivsettings.split(",")]; console.log(movingBlockSettings)
var countsfdg = firstpartseed.split("2").length - 1;
coinsPerLevel = [countsfdg]
console.log(movingdivsettings)
console.log('cpl is ' + coinsPerLevel)
startEverything();
}