233 lines
6.5 KiB
JavaScript
233 lines
6.5 KiB
JavaScript
|
|
var express = require('express');
|
|
const worldgen = require("./worldgen.js")
|
|
const data = require("./tileinfo.js")
|
|
const util = require("./util.js")
|
|
const perlinScale = 3;
|
|
|
|
class Player {
|
|
constructor(id, color, name) {
|
|
this.name = name
|
|
this.id = id;
|
|
this.color = color;
|
|
this.buildings = [0,0,0,0,0,0,0,0,0,0,0,0,0]
|
|
this.gold = 100
|
|
this.power = 100 + util.randomNumber(2,50)
|
|
}
|
|
|
|
kill(client) {
|
|
if(game.players.length < 2){
|
|
for(let x = 0; x < game.gridSize[0]; x++) {
|
|
for(let y = 0; y < game.gridSize[1]; y++) {
|
|
if (game.world.map[x][y].owner == this.id) {
|
|
game.world.map[x][y].owner = null
|
|
game.world.map[x][y].color = null
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
console.log("KILL")
|
|
let playerList = game.players.filter(obj => obj.id != this.id);
|
|
while (playerList > 1) {
|
|
for (let i = 1; i < playerList.length-1; i++) {
|
|
if (playerList[util.randomNumber(0,i)].gold < playerList[i].gold) {
|
|
delete playerList[i];
|
|
}
|
|
}
|
|
playerlist = playerList.filter(obj => obj != null)
|
|
}
|
|
var player2 = playerList[0]
|
|
this.transfer(player2)
|
|
}
|
|
sendMap(client)
|
|
game.removePlayer(this.id);
|
|
}
|
|
|
|
transfer(player2) {
|
|
player2.gold += this.gold
|
|
player2.power += this.power
|
|
for(let x = 0; x < game.gridSize[0]; x++) {
|
|
for(let y = 0; y < game.gridSize[1]; y++) {
|
|
if (game.world.map[x][y].owner == this.id) {
|
|
game.world.map[x][y].owner = player2.id
|
|
game.world.map[x][y].color = player2.color
|
|
}
|
|
}
|
|
}
|
|
|
|
for (i = 0; i < this.buildings.length; i++) {
|
|
player2.buildings[i] += this.buildings[i]
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
class World {
|
|
constructor(gridSize, perlinScale) {
|
|
console.log(gridSize)
|
|
this.map = worldgen.generateWorld(gridSize, perlinScale)
|
|
}
|
|
|
|
// Determine what method of clicking is to be used by player
|
|
click(structure, x, y, client){
|
|
const player = game.getPlayerByID(client.id);
|
|
//Build Mode
|
|
if (this.map[x][y].owner == player.id) {
|
|
this.placeStructure(structure, x, y, player, client)
|
|
}
|
|
//Claim Mode
|
|
if (this.map[x][y].owner == null) {
|
|
this.claimLand(x, y, player, client)
|
|
}
|
|
|
|
if (this.map[x][y].owner != null && this.map[x][y].owner != player.id) {
|
|
//Attack Mode
|
|
}
|
|
}
|
|
|
|
// Function for placing buildings on land
|
|
placeStructure(structure, x, y, player, client) {
|
|
// Get info for the structure desired to be built from databank
|
|
const strucInfo = data.structureTypes[structure]
|
|
// Get the tile object the structure is to be built upon
|
|
const tileOfInterest = this.map[x][y];
|
|
// get info for the tile the structure is to be built upon from databank
|
|
const tileInfo = data.tileTypes[tileOfInterest.type]
|
|
const cost = strucInfo.buildPrice + tileInfo.buildPrice;
|
|
|
|
if (
|
|
// Check if the structure is valid to be built
|
|
strucInfo.buildable == true
|
|
// Check if player is still under building limit for structure
|
|
&& player.buildings[structure] < strucInfo.maxQuantity
|
|
// Ensure no other builings are already built in this tile
|
|
&& tileOfInterest.structure == 0
|
|
// Check to see if the structure can be built on the type of tile
|
|
&& strucInfo.buildableOn.includes(tileOfInterest.type)
|
|
// Check if player has enough gold to build this
|
|
&& player.gold > cost
|
|
) {
|
|
// Update tile on map
|
|
this.map[x][y].structure = structure
|
|
|
|
// Increment building count for this structure
|
|
player.buildings[structure] ++;
|
|
|
|
// Charge player for cost of structure
|
|
player.gold -= cost;
|
|
|
|
sendMap(client)
|
|
}
|
|
}
|
|
|
|
claimLand(x, y, player, client) {
|
|
const tileOfInterest = this.map[x][y];
|
|
|
|
if (data.structureTypes[tileOfInterest.structure].claimable == true) {
|
|
this.map[x][y].owner = player.id
|
|
this.map[x][y].color = player.color
|
|
sendMap(client)
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
function sendMap(client) {
|
|
client.broadcast.emit('sync',{world: game.world.map})
|
|
client.emit('sync',{world: game.world.map})
|
|
}
|
|
|
|
class Game {
|
|
constructor(gridSize) {
|
|
this.players = [];
|
|
this.world = new World(gridSize, perlinScale);
|
|
this.gridSize = gridSize
|
|
}
|
|
|
|
addPlayer(id, name) {
|
|
var color
|
|
switch(util.randomNumber(1,4)) {
|
|
case 1:
|
|
color = "red";
|
|
break;
|
|
|
|
case 2:
|
|
color = "aquamarine";
|
|
break;
|
|
|
|
case 3:
|
|
color = "green";
|
|
break;
|
|
|
|
case 4:
|
|
color = "yellow";
|
|
break;
|
|
}
|
|
const player = new Player(id, color, name);
|
|
this.players.push(player);
|
|
console.log(player.name)
|
|
}
|
|
|
|
removePlayer(id) {
|
|
this.players = this.players.filter(obj => obj.id != id);
|
|
console.log("removed player - " + id)
|
|
}
|
|
|
|
getPlayerByID(id) {
|
|
const player = this.players.filter(obj => obj.id == id)[0]
|
|
return player
|
|
}
|
|
|
|
}
|
|
var game = new Game([18, 18]);
|
|
|
|
|
|
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.emit('gameVars', {gridSize: game.gridSize, world: game.world.map})
|
|
|
|
|
|
|
|
client.on('disconnect', function(){
|
|
console.log(client.id + ' disconnected.')
|
|
game.getPlayerByID(client.id).kill(client)
|
|
// client.broadcast.emit('playerList', game.players)
|
|
})
|
|
|
|
client.on('joinGame', function(data){
|
|
console.log(game.players)
|
|
console.log(client.id + ' joined the game');
|
|
game.addPlayer(client.id, data.name);
|
|
// client.broadcast.emit('playerList', game.players)
|
|
// client.emit('playerList', game.players)
|
|
})
|
|
client.on('leaveGame', function(tank){
|
|
console.log(client.id + ' disconnected.')
|
|
// game.getPlayerByID(cliend.id).kill()
|
|
game.getPlayerByID(client.id).kill(client)
|
|
// client.broadcast.emit('playerList', game.players)
|
|
})
|
|
|
|
client.on('clickCanvas', function(data){
|
|
const xu = util.clamp(data.tilePosition[0], 0, game.gridSize[0])
|
|
const yu = util.clamp(data.tilePosition[1], 0, game.gridSize[1])
|
|
// game.world.map[xu][yu].structure = data.structure
|
|
game.world.click(data.structure, xu, yu, client)
|
|
// game.world.map[xu][yu].owner = game.getPlayerByID(client.id).color;
|
|
// client.broadcast.emit('sync',{world: game.world.map})
|
|
// client.emit('sync',{world: game.world.map})
|
|
})
|
|
|
|
|
|
});
|