/// <summary>Checks if the target midpoint is a valid midpoint.</summary> /// <param name="testColRef">Reference to the TestCollider script.</param> /// <param name="startPos">Start position to move from.</param> /// <param name="targetMidPoint">Midpoint to check for validity.</param> /// <param name="targetPos">Target position to move to.</param> /// <returns></returns> private bool CheckMidpointClear(TestCollider testColRef, Vector3 startPos, Vector3 targetMidPoint, Vector3 targetPos) { bool isClear = !testColRef.CheckIfLineWillHitWall(startPos, targetMidPoint); if (isClear) { isClear = !testColRef.CheckIfLineWillHitWall(targetMidPoint, targetPos); } return(isClear); }
/// <summary>Constructs a WallInWayDecision. /// Checks right away if there is a wall in the way and if there is, generates a midpoint /// such that traversing start -> mid -> target is clear.</summary> /// <param name="testColRef">Reference to the TestCollider script.</param> /// <param name="startPos">Start position to move from.</param> /// <param name="targetPos">Target position to move to.</param> public WallInWayDecision(TestCollider testColRef, Vector3 startPos, Vector3 targetPos) { // Do a physics cast to see if we can go directly to the target point wasWallInWay = testColRef.CheckIfLineWillHitWall(startPos, targetPos); if (wasWallInWay) { // If a wall was in the way, get the midpoin that would make movement valid midpoint = CreateMidpoint(testColRef, startPos, targetPos); } }
///<summary>Creates a valid midpoint that avoid walls.</summary> /// <param name="testColRef">Reference to the TestCollider script.</param> /// <param name="startPos">Start position to move from.</param> /// <param name="targetPos">Target position to move to.</param> private Vector3 CreateMidpoint(TestCollider testColRef, Vector3 startPos, Vector3 targetPos) { Vector3 xMid = new Vector3(startPos.x, targetPos.y); Vector3 yMid = new Vector3(targetPos.x, startPos.y); // If the x direction is clear if (CheckMidpointClear(testColRef, startPos, xMid, targetPos)) { return(xMid); } // If the y direction is clear if (CheckMidpointClear(testColRef, startPos, yMid, targetPos)) { return(yMid); } // If we can go neither, something is amis else { Debug.LogError("Error: Can change form in neither direction. This boolean logic or AvailableSpot detection may be flawed."); return(targetPos); } }
/// <summary> /// Updates which tiles the character is occuping. /// If the character enters a tile which is not traversable it is marked to be dealt with. /// If the character moves outside the boundries of the map it is marked to be dealth with. /// </summary> /// <param name="t_charEntity">The character gameobject that needs to be updated</param> void UpdateCharacterInMap(GameObject t_charEntity) { //List of the tiles the character was previously. List <MapIndex> prevOccupiedTiles = GetTilesWithEntity(t_charEntity); //List of the tiles the character is going to be in now. List <MapIndex> newOccupiedTiles = new List <MapIndex>(); //Collider of character used to check for change in tile. TestCollider collider = t_charEntity.GetComponent <TestCollider>(); //Bool for if a collision has occured with a tile that can not be traversed. bool collisionDetected = false; //Bool for if a character has move out side the boundry that is unguarded. bool outOfBounds = false; //Check if the collider scripts exists on the object. if (collider != null) { newOccupiedTiles.Add(WorldPositionToMapIndex(collider.m_pos + new Vector2(-collider.m_width / 2, -collider.m_hight / 2))); newOccupiedTiles.Add(WorldPositionToMapIndex(collider.m_pos + new Vector2(collider.m_width / 2, -collider.m_hight / 2))); newOccupiedTiles.Add(WorldPositionToMapIndex(collider.m_pos + new Vector2(collider.m_width / 2, collider.m_hight / 2))); newOccupiedTiles.Add(WorldPositionToMapIndex(collider.m_pos + new Vector2(-collider.m_width / 2, collider.m_hight / 2))); //Check each corner for the position with the world and the map. foreach (MapIndex corner in newOccupiedTiles) { //Check if we are not checking a tile that we are already in as that would make us be stuck in that tile for //ever is something was spawned in that tile. if (corner.m_x >= 0 && corner.m_x < m_width && corner.m_y >= 0 && corner.m_y < m_hight) { if (!m_grid[corner.m_x][corner.m_y].GetEntityList().Contains(t_charEntity)) { //Check if the tile is traversable. if (!m_grid[corner.m_x][corner.m_y].GetIsTraversable()) { //The tile is not traversable and thus collsion has been detected. collisionDetected = true; } } } else { outOfBounds = true; } } //Check if the collision has been detected. if (!collisionDetected && !outOfBounds) { foreach (MapIndex indexPos in prevOccupiedTiles) { if (!newOccupiedTiles.Contains(indexPos)) { m_grid[indexPos.m_x][indexPos.m_y].DeleteEntity(t_charEntity); } } foreach (MapIndex indexPos in newOccupiedTiles) { m_grid[indexPos.m_x][indexPos.m_y].AddEntity(t_charEntity); } } else if (outOfBounds) { //Do something here as a response to the character eg move back. Debug.Log("Character out of bounds detected!"); } else { //Do something here as a response to the character eg move back. Debug.Log("Character Collision Detected!"); } } }