///<summary>Adds a new node to the snake for one turn, with animation if necessary.</summary> public void grow(float timeToMove, bool shouldMoveSmoothly) { // like the move function, but instantiates a new node as the head instead of moving the tail up SnakeNode newHead = Instantiate(this.snakeNodePrefab, this.head.transform.position, this.head.transform.rotation, this.gameOrigin.transform) as SnakeNode; newHead.setDirection(this.head.getDirection()); newHead.setCoordinates(this.head.getCoordinates()); this.nodes.Add(newHead); newHead.StartCoroutine(newHead.moveToCoordinates(this.head.coordinatesOfNextNode(), timeToMove, shouldMoveSmoothly)); this.head.setNodeBefore(newHead); this.head = newHead; }
///<summary>Moves the nodes of the snake for one turn, with animation if necessary. /// Returns the game coordinates of the space just vacated (where the tail was before the move).</summary> public int[] move(float timeToMove, bool shouldMoveSmoothly) { int[] old = this.tail.getCoordinates(); SnakeNode newTail = this.tail.getNodeBefore(); SnakeNode newHead = this.tail; // the tail moves up to become the head this.ghostNode.goToCoordinates(this.tail.getCoordinates()); newHead.goToCoordinates(this.head.getCoordinates()); // slide ghostNode into the tail and newHead out of the current head to animate the snake's movement this.ghostNode.StartCoroutine(this.ghostNode.moveToCoordinates(newTail.getCoordinates(), timeToMove, shouldMoveSmoothly)); newHead.StartCoroutine(newHead.moveToCoordinates(this.head.coordinatesOfNextNode(), timeToMove, shouldMoveSmoothly)); newHead.setDirection(this.head.getDirection()); // point to the new head and tail this.head.setNodeBefore(newHead); newHead.setNodeBefore(null); this.tail = newTail; this.head = newHead; return(old); }