sfdagsghds

This commit is contained in:
Janis Meister
2023-02-23 13:44:57 +01:00
parent 1a9bcccfba
commit f85588e144
5 changed files with 69 additions and 10 deletions

View File

@@ -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

View File

@@ -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}

View File

@@ -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<GridBuildingSystem>();
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)
{

View File

@@ -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();
}
}

View File

@@ -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))
{