Now storing tiles using Json inside of 2d arrays

This commit is contained in:
A Bass 2022-04-19 19:11:41 -04:00
parent af78a25395
commit 4cba5b1b14

View file

@ -1,3 +1,11 @@
var world = Array.from(Array(24), () => new Array(24));
for (x = 0; x < 24; x++){
for (y = 0; y < 24; y++){
world[x][y] = {type: 0, structure: 0}
}
}
window.onload = function () { window.onload = function () {
// GET THE IMAGE. // GET THE IMAGE.
@ -25,29 +33,28 @@ window.onload = function () {
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
generateWorld()
ctx.drawImage(background, 0, 0); // DRAW THE IMAGE TO THE CANVAS. ctx.drawImage(background, 0, 0); // DRAW THE IMAGE TO THE CANVAS.
let x, y = 0 let x, y = 0
noise.seed(Math.random())
for(x = 0; x < getBlockDimensions()[0]; x++){ for(x = 0; x < getBlockDimensions()[0]; x++){
for(y = 0; y < getBlockDimensions()[1]; y++){ for(y = 0; y < getBlockDimensions()[1]; y++){
var value = (noise.perlin2(x/3.0, y/3.0))*10; if (world[x][y].type == 0) {
if (value >= -3) {
ctx.drawImage(tiles[0], x*24,y*24) ctx.drawImage(tiles[0], x*24,y*24)
} else if (value < -3) { } else if (world[x][y].type == 1) {
ctx.drawImage(tiles[2], x*24,y*24) ctx.drawImage(tiles[2], x*24,y*24)
} }
if (value > 1.4) { if (world[x][y].type == 2) {
ctx.drawImage(tiles[1], x*24,y*24) ctx.drawImage(tiles[1], x*24,y*24)
} }
if (world[x][y].structure == 1) {
ctx.drawImage(tiles[3], x*24,y*24)
}
} }
} }
for (i = 0; i < 90; i++){
ctx.drawImage(tiles[3], randomNumber(0,24)*24,randomNumber(0,24)*24)
}
} }
} }
@ -59,3 +66,32 @@ return [canvas.width/24, canvas.height/24]
function randomNumber(min, max) { function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min); return Math.floor(Math.random() * (max - min) + min);
} }
function generateWorld(){
let x, y = 0
noise.seed(Math.random())
for(x = 0; x < getBlockDimensions()[0]; x++){
for(y = 0; y < getBlockDimensions()[1]; y++){
var value = (noise.perlin2(x/3.0, y/3.0))*10;
if (value >= -3) {
world[x][y].type = 0
} else if (value < -3) {
world[x][y].type = 1
}
if (value > 1.4) {
world[x][y].type = 2
}
}
}
for (i = 0; i < 20; i){
x = randomNumber(0,24);
y = randomNumber(0,24);
if (world[x][y].type == 0) {
i++;
world[x][y].structure = 1
}
}
console.log(world)
}