Started adding preview for building buildings

This commit is contained in:
2026-03-31 13:14:06 +01:00
parent ec6cbca02e
commit 40cb81e740
3 changed files with 167 additions and 27 deletions

View File

@@ -1305,6 +1305,7 @@ RectTransform:
- {fileID: 2026916298} - {fileID: 2026916298}
- {fileID: 96288173} - {fileID: 96288173}
- {fileID: 1330313875} - {fileID: 1330313875}
- {fileID: 1194686691}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
@@ -1312,6 +1313,81 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0} m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0} m_Pivot: {x: 0, y: 0}
--- !u!1 &1194686690
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1194686691}
- component: {fileID: 1194686693}
- component: {fileID: 1194686692}
m_Layer: 5
m_Name: BuildingPreview
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1194686691
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1194686690}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1165634414}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 40, y: 40}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1194686692
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1194686690}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 0
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 0}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &1194686693
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1194686690}
m_CullTransparentMesh: 1
--- !u!1 &1314545342 --- !u!1 &1314545342
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -1516,7 +1592,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1330313874} m_GameObject: {fileID: 1330313874}
m_Enabled: 0 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name: m_Name:
@@ -1562,6 +1638,12 @@ MonoBehaviour:
ActiveCamera: {fileID: 330585545} ActiveCamera: {fileID: 330585545}
CursorCollider: {fileID: 1330313879} CursorCollider: {fileID: 1330313879}
RectTransform: {fileID: 1330313875} RectTransform: {fileID: 1330313875}
BuildingPreview: {fileID: 1194686692}
CursorImage: {fileID: 1330313876}
BuildingPrefab: {fileID: 8624048914473773482, guid: 1b5c88b3f8d08674a9b15155d6ba9a36,
type: 3}
CurrentMouseClickStrength: 0
GridPosition: {x: 0, y: 0}
--- !u!58 &1330313879 --- !u!58 &1330313879
CircleCollider2D: CircleCollider2D:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -7,5 +7,7 @@ namespace Fruitomation.Game
[Header("Building Properties")] [Header("Building Properties")]
[SerializeField] private Texture2D BuildingTexture; [SerializeField] private Texture2D BuildingTexture;
[field: SerializeField] public Vector2Int SizeOnGrid { get; private set; } [field: SerializeField] public Vector2Int SizeOnGrid { get; private set; }
public Texture2D Texture => BuildingTexture;
} }
} }

View File

@@ -1,21 +1,35 @@
using System; using PashaBibko.Pacore.Attributes;
using System.Collections.Generic; using System.Collections.Generic;
using Fruitomation.Global;
using Fruitomation.Game; using Fruitomation.Game;
using UnityEngine.UI;
using UnityEngine; using UnityEngine;
using System;
namespace Fruitomation.UI namespace Fruitomation.UI
{ {
public class GameCursor : MonoBehaviour public class GameCursor : MonoBehaviour
{ {
private const float MIN_STRENGTH = 0.5f;
private const float MAX_STRENGTH = 1.7f;
[Header("References")] [Header("References")]
[SerializeField] private Camera ActiveCamera; [SerializeField] private Camera ActiveCamera;
[SerializeField] private CircleCollider2D CursorCollider; [SerializeField] private CircleCollider2D CursorCollider;
[SerializeField] private RectTransform RectTransform; [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 ContactFilter2D ContactFilter = new();
private readonly List<Collider2D> Colliders = new(); private readonly List<Collider2D> Colliders = new();
private float CurrentMouseClickStrength;
private float StartOfMouseClick; private float StartOfMouseClick;
private void UpdatePosition() private void UpdatePosition()
@@ -27,10 +41,56 @@ namespace Fruitomation.UI
transform.position = position; 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)) if (Input.GetMouseButtonUp(0))
{ {
CurrentMouseClickStrength = MIN_STRENGTH; CurrentMouseClickStrength = MIN_STRENGTH;
@@ -43,7 +103,7 @@ namespace Fruitomation.UI
if (col.transform.name == "Sprite") if (col.transform.name == "Sprite")
{ {
FruitBehaviour fruit = col.GetComponentInParent<FruitBehaviour>(); FruitBehaviour fruit = col.GetComponentInParent<FruitBehaviour>();
Debug.Assert(fruit != null, "Couldn't find FruitBehaviour"); Debug.Assert(fruit, "Couldn't find FruitBehaviour");
fruit.TriggerDestruction(); fruit.TriggerDestruction();
} }
@@ -60,7 +120,6 @@ namespace Fruitomation.UI
} }
CurrentMouseClickStrength = Time.time - StartOfMouseClick; CurrentMouseClickStrength = Time.time - StartOfMouseClick;
CurrentMouseClickStrength = Mathf.Clamp(CurrentMouseClickStrength, MIN_STRENGTH, 1.7f);
} }
else else
@@ -69,28 +128,25 @@ namespace Fruitomation.UI
} }
} }
private void CalculateGridPosition() private void UpdateMouseStateBuilding()
{ {
Vector2 p0 = RectTransform.anchoredPosition; CursorImage.enabled = false;
Vector2 p1 = p0 - new Vector2(0, 100f); // Offset of the grid from the middle of the screen BuildingPreview.enabled = true;
Vector2 p2 = p1 / 40f;
Vector2 p3 = p2 + new Vector2(48f, 24f); // Half size of the grid //
Vector2Int p4 = Vector2Int.FloorToInt(p3); Vector2Int p0 = GridPosition - new Vector2Int(48, 24);
Vector2Int p5 = new Vector2Int p1 = p0 * 40;
( Vector2Int p2 = p1 + new Vector2Int(20, 100);
Math.Clamp(p4.x, 0, 95), // size.x - 1 BuildingPreview.rectTransform.anchoredPosition = p2;
Math.Clamp(p4.y, 0, 48) // size.y - 1
); //
Debug.Log(p5); BuildingBase building = BuildingPrefab.GetComponent<BuildingBase>();
} BuildingPreview.material = new Material(Shader.Find("UI/Default"))
{
mainTexture = building.Texture
};
private void Update() BuildingPreview.rectTransform.sizeDelta = building.SizeOnGrid * 40;
{
UpdateMouseState();
UpdatePosition();
CalculateGridPosition();
RectTransform.localScale = new Vector3(CurrentMouseClickStrength, CurrentMouseClickStrength, 1f);
} }
} }
} }