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

257
index.js
View file

@ -1,147 +1,50 @@
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;
var express = require('express');
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)
<<<<<<< HEAD
class Server {
constructor(){
this.games = []
}
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
}
}
}
addGame(name) {
let roomNames = []
for (let i = 0; i < this.games.length; i++) {
roomNames.push(this.games[i].name)
}
if (!roomNames.includes(name)) {
const game = new Game([18,18], name)
this.games.push(game)
console.log("added game - " + game.name)
} 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
console.log("tried to make room already taken")
}
}
// 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;
getGameByName(name, mode) {
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)
var game = this.games.filter(obj => obj.name == name)[0]
console.log(name, mode, game)
console.log(game == undefined && mode)
if (game == undefined && mode) {
this.addGame(name)
return this.games.filter(obj => obj.name == name)[0]
}
return game;
}
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)
}
removeGame(name) {
this.players = this.players.filter(obj => obj.name != name);
console.log("removed goom - " + name)
}
}
function sendMap(client) {
client.broadcast.emit('sync',{world: game.world.map})
client.emit('sync',{world: game.world.map})
}
class Game {
constructor(gridSize) {
constructor(gridSize, roomName) {
this.name = roomName
this.players = [];
this.world = new World(gridSize, perlinScale);
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
@ -194,39 +155,41 @@ 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.getPlayerByID(client.id).kill(client)
// client.broadcast.emit('playerList', game.players)
game.removePlayer(client.id)
client.broadcast.emit('playerList', game.players)
})
client.on('joinGame', function(data){
console.log(game.players)
client.on('joinGame', function(tank){
game.addPlayer(client.id);
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.emit('gameVars', {gridSize: gridSize, world: game.world})
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)
game.removePlayer(client.id)
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})
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})
})
});
function sendMap(client) {
client.broadcast.emit('sync',{world: game.world.map})
client.emit('sync',{world: game.world.map})
}

0
net.js
View file