void Move() { float newXpos = 0.0f; float newYpos = 0.0f; if (Input.GetAxis("Player1Horizontal") != 0.0f) { newXpos = Input.GetAxis("Player1Horizontal") * movementSpeed * Time.deltaTime; if (Input.GetAxis("Player1Horizontal") < 0.0f) { faceing = Faceing.Left; } else if (Input.GetAxis("Player1Horizontal") > 0.0f) { faceing = Faceing.Right; } } if (Input.GetAxis("Player1Vertical") != 0.0f) { newYpos = Input.GetAxis("Player1Vertical") * movementSpeed * Time.deltaTime; if (Input.GetAxis("Player1Vertical") < 0.0f) { faceing = Faceing.Down; } else if (Input.GetAxis("Player1Vertical") > 0.0f) { faceing = Faceing.Up; } } rb.velocity = new Vector2(newXpos, newYpos); }
public void Solve() { // For part 2 we need a compleat list of all the point we pass through. // not just the directions at each intersection. // To do this we make a list of positions. List <Position> placesWeHaveBeen = new List <Position>(); // Since the answer might be where we started, we will add the start. placesWeHaveBeen.Add(new Position(0, 0)); // and to let us know when to stop looking, we set a bool bool duplicatePlaceFound = false; // We need to move differantly than in part 1 foreach (Instruction i in Directions) { /* Part 1 code: * //this.face = Face(this.face, i.Rotation); * //Move(i.Distance, this.face); */ // First we start by updating our faceing: this.face = Face(this.face, i.Rotation); // Then instead of moving all the blocks at once we move one by one. for (int c = 0; c < i.Distance; c++) { Move(1, this.face); // Have we been here before? // We can check this by compareing our coordinates with all the other coordinates we have visited. foreach (Position p in placesWeHaveBeen) { if (this.x == p.X && this.y == p.Y) { // we have been here before! duplicatePlaceFound = true; break; } } // Add this place to the list of places we have been placesWeHaveBeen.Add(new Position(this.x, this.y)); if (duplicatePlaceFound) { break; } } if (duplicatePlaceFound) { break; } } Console.WriteLine("The end is {0} blocks away from here.", Math.Abs(this.x) + Math.Abs(this.y)); }
private Faceing Face(Faceing face, char turn) { if (turn == 'R') { switch (face) { case Faceing.NORTH: return(Faceing.EAST); case Faceing.SOUTH: return(Faceing.WEST); case Faceing.EAST: return(Faceing.SOUTH); case Faceing.WEST: return(Faceing.NORTH); default: return(face); } } else { switch (face) { case Faceing.NORTH: return(Faceing.WEST); case Faceing.SOUTH: return(Faceing.EAST); case Faceing.EAST: return(Faceing.NORTH); case Faceing.WEST: return(Faceing.SOUTH); default: return(face); } } }
private void Move(int blocks, Faceing face) { switch (face) { case Faceing.NORTH: this.x = this.x + blocks; break; case Faceing.SOUTH: this.x = this.x - blocks; break; case Faceing.EAST: this.y = this.y + blocks; break; case Faceing.WEST: this.y = this.y - blocks; break; default: break; } }