/// <summary> /// only used for when it is trapped /// </summary> /// <returns></returns> bool GetTrappedMovingDirection() { Detection.DIRECTIONS kittenDirection = mouseDetection.GetTargetDirection(mouseDetection.kittenObject.transform.position); Detection.DIRECTIONS playerDirection = mouseDetection.GetTargetDirection(mouseDetection.playerObject.transform.position); Detection.DIRECTIONS tempDir = Detection.DIRECTIONS.NONE; tempDir = GetTrappedRunningDir(playerDirection, kittenDirection); // theres no other path if (tempDir == Detection.DIRECTIONS.NONE) { // idk what to do here yet // this is where it needs to do bouncing back and forth isTrappedMovement = true; // set a random moving direction to start the bounce movingDir = playerDirection; } else { movingDir = tempDir; return(true); } return(false); }
public void UpdateAnimation(bool isMoving, Detection.DIRECTIONS dir) { Vector2 startingDir = Vector2.zero; switch (kittenDetection.startingDir) { case Detection.DIRECTIONS.UP: startingDir = new Vector2(0, 1); break; case Detection.DIRECTIONS.DOWN: startingDir = new Vector2(0, -1); break; case Detection.DIRECTIONS.LEFT: startingDir = new Vector2(-1, 0); break; case Detection.DIRECTIONS.RIGHT: startingDir = new Vector2(1, 0); break; } UpdateAnimation(isMoving, startingDir); }
/// <summary> /// Takes in the direction, passes in the position to the map manager /// Checks if that direction is clear and returns true if it is /// </summary> /// <param name="directionToCheck">The direction to check against the map manager</param> /// <returns>Return true if the path is clear</returns> public bool CheckDirection(Detection.DIRECTIONS directionToCheck) { // Get its current tile pos Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); // idk if i should check just the first tile or more than 1 // for now i'll just check once // EDIT: once does not work. I repeat, once does not work aifioasiofnasionfoashfhio int currentTileCounter = 0; while (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { currentTileCounter++; // Move the currentTilePos switch (directionToCheck) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; default: Debug.LogError("It shouldn't reach here lol"); break; } } // if the current tile counter is more than the number of empty tiles in the current direction // it means that it found a better direction if (currentTileCounter > currentDirTileCount) { // Set the new moving direction movingDir = directionToCheck; currentDirTileCount = currentTileCounter; return(true); } else { return(false); } //if (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) // return true; }
/// <summary> /// Uses the Map manager to find the end tile in the direction it needs to move in /// It'll make it so the cat will run all the way to the end /// </summary> /// <param name="directionToCheck">Direction to check</param> /// <returns> Returns the vector 2 position </returns> public Vector2 FindEndTile(Detection.DIRECTIONS directionToCheck) { // NOTE: // ONE WAY TO TRY AND FIX JITTERY MOVEMENTS // JITTERY MOVEMENTS AS IN IT'LL KEEP CHANGING BETWEEN DIRECTIONS // BECAUSE ITS A LIL PIECE OF SHIT // i'll make it so it'll check for the target object // and stop at them // i.e if im checking on the up direction then i'll stop at their y position instead of going all the way // actually i dont think this is gonna fix it // but it do b like that sometimes // Get the current tile position in the map Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); //Create a variable to store the vector Vector2 targetTilePos = Vector2.zero; while (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { // store the current tile it checked as the target tile pos targetTilePos = MapManager.Instance.GetTileToWorldPos(currentTilePos); switch (directionToCheck) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; case Detection.DIRECTIONS.NONE: break; default: Debug.Log("It shouldn't reach here omo, u fuked up ASNfgioahOI"); break; } } return(targetTilePos); }
/// <summary> /// Check which direction to move inbetween every tile /// </summary> /// <returns> true if it found a direction </returns> bool GetMovingDirection() { if (CheckDirection(kittenDetection.targetDir)) { movingDir = kittenDetection.targetDir; } else { // Check the direction of the target // there is no point checking the opposite of the target // the kitten is suppose to be chasing switch (kittenDetection.targetDir) { case Detection.DIRECTIONS.UP: movingDir = GetAltDirection(Detection.DIRECTIONS.DOWN, kittenDetection.targetDir); break; case Detection.DIRECTIONS.DOWN: movingDir = GetAltDirection(Detection.DIRECTIONS.UP, kittenDetection.targetDir); break; case Detection.DIRECTIONS.LEFT: movingDir = GetAltDirection(Detection.DIRECTIONS.RIGHT, kittenDetection.targetDir); break; case Detection.DIRECTIONS.RIGHT: movingDir = GetAltDirection(Detection.DIRECTIONS.LEFT, kittenDetection.targetDir); break; case Detection.DIRECTIONS.NONE: break; default: break; } } if (movingDir != Detection.DIRECTIONS.NONE) { return(true); } return(false); }
Detection.DIRECTIONS GetTrappedRunningDir(Detection.DIRECTIONS playerDirection, Detection.DIRECTIONS kittenDirection) { Detection.DIRECTIONS tempDirection = Detection.DIRECTIONS.NONE; // Detection.DIRECTIONS tempDirection = Detection.DIRECTIONS.NONE; int tempDirectionSize = 0; Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); // loop through all the 4 directions for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index) { // dont check the other 2 directions if (index == (int)playerDirection) { continue; } else if (index == (int)kittenDirection) { continue; } // the current direction being checked Detection.DIRECTIONS currDirection = (Detection.DIRECTIONS)index; int currDirectionSize = 0; currDirectionSize = CheckDirectionSize(currDirection); // if theres more spaces in this direction if (currDirectionSize >= tempDirectionSize) { // set the direction size tempDirectionSize = currDirectionSize; tempDirection = currDirection; } } // if tempDirectionSize is too small if (tempDirectionSize > 1) { return(tempDirection); } return(Detection.DIRECTIONS.NONE); }
/// <summary> /// Searches through the map manager to find the end tile in a direction /// If its running, it will run all the way until it hits a wall /// </summary> /// <param name="directionToCheck">The direction it wants to move in</param> /// <returns>Return the vector of the last tile</returns> public Vector2 FindEndTile(Detection.DIRECTIONS directionToCheck) { // Get the tile position of the current tile Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); Vector2 TargetTilePos = Vector2.zero; while (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { // store the current tile it checked as the target tile pos TargetTilePos = MapManager.Instance.GetTileToWorldPos(currentTilePos); // Move the currentTilePos switch (directionToCheck) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; default: Debug.Log("It shouldn't reach here lol"); break; } } return(TargetTilePos); }
/// <summary> /// Check how many empty tiles there are in a direction /// </summary> /// <returns>The number of empty tiles</returns> int CheckDirectionSize(Detection.DIRECTIONS dirToCheck) { int currentTileCounter = 0; Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); // while the path is clear // keep checking to find out how many // will take the direction with the longest clear path while (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { currentTileCounter++; switch (dirToCheck) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; default: Debug.LogError("It shouldn't reach here lol"); break; } } return(currentTileCounter); }
/// <summary> /// Checks a direction /// </summary> /// <param name="dirToCheck">The direction to check</param> /// <returns>True if the path is clear</returns> bool CheckDirection(Detection.DIRECTIONS dirToCheck) { // Get the current tile position Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); switch (dirToCheck) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; case Detection.DIRECTIONS.NONE: break; default: break; } if (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { return(true); } return(false); }
// TODO: Add a On collide enter with the rock // if the rat collides // then clear the movable tiles and set its position to the last position // this is cause hte cat will keep trying to move into the rock private void OnCollisionEnter2D(Collision2D collision) { // if it collided with an obstacle if (collision.gameObject.tag == "Obstacles") { // onl yif it is running if (mouseDetection.characterState == Detection.STATE.RUNNING) { // set the position to the last tile // - 1 cause the current index is gonna be pointing to the next tile Vector2Int LastTilePosition = ListOfMovableTiles[currentIndex - 1]; m_rigidBody.MovePosition(MapManager.Instance.GetTileToWorldPos(LastTilePosition)); // clear the list of movable tiles ListOfMovableTiles.Clear(); currentIndex = 0; StopMovement(); mouseDetection.StopMovement(); movingDir = Detection.DIRECTIONS.NONE; UpdateAnimation(false); } } }
/// <summary> /// Checks which direction to move before it starst to move to the next tile /// /// </summary> bool GetMovingDirection() { switch (mouseDetection.targetDir) { case Detection.DIRECTIONS.UP: // If the target is coming from Above // he'll want to move Down if (CheckDirection(Detection.DIRECTIONS.DOWN)) { movingDir = Detection.DIRECTIONS.DOWN; } else { movingDir = GetAltDirection(Detection.DIRECTIONS.DOWN, mouseDetection.targetDir); } break; case Detection.DIRECTIONS.DOWN: // If target is coming from below // he'll want to move up if (CheckDirection(Detection.DIRECTIONS.UP)) { movingDir = Detection.DIRECTIONS.UP; } else { movingDir = GetAltDirection(Detection.DIRECTIONS.UP, mouseDetection.targetDir); } break; case Detection.DIRECTIONS.LEFT: // If target coming from the left, move right if (CheckDirection(Detection.DIRECTIONS.RIGHT)) { movingDir = Detection.DIRECTIONS.RIGHT; } else { movingDir = GetAltDirection(Detection.DIRECTIONS.RIGHT, mouseDetection.targetDir); } break; case Detection.DIRECTIONS.RIGHT: // if target coming from right, move left if (CheckDirection(Detection.DIRECTIONS.LEFT)) { movingDir = Detection.DIRECTIONS.LEFT; } else { movingDir = GetAltDirection(Detection.DIRECTIONS.LEFT, mouseDetection.targetDir); } break; case Detection.DIRECTIONS.NONE: Debug.Log("O no it shudnt be here o no no no"); break; default: break; } if (movingDir == Detection.DIRECTIONS.NONE) { return(false); } return(true); }
/// <summary> /// for resetting movements /// </summary> public void StopMovement() { targetReached = false; movingDir = Detection.DIRECTIONS.NONE; targetTilePosition = Vector2Int.zero; }
/// <summary> /// Gets all the tiles in that direction /// </summary> /// <param name="dir">The direction to check with</param> /// <returns>True if they</returns> public bool GetDirectionalTiles(Detection.DIRECTIONS dir) { // Get the curernt tile position Vector2Int currentTile = MapManager.Instance.GetWorldToTilePos(transform.position); // Moove once in the direction switch (dir) { case Detection.DIRECTIONS.UP: currentTile.y++; break; case Detection.DIRECTIONS.DOWN: currentTile.y--; break; case Detection.DIRECTIONS.LEFT: currentTile.x--; break; case Detection.DIRECTIONS.RIGHT: currentTile.x++; break; case Detection.DIRECTIONS.NONE: break; default: break; } // Keep on checking until it finds the wall while (MapManager.Instance.IsThereTileOnMap(currentTile) == false) { //Vector2 tileWorldPosition = MapManager.Instance.GetTileToWorldPos(currentTile); // Add the tile to the list ListOfMovableTiles.Add(currentTile); // Increment again by one // to check the next tile switch (dir) { case Detection.DIRECTIONS.UP: currentTile.y++; break; case Detection.DIRECTIONS.DOWN: currentTile.y--; break; case Detection.DIRECTIONS.LEFT: currentTile.x--; break; case Detection.DIRECTIONS.RIGHT: currentTile.x++; break; case Detection.DIRECTIONS.NONE: break; default: break; } } return(true); }
Detection.DIRECTIONS GetAltDirection(Detection.DIRECTIONS movingDir, Detection.DIRECTIONS targetDir) { // Before checking for alternate path // check if it still needs to move // if there isn't then you don't have to find an alternate path if (mouseDetection.CheckForEnemies() == false && mouseDetection.playerInSight == false) { StopMovement(); mouseDetection.StopMovement(); UpdateAnimation(false); return(Detection.DIRECTIONS.NONE); } Detection.DIRECTIONS tempDirection = Detection.DIRECTIONS.NONE; Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); // loop through all the 4 directions for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index) { // dont check the other 2 directions if (index == (int)movingDir) { continue; } else if (index == (int)targetDir) { continue; } Detection.DIRECTIONS currDirection = (Detection.DIRECTIONS)index; Vector2Int targetTilePos = Vector2Int.zero; if (mouseDetection.targetObject != null) { targetTilePos = MapManager.Instance.GetWorldToTilePos(mouseDetection.targetObject.transform.position); } else { return(Detection.DIRECTIONS.NONE); } // if its only chcking along the X Axis if (currDirection == Detection.DIRECTIONS.LEFT || currDirection == Detection.DIRECTIONS.RIGHT) { // if its more than 2 blocks to the left or more than 2 blocks to the right // then it can go left or right depending on which has more space // if (Mathf.Abs(targetTilePos.x) < Mathf.Abs(currentTilePos.x - 1) || Mathf.Abs(targetTilePos.x) > Mathf.Abs(currentTilePos.x + 1)) if (targetTilePos.x > (currentTilePos.x - 1) && targetTilePos.x < (currentTilePos.x + 1)) { // if the number of empty tiles on the left is less than the right if (CheckDirectionSize(Detection.DIRECTIONS.LEFT) < CheckDirectionSize(Detection.DIRECTIONS.RIGHT)) { // the moving direction should be right // tempDirection = Detection.DIRECTIONS.RIGHT; } else { // else the moving direction should be left //Debug.Log("Moving Left"); tempDirection = Detection.DIRECTIONS.LEFT; } } // else it will need to go in the direction that is further away from the target object else { // if the target is on the left side if (targetTilePos.x <= transform.position.x) { // Debug.Log("Moving Right 2"); tempDirection = Detection.DIRECTIONS.RIGHT; if (CheckDirectionSize(Detection.DIRECTIONS.RIGHT) <= 1) { tempDirection = Detection.DIRECTIONS.LEFT; } //return Detection.DIRECTIONS.RIGHT; } else // the target is on the right { // Debug.Log("Moving Left 2"); tempDirection = Detection.DIRECTIONS.LEFT; if (CheckDirectionSize(Detection.DIRECTIONS.LEFT) <= 1) { tempDirection = Detection.DIRECTIONS.RIGHT; } //return Detection.DIRECTIONS.LEFT; } } } // if its only checking along the Y Axis else if (currDirection == Detection.DIRECTIONS.UP || currDirection == Detection.DIRECTIONS.DOWN) { // if its more than 2 blocks to the left or more than 2 blocks to the right // then it can go left or right depending on which has more space // if (targetTilePos.y < (currentTilePos.y - 1) || targetTilePos.y > (currentTilePos.y + 1)) if (targetTilePos.y > (currentTilePos.y - 1) && targetTilePos.y < (currentTilePos.y + 1)) { // if the number of empty tiles on above is less than below if (CheckDirectionSize(Detection.DIRECTIONS.UP) < CheckDirectionSize(Detection.DIRECTIONS.DOWN)) { // the moving direction should be right tempDirection = Detection.DIRECTIONS.DOWN; } else { // else the moving direction should be left tempDirection = Detection.DIRECTIONS.UP; } } else { // the target is below //TODO::DUCTTAPE TO THE MAX, MAKE SURE THERES SPACE BEFORE RUNNING if (targetTilePos.y <= transform.position.y) { tempDirection = Detection.DIRECTIONS.UP; //if up no space if (CheckDirectionSize(Detection.DIRECTIONS.UP) <= 1) { tempDirection = Detection.DIRECTIONS.DOWN; } } // the target is ontop else { tempDirection = Detection.DIRECTIONS.DOWN; if (CheckDirectionSize(Detection.DIRECTIONS.DOWN) <= 1) { tempDirection = Detection.DIRECTIONS.UP; } } } } // might not need this // might need this // i dont know break; } switch (tempDirection) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; case Detection.DIRECTIONS.NONE: break; default: break; } if (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { // Check if the kitten is along that direction if (mouseDetection.CheckForKitten(tempDirection) == false) { return(tempDirection); } } return(Detection.DIRECTIONS.NONE); }
private void FixedUpdate() { //TODO: Stop it from moving small diagonals // i could just make it so if it moves along x or y axis // i set the other axis to 0 // i.e if its moving left or right, targetvector Y will be 0 to stop it from moving up or down // or if its moving up and down, target vector X will be 0 to stop it from moving side ways while going up or down if (juststopplease) { return; } if (isMoving) { Vector3 direction = (targetVector - (Vector2)transform.position).normalized; float targetXPos = targetVector.x; float targetYPos = targetVector.y; if (isStuck) { // if its moving to the left if (tempMovingDir == Detection.DIRECTIONS.LEFT) { // Set the y axis to 0 // it doesn't need to move up and down direction.y = 0; // when it reaches less than or equal to the target xposition // left is negative for x axis if (transform.position.x <= targetXPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; isMoving = false; tempMovingDir = Detection.DIRECTIONS.NONE; targetReached = true; if (targetReached == false) { targetReached = true; return; } } } else if (tempMovingDir == Detection.DIRECTIONS.RIGHT) { // Set the y axis to 0 // it doesn't need to move up and down direction.y = 0; // when it reaches more than or equal to the target x position // right is positive for x axis if (transform.position.x >= targetXPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; isMoving = false; tempMovingDir = Detection.DIRECTIONS.NONE; targetReached = true; if (targetReached == false) { targetReached = true; return; } } } else if (tempMovingDir == Detection.DIRECTIONS.UP) { // Set the x axis to 0 // it doesn't need to move left and right direction.x = 0; // when it reaches more than or equal to the target y position // moving up is positive for y axis if (transform.position.y >= targetYPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; isMoving = false; tempMovingDir = Detection.DIRECTIONS.NONE; targetReached = true; if (targetReached == false) { targetReached = true; return; } } } else if (tempMovingDir == Detection.DIRECTIONS.DOWN) { // Set the x axis to 0 // it doesn't need to move left and right direction.x = 0; // when it reaches less than or equal to the target y position // moving down is negative for y axis if (transform.position.y <= targetYPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; isMoving = false; tempMovingDir = Detection.DIRECTIONS.NONE; if (targetReached == false) { targetReached = true; return; } } } } // if it isnt stuck // continue with the normal movement else { // if its moving to the left if (movingDir == Detection.DIRECTIONS.LEFT) { // Set the y axis to 0 // it doesn't need to move up and down direction.y = 0; // when it reaches less than or equal to the target xposition // left is negative for x axis if (transform.position.x <= targetXPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; ResetMovement(); targetReached = true; if (targetReached == false) { targetReached = true; return; } } } else if (movingDir == Detection.DIRECTIONS.RIGHT) { // Set the y axis to 0 // it doesn't need to move up and down direction.y = 0; // when it reaches more than or equal to the target x position // right is positive for x axis if (transform.position.x >= targetXPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; ResetMovement(); targetReached = true; if (targetReached == false) { targetReached = true; return; } } } else if (movingDir == Detection.DIRECTIONS.UP) { // Set the x axis to 0 // it doesn't need to move left and right direction.x = 0; // when it reaches more than or equal to the target y position // moving up is positive for y axis if (transform.position.y >= targetYPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; ResetMovement(); targetReached = true; if (targetReached == false) { targetReached = true; return; } } } else if (movingDir == Detection.DIRECTIONS.DOWN) { // Set the x axis to 0 // it doesn't need to move left and right direction.x = 0; // when it reaches less than or equal to the target y position // moving down is negative for y axis if (transform.position.y <= targetYPos) { transform.position = targetVector; kittenBehaviour.isRunning = false; ResetMovement(); if (targetReached == false) { targetReached = true; return; } } } } // Return when they reach the target to prevent any extra movements //update it's position rigidBody.MovePosition(transform.position + direction * moveSpeed * Time.fixedDeltaTime); //Debug.Log("Target reached " + targetReached); // If the target already reached // and it looped back through then that means that it hasnt move // and that should mean that its stuck if (targetReached) { isStuck = true; } } }
/// <summary> /// Resets the movement variables of ktiten movement /// </summary> public void ResetMovement() { isMoving = false; movingDir = Detection.DIRECTIONS.NONE; }
/// <summary> /// This is for the bouncing back /// </summary> /// <param name="currentDirection"></param> void GetNextTrapTilePosition(Detection.DIRECTIONS currentDirection) { // Get the first tile in that direction // i repeated the lines just in case lol Vector2Int targetTilePosition = MapManager.Instance.GetWorldToTilePos(transform.position); Vector2Int currentTilePosition = MapManager.Instance.GetWorldToTilePos(transform.position); switch (currentDirection) { case Detection.DIRECTIONS.UP: targetTilePosition.y++; break; case Detection.DIRECTIONS.DOWN: targetTilePosition.y--; break; case Detection.DIRECTIONS.LEFT: targetTilePosition.x--; break; case Detection.DIRECTIONS.RIGHT: targetTilePosition.x++; break; case Detection.DIRECTIONS.NONE: break; default: break; } // set it to that tile // Add the tile that it needs to move to ListOfMovableTiles.Add(targetTilePosition); // add the original tile it was in ListOfMovableTiles.Add(currentTilePosition); Detection.DIRECTIONS kittenDirection = mouseDetection.GetTargetDirection(mouseDetection.kittenObject.transform.position); Detection.DIRECTIONS playerDirection = mouseDetection.GetTargetDirection(mouseDetection.playerObject.transform.position); // change to the next direction // loop through all the 4 directions for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index) { // dont check the other 2 directions if (index == (int)playerDirection) { continue; } else if (index == (int)kittenDirection) { continue; } else if (index == (int)movingDir) { continue; } // set the direction movingDir = (Detection.DIRECTIONS)index; break; } }
// Update is called once per frame void FixedUpdate() { if (m_Stop) { return; } //Debug.Log("current position" + transform.position); //Debug.Log("target tile position " + targetTilePosition); if (mouseDetection.characterState == Detection.STATE.RUNNING) { // if the list is currently empty if (ListOfMovableTiles.Count <= 0) { if (mouseDetection.isTrapped) { // it found an alternate path to run to if (GetTrappedMovingDirection()) { GetDirectionalTiles(movingDir); // no trapped movement isTrappedMovement = false; } // if is trapped movement // means we bounce if (isTrappedMovement) { GetNextTrapTilePosition(movingDir); } } else { // Get the direction to move towards if (GetMovingDirection()) { // If it managed to find a direction // Fill up the list of tiles in that direction GetDirectionalTiles(movingDir); } } } Vector3 nextPos = transform.position + directionVector * moveSpeed * Time.fixedDeltaTime; if (targetTilePosition != Vector2.zero || (targetTilePosition == Vector2.zero && m_NextTileIsZeroPos)) { // If it is moving along the X Axis if (movingDir == Detection.DIRECTIONS.LEFT) { float targetXPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).x; //Debug.Log("kitten target tile position" + targetTilePosition); //Debug.Log("kitten position" + transform.position); if (nextPos.x <= targetXPos) { // it reached the tile targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.RIGHT) { float targetXPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).x; if (nextPos.x >= targetXPos) { // it reached the tile targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.UP) { float targetYPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).y; if (nextPos.y >= targetYPos) { targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.DOWN) { float targetYPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).y; if (nextPos.y <= targetYPos) { targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.NONE) { targetTilePosition = Vector2Int.zero; } // it reached the tile if (targetReached == true) { //transform.position = MapManager.Instance.GetTileToWorldPos(targetTilePosition); m_rigidBody.MovePosition(MapManager.Instance.GetTileToWorldPos(targetTilePosition)); StopMovement(); return; } } // if it has no target vector position to go to now if (targetTilePosition == Vector2.zero && !m_NextTileIsZeroPos) { // if it reached the end of the movable tiles // theres no more to move if (currentIndex >= ListOfMovableTiles.Count || mouseDetection.IsEnemyNearby(Detection.CHARACTERS.PLAYER)) { //transform.position = MapManager.Instance.GetTileToWorldPos(ListOfMovableTiles[currentIndex - 1]); ListOfMovableTiles.Clear(); currentIndex = 0; StopMovement(); mouseDetection.StopMovement(); movingDir = Detection.DIRECTIONS.NONE; UpdateAnimation(false); return; } // Set the target tile position to the first index of the list targetTilePosition = ListOfMovableTiles[currentIndex]; //TODO:: NEED CHECK IF ITS REALLY JUST 0,0 m_NextTileIsZeroPos = targetTilePosition == Vector2.zero; Vector2Int Vec2Direction = (targetTilePosition - MapManager.Instance.GetWorldToTilePos(transform.position)); directionVector = new Vector3(Vec2Direction.x, Vec2Direction.y, 0); UpdateAnimation(true); // increment the index currentIndex++; } if (targetTilePosition != Vector2.zero) { PlayWalkingSound(); } // move it move it to the limit limit m_rigidBody.MovePosition(transform.position + directionVector * moveSpeed * Time.fixedDeltaTime); } }
Detection.DIRECTIONS GetAltDirection(Detection.DIRECTIONS movingDir, Detection.DIRECTIONS targetDir) { Detection.DIRECTIONS tempDirection = Detection.DIRECTIONS.NONE; Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); // loop through the directions for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index) { // dont check the other 2 directions if (index == (int)movingDir) { continue; } else if (index == (int)targetDir) { continue; } Detection.DIRECTIONS currDirection = (Detection.DIRECTIONS)index; Vector2Int targetTilePos = Vector2Int.zero; if (kittenDetection.targetObject != null) { targetTilePos = MapManager.Instance.GetWorldToTilePos(kittenDetection.targetObject.transform.position); } else { return(Detection.DIRECTIONS.NONE); } // if its only chcking along the X Axis if (currDirection == Detection.DIRECTIONS.LEFT || currDirection == Detection.DIRECTIONS.RIGHT) { // if its more than 2 blocks to the left or more than 2 blocks to the right // then it can go left or right depending on which has more space // if (Mathf.Abs(targetTilePos.x) < Mathf.Abs(currentTilePos.x - 1) || Mathf.Abs(targetTilePos.x) > Mathf.Abs(currentTilePos.x + 1)) if (targetTilePos.x > (currentTilePos.x - 1) && targetTilePos.x < (currentTilePos.x + 1)) { // if the number of empty tiles on the left is less than the right if (CheckDirectionSize(Detection.DIRECTIONS.LEFT) < CheckDirectionSize(Detection.DIRECTIONS.RIGHT)) { // the moving direction should be right Debug.Log("Moving Right"); tempDirection = Detection.DIRECTIONS.RIGHT; } else { // else the moving direction should be left Debug.Log("Moving Left"); tempDirection = Detection.DIRECTIONS.LEFT; } } // else it will need to go in the direction that is further away from the target object else { // if the target is on the left side if (targetTilePos.x <= transform.position.x) { Debug.Log("Moving Right 2"); tempDirection = Detection.DIRECTIONS.RIGHT; //return Detection.DIRECTIONS.RIGHT; } else // the target is on the right { Debug.Log("Moving Left 2"); tempDirection = Detection.DIRECTIONS.LEFT; //return Detection.DIRECTIONS.LEFT; } } } // if its only checking along the Y Axis else if (currDirection == Detection.DIRECTIONS.UP || currDirection == Detection.DIRECTIONS.DOWN) { // if its more than 2 blocks to the left or more than 2 blocks to the right // then it can go left or right depending on which has more space if (targetTilePos.y < (currentTilePos.y - 1) || targetTilePos.y > (currentTilePos.y + 1)) { // if the number of empty tiles on above is less than below if (CheckDirectionSize(Detection.DIRECTIONS.UP) < CheckDirectionSize(Detection.DIRECTIONS.DOWN)) { // the moving direction should be right tempDirection = Detection.DIRECTIONS.DOWN; } else { // else the moving direction should be left tempDirection = Detection.DIRECTIONS.UP; } } else { // the target is below if (targetTilePos.y <= transform.position.y) { tempDirection = Detection.DIRECTIONS.UP; } // the target is ontop else { tempDirection = Detection.DIRECTIONS.DOWN; } } } // might not need this // might need this // i dont know break; } switch (tempDirection) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; case Detection.DIRECTIONS.NONE: break; default: break; } if (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { return(tempDirection); } return(Detection.DIRECTIONS.NONE); }
public void SetKittenDirection(Detection.DIRECTIONS targetDir) { // if it is stuck // it has to find another way to move to the rat // (i.e another direction) if (isStuck == true) { if (kittenBehaviour.isTired) { ResetMovement(); return; } Vector2 Dir = kittenBehaviour.GetTargetDirVector(); // get the original moving direction movingDir = kittenBehaviour.GetTargetDirection(); // if it is moving up or down and it got stuck // it'll check left and right if (movingDir == Detection.DIRECTIONS.UP || movingDir == Detection.DIRECTIONS.DOWN) { // if its positive then move right if (Dir.x >= 0.0f) { tempMovingDir = Detection.DIRECTIONS.RIGHT; } // if its negative then move left else if (Dir.x <= 0.0f) { tempMovingDir = Detection.DIRECTIONS.LEFT; } } // if its moving left and right // then it'll check up and down else if (movingDir == Detection.DIRECTIONS.LEFT || movingDir == Detection.DIRECTIONS.RIGHT) { if (Dir.y >= 0.0f) { tempMovingDir = Detection.DIRECTIONS.UP; } else if (Dir.y <= 0.0f) { tempMovingDir = Detection.DIRECTIONS.DOWN; } } if (targetVector != FindEndTile(tempMovingDir)) { targetVector = FindEndTile(tempMovingDir); isMoving = true; targetReached = false; } // Check the original moving direction if the path is clear Vector2Int currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); switch (movingDir) { case Detection.DIRECTIONS.UP: currentTilePos.y++; break; case Detection.DIRECTIONS.DOWN: currentTilePos.y--; break; case Detection.DIRECTIONS.LEFT: currentTilePos.x--; break; case Detection.DIRECTIONS.RIGHT: currentTilePos.x++; break; case Detection.DIRECTIONS.NONE: break; default: Debug.LogError("pee pee poo poo"); break; } movingDir = Detection.DIRECTIONS.NONE; // if the path infront is clear if (MapManager.Instance.IsThereTileOnMap(currentTilePos) == false) { currentTilePos = MapManager.Instance.GetWorldToTilePos(transform.position); Vector2 tilePosition = MapManager.Instance.GetTileToWorldPos(currentTilePos); transform.position = tilePosition; tempMovingDir = Detection.DIRECTIONS.NONE; isStuck = false; } } else { //set the moving direction to where it should go // it should always be moving in the direction of the cat movingDir = targetDir; // if the target vector changes if (targetVector != FindEndTile(movingDir)) { targetVector = FindEndTile(movingDir); targetReached = false; } isMoving = true; } // if its already moving //if (isMoving == true) //{ // return; //} // How should the kitten move after the rat // it'll chase in the direction that the cat is in // it shouldn't need to check which direction is clear like the rat // it will keep moving until it hits a wall // when it hits the wall, and it didn't hit the rat // it will try to find how to get to the rat // it will have to move another axis }
// Update is called once per frame void FixedUpdate() { if (m_Stop) { return; } // theres some issues with restarting // where this ends up being null if (mouseMovement == null) { mouseMovement = kittenDetection.ratObject.GetComponent <MouseMovement>(); } // if the cat is currently chasing if (kittenDetection.characterState == Detection.STATE.CHASING || kittenDetection.characterState == Detection.STATE.TIRED) { //Vector2 distanceVector = (Vector2)transform.position - startingPos; //float distanceFromStarting = distanceVector.magnitude; // Only check if it can get tired while chasing // this is so that it wont repeatedly check to see if its tired if (kittenDetection.characterState == Detection.STATE.CHASING) { // Check rat movement // If the target's tile position isn't a zero vector // it means it has a path if (mouseMovement.targetTilePosition != Vector2.zero) { Vector2Int tileToAdd = mouseMovement.targetTilePosition; //// if it doesnt contain the target tile position if (ListOfRatTiles.Contains(tileToAdd) == false) { // add it to the list ListOfRatTiles.Add(tileToAdd); Debug.Log("Added rat tile in movement" + tileToAdd); } } if (currentIndex > maxTravelDistance) { // if the mouse is tired // clear the list of tiles because it doesnt need to chase anymore ListOfRatTiles.Clear(); currentRatIndex = 0; // To clear target object and reset character state kittenDetection.StopMovement(); kittenDetection.characterState = Detection.STATE.TIRED; StopMovement(); UpdateAnimation(false); return; } } Vector3 nextPos = transform.position + directionVector * moveSpeed * Time.fixedDeltaTime; if (targetTilePosition != Vector2.zero) { // If it is moving along the X Axis if (movingDir == Detection.DIRECTIONS.LEFT) { float targetXPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).x; //Debug.Log("kitten target tile position" + targetTilePosition); //Debug.Log("kitten position" + transform.position); if (nextPos.x <= targetXPos) { // it reached the tile targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.RIGHT) { float targetXPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).x; if (nextPos.x >= targetXPos) { // it reached the tile targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.UP) { float targetYPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).y; if (transform.position.y >= targetYPos) { targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.DOWN) { float targetYPos = MapManager.Instance.GetTileToWorldPos(targetTilePosition).y; if (transform.position.y <= targetYPos) { targetReached = true; } } else if (movingDir == Detection.DIRECTIONS.NONE) { // it shouldn't reach here lol targetTilePosition = Vector2Int.zero; } if (targetReached == true) { // it reached the tile //transform.position = MapManager.Instance.GetTileToWorldPos(targetTilePosition); m_rigidBody.MovePosition(MapManager.Instance.GetTileToWorldPos(targetTilePosition)); StopMovement(); return; } } // if there is no target tile yet // find the target tile if (targetTilePosition == Vector2.zero) { // If the kitten is tired // move based on the list of tiles instead of a target object if (kittenDetection.characterState == Detection.STATE.TIRED) { // im gonna need to change this later probably if (currentIndex <= 0) { transform.position = MapManager.Instance.GetTileToWorldPos(startingPos); UpdateAnimation(false); kittenDetection.characterState = Detection.STATE.IDLE; //Clear the list ListOfTilesTravelled.Clear(); // add the starting position ListOfTilesTravelled.Add(startingPos); // reset the index //currentIndex = 1; StopMovement(); return; } // set the target to the last position on the list targetTilePosition = ListOfTilesTravelled[currentIndex - 1]; Vector2Int Vec2Direction = (targetTilePosition - MapManager.Instance.GetWorldToTilePos(transform.position)); directionVector = new Vector3(Vec2Direction.x, Vec2Direction.y, 0); UpdateAnimation(true); currentIndex--; // Get the direction to the next tile movingDir = kittenDetection.GetTargetDirection(targetTilePosition); // Set the viewing direction to be the same as the kitten's moving dir kittenDetection.SetViewDirection(movingDir); } else { // if there are tiles to follow if (ListOfRatTiles.Count > 0) { if (currentRatIndex < ListOfRatTiles.Count) { // set the target tiles targetTilePosition = ListOfRatTiles[currentRatIndex]; Vector2Int Vec2Direction = (targetTilePosition - MapManager.Instance.GetWorldToTilePos(transform.position)); directionVector = new Vector3(Vec2Direction.x, Vec2Direction.y, 0); directionVector.Normalize(); UpdateAnimation(true); // increment the rat index to keep track currentRatIndex++; // Add it to the list of travelled tiles ListOfTilesTravelled.Add(targetTilePosition); // increment the curent index also currentIndex++; // Get the moving direction movingDir = kittenDetection.GetTargetDirection(targetTilePosition); kittenDetection.SetViewDirection(movingDir); } } else { // if it is not in its starting pos if (MapManager.Instance.GetWorldToTilePos(transform.position) != startingPos) { kittenDetection.characterState = Detection.STATE.TIRED; } } ////Get the moving Direction //if (GetMovingDirection()) //{ // targetTilePosition = GetNextTile(); // directionVector = (targetTilePosition - (Vector2)transform.position).normalized; // UpdateAnimation(true); // // For checking if the kitten is tired // // Store the tiles it traversed in a list // ListOfTilesTravelled.Add(targetTilePosition); // // Increment the index everytime it changes // currentIndex++; //} } } if (targetTilePosition != Vector2.zero) { PlayWalkingSound(); } m_rigidBody.MovePosition(transform.position + directionVector * moveSpeed * Time.fixedDeltaTime); } }
/// <summary> /// Moves the prey in the target direction /// They should move in the opposite direction of the target object /// If that direction isn't available then the next best direction /// </summary> /// <param name="dir">target Direction to move towards to</param> public void MovePrey(Detection.DIRECTIONS dir, Detection.DIRECTIONS targetDir) { // if it is already moving then the theres no point checking if (isMoving == true) { return; } movingDir = dir; UpdateAnimation(); bool directionClear = false; // Check the first direction to run towards CheckDirection(dir); Vector2 targetDirVector = preyBehaviour.GetTargetDirVector(); // Check alternative paths // if the target is coming from the left or right if (targetDir == Detection.DIRECTIONS.LEFT || targetDir == Detection.DIRECTIONS.RIGHT) { // check whether the object is coming at a diagonal // example, to prevent it from running downwards if the target is below as well // if it is negative y vector // if the target is below the prey if (targetDirVector.y <= 0.0f) { CheckDirection(Detection.DIRECTIONS.UP); } // if the target is above the prey else if (targetDirVector.y >= 0.0f) { CheckDirection(Detection.DIRECTIONS.DOWN); } } else if (targetDir == Detection.DIRECTIONS.UP || targetDir == Detection.DIRECTIONS.DOWN) { // check if the object is coming at a diagonal // example, if its coming from the top left, the rat shouldn't run left // if it is coming from the left if (targetDirVector.x <= 0.0f) { CheckDirection(Detection.DIRECTIONS.RIGHT); } // if it is coming from the right else if (targetDirVector.x >= 0.0f) { CheckDirection(Detection.DIRECTIONS.LEFT); } } //for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index) //{ // if (index == (int)targetDir) // continue; // // check through the 4 directions // CheckDirection((Detection.DIRECTIONS)index); //} // After finding the moving direction // we need to set the view direction // we want the rat to look in the opposite direction it ran from switch (movingDir) { case Detection.DIRECTIONS.UP: preyBehaviour.viewDir = Detection.DIRECTIONS.DOWN; break; case Detection.DIRECTIONS.DOWN: preyBehaviour.viewDir = Detection.DIRECTIONS.UP; break; case Detection.DIRECTIONS.LEFT: preyBehaviour.viewDir = Detection.DIRECTIONS.RIGHT; break; case Detection.DIRECTIONS.RIGHT: preyBehaviour.viewDir = Detection.DIRECTIONS.LEFT; break; case Detection.DIRECTIONS.NONE: Debug.Log("haha pee pee poo poo"); break; default: break; } //// Check the diection it wants to move in //// if the opposite direction is not clear //// check all the others //if (CheckDirection(dir) == false) //{ // for (int index = 0; index < (int)Detection.DIRECTIONS.NONE; ++index) // { // // If it reaches the same direction as the direction given earlier // // or if its in the same direction as the cat // // skip it // if (index == (int)dir) // continue; // else if (index == (int)targetDir) // continue; // // If that direction is blocked/not very long // // Check it through the function again // // Convert int to direction // if (CheckDirection((Detection.DIRECTIONS)index)) // { // //Change the moving direction to the new one // movingDir = (Detection.DIRECTIONS)index; // directionClear = true; // break; // } // // If it doesnt pass any direction // // direction clear will be set to false // directionClear = false; // } //} //else // // if the first direction it checked is already clear // directionClear = true; //// if the direction isnt clear anywhere //// return and not move at all //// the prey is probably stuck/bugged //// should never reach here //if (directionClear == false) // return; // Move in the direction // Find the end tile position to move towards targetVector = FindEndTile(movingDir); // Debug the target vector to see if its working Debug.Log("tile position" + targetVector); Debug.Log("world position" + transform.position); Debug.Log("Is Moving"); Debug.Log(movingDir); //Toggle the isMoving flag to true // this is to start movement in update isMoving = true; UpdateAnimation(); }