class Civilization { constructor(player, game, x, y, addEnvironment = true, addTownhall = true) { this.player = player; this.game = game; this.origin = {x: x, y: y}; this.uuid = guid(); this.color = player.color; this.balance = 10; if(addEnvironment) { console.log(x, y); this.spawn(); } console.log(addTownhall); if(addTownhall) this.townhall = new Townhall(this, this.game); } spawn() { const field = this.game.field; field[this.origin.x][this.origin.y].overtake(this); field[this.origin.x][this.origin.y].getAdjacentTiles().forEach(tile => tile.overtake(this)); } destroy() { this.game.field.forEach(col => { col.forEach(tile => { if(tile.claimedBy === this) { tile.claimedBy = null; if(tile.object) { if(tile.object instanceof Warrior) tile.object.kill(); else if(tile.object instanceof Building) tile.object.destroy(); else if(tile.object instanceof Tree) tile.object.cutDown(); } } }) }) } getBalanceChange() { let balanceChange = this.getSize(); balanceChange += 4 * this.getFarms().length; for(let tower of this.getTowers()) { balanceChange -= tower.costsPerRound; } for(let troop of this.getTroops()) { balanceChange -= troop.costsPerRound; } balanceChange -= this.getTreeCount(); return balanceChange; } getCivilizationTiles() { const tiles = []; for(let x = 0; x < this.game.fieldSize.x; x++) { for(let y = 0; y < this.game.fieldSize.y; y++) { if(!!this.game.field[x][y].claimedBy && this.game.field[x][y].claimedBy.uuid === this.uuid) { tiles.push(this.game.field[x][y]); } } } return tiles; } getFarms() { return Object.values(this.game.buildings).filter(building => building instanceof Farm && building.civilization === this); } getSize() { return this.getCivilizationTiles().length; } getTowers() { return []; } getTreeCount() { return this.getCivilizationTiles().filter(tile => !!tile.object && tile.object instanceof Tree).length; } getTroops() { return Object.values(this.game.warriors).filter(warrior => warrior.civilization === this); } processRound() { this.balance += this.getBalanceChange(); if(this.balance <= 0) { this.getTroops().forEach(troop => { troop.kill(); }); } } select(selected) { this.isSelected = selected; // TODO: Update UI } split() { let tiles = this.getCivilizationTiles(); const initialSize = tiles.length; console.log('hallo welt', tiles.length, tiles[0].getAdjacentCivilizationTiles(this).length); if(tiles.length === tiles[0].getAdjacentCivilizationTiles(this).length + 1 && !!this.townhall) return; const oldIndex = game.civilizations[this.player.uuid].findIndex(civ => civ.uuid === this.uuid); game.civilizations[this.player.uuid].splice(oldIndex, 1); if(tiles.length === 0) return; const newCivs = []; if(!!this.townhall) this.townhall.destroy(); while(tiles.length > 0) { console.log('hey'); const civTiles = [tiles[0], ...tiles[0].getAdjacentCivilizationTiles(this)]; tiles = tiles.filter(tile => !civTiles.includes(tile)); const availableTiles = civTiles.filter(tile => !tile.object); let newOrigin = civTiles[Math.floor(Math.random() * civTiles.length)]; if(availableTiles.length > 0) { newOrigin = availableTiles[Math.floor(Math.random() * availableTiles.length)]; } console.log(availableTiles.length > 0); const newCivilization = game.addCivilization(this.player, newOrigin.pos.x, newOrigin.pos.y, this.constructor !== CivilizationAI, false, availableTiles.length > 0); newCivs.push(newCivilization); newCivilization.balance = Math.round(this.balance * (civTiles.length / initialSize)); civTiles.forEach(tile => { tile.claimedBy = newCivilization; if(!!tile.object && (tile.object instanceof Warrior || tile.object instanceof Tower)) { tile.object.civilization = newCivilization; } }); } newCivs.forEach(civ => { }); } }