Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building 2 - Construction of ships #70

Merged
merged 1 commit into from Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions board.js
Expand Up @@ -160,15 +160,15 @@ let gameBoard = {


// Creation of ships
this.boardArray[boardCenter-1][col-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
this.boardArray[boardCenter+1][col-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter+1, homeCol: col-1};
this.boardArray[0][boardCenter-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '180', used: 'unused', damageStatus: 5, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: 0, homeCol: boardCenter-1};
this.boardArray[0][boardCenter+1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '180', used: 'unused', damageStatus: 5, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: 0, homeCol: boardCenter+1};
this.boardArray[row-1][boardCenter-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '0', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0, production: 0, homeRow: row-1, homeCol: boardCenter-1};
this.boardArray[row-1][boardCenter+1].pieces = {populatedSquare: true, category: 'Transport', type: 'warship', direction: '0', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0, production: 0, homeRow: row-1, homeCol: boardCenter+1};
this.boardArray[row-2][boardCenter].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '0', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0, production: 0, homeRow: row-1, homeCol: boardCenter+1};
this.boardArray[boardCenter-1][0].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '90', used: 'unused', damageStatus: 5, team: 'Blue Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: 0};
this.boardArray[boardCenter+1][0].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '90', used: 'unused', damageStatus: 5, team: 'Blue Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter+1, homeCol: 0};
this.boardArray[boardCenter-1][col-1].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
//this.boardArray[boardCenter+1][col-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter+1, homeCol: col-1};
this.boardArray[0][boardCenter-1].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '180', used: 'unused', damageStatus: 5, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: 0, homeCol: boardCenter-1};
//this.boardArray[0][boardCenter+1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '180', used: 'unused', damageStatus: 5, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: 0, homeCol: boardCenter+1};
//this.boardArray[row-1][boardCenter-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '0', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0, production: 0, homeRow: row-1, homeCol: boardCenter-1};
this.boardArray[row-1][boardCenter+1].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '0', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0, production: 0, homeRow: row-1, homeCol: boardCenter+1};
//this.boardArray[row-2][boardCenter].pieces = {populatedSquare: true, category: 'Transport', type: 'warship', direction: '0', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0, production: 0, homeRow: row-1, homeCol: boardCenter+1};
//this.boardArray[boardCenter-1][0].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '90', used: 'unused', damageStatus: 5, team: 'Blue Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: 0};
this.boardArray[boardCenter+1][0].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '90', used: 'unused', damageStatus: 5, team: 'Blue Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter+1, homeCol: 0};

