private MoveCopy CopyMove(MoveCopy moveCopy, BodyPart bodyPart, bool isAddingNewBodyPart = false) { MoveCopy returnMoveCopy = null; if (isAddingNewBodyPart == false) { returnMoveCopy = new MoveCopy(); } if (isAddingNewBodyPart == false) { returnMoveCopy.Position = bodyPart.transform.position; } bodyPart.transform.position = moveCopy.Position; if (isAddingNewBodyPart == false) { returnMoveCopy.MoveDirection = bodyPart.MoveDirection; } bodyPart.MoveDirection = moveCopy.MoveDirection; switch (bodyPart.MoveDirection) { case MoveDirection.Forward: bodyPart.transform.eulerAngles = new Vector3(0, 0, 0); break; case MoveDirection.Left: bodyPart.transform.eulerAngles = new Vector3(0, -90, 0); break; case MoveDirection.Right: bodyPart.transform.eulerAngles = new Vector3(0, 90, 0); break; default: // Back bodyPart.transform.eulerAngles = new Vector3(0, 180, 0); break; } if (isAddingNewBodyPart == false) { returnMoveCopy.Ondulation = bodyPart.Ondulation; } bodyPart.SetOndulation(moveCopy.Ondulation); if (isAddingNewBodyPart == false) { return(returnMoveCopy); } if (_newBodyPartEntries == null) { _newBodyPartEntries = new List <Move>(); } _newBodyPartEntries.Add(moveCopy.Move); return(moveCopy); }
public void MoveSnake(Move move) { SnakeIsOnEndCube = move.ToMapCube.IsEnd; Apple theApple = Game._.LevelController.MapMaker.Apples .FirstOrDefault(a => a.Pos.x == move.ToMapCube.Pos.x && a.Pos.y == move.ToMapCube.Pos.y); MoveCopy moveCopy = MoveBodyPart(move, SnakeHead); Game._.EnergyController.UseEnergy(); foreach (var bodyPart in BodyParts) { if (moveCopy != null) { if (bodyPart.gameObject.activeSelf == false) { bodyPart.gameObject.SetActive(true); } moveCopy = CopyMove(moveCopy, bodyPart); } } if (theApple != null) { theApple.Consume(); Game._.EnergyController.ConsumeApple(); AddBodyPart(); if (SnakeHeadIndex > 1) { if (BodyParts[BodyParts.Count - 1].gameObject.activeSelf == false) { BodyParts[BodyParts.Count - 1].gameObject.SetActive(true); } moveCopy = CopyMove(moveCopy, BodyParts[BodyParts.Count - 1], isAddingNewBodyPart: true); } } bool canWeSeeTail = !(SnakeHeadIndex == 0); if (canWeSeeTail == false) { return; } if (moveCopy != null) { if (SnakeTail.gameObject.activeSelf == false) { SnakeTail.gameObject.SetActive(true); } if (_newBodyPartEntries != null) { _newBodyPartEntries.RemoveAt(_newBodyPartEntries.Count - 1); if (_newBodyPartEntries.Count == 0) { _newBodyPartEntries = null; } return; } CopyMove(moveCopy, SnakeTail); } }
private MoveCopy MoveBodyPart(Move move, BodyPart bodyPart) { MoveCopy moveCopy = new MoveCopy(); moveCopy.Move = move; moveCopy.Position = bodyPart.transform.position; bodyPart.transform.position = move.ToMapCube.transform.position; SnakeHeadPos = move.ToMapCube.Pos; moveCopy.MoveDirection = bodyPart.MoveDirection; bodyPart.MoveDirection = GetHeadDirection(move); moveCopy.Ondulation = bodyPart.Ondulation; switch (bodyPart.MoveDirection) { case MoveDirection.Forward: bodyPart.transform.eulerAngles = new Vector3(0, 0, 0); if (move.Next.y == 1) { bodyPart.SetOndulation(Ondulation.Keep); } else if (move.Next.x == 1) { bodyPart.SetOndulation(Ondulation.Right); } else { bodyPart.SetOndulation(Ondulation.Left); } break; case MoveDirection.Left: bodyPart.transform.eulerAngles = new Vector3(0, -90, 0); if (move.Next.x == -1) { bodyPart.SetOndulation(Ondulation.Keep); } else if (move.Next.y == 1) { bodyPart.SetOndulation(Ondulation.Right); } else { bodyPart.SetOndulation(Ondulation.Left); } break; case MoveDirection.Right: bodyPart.transform.eulerAngles = new Vector3(0, 90, 0); if (move.Next.x == 1) { bodyPart.SetOndulation(Ondulation.Keep); } else if (move.Next.y == 1) { bodyPart.SetOndulation(Ondulation.Left); } else { bodyPart.SetOndulation(Ondulation.Right); } break; default: // Back bodyPart.transform.eulerAngles = new Vector3(0, 180, 0); if (move.Next.y == -1) { bodyPart.SetOndulation(Ondulation.Keep); } else if (move.Next.x == 1) { bodyPart.SetOndulation(Ondulation.Left); } else { bodyPart.SetOndulation(Ondulation.Right); } break; } return(moveCopy); // Debug.Log(bodyPart.MoveDirection.ToString() + " - to " + bodyPart.Ondulation.ToString()); }