From f209af082bb4fe489dfc460997edf3616ea92355 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sun, 13 Oct 2024 15:29:08 +0100 Subject: [PATCH] ECSey LOOP NORMALISATION --- public/board.js | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/public/board.js b/public/board.js index ddba2e1..a771697 100644 --- a/public/board.js +++ b/public/board.js @@ -439,6 +439,114 @@ class Board{ } + // boardElement, cardData?, position?, size?, cardStatus, player, listPosition + ECSLoop(boardElementId = null, playerId = null, cardStatusId = null, listPositionId = null){ + // So, the intent here is to normalise my nested loop I keep duping (parts of) + // This will recieve each piece of content that can be stored in each ECS element + // Then will loop each individually, setting a new array to be returned + // This new array will then be used for tapping, untapping, moving positions, etc. + + // This is the 'core' of the loop, as it'll keep changing what's in it, removing + // elements at each step. I.e. Will check boardElement for 'mana', removing all other boardElements + // Then with just the 'mana' elements will check the player. OR can return all mana, OR all player items + let newItems = []; + let tempArray = []; + + // Set to all items in itemArray to start + newItems = item; + + //let elements = ['deck','board','hand','mana','shield', 'grave']; + // Stuff to be looped, and compared + //let boardElement = {}; + //let cardData = {}; + //let position = {}; + //let size = {}; + //let cardStatus = {}; // tapped, attacking, inspected, untargettable (TODO:maybe used this instead of inEvent later) + //let player = {}; + //let listPosition = {}; + + // These have been re-ordered to try to minise looping + // Example, player in theory halfs the cards instantly, whereas boardElement deck, may do little + // boardElement often will be better though, but??? + + // PLAYER + if(playerId !== null){ + for(let newItem = 0; newItem < newItems.length; newItem++){ + let itemKey = newItems[newItem]; + // If the playerId of item shares the passed playerId + if(playerId == player[itemKey]){ + tempArray.push(newItems[newItem]); + } + } + // Set newItems to what remains in tempArray + newItems = tempArray; + } + // Reset tempArray so it can be reused + tempArray = []; + + // CARD STATUS + if(cardStatusId !== null){ + for(let newItem = 0; newItem < newItems.length; newItem++){ + let itemKey = newItems[newItem]; + // If the playerId of item shares the passed playerId + if(cardStatusId == cardStatus[itemKey]){ + tempArray.push(newItems[newItem]); + } + } + // Set newItems to what remains in tempArray + newItems = tempArray; + } + // Reset tempArray so it can be reused + tempArray = []; + + // LIST POSITION + if(listPositionId !== null){ + for(let newItem = 0; newItem < newItems.length; newItem++){ + let itemKey = newItems[newItem]; + // If the playerId of item shares the passed playerId + if(listPositionId == listPosition[itemKey]){ + tempArray.push(newItems[newItem]); + } + } + // Set newItems to what remains in tempArray + newItems = tempArray; + } + // Reset tempArray so it can be reused + tempArray = []; + + + // BOARD ELEMENT + // Loop items for boardElement of elementType passed + // Only do the loop if the element type was passed too, no reason to waste resources + if(boardElementId !== null){ + for(let newItem = 0; newItem < newItems.length; newItem++){ + // Add the itemKey to tempArray + // itemKey can be different to the array's elementId, and we want the original itemKey, + // not the new elementId, as that would then add the wrong item + + // Set the itemKey, so things make sense + let itemKey = newItems[newItem]; //newItems[0] could be itemKey 12, so want the content + + // If the boardElement of item shares the passed boardElement add it to current return + if(boardElementId == boardElement[itemKey]){ + tempArray.push(newItems[newItem]); + } + } + + // Set newItems to what remains in tempArray + newItems = tempArray; + } + // Reset tempArray so it can be reused + tempArray = []; + + return newItems; + + // TODO: May look at restructuring this again. Maybe one loop with each check against + // the element data passed. Depends on which seems fastest, may do a compare on a large + // dataset to see + // TODO: ALSO in different func, Something like x.element.player.status to return the itemKey? + } + addFromBoardElement(playerFrom, fromPosition, elementFrom, elementTo, toPosition=null, playerTo=null){ // Move itemKey fromPosition in elementFrom to toPosition in elementTo // can also switch item between players (of from and to supplied) @@ -607,6 +715,7 @@ class Board{ this.drawBoard(); } + inspectCard(cardToInspect){ // Set inspectedCard (for now) to the card itemKey inEvent = ['inspect', cardToInspect]; @@ -1247,4 +1356,15 @@ function calculateItemSizePosition(itemKey){ position[itemKey] = [positionX,positionY]; } +function ECSLoopTest(){ + // boardElement, player, cardStatus, listPosition + // TODO: ?cardData?, position?, size? + let boardElementId = 'board'; + let playerId = 0; + let cardStatusId = null; + let listPositionId = null; + let items = board.ECSLoop(boardElementId, playerId, cardStatusId, listPositionId); + console.log('Items in boardElement: '+boardElementId+' player: '+playerId+' cardStatus: '+cardStatusId+' listPosition: '+listPositionId); + console.log(items); +}