mirror of
https://github.com/DerTyp7/industrialize-unity.git
synced 2025-10-30 21:07:11 +01:00
new init
This commit is contained in:
@@ -14,20 +14,25 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||
Transform selectedGameObjectTransform;
|
||||
|
||||
Vector3 conveyorStartPosition;
|
||||
List<GameObject> placingConveyorBlueprints = new List<GameObject>();
|
||||
bool isPlacingConveyor = false;
|
||||
|
||||
public Color cannotBuildColor; // Maybe put this into every single PlacedObjectTypeSO for better customized use
|
||||
|
||||
// Debug
|
||||
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
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y) // FOR DEBUG TRUE
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
isAccessable = _isAccessable;
|
||||
}
|
||||
public void SetPlacedObject(PlacedObject newPlacedObject)
|
||||
{
|
||||
@@ -36,14 +41,13 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
}
|
||||
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;
|
||||
return placedObject == null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
@@ -60,60 +64,43 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
if (selectedGameObjectTransform != null)
|
||||
{
|
||||
UpdateSelectedGameObject();
|
||||
if (selectedPlacedObjectTypeSO.placedObjectCategory == PlacedObjectTypeSO.PlacedObjectCategory.BUILDING)
|
||||
{
|
||||
UpdateSelectedBuilding();
|
||||
}
|
||||
|
||||
if (selectedPlacedObjectTypeSO.placedObjectCategory == PlacedObjectTypeSO.PlacedObjectCategory.CONVEYOR)
|
||||
{
|
||||
UpdateSelectedConveyor();
|
||||
}
|
||||
}
|
||||
|
||||
#region debug
|
||||
// DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.Alpha1))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[0]);
|
||||
SelectPlacedObjectTypeSO(DEBUG_OBJS[0]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha2))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[1]);
|
||||
SelectPlacedObjectTypeSO(DEBUG_OBJS[1]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha3))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[2]);
|
||||
SelectPlacedObjectTypeSO(DEBUG_OBJS[2]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha4))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[3]);
|
||||
SelectPlacedObjectTypeSO(DEBUG_OBJS[3]);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha5))
|
||||
{
|
||||
SelectBuilding(DEBUG_OBJS[4]);
|
||||
SelectPlacedObjectTypeSO(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]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
|
||||
void UpdateSelectedGameObject()
|
||||
void UpdateSelectedBuilding()
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
@@ -134,18 +121,128 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
PlaceBuilding(selectedGameObjectTransform.position);
|
||||
PlacePlacedObjectTypeSO(selectedGameObjectTransform.position, selectedPlacedObjectTypeSO);
|
||||
}
|
||||
|
||||
|
||||
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
DeselectBuilding();
|
||||
DeselectPlacedObjectTypeSO();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#region ConveyorPlacing
|
||||
void UpdateSelectedConveyor()
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
if (isPlacingConveyor == false)
|
||||
{
|
||||
conveyorStartPosition = buildingGrid.GetWorldPosition(x, y);
|
||||
isPlacingConveyor = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ConveyorCanBuild())
|
||||
{
|
||||
PlaceConveyorPath();
|
||||
isPlacingConveyor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DrawConveyorPath();
|
||||
|
||||
if (ConveyorCanBuild())
|
||||
{
|
||||
RecolorConveyorPath(Color.white);
|
||||
}
|
||||
else
|
||||
{
|
||||
RecolorConveyorPath(cannotBuildColor);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
ClearConveyorPath();
|
||||
DeselectPlacedObjectTypeSO();
|
||||
}
|
||||
}
|
||||
|
||||
void PlaceConveyorPath()
|
||||
{
|
||||
if (placingConveyorBlueprints.Count > 0)
|
||||
{
|
||||
foreach (GameObject blueprint in placingConveyorBlueprints)
|
||||
{
|
||||
PlacePlacedObjectTypeSO(blueprint.transform.position, blueprint.gameObject.GetComponent<PlacedObject>().placedObjectTypeSO);
|
||||
}
|
||||
ClearConveyorPath();
|
||||
}
|
||||
}
|
||||
|
||||
void ClearConveyorPath()
|
||||
{
|
||||
if (placingConveyorBlueprints.Count > 0)
|
||||
{
|
||||
foreach (GameObject blueprint in placingConveyorBlueprints)
|
||||
{
|
||||
Destroy(blueprint);
|
||||
}
|
||||
placingConveyorBlueprints.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
void DrawConveyorPath()
|
||||
{
|
||||
if (isPlacingConveyor == true)
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
Vector3 endPosition = buildingGrid.GetWorldPosition(x, y);
|
||||
|
||||
ClearConveyorPath();
|
||||
|
||||
foreach (Vector3 position in Pathfinding.instance.FindPath(conveyorStartPosition, endPosition, true))
|
||||
{
|
||||
buildingGrid.GetXY(position, out x, out y);
|
||||
|
||||
GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(x, y), Quaternion.identity);
|
||||
placingConveyorBlueprints.Add(conveyorBlueprint);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void RecolorConveyorPath(Color color)
|
||||
{
|
||||
foreach (GameObject blueprint in placingConveyorBlueprints)
|
||||
{
|
||||
blueprint.GetComponent<SpriteRenderer>().color = color;
|
||||
}
|
||||
}
|
||||
bool ConveyorCanBuild()
|
||||
{
|
||||
bool canBuild = true;
|
||||
|
||||
foreach (GameObject blueprint in placingConveyorBlueprints)
|
||||
{
|
||||
buildingGrid.GetXY(blueprint.transform.position, out int x, out int y);
|
||||
|
||||
List<Vector2Int> gridPositionList = blueprint.GetComponent<PlacedObject>().placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
if (CanBuild(gridPositionList) == false)
|
||||
{
|
||||
canBuild = false;
|
||||
Debug.Log("Cannot build conveyor here");
|
||||
}
|
||||
}
|
||||
return canBuild;
|
||||
}
|
||||
#endregion
|
||||
public void DemolishBuilding(Vector3 position)
|
||||
{
|
||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||
@@ -179,12 +276,12 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
return canBuild;
|
||||
}
|
||||
|
||||
public GameObject PlaceBuilding(Vector3 position)
|
||||
public GameObject PlacePlacedObjectTypeSO(Vector3 position, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
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));
|
||||
List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
|
||||
// DEBUG
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
@@ -194,7 +291,7 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
if (CanBuild(gridPositionList))
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), selectedPlacedObjectTypeSO);
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), placedObjectTypeSO);
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
@@ -209,7 +306,7 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
|
||||
public void SelectPlacedObjectTypeSO(PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
|
||||
// Delete all previous blueprints
|
||||
@@ -226,12 +323,12 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
selectedPlacedObjectTypeSO = placedObjectTypeSO;
|
||||
}
|
||||
|
||||
public void DeselectBuilding()
|
||||
public void DeselectPlacedObjectTypeSO()
|
||||
{
|
||||
Destroy(selectedGameObjectTransform.gameObject);
|
||||
selectedPlacedObjectTypeSO = null;
|
||||
selectedGameObjectTransform = null;
|
||||
isPlacingConveyor = false;
|
||||
placingConveyorBlueprints.Clear();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user