From f85588e1443d7cc4497880756c008a6f14f954d2 Mon Sep 17 00:00:00 2001 From: Janis Meister Date: Thu, 23 Feb 2023 13:44:57 +0100 Subject: [PATCH] sfdagsghds --- Assets/Items/Hoe.asset | 6 ++-- Assets/Scenes/SampleScene.unity | 3 +- Assets/Scripts/GridBuildingSystem.cs | 44 +++++++++++++++++++++++++--- Assets/Scripts/Items/Hoe.cs | 4 ++- Assets/Scripts/PlayerController.cs | 22 +++++++++++++- 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/Assets/Items/Hoe.asset b/Assets/Items/Hoe.asset index ccec6b0..0f3688c 100644 --- a/Assets/Items/Hoe.asset +++ b/Assets/Items/Hoe.asset @@ -12,8 +12,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ab99e668aa1ba5a469eef680f3bb4b45, type: 3} m_Name: Hoe m_EditorClassIdentifier: - uuid: + uuid: Dia Hoe itemName: sprite: {fileID: 0} - stackable: 1 - maxStackSize: 100 + stackable: 0 + maxStackSize: 1 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 319588f..9316669 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -495,8 +495,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: fd29210254821be4dab438ab0312df18, type: 3} m_Name: m_EditorClassIdentifier: - testItem1: {fileID: 11400000, guid: 601d7a33a6c338d49a35134d5c772940, type: 2} + testItem1: {fileID: 11400000, guid: 030984201821ba840872812944c9dc13, type: 2} testItem2: {fileID: 11400000, guid: cbbbe8384f7ec2845b4145744d51ecf8, type: 2} + interactionDistance: 2 hotbarSlotCount: 9 activeHotbarSlot: 0 playerInventory: {fileID: 0} diff --git a/Assets/Scripts/GridBuildingSystem.cs b/Assets/Scripts/GridBuildingSystem.cs index c96f55c..e4e9907 100644 --- a/Assets/Scripts/GridBuildingSystem.cs +++ b/Assets/Scripts/GridBuildingSystem.cs @@ -6,6 +6,23 @@ using UnityEngine.Tilemaps; public class GridBuildingSystem : MonoBehaviour { + private static GridBuildingSystem instance; + + public static GridBuildingSystem Instance + { + get + { + if (instance == null) + { + instance = FindObjectOfType(); + if (instance == null) + { + Debug.LogError("No GridBuildingSystem found in the scene."); + } + } + return instance; + } + } public PlaceableObject TESTPO; //! DEBUG public Tilemap collisionTm; @@ -16,12 +33,20 @@ public class GridBuildingSystem : MonoBehaviour private GameObject newBuildingObject; private bool isDemolishing = false; + private void Awake() + { + if (instance != null && instance != this) + { + Destroy(this.gameObject); + return; + } + instance = this; + DontDestroyOnLoad(gameObject); + } + private void Update() { - Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); - int snappedX = Mathf.FloorToInt(mousePosition.x); - int snappedY = Mathf.FloorToInt(mousePosition.y); - Vector3Int snappedMousePosition = new Vector3Int(snappedX, snappedY, 0); + Vector3Int snappedMousePosition = GetSnappedMousePosition(); if (Input.GetKeyDown(KeyCode.N)) { @@ -88,6 +113,17 @@ public class GridBuildingSystem : MonoBehaviour } } + public Vector3Int GetSnappedMousePosition() + { + return GetSnappedPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition)); + } + + public Vector3Int GetSnappedPosition(Vector3 pos) + { + int snappedX = Mathf.FloorToInt(pos.x); + int snappedY = Mathf.FloorToInt(pos.y); + return new Vector3Int(snappedX, snappedY, 0); + } public void SelectBuilding(PlaceableObject newPo) { diff --git a/Assets/Scripts/Items/Hoe.cs b/Assets/Scripts/Items/Hoe.cs index 25cc19a..d73e866 100644 --- a/Assets/Scripts/Items/Hoe.cs +++ b/Assets/Scripts/Items/Hoe.cs @@ -5,11 +5,13 @@ public class Hoe : Tool { public override void OnUse() { - throw new System.NotImplementedException(); + Debug.Log("Hoe on use"); + } public override void Use() { + Debug.Log("Hoe use"); OnUse(); } } \ No newline at end of file diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 702b8cb..a05d1b9 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -9,7 +9,7 @@ public class PlayerController : MonoBehaviour public static Action OnActiveSlotChanged; public Item testItem1; public Item testItem2; - + public float interactionDistance = 2.5f; public int hotbarSlotCount = 9; public int activeHotbarSlot = 0; [SerializeField] private Inventory playerInventory; @@ -44,6 +44,26 @@ public class PlayerController : MonoBehaviour GuiManager.Instance.TogglePanel("Inventory"); } + if (Input.GetMouseButtonDown(0)) + { + Vector3Int snappedMousePosition = GridBuildingSystem.Instance.GetSnappedMousePosition(); + Vector3Int snappedPlayerPosition = GridBuildingSystem.Instance.GetSnappedPosition(gameObject.transform.position); + if (Vector3Int.Distance(snappedMousePosition, snappedPlayerPosition) <= interactionDistance) + { + if (playerInventory.items[activeHotbarSlot]?.item is Tool) + { + Tool tool = playerInventory.items[activeHotbarSlot].item as Tool; + tool.Use(); + } + } + else + { + Debug.Log("Not in range"); + + } + + } + //! DEBUG if (Input.GetKeyDown(KeyCode.U)) {