mirror of
https://github.com/DerTyp7/industrialize-unity.git
synced 2025-10-30 21:07:11 +01:00
conveyor placement rework
This commit is contained in:
@@ -199,13 +199,24 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
|
|
||||||
ClearConveyorPath();
|
ClearConveyorPath();
|
||||||
|
|
||||||
|
List<Vector2Int> pathPoints = VectorDrawing.FindVectorPath(conveyorStartPosition, endPosition);
|
||||||
|
Debug.Log(pathPoints.Count);
|
||||||
|
foreach (Vector2Int position in pathPoints)
|
||||||
|
{
|
||||||
|
Debug.Log("TEST");
|
||||||
|
Debug.Log(position);
|
||||||
|
GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(position.x, position.y), Quaternion.identity);
|
||||||
|
placingConveyorBlueprints.Add(conveyorBlueprint);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
foreach (Vector3 position in Pathfinding.instance.FindPath(conveyorStartPosition, endPosition, true))
|
foreach (Vector3 position in Pathfinding.instance.FindPath(conveyorStartPosition, endPosition, true))
|
||||||
{
|
{
|
||||||
buildingGrid.GetXY(position, out x, out y);
|
buildingGrid.GetXY(position, out x, out y);
|
||||||
|
|
||||||
GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(x, y), Quaternion.identity);
|
GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(x, y), Quaternion.identity);
|
||||||
placingConveyorBlueprints.Add(conveyorBlueprint);
|
placingConveyorBlueprints.Add(conveyorBlueprint);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
Assets/Scripts/GridSystem/VectorDrawing.cs
Normal file
58
Assets/Scripts/GridSystem/VectorDrawing.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class VectorDrawing : MonoBehaviour
|
||||||
|
{
|
||||||
|
public static float DiagonalDistance(Vector3 startWorldPosition, Vector3 endWorldPosition)
|
||||||
|
{
|
||||||
|
float dx = endWorldPosition.x - startWorldPosition.x;
|
||||||
|
float dy = endWorldPosition.y - startWorldPosition.y;
|
||||||
|
|
||||||
|
return Mathf.Sqrt(dx * dx + dy * dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Vector2Int> FindVectorPath(Vector3 startWorldPosition, Vector3 endWorldPosition)
|
||||||
|
{
|
||||||
|
List<Vector2Int> points = new List<Vector2Int>();
|
||||||
|
Grid<PathNode> grid = Pathfinding.instance.GetGrid();
|
||||||
|
float N = DiagonalDistance(startWorldPosition, endWorldPosition);
|
||||||
|
|
||||||
|
grid.GetXY(startWorldPosition, out int startX, out int startY);
|
||||||
|
grid.GetXY(endWorldPosition, out int endX, out int endY);
|
||||||
|
|
||||||
|
points.Add(new Vector2Int(startX, startY));
|
||||||
|
|
||||||
|
for (int i = 0; i < N + 1; i++) // N+1 for endWorldPosition
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
if (i < N)
|
||||||
|
{
|
||||||
|
float t = (float)i / N;
|
||||||
|
Vector3 point = Vector3.Lerp(startWorldPosition, endWorldPosition, t);
|
||||||
|
|
||||||
|
grid.GetXY(point, out x, out y);
|
||||||
|
}
|
||||||
|
else // is endWorldPosition
|
||||||
|
{
|
||||||
|
x = endX;
|
||||||
|
y = endY;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Sample points -> no diagonal connection
|
||||||
|
if (i != 0)
|
||||||
|
{
|
||||||
|
if (x != points[points.Count - 1].x && y != points[points.Count - 1].y)
|
||||||
|
{
|
||||||
|
points.Add(new Vector2Int(x, points[points.Count - 1].y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
points.Add(new Vector2Int(x, y));
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/GridSystem/VectorDrawing.cs.meta
Normal file
11
Assets/Scripts/GridSystem/VectorDrawing.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 474b4c8675764eb4bba214bf41695edd
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -126,7 +126,7 @@ public class Pathfinding
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<PathNode> GetNeighbourList(PathNode currentNode)
|
public List<PathNode> GetNeighbourList(PathNode currentNode)
|
||||||
{
|
{
|
||||||
List<PathNode> neighbourList = new List<PathNode>();
|
List<PathNode> neighbourList = new List<PathNode>();
|
||||||
|
|
||||||
@@ -175,6 +175,14 @@ public class Pathfinding
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int CalculateDistance(PathNode a, PathNode b)
|
||||||
|
{
|
||||||
|
int xDistance = Mathf.Abs(a.x - b.x);
|
||||||
|
int yDistance = Mathf.Abs(a.y - b.y);
|
||||||
|
int remaining = Mathf.Abs(xDistance - yDistance);
|
||||||
|
return Mathf.Min(xDistance, yDistance) + remaining;
|
||||||
|
}
|
||||||
|
|
||||||
private int CalculateDistanceCost(PathNode a, PathNode b)
|
private int CalculateDistanceCost(PathNode a, PathNode b)
|
||||||
{
|
{
|
||||||
int xDistance = Mathf.Abs(a.x - b.x);
|
int xDistance = Mathf.Abs(a.x - b.x);
|
||||||
|
|||||||
Reference in New Issue
Block a user