mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-30 04:57:09 +01:00
add gruid buildingsystem
This commit is contained in:
25
Assets/Scripts/Building.cs
Normal file
25
Assets/Scripts/Building.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Building : MonoBehaviour
|
||||
{
|
||||
public bool isPlaced = false;
|
||||
public PlaceableObject placeableObject;
|
||||
|
||||
public void Place()
|
||||
{
|
||||
isPlaced = true;
|
||||
BuildingManager.buildings.Add(this);
|
||||
}
|
||||
|
||||
public void Demolish()
|
||||
{
|
||||
isPlaced = false;
|
||||
BuildingManager.buildings.Remove(this);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
public Vector3Int GetGridPosition()
|
||||
{
|
||||
return new Vector3Int(Mathf.RoundToInt(transform.position.x), Mathf.RoundToInt(transform.position.y), 0);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Building.cs.meta
Normal file
11
Assets/Scripts/Building.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a7737d4424cb89b4caa8dc00101053f9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Assets/Scripts/BuildingManager.cs
Normal file
25
Assets/Scripts/BuildingManager.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Tilemaps;
|
||||
public class BuildingManager : MonoBehaviour
|
||||
{
|
||||
public static List<Building> buildings { get; private set; } = new List<Building>();
|
||||
|
||||
public static Building GetBuildingByGridPosition(Vector3Int tilePosition)
|
||||
{
|
||||
foreach (Building building in buildings)
|
||||
{
|
||||
PlaceableObject po = building.placeableObject;
|
||||
Vector3Int buildingPosition = building.GetGridPosition();
|
||||
BoundsInt area = new BoundsInt(buildingPosition, po.GetSizeVector());
|
||||
if (area.Contains(tilePosition))
|
||||
{
|
||||
return building;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/BuildingManager.cs.meta
Normal file
11
Assets/Scripts/BuildingManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7f6b0f7d7d44d8a4c9fa79d01ee4ec53
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -5,45 +5,135 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
public PlaceableObject placeableObject;
|
||||
|
||||
|
||||
public PlaceableObject TESTPO; //! DEBUG
|
||||
public Tilemap collisionTm;
|
||||
public TileBase occupiedTile;
|
||||
|
||||
GameObject newBuilding;
|
||||
private PlaceableObject placeableObject;
|
||||
private GameObject newBuildingObject;
|
||||
private bool isDemolishing = false;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
newBuilding = Instantiate(placeableObject.prefab, Vector3.zero, Quaternion.identity);
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (newBuilding)
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
int snappedX = Mathf.RoundToInt(mousePosition.x);
|
||||
int snappedY = Mathf.RoundToInt(mousePosition.y);
|
||||
Vector3Int snappedMousePosition = new Vector3Int(snappedX, snappedY, 0);
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.N))
|
||||
{
|
||||
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);
|
||||
isDemolishing = !isDemolishing;
|
||||
Debug.Log(isDemolishing);
|
||||
}
|
||||
|
||||
newBuilding.transform.position = snappedPosition;
|
||||
|
||||
// TODO change to red if occupied
|
||||
|
||||
if (Input.GetMouseButtonDown(0) && !IsOccupied(snappedPosition, placeableObject))
|
||||
if (!isDemolishing)
|
||||
{
|
||||
//! DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.B))
|
||||
{
|
||||
placeableObject.Place();
|
||||
// TODO mark tiles as occupied
|
||||
// TODO unselect
|
||||
SelectBuilding(TESTPO);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
UnselectBuilding();
|
||||
}
|
||||
|
||||
if (newBuildingObject)
|
||||
{
|
||||
Cursor.visible = false;
|
||||
|
||||
newBuildingObject.transform.position = snappedMousePosition;
|
||||
bool isOccupied = IsOccupied(snappedMousePosition, placeableObject.GetSizeVector());
|
||||
|
||||
if (isOccupied)
|
||||
{
|
||||
newBuildingObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
newBuildingObject.GetComponent<SpriteRenderer>().color = Color.white;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0) && !isOccupied)
|
||||
{
|
||||
newBuildingObject.GetComponent<Building>().Place();
|
||||
newBuildingObject.GetComponent<Building>().placeableObject = placeableObject;
|
||||
OccupyTiles(snappedMousePosition, placeableObject.GetSizeVector());
|
||||
newBuildingObject = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Cursor.visible = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Building demoBuilding = BuildingManager.GetBuildingByGridPosition(snappedMousePosition);
|
||||
|
||||
if (demoBuilding)
|
||||
{
|
||||
demoBuilding.Demolish();
|
||||
UnOccupyTiles(demoBuilding.GetGridPosition(), demoBuilding.placeableObject.GetSizeVector());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private bool IsOccupied(Vector3Int startPos, PlaceableObject po)
|
||||
public void SelectBuilding(PlaceableObject newPo)
|
||||
{
|
||||
Vector3Int poSize = new Vector3Int(po.width, po.height, 1);
|
||||
if (newBuildingObject)
|
||||
{
|
||||
Destroy(newBuildingObject);
|
||||
}
|
||||
|
||||
placeableObject = newPo;
|
||||
newBuildingObject = Instantiate(placeableObject.prefab, Vector3.zero, Quaternion.identity);
|
||||
}
|
||||
|
||||
public void UnselectBuilding()
|
||||
{
|
||||
if (newBuildingObject)
|
||||
{
|
||||
Destroy(newBuildingObject);
|
||||
}
|
||||
newBuildingObject = null;
|
||||
placeableObject = null;
|
||||
}
|
||||
|
||||
private void OccupyTiles(Vector3Int startPos, Vector3Int poSize)
|
||||
{
|
||||
BoundsInt area = new BoundsInt(startPos, poSize);
|
||||
TileBase[] tileArray = new TileBase[area.size.x * area.size.y * area.size.z];
|
||||
for (int index = 0; index < tileArray.Length; index++)
|
||||
{
|
||||
tileArray[index] = occupiedTile;
|
||||
}
|
||||
|
||||
collisionTm.SetTilesBlock(area, tileArray);
|
||||
|
||||
}
|
||||
|
||||
private void UnOccupyTiles(Vector3Int startPos, Vector3Int poSize)
|
||||
{
|
||||
BoundsInt area = new BoundsInt(startPos, poSize);
|
||||
TileBase[] tileArray = new TileBase[area.size.x * area.size.y * area.size.z];
|
||||
for (int index = 0; index < tileArray.Length; index++)
|
||||
{
|
||||
tileArray[index] = null;
|
||||
}
|
||||
|
||||
collisionTm.SetTilesBlock(area, tileArray);
|
||||
}
|
||||
|
||||
private bool IsOccupied(Vector3Int startPos, Vector3Int poSize)
|
||||
{
|
||||
|
||||
|
||||
BoundsInt area = new BoundsInt(startPos, poSize);
|
||||
TileBase[] tileArray = collisionTm.GetTilesBlock(area);
|
||||
|
||||
@@ -9,8 +9,8 @@ public class PlaceableObject : ScriptableObject
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
public void Place()
|
||||
public Vector3Int GetSizeVector()
|
||||
{
|
||||
Debug.Log("PLACE");
|
||||
return new Vector3Int(width, height, 1);
|
||||
}
|
||||
}
|
||||
@@ -39,10 +39,10 @@ public class TimeManager : MonoBehaviour
|
||||
{
|
||||
Day++;
|
||||
Hour = 0;
|
||||
OnDayChanged();
|
||||
OnDayChanged?.Invoke();
|
||||
}
|
||||
|
||||
OnHourChanged();
|
||||
OnHourChanged?.Invoke();
|
||||
}
|
||||
|
||||
timer = minuteToRealTime;
|
||||
|
||||
Reference in New Issue
Block a user