Switch loops to normalised loop

develop
Nathan Steel 1 year ago
parent f209af082b
commit 6458e7d85a

@ -554,47 +554,29 @@ class Board{
playerTo = playerFrom;
}
// First (and only) item returned is the key thins should be done with
let itemKey = this.ECSLoop(elementFrom, playerFrom, null, fromPosition)[0];
if(listPosition[itemKey] == fromPosition){
// Check if a toPostion supplied
if(toPosition == null){
// Get the new position of item based on new boardElement
toPosition = getCurrentPositionAndLength(elementTo, playerTo)[0]+1
}
//console.log('itemKey: '+itemKey+' fromPosition: '+fromPosition+' elementFrom: '+elementFrom+' elementTo: '+elementTo+' toPosition: '+toPosition+' playerFrom: '+playerFrom+' playerTo: '+playerTo);
// Loop all items, get the item with boardElement elementFrom and player playerFrom
for(let itemKey = 0; itemKey < item.length; itemKey++){
// Get the item from the element
if(itemKey in boardElement && boardElement[itemKey] == elementFrom){
// Check if item belongs to playerFrom
if(itemKey in player && player[itemKey] == playerFrom){
// Get item based on fromPosition listPosition
if(listPosition[itemKey] == fromPosition){
// Check if a toPostion supplied (likely won't be, dunno if I'll do this)
if(toPosition == null){
// Get the new position of item based on new boardElement
toPosition = getCurrentPositionAndLength(elementTo, playerTo)[0]+1
//console.log(toPosition);
}
//console.log('itemKey: '+itemKey+' fromPosition: '+fromPosition+' elementFrom: '+elementFrom+' elementTo: '+elementTo+' toPosition: '+toPosition+' playerFrom: '+playerFrom+' playerTo: '+playerTo);
listPosition[itemKey] = toPosition;
// Move from elementFrom to elementTo
//console.log('elementTo: '+elementTo);
//console.log(boardElement[itemKey]);
boardElement[itemKey] = elementTo;
//console.log(itemKey);
//console.log(boardElement[itemKey]);
// Move down(0) the positions of elementFrom, from fromPosition for player
//console.log(JSON.stringify(boardElement));
//console.log(JSON.stringify(listPosition));
this.moveElementPositions(0, elementFrom, fromPosition, playerFrom);
//console.log(JSON.stringify(boardElement));
//console.log(JSON.stringify(listPosition));
this.removeItemStatus(itemKey);
this.drawBoard();
return 1; // Got a loop that calls a loop, and checks the new values atm, so this keeps counting down
// Move item to it's new position
listPosition[itemKey] = toPosition;
// Move item to it's new element
boardElement[itemKey] = elementTo;
}
}
}
// Move down(0) the positions of elementFrom, from fromPosition for player
this.moveElementPositions(0, elementFrom, fromPosition, playerFrom);
this.removeItemStatus(itemKey);
this.drawBoard();
return 1; // Got a loop that calls a loop, and checks the new values atm, so this keeps counting down
}
}
moveElementPositions(direction, elementFrom, fromPosition, playerFrom){
@ -604,31 +586,18 @@ class Board{
// Loop the elementFrom, and move positions for anything after
// item taken from position 34, 35..60 need to be moved down to 34..59
// For each item
for(let itemKey = 0; itemKey < item.length; itemKey++){
// If boardElement is elementFrom
if(itemKey in boardElement && boardElement[itemKey] == elementFrom){
// And from the playerFrom (id)
if(itemKey in player && player[itemKey] == playerFrom){
//console.log(elementFrom);
//console.log(boardElement[itemKey]);
//console.log(listPosition[itemKey]);
//console.log(itemKey);
if(listPosition[itemKey] > fromPosition){
//console.log('position: '+listPosition[itemKey]+' fromPosition: '+fromPosition);
// Move items up, i.e. added to top of deck
if(direction){
listPosition[itemKey]++;
}
// Move items down, i.e. taken from top of deck
listPosition[itemKey]--;
}
let items = this.ECSLoop(elementFrom, playerFrom, null, null);
for(let item = 0; item < items.length; item++){
let itemKey = items[item];
if(listPosition[itemKey] > fromPosition){
// Move items up, i.e. added to top of deck
if(direction){
listPosition[itemKey]++;
}
// Move items down, i.e. taken from top of deck
listPosition[itemKey]--;
}
}
//console.log(listPosition);
}
// Draw a card, traditional TCG
drawACard(playerId, cardsToDraw = 1){
@ -860,28 +829,19 @@ class Board{
alert('Shield not tapped, cannot be destroyed');
return false;
}
// Check if all other shields are tapped before destroying the shield
// TODO: Normalise the loop!!!
// Think this can be done by looping what's needed, then returning itemKeys in array for each
// item that hits criteria, then looping that new array and applying logic?
// Loop all items, get the item with boardElement elementFrom and player playerFrom
for(let itemKey = 0; itemKey < item.length; itemKey++){
// Get the item from the element
if(itemKey in boardElement && boardElement[itemKey] == 'shield'){
// Check each shield on the board of the owner of shield being attacked
if(itemKey in player && player[itemKey] == player[itemKey]){
// If ANY of their shields are untapped, you can't destroy target
if(cardStatus[itemKey] != 'tapped'){
alert('There is an untapped shield, cannot destroy target');
return false;
}
}
// Get all shields, that belong to the player whose shield was targetted.
// Check if they're all tapped before destroying the target
let items = this.ECSLoop('shield', player[itemKey], null, null);
for(let item = 0; item < items.length; item++){
let itemKey = items[item];
// If ANY of their shields are untapped, you can't destroy target
if(cardStatus[itemKey] != 'tapped'){
alert('There is an untapped shield, cannot destroy target');
return false;
}
}
// Shield is now destroyed, move it from shield to owners hand (for the catchup mechanic)
//addFromBoardElement(playerFrom, fromPosition, elementFrom, elementTo, toPosition=null, playerTo=null)
this.addFromBoardElement(player[itemKey], listPosition[itemKey], boardElement[itemKey], 'hand', null, null);
return true;
}
@ -942,6 +902,7 @@ canvas.addEventListener('contextmenu', function(event) {
continue;
}
// TODO: Normalise this too, may need to change how size+position work?
// Check the item has a size and position
if(itemKey in size && itemKey in position){
// Compare the event XY position to the item
@ -992,6 +953,7 @@ canvas.addEventListener('click', function(event) {
// TODO:Maybe write this into a function? If XY WH is hit return true, and the itemKey
// then it can be re-used in contextclick, hover, etc without rewrite
//console.log('X: '+x+' Y: '+y);
// TODO: Normalise this too
for(let itemKey = 0; itemKey < item.length; itemKey++){
if(elements[itemKey] == 'deck'){
@ -1186,20 +1148,10 @@ function shuffleDeck(playerId){
}
function untapZone(elementFrom, playerFrom){
// TODO: Currently copy of the loop. Make the loop multi-purpose without needing to dupe the layers
// of the loop (THIS APPLIES FOR ALL USES OF THE LOOP!!)
for(let itemKey = 0; itemKey < item.length; itemKey++){
// Get the item from the element
if(itemKey in boardElement && boardElement[itemKey] == elementFrom){
// Check if item belongs to playerFrom
if(itemKey in player && player[itemKey] == playerFrom){
// Just basic, if tapped, untap logic
// See why the loop shouldn't need re-adding each time?
if(cardStatus[itemKey] == 'tapped'){
board.untapCard(itemKey);
}
}
}
let items = board.ECSLoop(elementFrom, playerFrom, null, null);
for(let item = 0; item < items.length; item++){
let itemKey = items[item];
if(board.isTapped(itemKey)){ board.untapCard(itemKey); }
}
board.drawBoard();
}
@ -1220,24 +1172,16 @@ function getCurrentPositionAndLength(elementName, playerId){
let highestListPosition = 0;
let length = 0;
// Loop all the items
// Think making the for loop, and the if into something to be called that fires functions would be smart?
for(let itemKey = 0; itemKey < item.length; itemKey++){
// Check the item is the correct boardElement
// 'key' in 'object'
if(itemKey in boardElement && boardElement[itemKey] == elementName){
// Check if item belongs to the player
if(itemKey in player && player[itemKey] == playerId){
if(listPosition[itemKey]){
if(listPosition[itemKey] >= highestListPosition){
highestListPosition = listPosition[itemKey];
length++;
}
}
let items = board.ECSLoop(elementName, playerId, null, null);
for(let item = 0; item < items.length; item++){
let itemKey = items[item];
}
if(listPosition[itemKey] >= highestListPosition){
highestListPosition = listPosition[itemKey];
length++;
}
}
return [highestListPosition, length];

Loading…
Cancel
Save