// Creation of pirate ships and pirate harbours
this.boardArray[4][6] = {xpos: 4, ypos: 6, terrain: 'sea', subTerrain: 'pirateHarbour', activeStatus: 'inactive', pieces: {populatedSquare: true, category: 'Transport', type: 'warship', direction: '135', used: 'unused', damageStatus: 5, team: 'Pirate', goods: 'none', stock: 0, ref: 0}};
Expand Down Expand Up @@ -200,11 +200,8 @@ let gameBoard = {
this.boardArray[boardCenter][boardCenter+1].pieces = {populatedSquare: true, category: 'Resources', type: 'plantation', direction: '0', used: 'unused', damageStatus: 5, team: 'Kingdom', goods: 'coffee', stock: 0, production: 2};

// TEST AREA
/* this.boardArray[(row-4)][boardCenter].terrain = 'land';
this.boardArray[(row-4)][boardCenter].pieces = {populatedSquare: true, category: 'Settlements', type: 'fort', direction: '0', used: 'unused', damageStatus: 5, team: 'Kingdom', goods: 'none', stock: 0};
tradeContracts.contractsArray[2].row = row-4;
tradeContracts.contractsArray[2].col = boardCenter;

/*
// Battle Royale
this.boardArray[row-4][boardCenter].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '45', used: 'unused', damageStatus: 5, team: 'Pirate', goods: 'none', stock: 0};
this.boardArray[row-4][boardCenter + 1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '45', used: 'unused', damageStatus: 5, team: 'Pirate', goods: 'none', stock: 0};
Expand All @@ -231,7 +228,7 @@ let gameBoard = {
{i: row-1, j: boardCenter+1, team: 'Green Team'},
{i: boardCenter+1, j: 0, team: 'Blue Team'}]

// function picks a tile form the resource deck and checks polayer does not already have this Resource allocated
// function picks a tile form the resource deck and checks player does not already have this Resource allocated
function completeAllocatedTile(locali, localj, localTeam) {
stockDashboard.stockTake();
//keep for debugging - console.log(stockDashboard.pieceTotals);
Expand All @@ -241,7 +238,7 @@ let gameBoard = {
var deckCard = resourceManagement.pickFromResourceDeck();
//keep for debugging - console.log(deckCard.type);
}
while (stockDashboard.pieceTotals[pieceTotalsTeamPosition].pieces[deckCard.type] > 0)
while (stockDashboard.pieceTotals[pieceTotalsTeamPosition].pieces[deckCard.type].quantity > 0)

gameBoard.boardArray[locali][localj].pieces = {populatedSquare: true, category: 'Resources', type: deckCard.type, direction: '0', used: 'unused', damageStatus: 5, team: localTeam, goods: deckCard.goods, stock: 0};
}
Expand Down
162 changes: 162 additions & 0 deletions build.js
@@ -0,0 +1,162 @@
// Building object - methods and arrays to allow building of ships
let buildItem = {

// Array to hold building recipes
// -------------------------------
buildRecipe: [
{type: 'warship', recipe: [{goods: 'wood', quantity: 8}, {goods: 'iron', quantity: 12}, {goods: 'stone', quantity: 8}]},
{type: 'cargo ship', recipe: [{goods: 'wood', quantity: 10}, {goods: 'iron', quantity: 10}, {goods: 'stone', quantity: 8}]},
],

// Method to handle clicks on stock dashboard
// ------------------------------------------
// Only build actions currently operational from clicking on stock dashboard
clickStock: function(e) {
if (e.target.classList.contains('buildcargo') || e.target.parentNode.classList.contains('buildcargo')) {
buildItem.clickBuild(e, 'cargo ship');
} else if (e.target.classList.contains('buildwarship') || e.target.parentNode.classList.contains('buildwarship')) {
buildItem.clickBuild(e, 'warship');
}
},

// Method to operate building slider and check if chosen ship can be built
// -----------------------------------------------------------------------
clickBuild: function(stockElement, localBuild) {
// Resets any half-made board moves and deactivates tiles
resetMove();

// Clears building slider for use
buildItem.clearBuilding();

// Slides building slider up and commentary down
commentary.style.bottom = '-10%';
building.style.bottom = 0;

// Refreshes amount of stock held by players and set up variables
stockDashboard.goodsStockTake();
let teamNo = gameManagement.teamArray.indexOf(gameManagement.turn);
let buildNo = buildItem.buildRecipe.findIndex(y => y.type == localBuild);

// Add icon of ship clicked on
let buildIcon = building.appendChild(gameBoard.createActionTile(0, 0, localBuild, gameManagement.turn, 'buildPiece', 10, (screenWidth - 2*surroundSize) * 0.04 - (gridSize + 2*tileBorder)/2, 1.5, 0));

// Loop through each goods type in build recipe and only allow construction if sufficient goods stock available
let allowConstruction = true;
for (var k = 0; k < buildItem.buildRecipe[buildNo].recipe.length; k++) {
if(stockDashboard.goodsTotals[teamNo].land[buildItem.buildRecipe[buildNo].recipe[k].goods] < buildItem.buildRecipe[buildNo].recipe[k].quantity) {
allowConstruction = false;
}
// Adds goods icons to illustrate quantity held vs quantity required
for (var i = 0; i < Math.min(stockDashboard.goodsTotals[teamNo].land[buildItem.buildRecipe[buildNo].recipe[k].goods], buildItem.buildRecipe[buildNo].recipe[k].quantity); i++) {
building.appendChild(gameBoard.createIcon(buildItem.buildRecipe[buildNo].recipe[k].goods + i, 1.5, buildItem.buildRecipe[buildNo].recipe[k].goods, (screenWidth - 2*surroundSize) * ((k+2) * 0.20) - tileBorder/2 + (((i % 10) - 0.5) * (gridSize + tileBorder) / 1.5), 10 + Math.floor(i/10) * ((gridSize + tileBorder) / 1.5)));
for (var z = 0; z < building.lastChild.children.length; z++) {
let nextChild = building.lastChild.children[z];
nextChild.setAttribute('class', building.lastChild.children[z].baseVal + ' ' + gameManagement.turn + ' team_stroke team_fill');
}
}
for (var j = stockDashboard.goodsTotals[teamNo].land[buildItem.buildRecipe[buildNo].recipe[k].goods]; i < buildItem.buildRecipe[buildNo].recipe[k].quantity; i++) {
building.appendChild(gameBoard.createIcon('stock' + i, 1.5, buildItem.buildRecipe[buildNo].recipe[k].goods, (screenWidth - 2*surroundSize) * ((k+2) * 0.20) - tileBorder/2 + (((i % 10) - 0.5) * (gridSize + tileBorder) / 1.5), 10 + Math.floor(i/10) * ((gridSize + tileBorder) / 1.5)));
}
}

// Adds ship construction details to building slider
firstBuildLine.innerText = 'Construction of ' + localBuild + ' requires:';
secondBuildLine.innerText = buildItem.buildRecipe[buildNo].recipe[0].goods + ' ' + buildItem.buildRecipe[buildNo].recipe[0].quantity + ', ' + buildItem.buildRecipe[buildNo].recipe[1].goods + ' ' + buildItem.buildRecipe[buildNo].recipe[1].quantity + ', ' + buildItem.buildRecipe[buildNo].recipe[2].goods + ' ' + buildItem.buildRecipe[buildNo].recipe[2].quantity ;
// Sets up piece movement array and adds ship type and launches construction
if (allowConstruction == true) {
thirdBuildLine.innerText = 'Click ship icon to confirm construction.';
pieceMovement.movementArray = {start: {row: '', col: ''}, end: {row: '', col: ''}};
pieceMovement.movementArray.start.pieces = {type: localBuild};
buildIcon.addEventListener('click', buildItem.startConstruction);
// Does not launch construction if insufficient stock
} else {
thirdBuildLine.innerText = 'Insufficient goods available to build ' + localBuild + '.';
}
},

// Method to activate harbours and capture start inputs of building turn
// ---------------------------------------------------------------------
// End inputs are captured as normal through buildMarkNode event listener
startConstruction: function() {
// Activating current team harbours
for (var i = 0; i < gameBoard.boardArray.length; i++) {
for (var j = 0; j < gameBoard.boardArray[i].length; j++) {
if (gameBoard.boardArray[i][j].pieces.team == gameManagement.turn && gameBoard.boardArray[i][j].subTerrain == 'harbour' && gameBoard.boardArray[i][j].pieces.populatedSquare == false) {
gameBoard.boardArray[i][j].activeStatus = 'active';
} else if (gameBoard.boardArray[i][j].pieces.team == gameManagement.turn && gameBoard.boardArray[i][j].pieces.type == 'fort') {
pieceMovement.movementArray.start.row = i;
pieceMovement.movementArray.start.col = j;
}
}
}

// Capture move inputs, since boardMarkNode has not been clicked on. Uses 'Building' as special designation for off-board action.
pieceMovement.movementArray.start.pieces = {populatedSquare: false, category: 'Building', type: pieceMovement.movementArray.start.pieces.type, direction: 0, used: 'unused', damageStatus: 3, team: gameManagement.turn, goods: 'none', stock: 0, production: 0};
gameBoard.drawActiveTiles();
startEnd = 'end';

// Changing building text
firstBuildLine.innerText = 'Select harbour for construction.';
secondBuildLine.innerText = '';
thirdBuildLine.innerText = '';
},

// Method to reduce stock levels of Resource and fort tiles for construction of ship
// ---------------------------------------------------------------------------------
// Fort stock is used prior to and Resource piece stock
constructionPayment: function(localBuild) {
// Updates stock numbers for use in this method
stockDashboard.stockTake();
let teamArrayPosition = stockDashboard.pieceTotals.findIndex(fI => fI.team == gameManagement.turn);
let buildNo = this.buildRecipe.findIndex(fI => fI.type == localBuild);

// Determines if any of the resources are in the team fort and reduces this balance first
let fortPaymentGoods = 'none';
let fortPaymentQuantity = 0;
for (var a = 0; a < this.buildRecipe[buildNo].recipe.length; a++) {
if(stockDashboard.pieceTotals[teamArrayPosition].pieces.fort.goods == this.buildRecipe[buildNo].recipe[a].goods) {
fortPaymentGoods = this.buildRecipe[buildNo].recipe[a].goods;
fortPaymentQuantity = Math.min(this.buildRecipe[buildNo].recipe[a].quantity, stockDashboard.pieceTotals[teamArrayPosition].pieces.fort.stock);
}
}

// Loops through each board tile and reduces the goods on Resource and fort tiles in accordance with construction costs
for (var i = 0; i < gameBoard.boardArray.length; i++) {
for (var j = 0; j < gameBoard.boardArray[i].length; j++) {
if (gameBoard.boardArray[i][j].pieces.team == gameManagement.turn && gameBoard.boardArray[i][j].pieces.category == 'Resources') {
for (var k = 0; k < this.buildRecipe[buildNo].recipe.length; k++) {
if (this.buildRecipe[buildNo].recipe[k].goods == gameBoard.boardArray[i][j].pieces.goods) {
gameBoard.boardArray[i][j].pieces.stock -= this.buildRecipe[buildNo].recipe[k].quantity
// Adjustment for fort stock which is reduced below
if (gameBoard.boardArray[i][j].pieces.goods == fortPaymentGoods) {
gameBoard.boardArray[i][j].pieces.stock += fortPaymentQuantity;
}
}
}
// Use of fort stock
} else if (gameBoard.boardArray[i][j].pieces.team == gameManagement.turn && gameBoard.boardArray[i][j].pieces.type == 'fort') {
gameBoard.boardArray[i][j].pieces.stock -= fortPaymentQuantity;
if (gameBoard.boardArray[i][j].pieces.stock == fortPaymentQuantity) {
gameBoard.boardArray[i][j].pieces.goods = 'none';
}

}
}
}
},

// Resets building slider
// ----------------------
clearBuilding: function() {
if(workFlow == 1) {console.log('Clearing building slider: ' + (Date.now() - launchTime)); }
for (var i = building.children.length - 1; i > -1; i--) {
if (building.children[i].id == 'firstBuildLine' || building.children[i].id == 'secondBuildLine' || building.children[i].id == 'thirdBuildLine') {
building.children[i].innerText = '';
} else {
building.children[i].remove();
}
}
},

// LAST BRACKET OF OBJECT
}