This commit is contained in:
Janis
2022-06-05 16:08:54 +02:00
parent 21e915bef3
commit 0a55ab6e93
2 changed files with 130 additions and 108 deletions

View File

@@ -4,13 +4,11 @@ using UnityEngine;
public class ConveyorPO : PlacedObject
{
[SerializeField] private float speed = 0.3f;
[SerializeField] private List<Sprite> sprites = new List<Sprite>(); // 0 = Up, 1 = Right, 2 = Down, 3 = Left
[SerializeField] private ConveyorPO previousConveyor;
[SerializeField] private ConveyorPO nextConveyor;
public ConveyorPO previousConveyor;
public ConveyorPO nextConveyor;
public List<Sprite> sprites = new List<Sprite>(); // 0 = Up, 1 = Right, 2 = Down, 3 = Left
#region Getters & Setters
public float GetSpeed()
{
if (nextConveyor != null)
@@ -23,6 +21,37 @@ public class ConveyorPO : PlacedObject
}
}
public ConveyorPO GetPreviousConveyor()
{
return previousConveyor;
}
public ConveyorPO GetNextConveyor()
{
return nextConveyor;
}
public void SetPreviousConveyor(ConveyorPO newPreviousConveyor)
{
if (newPreviousConveyor != nextConveyor)
{
previousConveyor = newPreviousConveyor;
}
UpdateChain();
}
public void SetNextConveyor(ConveyorPO newNextConveyor)
{
if (newNextConveyor != previousConveyor)
{
nextConveyor = newNextConveyor;
}
UpdateChain();
}
#endregion
// Gets all conveyors around this conveyor (UP RIGHT DOWN LEFT)
private List<ConveyorPO> GetConveyorsAround()
{
List<ConveyorPO> conveyors = new List<ConveyorPO>();
@@ -32,82 +61,80 @@ public class ConveyorPO : PlacedObject
foreach (GridBuildingSystem.GridObject gridObject in gridObjects)
{
if (gridObject.GetPlacedObject() != null)
if (gridObject.GetPlacedObject() != null) // If there is a placed object
{
PlacedObject placedObject = gridObject.GetPlacedObject();
if (placedObject.gameObject.GetComponent<ConveyorPO>() != null)
if (placedObject.gameObject.GetComponent<ConveyorPO>() != null) // If this placed object is a conveyor
{
conveyors.Add(placedObject.gameObject.GetComponent<ConveyorPO>());
}
}
}
return conveyors;
}
// Integrates this conveyor with the other conveyors around it
private void SetConveyorChain()
{
// TODO Last conveyor does not connect to first conveyor (circle)
List<ConveyorPO> possibleNextConveyors = GetConveyorsAround();
List<ConveyorPO> possiblePreviousConveyors = GetConveyorsAround();
List<ConveyorPO> possibleNextConveyors = GetConveyorsAround(); // Gets all possible next conveyors for this conveyor
List<ConveyorPO> possiblePreviousConveyors = GetConveyorsAround(); // Gets all possible previous conveyors for this conveyor
// If this conveyor is already fully integraded in the chain -> return
if (previousConveyor != null && nextConveyor != null)
{
return;
}
foreach (ConveyorPO c in GetConveyorsAround())
// Sort all conveyors around
foreach (ConveyorPO otherConveyor in GetConveyorsAround())
{
if (c == nextConveyor || c == previousConveyor || c == this || c.previousConveyor == this || c.nextConveyor == this)
// Even if this gets checked in multiple palces, I just want to be sure ;)
if (otherConveyor == nextConveyor || otherConveyor == previousConveyor || otherConveyor.GetPreviousConveyor() == this || otherConveyor.GetNextConveyor() == this)
{
continue;
}
else
{
if (c.nextConveyor == null)
if (otherConveyor.GetNextConveyor() == null) // If other conveyor is able to have a nextConveyor
{
if (nextConveyor != c)
if (nextConveyor != otherConveyor) // If this conveyor is not already connected to the other conveyor
{
possiblePreviousConveyors.Add(c);
possiblePreviousConveyors.Add(otherConveyor);
}
}
if (c.previousConveyor == null)
if (otherConveyor.GetPreviousConveyor() == null) // If other conveyor is able to have a previousConveyor
{
if (previousConveyor != c)
if (previousConveyor != otherConveyor) // If this conveyor is not already connected to the other conveyor
{
possibleNextConveyors.Add(c);
possibleNextConveyors.Add(otherConveyor);
}
}
}
}
// If there is a possible previous conveyor and this conveyor is able to have a previousConveyor
if (possiblePreviousConveyors.Count > 0 && previousConveyor == null)
{
Debug.Log("PREV " + possiblePreviousConveyors.Count);
foreach (ConveyorPO c in possiblePreviousConveyors)
foreach (ConveyorPO otherConveyor in possiblePreviousConveyors)
{
if (c.previousConveyor != this && c.nextConveyor == null)
if (otherConveyor.GetPreviousConveyor() != this && otherConveyor.GetNextConveyor() == null) // If other conveyor is not already connected to this conveyor
{
previousConveyor = c; // Maybe prioritize Conveyor which is already in a chain?
previousConveyor.nextConveyor = this;
previousConveyor.UpdateChain();
previousConveyor = otherConveyor; // Maybe prioritize Conveyor which is already in a chain?
previousConveyor.SetNextConveyor(this);
break;
}
}
}
// If there is a possible next conveyor and this conveyor is able to have a nextConveyor
if (possibleNextConveyors.Count > 0 && nextConveyor == null)
{
Debug.Log("Next " + possibleNextConveyors.Count);
foreach (ConveyorPO c in possibleNextConveyors)
foreach (ConveyorPO otherConveyor in possibleNextConveyors)
{
if (c.nextConveyor != this && c.previousConveyor == null)
if (otherConveyor.GetNextConveyor() != this && otherConveyor.GetPreviousConveyor() == null) // If other conveyor is not already connected to this conveyor
{
nextConveyor = c; // Maybe prioritize Conveyor which is already in a chain?
nextConveyor.previousConveyor = this;
nextConveyor.UpdateChain();
nextConveyor = otherConveyor; // Maybe prioritize Conveyor which is already in a chain?
nextConveyor.SetPreviousConveyor(this);
break;
}
}