|
|
|
@ -1,9 +1,7 @@ |
|
|
|
|
var water = "#1E90FF"; |
|
|
|
|
var beach = "#ffff00"; |
|
|
|
|
var grass = "#008000"; |
|
|
|
|
var threshold_water = 0.70; |
|
|
|
|
var threshold_beach = 0.80; |
|
|
|
|
var threshold_grass = 0.90; |
|
|
|
|
|
|
|
|
|
var maxValue = 30; |
|
|
|
|
var minValue = -30; |
|
|
|
|
var tilesColor = [water,beach,grass] |
|
|
|
@ -16,6 +14,9 @@ var tile_height=16; |
|
|
|
|
var width = Math.floor(1600 / tile_width); |
|
|
|
|
var height = Math.floor(800 / tile_height); |
|
|
|
|
var current_noise = []; |
|
|
|
|
var currentBoardArray = []; |
|
|
|
|
var clickCounter = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findHeighestNumber(array){ |
|
|
|
@ -98,34 +99,18 @@ class Ship{ |
|
|
|
|
function createBoard(height, width, array){ |
|
|
|
|
var board = document.getElementById("can"); |
|
|
|
|
var context = board.getContext("2d"); |
|
|
|
|
var heighestNumber = findHeighestNumber(array); |
|
|
|
|
|
|
|
|
|
console.log(array); |
|
|
|
|
console.log(heighestNumber); |
|
|
|
|
|
|
|
|
|
for(var i = 0; i < height; i++){ |
|
|
|
|
var row = []; |
|
|
|
|
for(var j = 0; j < width; j++){ |
|
|
|
|
//console.log(randomNumber);
|
|
|
|
|
var x = (array[i][j]/ heighestNumber)*100; |
|
|
|
|
//console.log(value);
|
|
|
|
|
//console.log(array[i][j]);
|
|
|
|
|
if(x <= 100.00 && x >= 70.00){ |
|
|
|
|
color = 1; |
|
|
|
|
}
|
|
|
|
|
if(x <= 69.00 && x >= 65.00){ |
|
|
|
|
color = 1; |
|
|
|
|
}
|
|
|
|
|
if(x <= 65.00 && x >= 0){ |
|
|
|
|
color = 0; |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tmp_tile = new Tile(j, i, color); |
|
|
|
|
var color = array[i][j].color; |
|
|
|
|
context.fillStyle = tilesColor[color]; |
|
|
|
|
context.fillRect(i*tile_height+1, j*tile_width+1, tile_width, tile_height); |
|
|
|
|
row.push(tmp_tile); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
board_numbers.push(row); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -150,7 +135,6 @@ function generate_perlin_noise(rounds,height,width){ |
|
|
|
|
var points_width = width; |
|
|
|
|
var points_height = height; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(var r = 0; r < rounds; r++){ |
|
|
|
|
var tmp_array = []; |
|
|
|
|
//console.log("------------------ Step "+r+" --------------");
|
|
|
|
@ -260,11 +244,39 @@ function run_perlin_noise(){ |
|
|
|
|
var noise_array = generate_perlin_noise(cycle,width,height); |
|
|
|
|
console.log(noise_array); |
|
|
|
|
current_noise = noise_array; |
|
|
|
|
createBoard(height,width,noise_array); |
|
|
|
|
currentBoardArray = createBoardArray(current_noise, height, width); |
|
|
|
|
createBoard(height,width,currentBoardArray); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createBoardArray(array,height,width){ |
|
|
|
|
var result = []; |
|
|
|
|
var heighestNumber = findHeighestNumber(array); |
|
|
|
|
for(var i = 0; i < height; i++){ |
|
|
|
|
var row = []; |
|
|
|
|
for(var j = 0; j < width; j++){ |
|
|
|
|
var x = (array[i][j]/ heighestNumber)*100; |
|
|
|
|
var color = 0; |
|
|
|
|
if(x <= 100.00 && x >= 70.00){ |
|
|
|
|
color = 1; |
|
|
|
|
}
|
|
|
|
|
if(x <= 69.00 && x >= 65.00){ |
|
|
|
|
color = 1; |
|
|
|
|
}
|
|
|
|
|
if(x <= 65.00 && x >= 0){ |
|
|
|
|
color = 0; |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tmp_tile = new Tile(j, i, color); |
|
|
|
|
row.push(tmp_tile); |
|
|
|
|
} |
|
|
|
|
result.push(row); |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function moveShip(canvas, e){ |
|
|
|
|
var rect = canvas.getBoundingClientRect(); |
|
|
|
|
var x = Math.floor(e.clientX - rect.left); |
|
|
|
@ -277,7 +289,12 @@ function moveShip(canvas, e){ |
|
|
|
|
window.onload = (function(){ |
|
|
|
|
var can = document.getElementById("can"); |
|
|
|
|
can.addEventListener('mousedown', function(e){ |
|
|
|
|
createBoard(height,width,current_noise); |
|
|
|
|
if(clickCounter === 2){ |
|
|
|
|
clickcounter = 1; |
|
|
|
|
} else { |
|
|
|
|
clickCounter++; |
|
|
|
|
} |
|
|
|
|
createBoard(height,width,currentBoardArray); |
|
|
|
|
moveShip(can, e); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|