178 lines
6.2 KiB
C#
178 lines
6.2 KiB
C#
using PashaBibko.Pacore.Attributes;
|
|
using System.Collections.Generic;
|
|
using Fruitomation.Global;
|
|
using Fruitomation.Game;
|
|
using UnityEngine.UI;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
namespace Fruitomation.UI
|
|
{
|
|
public class GameCursor : MonoBehaviour
|
|
{
|
|
private const float MIN_STRENGTH = 0.5f;
|
|
private const float MAX_STRENGTH = 1.7f;
|
|
|
|
[Header("References")]
|
|
[SerializeField] private Camera ActiveCamera;
|
|
[SerializeField] private CircleCollider2D CursorCollider;
|
|
[SerializeField] private RectTransform RectTransform;
|
|
[SerializeField] private Image BuildingPreview;
|
|
[SerializeField] private Image CursorImage;
|
|
[SerializeField] private BuildingManager BuildingManager;
|
|
|
|
[Header("Read Only")]
|
|
[SerializeField, InspectorReadOnly] private float CurrentMouseClickStrength;
|
|
[SerializeField, InspectorReadOnly] private Vector2Int GridPosition;
|
|
[SerializeField, InspectorReadOnly] private GameObject SelectedBuildingToBuild;
|
|
|
|
private readonly ContactFilter2D ContactFilter = new();
|
|
private readonly List<Collider2D> Colliders = new();
|
|
|
|
private BuildingBase SelectedBuildingsBuildingBase;
|
|
|
|
private float StartOfMouseClick;
|
|
private bool MouseOnGrid;
|
|
|
|
public void SetSelectedBuildingToBuild(GameObject prefab)
|
|
{
|
|
SelectedBuildingsBuildingBase = prefab.GetComponent<BuildingBase>();
|
|
SelectedBuildingToBuild = prefab;
|
|
}
|
|
|
|
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 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);
|
|
GridPosition = new Vector2Int
|
|
(
|
|
Math.Clamp(p4.x, 0, 95), // size.x - 1
|
|
Math.Clamp(p4.y, 0, 48) // size.y - 1
|
|
);
|
|
|
|
MouseOnGrid = GridPosition == p4;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdatePosition();
|
|
|
|
CursorImage.enabled = false;
|
|
BuildingPreview.enabled = false;
|
|
|
|
CurrentMouseClickStrength = MIN_STRENGTH;
|
|
switch (GameStateController.State)
|
|
{
|
|
case GameState.Simulation:
|
|
UpdateMouseStateSimulation();
|
|
break;
|
|
|
|
case GameState.Building:
|
|
UpdateMouseStateBuilding();
|
|
break;
|
|
|
|
case GameState.UpgradeMenu:
|
|
case GameState.Paused:
|
|
case GameState.Default:
|
|
case GameState.BuildingMenu:
|
|
break; // No extra logic needed
|
|
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
|
|
CurrentMouseClickStrength = Mathf.Clamp(CurrentMouseClickStrength, MIN_STRENGTH, MAX_STRENGTH);
|
|
|
|
CalculateGridPosition();
|
|
|
|
RectTransform.localScale = new Vector3(CurrentMouseClickStrength, CurrentMouseClickStrength, 1f);
|
|
}
|
|
|
|
private void UpdateMouseStateSimulation()
|
|
{
|
|
CursorImage.enabled = true;
|
|
|
|
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, "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;
|
|
}
|
|
|
|
else
|
|
{
|
|
CurrentMouseClickStrength = MIN_STRENGTH;
|
|
}
|
|
}
|
|
|
|
private void UpdateMouseStateBuilding()
|
|
{
|
|
BuildingPreview.enabled = true;
|
|
|
|
BuildingPreview.material = new Material(Shader.Find("UI/Default"))
|
|
{
|
|
mainTexture = SelectedBuildingsBuildingBase.Texture
|
|
};
|
|
|
|
BuildingPreview.rectTransform.sizeDelta = SelectedBuildingsBuildingBase.SizeOnGrid * 40;
|
|
|
|
//
|
|
Vector2Int p0 = new
|
|
(
|
|
Math.Clamp(GridPosition.x, 0, 96 - SelectedBuildingsBuildingBase.SizeOnGrid.x),
|
|
Math.Clamp(GridPosition.y, 0, 49 - SelectedBuildingsBuildingBase.SizeOnGrid.y)
|
|
);
|
|
Vector2Int p1 = p0 - new Vector2Int(48, 24);
|
|
Vector2Int p2 = p1 * 40;
|
|
Vector2Int p3 = p2 + new Vector2Int(20, 100);
|
|
|
|
Vector2 o0 = SelectedBuildingsBuildingBase.SizeOnGrid - Vector2.one;
|
|
Vector2 o1 = o0 * 20;
|
|
Vector2 p4 = p3 + o1;
|
|
BuildingPreview.rectTransform.anchoredPosition = p4;
|
|
|
|
//
|
|
if (Input.GetMouseButtonDown(0) && MouseOnGrid)
|
|
{
|
|
BuildingManager.AddBuildingAt(p1, SelectedBuildingToBuild);
|
|
}
|
|
}
|
|
}
|
|
}
|