mirror of
https://github.com/DerTyp7/industrialize-unity.git
synced 2025-10-30 21:07:11 +01:00
CameraMovement
This commit is contained in:
95
Assets/Scripts/GridSystem/Grid.cs
Normal file
95
Assets/Scripts/GridSystem/Grid.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Grid<TGridObject>
|
||||
{
|
||||
int width, height;
|
||||
float cellSize;
|
||||
Vector3 originPosition;
|
||||
public TGridObject[,] gridArray;
|
||||
|
||||
bool showDebug = true;
|
||||
|
||||
public int GetWidth() => width;
|
||||
public int GetHeight() => height;
|
||||
public float GetCellSize() => cellSize;
|
||||
|
||||
public Grid(int _width, int _height, float _cellSize, Vector3 _originPosition, Func<Grid<TGridObject>, int, int, TGridObject> createGridObject)
|
||||
{
|
||||
width = _width;
|
||||
height = _height;
|
||||
cellSize = _cellSize;
|
||||
originPosition = _originPosition;
|
||||
|
||||
gridArray = new TGridObject[width, height];
|
||||
|
||||
for (int x = 0; x < gridArray.GetLength(0); x++)
|
||||
{
|
||||
for (int y = 0; y < gridArray.GetLength(1); y++)
|
||||
{
|
||||
gridArray[x, y] = createGridObject(this, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
if (showDebug)
|
||||
{
|
||||
for (int x = 0; x < gridArray.GetLength(0); x++)
|
||||
{
|
||||
for (int y = 0; y < gridArray.GetLength(1); y++)
|
||||
{
|
||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
|
||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
|
||||
}
|
||||
}
|
||||
Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
|
||||
Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Vector3 GetWorldPosition(int x, int y)
|
||||
{
|
||||
return new Vector3(x, y) * cellSize + originPosition;
|
||||
}
|
||||
|
||||
public void GetXY(Vector3 worldPosition, out int x, out int y)
|
||||
{
|
||||
x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
|
||||
y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
|
||||
}
|
||||
|
||||
public void SetGridObject(int x, int y, TGridObject value)
|
||||
{
|
||||
if (x >= 0 && y >= 0 && x < width && y < height)
|
||||
{
|
||||
gridArray[x, y] = value;
|
||||
Debug.Log(x.ToString() + " " + y.ToString() + " -> " + value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void SetGridObject(Vector3 worldPosition, TGridObject value)
|
||||
{
|
||||
int x, y;
|
||||
GetXY(worldPosition, out x, out y);
|
||||
SetGridObject(x, y, value);
|
||||
}
|
||||
|
||||
public TGridObject GetGridObject(int x, int y)
|
||||
{
|
||||
if (x >= 0 && y >= 0 && x < width && y < height)
|
||||
{
|
||||
return gridArray[x, y];
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(TGridObject);
|
||||
}
|
||||
}
|
||||
|
||||
public TGridObject GetGridObject(Vector3 worldPosition)
|
||||
{
|
||||
int x, y;
|
||||
GetXY(worldPosition, out x, out y);
|
||||
return GetGridObject(x, y);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GridSystem/Grid.cs.meta
Normal file
11
Assets/Scripts/GridSystem/Grid.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a09de9f61e5400543a4460d8bc195f98
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
237
Assets/Scripts/GridSystem/GridBuildingSystem.cs
Normal file
237
Assets/Scripts/GridSystem/GridBuildingSystem.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// Written with https://www.youtube.com/watch?v=dulosHPl82A
|
||||
public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
public static GridBuildingSystem instance;
|
||||
public Grid<GridObject> buildingGrid;
|
||||
|
||||
public int gridWidth;
|
||||
public int gridHeight;
|
||||
public float cellSize;
|
||||
|
||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||
Transform selectedGameObjectTransform;
|
||||
|
||||
public List<PlacedObjectTypeSO> DEBUG_OBJS = new List<PlacedObjectTypeSO>();
|
||||
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
bool isAccessable; // if true, can be placed on -> To limit the building area in the future
|
||||
Grid<GridObject> grid;
|
||||
PlacedObject placedObject;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y, bool _isAccessable = true) // FOR DEBUG TRUE
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
isAccessable = _isAccessable;
|
||||
}
|
||||
public void SetPlacedObject(PlacedObject newPlacedObject)
|
||||
{
|
||||
placedObject = newPlacedObject;
|
||||
Debug.Log("SetPlacedObject");
|
||||
}
|
||||
public PlacedObject GetPlacedObject() => placedObject;
|
||||
public void ClearPlacedObject() => placedObject = null;
|
||||
public void SetIsAccessable(bool _isAccessable) => isAccessable = _isAccessable;
|
||||
public void SwitchIsAccessable() => isAccessable = !isAccessable;
|
||||
public bool IsAccessable() => isAccessable;
|
||||
public bool CanBuild()
|
||||
{
|
||||
return placedObject == null && isAccessable;
|
||||
}
|
||||
}
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
else
|
||||
Destroy(gameObject);
|
||||
|
||||
buildingGrid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (selectedGameObjectTransform != null)
|
||||
{
|
||||
UpdateSelectedGameObject();
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.Alpha1))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[0]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha2))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[1]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha3))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[2]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha4))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[3]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha5))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[4]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha6))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[5]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha7))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[6]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha8))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[7]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha9))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[8]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void UpdateSelectedGameObject()
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
|
||||
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
|
||||
if (CanBuild(gridPositionList))
|
||||
{
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
|
||||
}
|
||||
else
|
||||
{
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
PlaceBuilding(selectedGameObjectTransform.position);
|
||||
}
|
||||
|
||||
|
||||
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
DeselectBuilding();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void DemolishBuilding(Vector3 position)
|
||||
{
|
||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||
PlacedObject placedObject = gridObject.GetPlacedObject();
|
||||
|
||||
if (placedObject != null)
|
||||
{
|
||||
placedObject.DestroySelf();
|
||||
|
||||
List<Vector2Int> gridPositionList = placedObject.GetGridPositionList();
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CanBuild(List<Vector2Int> gridPositionList)
|
||||
{
|
||||
bool canBuild = true;
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
if (!buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild())
|
||||
{
|
||||
// Cannot build here
|
||||
canBuild = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return canBuild;
|
||||
}
|
||||
|
||||
public GameObject PlaceBuilding(Vector3 position)
|
||||
{
|
||||
position = new Vector3(position.x, position.y);
|
||||
buildingGrid.GetXY(position, out int x, out int y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
|
||||
// DEBUG
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
Debug.Log(gridPosition);
|
||||
}
|
||||
|
||||
if (CanBuild(gridPositionList))
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), selectedPlacedObjectTypeSO);
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||
}
|
||||
return placedObject.gameObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + position);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
|
||||
// Delete all previous blueprints
|
||||
foreach (GameObject o in GameObject.FindGameObjectsWithTag("PlacedObject"))
|
||||
{
|
||||
if (o.GetComponent<PlacedObject>().GetIsBlueprint())
|
||||
{
|
||||
Destroy(o);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
selectedGameObjectTransform = Instantiate(placedObjectTypeSO.prefab, mousePosition, Quaternion.identity);
|
||||
selectedPlacedObjectTypeSO = placedObjectTypeSO;
|
||||
}
|
||||
|
||||
public void DeselectBuilding()
|
||||
{
|
||||
Destroy(selectedGameObjectTransform.gameObject);
|
||||
selectedPlacedObjectTypeSO = null;
|
||||
selectedGameObjectTransform = null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/GridSystem/GridBuildingSystem.cs.meta
Normal file
11
Assets/Scripts/GridSystem/GridBuildingSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1de0d91b0ca828941bdb4c60d0a0f154
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
24
Assets/Scripts/GridSystem/PathfindingSystem.cs
Normal file
24
Assets/Scripts/GridSystem/PathfindingSystem.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PathfindingSystem : MonoBehaviour
|
||||
{
|
||||
public static PathfindingSystem instance { get; private set; }
|
||||
public Pathfinding pathfinding;
|
||||
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
instance = this;
|
||||
|
||||
int gridWidth = GridBuildingSystem.instance.gridWidth;
|
||||
int gridHeight = GridBuildingSystem.instance.gridHeight;
|
||||
float cellSize = GridBuildingSystem.instance.cellSize;
|
||||
|
||||
pathfinding = new Pathfinding(gridWidth, gridHeight, cellSize);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GridSystem/PathfindingSystem.cs.meta
Normal file
11
Assets/Scripts/GridSystem/PathfindingSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d31ae6efb7031ec4e8b9eeb0ed466657
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
79
Assets/Scripts/GridSystem/PlacedObject.cs
Normal file
79
Assets/Scripts/GridSystem/PlacedObject.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
public abstract class PlacedObject : MonoBehaviour
|
||||
{
|
||||
public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
Transform placeObjectTransform = Instantiate(placedObjectTypeSO.prefab, worldPosition, Quaternion.identity);
|
||||
|
||||
PlacedObject placedObject = placeObjectTransform.GetComponent<PlacedObject>();
|
||||
placedObject.placedObjectTypeSO = placedObjectTypeSO;
|
||||
placedObject.origin = origin;
|
||||
|
||||
placedObject.OnPlace();
|
||||
placedObject.SetIsBlueprint(false);
|
||||
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
foreach(Vector2Int position in placedObject.GetGridPositionList())
|
||||
{
|
||||
Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return placedObject;
|
||||
}
|
||||
|
||||
public PlacedObjectTypeSO placedObjectTypeSO;
|
||||
Vector2Int origin;
|
||||
|
||||
[SerializeField] private bool isBlueprint = true;
|
||||
|
||||
public bool GetIsBlueprint() => isBlueprint;
|
||||
public void SetIsBlueprint(bool newIsBlueprint)
|
||||
{
|
||||
|
||||
if (GetComponent<Collider2D>() != null)
|
||||
{
|
||||
if (newIsBlueprint)
|
||||
{
|
||||
GetComponent<Collider2D>().enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<Collider2D>().enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
isBlueprint = newIsBlueprint;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
SetIsBlueprint(true);
|
||||
}
|
||||
|
||||
public abstract void OnPlace();
|
||||
|
||||
public List<Vector2Int> GetGridPositionList()
|
||||
{
|
||||
return placedObjectTypeSO.GetGridPositionList(origin);
|
||||
}
|
||||
|
||||
public void DestroySelf()
|
||||
{
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
foreach (Vector2Int position in GetGridPositionList())
|
||||
{
|
||||
Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(false);
|
||||
}
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GridSystem/PlacedObject.cs.meta
Normal file
11
Assets/Scripts/GridSystem/PlacedObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 024343b97b688a24e8075e525dee8048
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Assets/Scripts/GridSystem/PlacedObjectTypeSO.cs
Normal file
31
Assets/Scripts/GridSystem/PlacedObjectTypeSO.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(menuName = "ScriptableObjects/PlacedObjectTypeSO")]
|
||||
public class PlacedObjectTypeSO : ScriptableObject
|
||||
{
|
||||
public string placedObjectID;
|
||||
public string nameString;
|
||||
public Transform prefab;
|
||||
public int width;
|
||||
public int height;
|
||||
public bool isWalkable = false;
|
||||
public Sprite iconSprite;
|
||||
|
||||
public List<Vector2Int> GetGridPositionList(Vector2Int offset)
|
||||
{
|
||||
List<Vector2Int> gridPositionList = new List<Vector2Int>();
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
gridPositionList.Add(offset + new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
return gridPositionList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/GridSystem/PlacedObjectTypeSO.cs.meta
Normal file
11
Assets/Scripts/GridSystem/PlacedObjectTypeSO.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 632704925c0ac0848a70bc1c20e19aba
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user