Started adding preview for building buildings
This commit is contained in:
@@ -1,21 +1,35 @@
|
||||
using System;
|
||||
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;
|
||||
|
||||
[Header("Dev")]
|
||||
[SerializeField] private GameObject BuildingPrefab;
|
||||
|
||||
[Header("Read Only")]
|
||||
[SerializeField, InspectorReadOnly] private float CurrentMouseClickStrength;
|
||||
[SerializeField, InspectorReadOnly] private Vector2Int GridPosition;
|
||||
|
||||
private readonly ContactFilter2D ContactFilter = new();
|
||||
private readonly List<Collider2D> Colliders = new();
|
||||
|
||||
private float CurrentMouseClickStrength;
|
||||
private float StartOfMouseClick;
|
||||
|
||||
private void UpdatePosition()
|
||||
@@ -27,10 +41,56 @@ namespace Fruitomation.UI
|
||||
transform.position = position;
|
||||
}
|
||||
|
||||
private void UpdateMouseState()
|
||||
private void CalculateGridPosition()
|
||||
{
|
||||
const float MIN_STRENGTH = 0.5f;
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdatePosition();
|
||||
|
||||
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:
|
||||
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;
|
||||
BuildingPreview.enabled = false;
|
||||
|
||||
if (Input.GetMouseButtonUp(0))
|
||||
{
|
||||
CurrentMouseClickStrength = MIN_STRENGTH;
|
||||
@@ -43,7 +103,7 @@ namespace Fruitomation.UI
|
||||
if (col.transform.name == "Sprite")
|
||||
{
|
||||
FruitBehaviour fruit = col.GetComponentInParent<FruitBehaviour>();
|
||||
Debug.Assert(fruit != null, "Couldn't find FruitBehaviour");
|
||||
Debug.Assert(fruit, "Couldn't find FruitBehaviour");
|
||||
|
||||
fruit.TriggerDestruction();
|
||||
}
|
||||
@@ -60,7 +120,6 @@ namespace Fruitomation.UI
|
||||
}
|
||||
|
||||
CurrentMouseClickStrength = Time.time - StartOfMouseClick;
|
||||
CurrentMouseClickStrength = Mathf.Clamp(CurrentMouseClickStrength, MIN_STRENGTH, 1.7f);
|
||||
}
|
||||
|
||||
else
|
||||
@@ -69,28 +128,25 @@ namespace Fruitomation.UI
|
||||
}
|
||||
}
|
||||
|
||||
private void CalculateGridPosition()
|
||||
private void UpdateMouseStateBuilding()
|
||||
{
|
||||
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);
|
||||
}
|
||||
CursorImage.enabled = false;
|
||||
BuildingPreview.enabled = true;
|
||||
|
||||
//
|
||||
Vector2Int p0 = GridPosition - new Vector2Int(48, 24);
|
||||
Vector2Int p1 = p0 * 40;
|
||||
Vector2Int p2 = p1 + new Vector2Int(20, 100);
|
||||
BuildingPreview.rectTransform.anchoredPosition = p2;
|
||||
|
||||
//
|
||||
BuildingBase building = BuildingPrefab.GetComponent<BuildingBase>();
|
||||
BuildingPreview.material = new Material(Shader.Find("UI/Default"))
|
||||
{
|
||||
mainTexture = building.Texture
|
||||
};
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateMouseState();
|
||||
UpdatePosition();
|
||||
CalculateGridPosition();
|
||||
|
||||
RectTransform.localScale = new Vector3(CurrentMouseClickStrength, CurrentMouseClickStrength, 1f);
|
||||
BuildingPreview.rectTransform.sizeDelta = building.SizeOnGrid * 40;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user