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,8 @@
fileFormatVersion: 2
guid: d7dca3d9e70abff4c98a2913b1182060
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 66971ee377514a046acbb1e1c0b45d44, type: 3}
m_Name: house
m_EditorClassIdentifier:
objectName: house
prefab: {fileID: 6244317213382018835, guid: 5eebe6599cdf53b4da6f656f81588cb8, type: 3}
width: 4
height: 5

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 40437410555fcda43ab2ec4c9a4c2807
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -11,7 +11,6 @@ GameObject:
- component: {fileID: 5852600358559839619}
- component: {fileID: 2080657515599704299}
- component: {fileID: 95789179383567876}
- component: {fileID: 6347999927300409482}
m_Layer: 0
m_Name: House
m_TagString: Untagged
@@ -85,7 +84,7 @@ SpriteRenderer:
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
m_SpriteSortPoint: 1
--- !u!61 &95789179383567876
BoxCollider2D:
m_ObjectHideFlags: 0
@@ -99,31 +98,16 @@ BoxCollider2D:
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_Offset: {x: 2, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 4, y: 4.5625}
pivot: {x: 0, y: 0}
oldSize: {x: 4, y: 5}
newSize: {x: 4, y: 4.5625}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 4, y: 4.5625}
m_Size: {x: 3, y: 1}
m_EdgeRadius: 0
--- !u!114 &6347999927300409482
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6244317213382018835}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b98c0e6f554197e4281920fafc6bab25, type: 3}
m_Name:
m_EditorClassIdentifier:
area:
m_Position: {x: 0, y: 0, z: 0}
m_Size: {x: 4, y: 5, z: 1}

View File

@@ -237,7 +237,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 224768186}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -5.5101213, y: 0.036043525, z: 0}
m_LocalPosition: {x: -7.45, y: 4.21, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
@@ -29650,12 +29650,11 @@ MonoBehaviour:
m_GameObject: {fileID: 1507699183}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2e90c7a6153f1b345a7aa678ca6b61cd, type: 3}
m_Script: {fileID: 11500000, guid: 8c4997c1e7757ea49a7ea73e46e528c2, type: 3}
m_Name:
m_EditorClassIdentifier:
gridLayout: {fileID: 1507699184}
mainTilemap: {fileID: 1579890810}
takenTile: {fileID: 11400000, guid: 4815673d17e2b7246b5812e51b47cd6d, type: 2}
placeableObject: {fileID: 11400000, guid: 40437410555fcda43ab2ec4c9a4c2807, type: 2}
collisionTm: {fileID: 1579890810}
--- !u!1 &1579890809
GameObject:
m_ObjectHideFlags: 0
@@ -29668,7 +29667,7 @@ GameObject:
- component: {fileID: 1579890810}
- component: {fileID: 1579890811}
m_Layer: 0
m_Name: Buildings
m_Name: Occupied
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
@@ -30166,7 +30165,7 @@ TilemapRenderer:
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_SortingOrder: 99
m_ChunkSize: {x: 32, y: 32, z: 32}
m_ChunkCullingBounds: {x: 0, y: 0, z: 0}
m_MaxChunkCount: 16
@@ -30248,11 +30247,11 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 5852600358559839619, guid: 5eebe6599cdf53b4da6f656f81588cb8, type: 3}
propertyPath: m_LocalPosition.x
value: 0
value: 2
objectReference: {fileID: 0}
- target: {fileID: 5852600358559839619, guid: 5eebe6599cdf53b4da6f656f81588cb8, type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 15
objectReference: {fileID: 0}
- target: {fileID: 5852600358559839619, guid: 5eebe6599cdf53b4da6f656f81588cb8, type: 3}
propertyPath: m_LocalPosition.z

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:

File diff suppressed because it is too large Load Diff