Advertisement
jwow22

Entity Controller

Jan 4th, 2022
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. [RequireComponent(typeof(Snake))]
  2. public class EntityController : MonoBehaviour, IEntityController
  3. {
  4.     [SerializeField] public bool enableMovement;
  5.     private Direction _currentDirection;
  6.     private Direction _desiredDirection;
  7.     private Direction _oppositeDirection;
  8.     private Transform _transform;
  9.     private Vector2 worldPosition;
  10.  
  11.     [NonSerialized] public Snake snake;
  12.     private Spawner spawner;
  13.  
  14.     [NonSerialized] public Body headBody;
  15.     [NonSerialized] public bool enableInput;
  16.    
  17.     // Set linked body positions
  18.     public void EvaluateBodyPositions()
  19.     {
  20.         if (!enableMovement) { return; }
  21.        
  22.         // Start traversing from head
  23.         var bodyNode = snake.bodyParts.Head;
  24.  
  25.         while (bodyNode.Next != null)
  26.         {
  27.             bodyNode.Next.Item.MoveToTile(bodyNode.Item.previousTile);
  28.             bodyNode = bodyNode.Next;
  29.         }
  30.     }
  31.  
  32.     // Locks snake from moving to previous tile
  33.     private bool IsValidMovement()
  34.     {
  35.         return headBody.gridObject.currentTile.neighbourTiles[(int)_desiredDirection] != headBody.previousTile;
  36.     }
  37.    
  38.     public void ChangeDirection(Direction direction)
  39.     {
  40.         _desiredDirection = direction;
  41.        
  42.         // Check if last input is a valid move
  43.         if (IsValidMovement())
  44.         {
  45.             _currentDirection = _desiredDirection;
  46.         }
  47.     }
  48.  
  49.     // Handle movement
  50.     public virtual void MovementTick()
  51.     {
  52.         if (!enableMovement) { return; }
  53.        
  54.         // Check if last input is a valid move
  55.         if (IsValidMovement())
  56.         {
  57.             _currentDirection = _desiredDirection;
  58.         }
  59.  
  60.         // Move head of snake to tile set by input
  61.         Tile newTile = headBody.gridObject.currentTile.neighbourTiles[(int) _currentDirection];
  62.         headBody.MoveToTile(newTile);
  63.  
  64.         // Evaluate remaining body positions
  65.         EvaluateBodyPositions();
  66.     }
  67.  
  68.     // Do after map spawning
  69.     public virtual void Start()
  70.     {
  71.         snake = GetComponent<Snake>();
  72.         spawner = FindObjectOfType<Spawner>();
  73.         _desiredDirection = Direction.Up;
  74.         _currentDirection = Direction.Up;
  75.         enableInput = true;
  76.        
  77.         if (snake.bodyParts != null)
  78.         {
  79.             headBody = snake.bodyParts.Head.Item;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement