Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Roots
- {
- public class RootLine : MonoBehaviour
- {
- // Serialized Fields
- [SerializeField] private LineRenderer _rootRenderer;
- [SerializeField] private EdgeCollider2D _rootCollider;
- [SerializeField] private Transform _endOfRootPivot;
- [SerializeField] private float _minDistanceBetweenPoints;
- // Private Variables
- private readonly List<Vector2> _rootPoints = new List<Vector2>();
- private int _costOfRoot;
- private bool _isCollidingWithRock;
- // Getters & Setters
- public LineRenderer Renderer { get => _rootRenderer; set => _rootRenderer = value; }
- public EdgeCollider2D Collider { get => _rootCollider; set => _rootCollider = value; }
- public float MinDistanceBetweenPoints { get => _minDistanceBetweenPoints; set => _minDistanceBetweenPoints = value; }
- public int CostOfRoot { get => _costOfRoot; set => _costOfRoot = value; }
- public bool IsCollidingWithRock { get => _isCollidingWithRock; set => _isCollidingWithRock = value; }
- public void Awake()
- {
- _rootCollider.transform.position -= transform.position;
- _isCollidingWithRock = false;
- }
- // Adds a world position point to the root renderer,
- // increasing the root size.
- public bool SetPositionOnRenderer(Vector2 newPointPosition)
- {
- if (!CanAppendPosition(newPointPosition)) return false;
- _rootPoints.Add(newPointPosition);
- _rootRenderer.positionCount++;
- _rootRenderer.SetPosition(_rootRenderer.positionCount - 1, newPointPosition);
- _rootCollider.points = _rootPoints.ToArray();
- _endOfRootPivot.transform.position = newPointPosition;
- _endOfRootPivot.transform.rotation = Quaternion.LookRotation(_endOfRootPivot.transform.forward, GetEndOfRootDirection());
- return true;
- }
- public void SetEndOfRootLine()
- {
- if (_rootRenderer.positionCount <= 1) return;
- _endOfRootPivot.transform.rotation = Quaternion.LookRotation(_endOfRootPivot.transform.forward, GetEndOfRootDirection());
- }
- private bool CanAppendPosition(Vector2 newPointPosition)
- {
- if (_rootRenderer.positionCount == 0) return true;
- return Vector2.Distance(_rootRenderer.GetPosition(_rootRenderer.positionCount - 1), newPointPosition) > _minDistanceBetweenPoints;
- }
- // If the last point added to the renderer is too close to any of
- // the older points, the root is colliding with self.
- public bool IsCollidingWithSelf()
- {
- bool isColliding = false;
- Vector2 lastPoint = _rootRenderer.GetPosition(_rootRenderer.positionCount - 1);
- for (int i = 0; i < _rootRenderer.positionCount - 1 && !isColliding; i++)
- {
- if(Vector2.Distance(lastPoint, _rootRenderer.GetPosition(i)) < _minDistanceBetweenPoints)
- {
- isColliding = true;
- }
- }
- return isColliding;
- }
- // The root end direction is defined by the direction vector
- // of the last two points.
- public Vector2 GetEndOfRootDirection()
- {
- Vector2 direction = new Vector2();
- if(_rootRenderer.positionCount > 2)
- {
- direction = _rootRenderer.GetPosition(_rootRenderer.positionCount - 1) - _rootRenderer.GetPosition(_rootRenderer.positionCount - 2);
- }
- return direction;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement