diff --git a/Assets/Sheet.xcf b/Assets/Sheet.xcf index a2aa245..f1a8c0e 100644 Binary files a/Assets/Sheet.xcf and b/Assets/Sheet.xcf differ diff --git a/index.js b/index.js index f55f727..8878882 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,153 @@ var express = require('express'); const worldgen = require("./worldgen.js") +const data = require("./tileinfo.js") const util = require("./util.js") -const gridSize = [18, 18]; const perlinScale = 3; class Player { - constructor(id, color) { + 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() { + constructor(gridSize) { this.players = []; - this.world = worldgen.generateWorld(gridSize, perlinScale); + this.world = new World(gridSize, perlinScale); + this.gridSize = gridSize } - addPlayer(id) { + addPlayer(id, name) { var color switch(util.randomNumber(1,4)) { case 1: @@ -37,8 +166,9 @@ class Game { color = "yellow"; break; } - const player = new Player(id, color); + const player = new Player(id, color, name); this.players.push(player); + console.log(player.name) } removePlayer(id) { @@ -47,14 +177,12 @@ class Game { } getPlayerByID(id) { - console.log(this.players) - console.log(id) const player = this.players.filter(obj => obj.id == id)[0] return player } } -var game = new Game(); +var game = new Game([18, 18]); var app = express() //Static resources server @@ -66,35 +194,38 @@ app.use(express.static(__dirname + '/www/'));var server = app.listen(8082, funct 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.removePlayer(client.id) - client.broadcast.emit('playerList', game.players) + game.getPlayerByID(client.id).kill(client) + // client.broadcast.emit('playerList', game.players) }) - client.on('joinGame', function(tank){ - game.addPlayer(client.id); + client.on('joinGame', function(data){ + console.log(game.players) console.log(client.id + ' joined the game'); - client.emit('gameVars', {gridSize: gridSize, world: game.world}) - client.broadcast.emit('playerList', game.players) - client.emit('playerList', game.players) + 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.removePlayer(client.id) - client.broadcast.emit('playerList', game.players) + // game.getPlayerByID(cliend.id).kill() + game.getPlayerByID(client.id).kill(client) + // client.broadcast.emit('playerList', game.players) }) client.on('clickCanvas', function(data){ - const xu = data.tilePosition[0] - const yu = data.tilePosition[1] - game.world[xu][yu].structure = data.structure - game.world[xu][yu].owner = game.getPlayerByID(client.id).color; - // console.log(world[xu][yu].owner = game.getPlayerbyID(client.id)) - - client.broadcast.emit('sync',{world: game.world}) - client.emit('sync',{world: game.world}) + 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}) }) diff --git a/net.js b/net.js new file mode 100644 index 0000000..e69de29 diff --git a/tileinfo.js b/tileinfo.js new file mode 100644 index 0000000..d59a2fd --- /dev/null +++ b/tileinfo.js @@ -0,0 +1,18 @@ +const tileTypes = +[ +{name: "Grass", textureID: 0, buildPrice: 20, capturePrice:10, capturePower: 10, captureTime: 2, sellPrice:5, recapturePrice: 15, recapturePower: 100, recaptureTime: 10}, +{name: "Hills", textureID: 1, buildPrice: 20, capturePrice:10, capturePower: 10, captureTime: 2, sellPrice:5, recapturePrice: 15, recapturePower: 100, recaptureTime: 10}, +{name: "Sea", textureID: 2, buildPrice: 20, capturePrice:10, capturePower: 10, captureTime: 2, sellPrice:5, recapturePrice: 15, recapturePower: 100, recaptureTime: 10} +]; + +const structureTypes = +[ +{name: "Air", textureID:null, claimable: true, buildable: false, destroyable: false, maxQuantity:null, buildPrice: null, sellPrice: null, buildableOn: null}, +{name: "Rock", textureID:4, claimable: false, buildable: false, destroyable: false, maxQuantity:null, buildPrice: null, sellPrice: null, buildableOn: [0,1]}, +{name: "Town", textureID:32, claimable: false, buildable: true, destroyable: true, maxQuantity:10, buildPrice: 10, sellPrice: 5, buildableOn: [0,2]}, +{name: "City", textureID:33, claimable: false, buildable: true, destroyable: true, maxQuantity:10, buildPrice: 20, sellPrice: 10, buildableOn: [0,2]}, +{name: "Capitol", textureID:34, claimable: false, buildable: true, destroyable: false, maxQuantity:1, buildPrice: 30, sellPrice: 20, buildableOn: [0,2]}, +] + + +module.exports = {tileTypes, structureTypes} diff --git a/util.js b/util.js index b3f9e1a..102b476 100644 --- a/util.js +++ b/util.js @@ -1,5 +1,7 @@ function randomNumber(min, max) { return Math.floor(Math.random() * (max - min) + min); } - -module.exports = {randomNumber} +function clamp(number, min, max) { + return Math.max(min, Math.min(number, max)); +} +module.exports = {randomNumber, clamp} diff --git a/worldgen.js b/worldgen.js index e41ef1e..e53581b 100644 --- a/worldgen.js +++ b/worldgen.js @@ -7,7 +7,7 @@ function generateWorld(gridSize, perlinScale){ 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, owner: null} + tempWorld[x][y] = {type: 0, structure: 0, color: null, owner: null} } } @@ -32,9 +32,9 @@ function generateWorld(gridSize, perlinScale){ y = util.randomNumber(0,gridSize[1]); if (tempWorld[x][y].type != 1) { i++; - tempWorld[x][y].structure = 1 - } + tempWorld[x][y].structure = util.randomNumber(1,4) } +} return tempWorld; } module.exports = { generateWorld }; diff --git a/www/index.html b/www/index.html index 7bf2e98..1369abb 100644 --- a/www/index.html +++ b/www/index.html @@ -7,22 +7,28 @@
-