Created
April 21, 2016 23:56
-
-
Save bredmor/b4b2824b0253cb8c618e4cc1e1b41904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var opentiles = []; | |
var tileId = 0; | |
var currentTiles = 0; | |
var maxTiles = 50; | |
var allTiles = []; | |
var tile_size = 20; | |
function boolRand(){ | |
return Math.random()<.5; | |
} | |
function randomKey(obj) { | |
var ret; | |
var c = 0; | |
for (var key in obj) | |
if (Math.random() < 1/++c) | |
ret = key; | |
return ret; | |
} | |
class side { | |
constructor(state) { | |
this.open = 'closed'; | |
if(state === true) { | |
this.open = 'open'; | |
} | |
} | |
get isopen() { | |
return this.open; | |
} | |
set isopen(state) { | |
this.open = 'closed'; | |
if(state === true) { | |
this.open = 'open'; | |
} | |
} | |
} | |
class position { | |
constructor(top, left) { | |
this.ptop = top; | |
this.pleft = left; | |
} | |
get top() { | |
return this.ptop; | |
} | |
get left() { | |
return this.pleft; | |
} | |
} | |
class maptile { | |
constructor(id, top, left) { | |
this.stop = new side(boolRand()); | |
this.sright = new side(boolRand()); | |
this.sleft = new side(boolRand()); | |
this.sbottom = new side(boolRand()); | |
this.position = new position(top, left); | |
this.id = id; | |
if(this.pos.top == 0) { | |
this.top = false; | |
} | |
if(this.pos.left == 0) { | |
this.left = false; | |
} | |
} | |
place() { | |
$('#map').append('<div id="' + this.id + '"></div>') | |
$('#' + this.id).css({'top':this.position.top, 'left':this.position.left}) | |
.addClass('tile') | |
.addClass('top' + this.top) | |
.addClass('right' + this.right) | |
.addClass('left' + this.left) | |
.addClass('bottom' + this.bottom) | |
.addClass('top' + this.top + 'right' + this.right +'left' + this.left + 'bottom' + this.bottom); | |
} | |
get sides() { | |
return {'top': this.top, | |
'right': this.right, | |
'left': this.left, | |
'bottom': this.bottom} | |
} | |
get getId() { | |
return this.id; | |
} | |
get pos() { | |
return this.position; | |
} | |
get top() { | |
return this.stop.isopen; | |
} | |
get left() { | |
return this.sleft.isopen; | |
} | |
get right() { | |
return this.sright.isopen; | |
} | |
get bottom() { | |
return this.sbottom.isopen; | |
} | |
set top(open) { | |
this.stop.isopen = open; | |
} | |
set right(open) { | |
this.sright.isopen = open; | |
} | |
set left(open) { | |
this.sleft.isopen = open; | |
} | |
set bottom(open) { | |
this.sbottom.isopen = open; | |
} | |
} | |
function seedfirst() { | |
var tile = new maptile(tileId, 0, 0); | |
tile.top = false; | |
tile.left = false; | |
tile.bottom = boolRand(); | |
if(tile.bottom == 'closed') { | |
tile.right = true; | |
} else { | |
tile.right = boolRand(); | |
} | |
tileId++; | |
return tile; | |
} | |
function addTile(tile) { | |
opentiles.push(tile); | |
tile.place(); | |
currentTiles++; | |
tileId++; | |
allTiles["l" + tile.pos.top + tile.pos.left] = tile; | |
} | |
function init() { | |
// create the first tile and add it to the array | |
addTile(seedfirst()); | |
tilescanner(); | |
} | |
function reinit(size) { | |
$('#map').html(''); | |
opentiles = []; | |
tileId = 0; | |
currentTiles = 0; | |
maxTiles = size; | |
allTiles = []; | |
init(); | |
} | |
function tilescanner() { | |
opentiles.forEach(function(element, index, array) { | |
var newtile; | |
if(element.top == 'open' && !(("l" + (element.pos.top-tile_size) + element.pos.left) in allTiles)) { | |
newtile = new maptile(tileId, (element.pos.top-tile_size),element.pos.left); | |
newtile.bottom = true; | |
addTile(newtile); | |
} | |
if(element.left == 'open' && !(("l" + element.pos.top + (element.pos.left-tile_size)) in allTiles)) { | |
newtile = new maptile(tileId, element.pos.top,(element.pos.left-tile_size)); | |
newtile.right = true; | |
addTile(newtile); | |
} | |
if(element.right == 'open' && !(("l" + element.pos.top + (element.pos.left+tile_size)) in allTiles)) { | |
newtile = new maptile(tileId, element.pos.top,(element.pos.left+tile_size)); | |
newtile.left = true; | |
addTile(newtile); | |
} | |
if(element.bottom == 'open' && !(("l" + (element.pos.top+tile_size) + element.pos.left) in allTiles)) { | |
newtile = new maptile(tileId, (element.pos.top+tile_size),element.pos.left); | |
newtile.top = true; | |
addTile(newtile); | |
} | |
opentiles.splice(index, 1); | |
}); | |
if((currentTiles < maxTiles) && (opentiles.length > 0)) { | |
tilescanner(); | |
} | |
// } else if((maxTiles >= currentTiles)) { | |
// console.log('Open tiles: ' + opentiles.length); | |
// accepted = false; | |
// while(accepted == false) { | |
// var modTile = allTiles[randomKey(allTiles)]; | |
// if((modTile.right == 'closed') && !(("l" + modTile.pos.top + (modTile.pos.left+tile_size)) in allTiles)) { | |
// modTile.right = 'open'; | |
// opentiles.push(modTile); | |
// accepted = true; | |
// console.log('Opened RIGHT side on ID ' + modTile.getId); | |
// $('#' + modTile.getId).remove(); | |
// addTile(modTile); | |
// } else if((modTile.bottom == 'closed') && !(("l" + (modTile.pos.top+tile_size) + modTile.pos.left) in allTiles)){ | |
// modTile.bottom = 'open'; | |
// opentiles.push(modTile); | |
// accepted = true; | |
// console.log('Opened BOTTOM side on ID ' + modTile.getId); | |
// } | |
// } | |
// tilescanner(); | |
// } | |
} | |
$(document).ready(function(){ | |
$('#generate').click(function(){ | |
reinit($('#max_tiles').val()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment