97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Fruitomation.Game;
|
|
using UnityEngine;
|
|
|
|
namespace Fruitomation.UI
|
|
{
|
|
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 CalculateGridPosition()
|
|
{
|
|
Vector2 p0 = RectTransform.anchoredPosition;
|
|
Vector2 p1 = p0 - new Vector2(0, 100f); // Offset of the grid from the middle of the screen
|
|
Vector2 p2 = p1 / 40f;
|
|
Vector2 p3 = p2 + new Vector2(48f, 24f); // Half size of the grid
|
|
Vector2Int p4 = Vector2Int.FloorToInt(p3);
|
|
Vector2Int p5 = new
|
|
(
|
|
Math.Clamp(p4.x, 0, 95), // size.x - 1
|
|
Math.Clamp(p4.y, 0, 48) // size.y - 1
|
|
);
|
|
Debug.Log(p5);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateMouseState();
|
|
UpdatePosition();
|
|
CalculateGridPosition();
|
|
|
|
RectTransform.localScale = new Vector3(CurrentMouseClickStrength, CurrentMouseClickStrength, 1f);
|
|
}
|
|
}
|
|
}
|