79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Fruitomation
|
|
{
|
|
public class GameCursor : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private Camera ActiveCamera;
|
|
[SerializeField] private CircleCollider2D CursorCollider;
|
|
[SerializeField] private RectTransform RectTransform;
|
|
|
|
private readonly ContactFilter2D ContactFilter = new();
|
|
private readonly List<Collider2D> Colliders = new();
|
|
|
|
private float CurrentMouseClickStrength;
|
|
private float StartOfMouseClick;
|
|
|
|
private void UpdatePosition()
|
|
{
|
|
Ray ray = ActiveCamera.ScreenPointToRay(Input.mousePosition);
|
|
|
|
float t = -ray.origin.z / ray.direction.z;
|
|
Vector2 position = ray.origin + t * ray.direction;
|
|
transform.position = position;
|
|
}
|
|
|
|
private void UpdateMouseState()
|
|
{
|
|
const float MIN_STRENGTH = 0.5f;
|
|
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
CurrentMouseClickStrength = MIN_STRENGTH;
|
|
|
|
CursorCollider.radius = CurrentMouseClickStrength * 30f;
|
|
Physics2D.OverlapCollider(CursorCollider, ContactFilter, Colliders);
|
|
|
|
foreach (Collider2D col in Colliders)
|
|
{
|
|
if (col.transform.name == "Sprite")
|
|
{
|
|
FruitBehaviour fruit = col.GetComponentInParent<FruitBehaviour>();
|
|
Debug.Assert(fruit != null, "Couldn't find FruitBehaviour");
|
|
|
|
fruit.TriggerDestruction();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
StartOfMouseClick = Time.time;
|
|
CurrentMouseClickStrength = MIN_STRENGTH;
|
|
return;
|
|
}
|
|
|
|
CurrentMouseClickStrength = Time.time - StartOfMouseClick;
|
|
CurrentMouseClickStrength = Mathf.Clamp(CurrentMouseClickStrength, MIN_STRENGTH, 1.7f);
|
|
}
|
|
|
|
else
|
|
{
|
|
CurrentMouseClickStrength = MIN_STRENGTH;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateMouseState();
|
|
UpdatePosition();
|
|
|
|
RectTransform.localScale = new Vector3(CurrentMouseClickStrength, CurrentMouseClickStrength, 1f);
|
|
}
|
|
}
|
|
}
|