List <Character> FindCharacters() { //Get the grid the character is currently in GridIndex index = WorldGrid.GetGridIndex(character.transform.position); GridCell cell = WorldGrid.GetTheWorldGridCell(index); //check if the cell position is within the search radius //bool xSearch = true, ySearch = true; //add all characters to a list List <Character> proximityCharacters = new List <Character>(); //TODO: add more intuitive and wider search logic depending on character behavior if (character.gridCell != null) { if (character.gridCell.character.Count > 0) { //Add the list of characters. This info is withing grid cell proximityCharacters.AddRange(character.gridCell.character); //Debug.Log("count : " + proximityCharacters.Count); //Debug.Log("names: " + proximityCharacters[0].name + " " + character.name); //Remove yourself from the characters list for (int i = 0; i < proximityCharacters.Count; i++) { if (proximityCharacters[i].gameObject == character.gameObject) { proximityCharacters.RemoveAt(i); //Debug.Log("Removed"); } } } } //TODO: Filter the characters to find the ones that are withing this character's FOV return(proximityCharacters); }
private void Start() { spriteRenderer = GetComponent <SpriteRenderer>(); Vector3 extents = spriteRenderer.sprite.bounds.extents; //leftNode.position = new Vector3(gameObject.transform.position.x - extents.x, // gameObject.transform.position.y + extents.y, // gameObject.transform.position.z); //rightNode.position = new Vector3(gameObject.transform.position.x + extents.x, // gameObject.transform.position.y + extents.y, // gameObject.transform.position.z); if (gameObject.tag == "tag_ladder") { leftNode.position = new Vector3(verticalAdjustment, -extents.y / leftNodeSpacingVariable, 0); rightNode.position = new Vector3(verticalAdjustment, extents.y / rightNodesSpacingVariable, 0); } else { leftNode.position = new Vector3(-extents.x / leftNodeSpacingVariable, extents.y + verticalAdjustment, 0); rightNode.position = new Vector3(extents.x / rightNodesSpacingVariable, extents.y + verticalAdjustment, 0); } //Matrix4x4 parentTransform = Matrix4x4.TRS(transform.parent.position, transform.parent.rotation, transform.parent.localScale); Matrix4x4 platformMatrix = Matrix4x4.TRS(gameObject.transform.position, gameObject.transform.rotation, gameObject.transform.lossyScale); //platformMatrix = platformMatrix * parentTransform; leftNode.position = platformMatrix.MultiplyPoint3x4(leftNode.position); rightNode.position = platformMatrix.MultiplyPoint3x4(rightNode.position); leftNode.platform = this; rightNode.platform = this; width = extents.x * gameObject.transform.localScale.x * 2; height = extents.y * gameObject.transform.localScale.y * 2; //Adding grid cells for both the nodes so that they register themselves to a certain part of the world GridIndex index; index = WorldGrid.GetGridIndex(leftNode.position); leftNode.gridCell = WorldGrid.GetTheWorldGridCell(index); WorldGrid.AddToCell(leftNode, leftNode.gridCell); index = WorldGrid.GetGridIndex(rightNode.position); rightNode.gridCell = WorldGrid.GetTheWorldGridCell(index); WorldGrid.AddToCell(rightNode, rightNode.gridCell); }
//Call this function to register player to the world grid protected void UpdatePositionInWorld()//Vector3 position, out GridCell gridCell) { GridIndex index = WorldGrid.GetGridIndex(gameObject.transform.position); if (gridCell == null) { gridCell = WorldGrid.GetTheWorldGridCell(index); } else if (index.x != gridCell.index.x || index.y != gridCell.index.y) { gridCell = WorldGrid.GetTheWorldGridCell(index); } if (gridCell != previousGridCell) { if (gridCell != null) { WorldGrid.AddToCell(this, gridCell); if (previousGridCell != null) { WorldGrid.RemoveFromCell(this, previousGridCell); } previousGridCell = gridCell; } else { Debug.Log("Something went wrong. Player doesn't have a grid cell"); } } //Debug.DrawLine(Vector3.zero, gridCell.worldPosition); //Debug.Log("Character: " + gameObject.name); //Debug.Log("Grid cell indices: " + gridCell.index.x + " " + gridCell.index.y); //Debug.Log("Grid cell indices: " + gridCell.index.x + " " + gridCell.index.y); //Debug.Log("Grid character count: " + gridCell.character.Count); //Debug.Log("Grid node count: " + gridCell.node.Count); //List<GridCell> cells = WorldGrid.Instance.gridArray[gridIndex.x, gridIndex.y]; }
public void Update(ref PHScanMechanic scan, ref Player player) { if (finalAnimationStarted) { //If the final animation is playing, nothing else must be played. //If the control reaches this block, just return so that the rest of the code doesn't execute FinalAnimation(ref scan); return; } if (!ContinuepHScan(ref scan, ref player)) { finalAnimationStarted = true; } if (!initialAnimationFinished) { InitialAnimation(); } if (initialAnimationFinished) { currentScanPosition.x += speed * direction * GameManager.Instance.DeltaTime; //Position of the scan visual object must be set scanVisualObject.transform.position = currentScanPosition; //Debug.Log("current scan position: " + currentScanPosition); //Debug.DrawLine(Vector3.zero, currentScanPosition); Debug.DrawLine(new Vector3(currentScanPosition.x, currentScanPosition.y - verticalReach, currentScanPosition.z), new Vector3(currentScanPosition.x, currentScanPosition.y + verticalReach, currentScanPosition.z), Color.red); //Search all the nodes starting from the top range of the player's node GridCell previousCell = null; //Iterate the grid cells from bottom to top in the column searching for the items and enemies for (int i = -verticalReach; i < verticalReach; i++) { GridCell cellToCheck = null; List <GridCell> gridCells = WorldGrid.Instance.gridArray[gridCell.index.x, gridCell.index.y + i]; List <Character> enemiesList = new List <Character>(); //if the grid cells exist, get the correct grid cell to check for enemies and items Vector3 positionToCheck = new Vector3(currentScanPosition.x, currentScanPosition.y + i, currentScanPosition.z); cellToCheck = WorldGrid.GetTheWorldGridCellWithoutCreatingNewCells(WorldGrid.GetGridIndex(positionToCheck)); if (cellToCheck == previousCell) { continue; } else { previousCell = cellToCheck; } if (cellToCheck == null) { Debug.LogError("Cell to check is null...Breaking out of the function"); continue; } Debug.Log("Cell to check position: " + cellToCheck.worldPosition); enemiesList = cellToCheck.character; Debug.Log("Drawing line"); Debug.DrawLine(Vector3.zero, cellToCheck.worldPosition, Color.red); if (enemiesList != null) { for (int j = 0; j < enemiesList.Count; j++) { if (direction > 0) { if (enemiesList[j].transform.position.x <= currentScanPosition.x) { Debug.Log("marked the enemy....Revealing pH"); if (enemiesList[j].GetComponent <CharacterMechanics>()) { RevealpH(enemiesList[j].GetComponent <CharacterMechanics>(), ref player); } } } else { if (enemiesList[j].transform.position.x >= currentScanPosition.x) { Debug.Log("marked the enemy....revealing pH"); if (enemiesList[j].GetComponent <CharacterMechanics>()) { RevealpH(enemiesList[j].GetComponent <CharacterMechanics>(), ref player); } } } } } } } }