Added a custom cursor

This commit is contained in:
2026-03-30 18:16:20 +01:00
parent 652df5ce01
commit d0bebb9333
4 changed files with 211 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
using PashaBibko.Pacore.Attributes;
using UnityEngine.UI;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Fruitomation
{
@@ -23,12 +24,8 @@ namespace Fruitomation
Spawner = spawner;
Body2D.velocity = Random.insideUnitCircle * 2.5f;
Button.onClick.AddListener(OnPlayerClicked);
}
private void OnPlayerClicked() => TriggerDestruction();
private void Update()
{
if (!GameStateController.Is(GameState.Simulation))
@@ -48,7 +45,7 @@ namespace Fruitomation
}
}
private void TriggerDestruction()
public void TriggerDestruction()
{
MoneyController.Add((ulong)Random.Range(1, 5));

View File

@@ -0,0 +1,78 @@
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);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4c69710c42ff4ee0bb660893e59c12fe
timeCreated: 1774874748