mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-29 12:32:10 +01:00
a
This commit is contained in:
@@ -14,5 +14,5 @@ MonoBehaviour:
|
||||
m_EditorClassIdentifier:
|
||||
nameString: Way
|
||||
prefab: {fileID: 3420327930372171566, guid: 1b0d9211d89253549b71b121b268c59b, type: 3}
|
||||
width: 1
|
||||
width: 2
|
||||
height: 1
|
||||
|
||||
@@ -10,6 +10,7 @@ GameObject:
|
||||
m_Component:
|
||||
- component: {fileID: 3420327930372171566}
|
||||
- component: {fileID: 3420327930372171567}
|
||||
- component: {fileID: 7845977379617268391}
|
||||
m_Layer: 0
|
||||
m_Name: way_normal
|
||||
m_TagString: Untagged
|
||||
@@ -84,3 +85,15 @@ SpriteRenderer:
|
||||
m_WasSpriteAssigned: 1
|
||||
m_MaskInteraction: 0
|
||||
m_SpriteSortPoint: 0
|
||||
--- !u!114 &7845977379617268391
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 3420327930372171564}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 56fadc4f2b895ad46bda623c68601c7e, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
|
||||
@@ -268,7 +268,8 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: a9e8b2a885476754e9652c90137f91bc, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
placedObjectTypeSO: {fileID: 11400000, guid: b08e32832db84ca489b3dd705df7eff7, type: 2}
|
||||
placedObjectTypeSOList:
|
||||
- {fileID: 11400000, guid: b08e32832db84ca489b3dd705df7eff7, type: 2}
|
||||
--- !u!4 &1522634446
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -5,7 +5,8 @@ using UnityEngine;
|
||||
// Written with https://www.youtube.com/watch?v=dulosHPl82A
|
||||
public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] PlacedObjectTypeSO placedObjectTypeSO;
|
||||
[SerializeField] List<PlacedObjectTypeSO> placedObjectTypeSOList = new List<PlacedObjectTypeSO>(); // https://youtu.be/dulosHPl82A?t=1192
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
|
||||
Grid<GridObject> grid;
|
||||
void Awake()
|
||||
@@ -15,25 +16,26 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
float cellSize = 2f;
|
||||
|
||||
grid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
placedObjectTypeSO = placedObjectTypeSOList[0];
|
||||
}
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
Grid<GridObject> grid;
|
||||
Transform transform;
|
||||
PlacedObject placedObject;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y)
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
}
|
||||
public void SetTransform(Transform _transform) => transform = _transform;
|
||||
public Transform GetTransform() => transform;
|
||||
public void ClearTransform() => transform = null;
|
||||
public void SetPlacedObject(PlacedObject _placedObject) => placedObject = _placedObject;
|
||||
public PlacedObject GetPlacedObject() => placedObject;
|
||||
public void ClearPlacedObject() => placedObject = null;
|
||||
|
||||
public bool CanBuild()
|
||||
{
|
||||
return transform == null;
|
||||
return placedObject == null;
|
||||
}
|
||||
}
|
||||
void Update()
|
||||
@@ -58,11 +60,12 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
if (canBuild)
|
||||
{
|
||||
Transform builtTransform = Instantiate(placedObjectTypeSO.prefab, grid.GetWorldPosition(x, y), Quaternion.identity);
|
||||
PlacedObject placedObject = PlacedObject.Create(grid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, placedObjectTypeSO);
|
||||
|
||||
|
||||
foreach(Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
grid.GetGridObject(gridPosition.x, gridPosition.y).SetTransform(builtTransform);
|
||||
grid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,6 +73,22 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
||||
}
|
||||
}else if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
GridObject gridObject = grid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition));
|
||||
PlacedObject placedObject = gridObject.GetPlacedObject();
|
||||
if (placedObject != null)
|
||||
{
|
||||
placedObject.DestroySelf();
|
||||
|
||||
List<Vector2Int> gridPositionList = placedObject.GetGridPositionList();
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
grid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
Assets/Scripts/PlacedObject.cs
Normal file
31
Assets/Scripts/PlacedObject.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlacedObject : MonoBehaviour
|
||||
{
|
||||
public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO.Dir dir, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
Transform placeObjectTransform = Instantiate(placedObjectTypeSO.prefab, worldPosition, Quaternion.identity);
|
||||
|
||||
PlacedObject placedObject = placeObjectTransform.GetComponent<PlacedObject>();
|
||||
placedObject.placedObjectTypeSO = placedObjectTypeSO;
|
||||
placedObject.origin = origin;
|
||||
placedObject.dir = dir;
|
||||
return placedObject;
|
||||
}
|
||||
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
Vector2Int origin;
|
||||
PlacedObjectTypeSO.Dir dir;
|
||||
|
||||
public List<Vector2Int> GetGridPositionList()
|
||||
{
|
||||
return placedObjectTypeSO.GetGridPositionList(origin, PlacedObjectTypeSO.Dir.Down);
|
||||
}
|
||||
|
||||
public void DestroySelf()
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlacedObject.cs.meta
Normal file
11
Assets/Scripts/PlacedObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 56fadc4f2b895ad46bda623c68601c7e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 313 B After Width: | Height: | Size: 351 B |
Reference in New Issue
Block a user