Advertisement
jwow22

Snake Slicing

Jan 4th, 2022
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. public class SnakeSlicing : MonoBehaviour
  2. {
  3.     [SerializeField] public bool enableSlicing;
  4.     private Snake _snake;
  5.     private Spawner _spawner;
  6.  
  7.     public void Slice(Body otherBody)
  8.     {
  9.         Body headBody = _snake.bodyParts[0];
  10.         if (otherBody.linked && otherBody.snake == headBody.snake)
  11.         {
  12.             Debug.Log("Self collision");
  13.             _snake.DeathBehaviour();
  14.             _spawner.DestroyObject(headBody.gridObject);
  15.         }
  16.         // Collision with unlinked body
  17.         else if (!otherBody.linked)
  18.         {
  19.             _spawner.DestroyObject(otherBody.gridObject);
  20.             _snake.AddBody();
  21.         }
  22.         // Collision with another snake
  23.         // Collision with body:
  24.         else if (otherBody.linked && otherBody != otherBody.snake.bodyParts[0])
  25.         {
  26.             if (_snake.size > otherBody.snake.size)
  27.             {
  28.                 int index = otherBody.snake.bodyParts.IndexOf(otherBody);
  29.                 otherBody.snake.RemoveBodiesUntil(index);
  30.                 _spawner.DestroyObject(otherBody.gridObject);
  31.                 _snake.AddBody();  
  32.             }
  33.             else
  34.             {
  35.                 _spawner.DestroyObject(headBody.gridObject);
  36.                 _snake.DeathBehaviour();
  37.             }
  38.         }
  39.         // Collision with other snake
  40.         // Collision with head: kill if bigger, die if smaller
  41.         else if (otherBody.linked && otherBody == otherBody.snake.bodyParts[0])
  42.         {
  43.             // If bigger snake: kill other snake
  44.             if (_snake.size > otherBody.snake.size)
  45.             {
  46.                 if (otherBody.snake.bodyParts.Count == 1)
  47.                 {
  48.                     otherBody.snake.DeathBehaviour();
  49.                     _spawner.DestroyObject(otherBody.gridObject);
  50.                     _snake.AddBody();
  51.                     return;
  52.                 }
  53.  
  54.                 _spawner.DestroyObject(otherBody.snake.bodyParts[0].gridObject);
  55.                 otherBody.snake.DeathBehaviour();
  56.             }
  57.             // If smaller snake: dies
  58.             else
  59.             {
  60.                 _spawner.DestroyObject(headBody.gridObject);
  61.                 _snake.DeathBehaviour();
  62.             }
  63.         }
  64.     }
  65.  
  66.     private void Awake()
  67.     {
  68.         _snake = GetComponent<Snake>();
  69.         _spawner = FindObjectOfType<Spawner>();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement