//AttemptMove overrides the AttemptMove function in the base class MovingObject //AttemptMove takes a generic parameter T which for Player will be of the type Wall, it also takes integers for x and y direction to move in. protected override void AttemptMove <T>(int xDir, int yDir) { //Every time player moves, subtract from food points total. food--; //Update food text display to reflect current score. foodText.text = "Food: " + food; //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move. base.AttemptMove <T>(xDir, yDir); //Hit allows us to reference the result of the Linecast done in Move. RaycastHit2D hit; //If Move returns true, meaning Player was able to move into an empty space. if (Move(xDir, yDir, out hit)) { //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from. SoundManager.instance.RandomizeSfx(moveSound1, moveSound2); // find tile where "P" is and move it to the correct one board.UpdateNode((int)transform.position.x, (int)transform.position.y, "F"); board.UpdateNode((int)transform.position.x + xDir, (int)transform.position.y + yDir, "P"); StartCoroutine(WaitBeforePathfinding()); } //Since the player has moved and lost food points, check if the game has ended. CheckIfGameOver(); //Set the playersTurn boolean of GameManager to false now that players turn is over. GameManager.instance.playersTurn = false; }
//Override the AttemptMove function of MovingObject to include functionality needed for Enemy to skip turns. //See comments in MovingObject for more on how base AttemptMove function works. protected override void AttemptMove <T> (int xDir, int yDir) { //Check if skipMove is true, if so set it to false and skip this turn. if (skipMove) { skipMove = false; return; } //Call the AttemptMove function from MovingObject. base.AttemptMove <T> (xDir, yDir); RaycastHit2D hit; //If Move returns true, meaning Player was able to move into an empty space. if (Move(xDir, yDir, out hit)) { int xx = (int)transform.position.x; int yy = (int)transform.position.y; board.UpdateNode(xx, yy, "F"); board.UpdateNode(xx + xDir, yy + yDir, "N"); } //Now that Enemy has moved, set skipMove to true to skip next move. skipMove = true; }