2022-04-20 04:32:17 +00:00
|
|
|
var noise = require('noisejs');
|
|
|
|
var express = require('express');
|
|
|
|
|
|
|
|
const gridSize = [18, 18];
|
|
|
|
const perlinScale = 3;
|
|
|
|
var world = generateWorld();
|
|
|
|
|
|
|
|
var app = express() //Static resources server
|
|
|
|
app.use(express.static(__dirname + '/www/'));var server = app.listen(8082, function () {
|
|
|
|
var port = server.address().port;
|
|
|
|
console.log('Server running at port %s', port);
|
|
|
|
});
|
|
|
|
|
|
|
|
var io = require('socket.io')(server);/* Connection events */
|
|
|
|
io.on('connection', function(client) {
|
|
|
|
console.log('User connected');
|
|
|
|
client.on('joinGame', function(tank){
|
|
|
|
console.log(client.id + ' joined the game');
|
|
|
|
|
|
|
|
// client.emit('addTank', { id: tank.id, type: tank.type, isLocal: true, x: initX, y: initY, hp: TANK_INIT_HP });
|
|
|
|
// client.broadcast.emit('addTank', { id: tank.id, type: tank.type, isLocal: false, x: initX, y: initY, hp: TANK_INIT_HP} );
|
|
|
|
// game.addTank({ id: tank.id, type: tank.type, hp: TANK_INIT_HP});
|
|
|
|
client.emit('gameVars', {gridSize: gridSize, world: world})
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('clickCanvas', function(data){
|
|
|
|
const xu = data.tilePosition[0]
|
|
|
|
const yu = data.tilePosition[1]
|
|
|
|
world[xu][yu].structure = data.structure
|
|
|
|
client.broadcast.emit('sync',{world: world})
|
|
|
|
client.emit('sync',{world: world})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function randomNumber(min, max) {
|
|
|
|
return Math.floor(Math.random() * (max - min) + min);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateWorld(){
|
|
|
|
let x, y = 0
|
|
|
|
|
|
|
|
let tempWorld = Array.from(Array(gridSize[0]), () => new Array(gridSize[1]));
|
|
|
|
for (x = 0; x < gridSize[0]; x++){
|
|
|
|
for (y = 0; y < gridSize[1]; y++){
|
|
|
|
tempWorld[x][y] = {type: 0, structure: 0}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
noise.seed(Math.random())
|
|
|
|
for(x = 0; x < gridSize[0]; x++){
|
|
|
|
for(y = 0; y < gridSize[1]; y++){
|
|
|
|
|
|
|
|
var value = (noise.perlin2(x/perlinScale, y/perlinScale))*10;
|
|
|
|
if (value >= -3) {
|
|
|
|
tempWorld[x][y].type = 0
|
|
|
|
} else if (value < -3) {
|
|
|
|
tempWorld[x][y].type = 1
|
|
|
|
}
|
|
|
|
if (value > 1.4) {
|
|
|
|
tempWorld[x][y].type = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < gridSize[0]*gridSize[1]/15; i){
|
|
|
|
x = randomNumber(0,gridSize[0]);
|
|
|
|
y = randomNumber(0,gridSize[1]);
|
|
|
|
if (tempWorld[x][y].type != 1) {
|
|
|
|
i++;
|
|
|
|
tempWorld[x][y].structure = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tempWorld;
|
|
|
|
}
|