Advertisement
Javierdds

Javier de Dueñas - Root Line Creation

May 9th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Roots
  6. {
  7.     public class RootLine : MonoBehaviour
  8.     {
  9.    
  10.         // Serialized Fields
  11.         [SerializeField] private LineRenderer _rootRenderer;
  12.         [SerializeField] private EdgeCollider2D _rootCollider;
  13.         [SerializeField] private Transform _endOfRootPivot;
  14.         [SerializeField] private float _minDistanceBetweenPoints;
  15.  
  16.         // Private Variables
  17.         private readonly List<Vector2> _rootPoints = new List<Vector2>();
  18.         private int _costOfRoot;
  19.         private bool _isCollidingWithRock;
  20.  
  21.         // Getters & Setters
  22.         public LineRenderer Renderer { get => _rootRenderer; set => _rootRenderer = value; }
  23.         public EdgeCollider2D Collider { get => _rootCollider; set => _rootCollider = value; }
  24.         public float MinDistanceBetweenPoints { get => _minDistanceBetweenPoints; set => _minDistanceBetweenPoints = value; }
  25.         public int CostOfRoot { get => _costOfRoot; set => _costOfRoot = value; }
  26.         public bool IsCollidingWithRock { get => _isCollidingWithRock; set => _isCollidingWithRock = value; }
  27.  
  28.         public void Awake()
  29.         {
  30.             _rootCollider.transform.position -= transform.position;
  31.             _isCollidingWithRock = false;
  32.         }
  33.  
  34.         // Adds a world position point to the root renderer,
  35.         // increasing the root size.
  36.         public bool SetPositionOnRenderer(Vector2 newPointPosition)
  37.         {
  38.             if (!CanAppendPosition(newPointPosition)) return false;
  39.  
  40.             _rootPoints.Add(newPointPosition);
  41.  
  42.             _rootRenderer.positionCount++;
  43.             _rootRenderer.SetPosition(_rootRenderer.positionCount - 1, newPointPosition);
  44.  
  45.             _rootCollider.points = _rootPoints.ToArray();
  46.             _endOfRootPivot.transform.position = newPointPosition;
  47.             _endOfRootPivot.transform.rotation = Quaternion.LookRotation(_endOfRootPivot.transform.forward, GetEndOfRootDirection());
  48.  
  49.             return true;
  50.         }
  51.  
  52.         public void SetEndOfRootLine()
  53.         {
  54.             if (_rootRenderer.positionCount <= 1) return;
  55.             _endOfRootPivot.transform.rotation = Quaternion.LookRotation(_endOfRootPivot.transform.forward, GetEndOfRootDirection());
  56.         }
  57.  
  58.         private bool CanAppendPosition(Vector2 newPointPosition)
  59.         {
  60.             if (_rootRenderer.positionCount == 0) return true;
  61.  
  62.             return Vector2.Distance(_rootRenderer.GetPosition(_rootRenderer.positionCount - 1), newPointPosition) > _minDistanceBetweenPoints;
  63.         }
  64.  
  65.         // If the last point added to the renderer is too close to any of
  66.         // the older points, the root is colliding with self.
  67.         public bool IsCollidingWithSelf()
  68.         {
  69.             bool isColliding = false;
  70.  
  71.             Vector2 lastPoint = _rootRenderer.GetPosition(_rootRenderer.positionCount - 1);
  72.             for (int i = 0; i < _rootRenderer.positionCount - 1 && !isColliding; i++)
  73.             {
  74.                 if(Vector2.Distance(lastPoint, _rootRenderer.GetPosition(i)) < _minDistanceBetweenPoints)
  75.                 {
  76.                     isColliding = true;
  77.                 }
  78.             }
  79.  
  80.             return isColliding;
  81.         }
  82.  
  83.         // The root end direction is defined by the direction vector
  84.         // of the last two points.
  85.         public Vector2 GetEndOfRootDirection()
  86.         {
  87.             Vector2 direction = new Vector2();
  88.             if(_rootRenderer.positionCount > 2)
  89.             {
  90.                 direction = _rootRenderer.GetPosition(_rootRenderer.positionCount - 1) - _rootRenderer.GetPosition(_rootRenderer.positionCount - 2);
  91.             }
  92.             return direction;
  93.         }
  94.     }
  95. }
  96.  
Tags: C# Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement