This commit is contained in:
A Bass 2022-04-30 16:18:05 -04:00
parent 9f1bf7eb4d
commit e88139e383
2 changed files with 110 additions and 147 deletions

255
index.js
View file

@ -1,147 +1,50 @@
var express = require('express');
const worldgen = require("./worldgen.js") const worldgen = require("./worldgen.js")
const data = require("./tileinfo.js")
const util = require("./util.js") const util = require("./util.js")
const gridSize = [18, 18];
const perlinScale = 3; const perlinScale = 3;
var express = require('express');
class Player { <<<<<<< HEAD
constructor(id, color, name) { class Server {
this.name = name constructor(){
this.id = id; this.games = []
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) { addGame(name) {
if(game.players.length < 2){ let roomNames = []
for(let x = 0; x < game.gridSize[0]; x++) { for (let i = 0; i < this.games.length; i++) {
for(let y = 0; y < game.gridSize[1]; y++) { roomNames.push(this.games[i].name)
if (game.world.map[x][y].owner == this.id) {
game.world.map[x][y].owner = null
game.world.map[x][y].color = null
}
}
} }
if (!roomNames.includes(name)) {
const game = new Game([18,18], name)
this.games.push(game)
console.log("added game - " + game.name)
} else { } else {
console.log("KILL") console.log("tried to make room already taken")
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 getGameByName(name, mode) {
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 ( var game = this.games.filter(obj => obj.name == name)[0]
// Check if the structure is valid to be built console.log(name, mode, game)
strucInfo.buildable == true console.log(game == undefined && mode)
// Check if player is still under building limit for structure if (game == undefined && mode) {
&& player.buildings[structure] < strucInfo.maxQuantity this.addGame(name)
// Ensure no other builings are already built in this tile return this.games.filter(obj => obj.name == name)[0]
&& 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)
} }
return game;
} }
claimLand(x, y, player, client) { removeGame(name) {
const tileOfInterest = this.map[x][y]; this.players = this.players.filter(obj => obj.name != name);
console.log("removed goom - " + name)
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 { class Game {
constructor(gridSize) { constructor(gridSize, roomName) {
this.name = roomName
this.players = []; this.players = [];
this.world = new World(gridSize, perlinScale); this.world = new World(gridSize, perlinScale);
this.gridSize = gridSize this.gridSize = gridSize
@ -182,7 +85,65 @@ class Game {
} }
} }
var game = new Game([18, 18]);
class Player {
constructor(id, color) {
this.id = id;
this.color = color;
}
=======
class Player {
constructor(id, color) {
this.id = id;
this.color = color;
}
>>>>>>> parent of 9f1bf7e (Good enough for now)
}
class Game {
constructor() {
this.players = [];
this.world = worldgen.generateWorld(gridSize, perlinScale);
}
addPlayer(id) {
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);
this.players.push(player);
}
removePlayer(id) {
this.players = this.players.filter(obj => obj.id != id);
console.log("removed player - " + id)
}
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 app = express() //Static resources server var app = express() //Static resources server
@ -194,39 +155,41 @@ app.use(express.static(__dirname + '/www/'));var server = app.listen(8082, funct
var io = require('socket.io')(server);/* Connection events */ var io = require('socket.io')(server);/* Connection events */
io.on('connection', function(client) { io.on('connection', function(client) {
console.log('User connected'); console.log('User connected');
client.emit('gameVars', {gridSize: game.gridSize, world: game.world.map})
client.on('disconnect', function(){ client.on('disconnect', function(){
console.log(client.id + ' disconnected.') console.log(client.id + ' disconnected.')
game.getPlayerByID(client.id).kill(client) game.removePlayer(client.id)
// client.broadcast.emit('playerList', game.players) client.broadcast.emit('playerList', game.players)
}) })
client.on('joinGame', function(data){ client.on('joinGame', function(tank){
console.log(game.players) game.addPlayer(client.id);
console.log(client.id + ' joined the game'); console.log(client.id + ' joined the game');
game.addPlayer(client.id, data.name); client.emit('gameVars', {gridSize: gridSize, world: game.world})
// client.broadcast.emit('playerList', game.players) client.broadcast.emit('playerList', game.players)
// client.emit('playerList', game.players) client.emit('playerList', game.players)
}) })
client.on('leaveGame', function(tank){ client.on('leaveGame', function(tank){
console.log(client.id + ' disconnected.') console.log(client.id + ' disconnected.')
// game.getPlayerByID(cliend.id).kill() game.removePlayer(client.id)
game.getPlayerByID(client.id).kill(client) client.broadcast.emit('playerList', game.players)
// client.broadcast.emit('playerList', game.players)
}) })
client.on('clickCanvas', function(data){ client.on('clickCanvas', function(data){
const xu = util.clamp(data.tilePosition[0], 0, game.gridSize[0]) const xu = data.tilePosition[0]
const yu = util.clamp(data.tilePosition[1], 0, game.gridSize[1]) const yu = data.tilePosition[1]
// game.world.map[xu][yu].structure = data.structure game.world[xu][yu].structure = data.structure
game.world.click(data.structure, xu, yu, client) game.world[xu][yu].owner = game.getPlayerByID(client.id).color;
// game.world.map[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.map})
// client.emit('sync',{world: game.world.map}) client.broadcast.emit('sync',{world: game.world})
client.emit('sync',{world: game.world})
}) })
}); });
function sendMap(client) {
client.broadcast.emit('sync',{world: game.world.map})
client.emit('sync',{world: game.world.map})
}

0
net.js
View file