add grid building system

This commit is contained in:
Janis
2023-02-15 18:09:54 +01:00
parent 4b965000a1
commit 4572b0dc27
10 changed files with 583 additions and 466 deletions

View File

@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class GridBuildingSystem : MonoBehaviour
{
public PlaceableObject placeableObject;
public Tilemap collisionTm;
GameObject newBuilding;
private void Start()
{
newBuilding = Instantiate(placeableObject.prefab, Vector3.zero, Quaternion.identity);
}
private void Update()
{
if (newBuilding)
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//* Cursor.visible = false;
int snappedX = Mathf.RoundToInt(newPosition.x);
int snappedY = Mathf.RoundToInt(newPosition.y);
Vector3Int snappedPosition = new Vector3Int(snappedX, snappedY, 0);
newBuilding.transform.position = snappedPosition;
// TODO change to red if occupied
if (Input.GetMouseButtonDown(0) && !IsOccupied(snappedPosition, placeableObject))
{
placeableObject.Place();
// TODO mark tiles as occupied
// TODO unselect
}
}
}
private bool IsOccupied(Vector3Int startPos, PlaceableObject po)
{
Vector3Int poSize = new Vector3Int(po.width, po.height, 1);
BoundsInt area = new BoundsInt(startPos, poSize);
TileBase[] tileArray = collisionTm.GetTilesBlock(area);
foreach (TileBase tile in tileArray)
{
if (tile != null)
{
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8c4997c1e7757ea49a7ea73e46e528c2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
using UnityEngine;
[CreateAssetMenu(fileName = "New PlacableObject", menuName = "Harvestdale/PlaceableObjects", order = 0)]
public class PlaceableObject : ScriptableObject
{
public string objectName;
// public Sprite icon;
public GameObject prefab;
public int width;
public int height;
public void Place()
{
Debug.Log("PLACE");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 66971ee377514a046acbb1e1c0b45d44
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: