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 public class ConveyorPO : PlacedObject
{ {
[SerializeField] private float speed = 0.3f; [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; #region Getters & Setters
public ConveyorPO nextConveyor;
public List<Sprite> sprites = new List<Sprite>(); // 0 = Up, 1 = Right, 2 = Down, 3 = Left
public float GetSpeed() public float GetSpeed()
{ {
if (nextConveyor != null) 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() private List<ConveyorPO> GetConveyorsAround()
{ {
List<ConveyorPO> conveyors = new List<ConveyorPO>(); List<ConveyorPO> conveyors = new List<ConveyorPO>();
@@ -32,82 +61,80 @@ public class ConveyorPO : PlacedObject
foreach (GridBuildingSystem.GridObject gridObject in gridObjects) foreach (GridBuildingSystem.GridObject gridObject in gridObjects)
{ {
if (gridObject.GetPlacedObject() != null) if (gridObject.GetPlacedObject() != null) // If there is a placed object
{ {
PlacedObject placedObject = gridObject.GetPlacedObject(); 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>()); conveyors.Add(placedObject.gameObject.GetComponent<ConveyorPO>());
} }
} }
} }
return conveyors; return conveyors;
} }
// Integrates this conveyor with the other conveyors around it
private void SetConveyorChain() private void SetConveyorChain()
{ {
// TODO Last conveyor does not connect to first conveyor (circle) List<ConveyorPO> possibleNextConveyors = GetConveyorsAround(); // Gets all possible next conveyors for this conveyor
List<ConveyorPO> possibleNextConveyors = GetConveyorsAround(); List<ConveyorPO> possiblePreviousConveyors = GetConveyorsAround(); // Gets all possible previous conveyors for this conveyor
List<ConveyorPO> possiblePreviousConveyors = GetConveyorsAround();
// If this conveyor is already fully integraded in the chain -> return
if (previousConveyor != null && nextConveyor != null) if (previousConveyor != null && nextConveyor != null)
{
return; 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; continue;
} }
else 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) if (possiblePreviousConveyors.Count > 0 && previousConveyor == null)
{ {
Debug.Log("PREV " + possiblePreviousConveyors.Count); foreach (ConveyorPO otherConveyor in possiblePreviousConveyors)
foreach (ConveyorPO c 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 = otherConveyor; // Maybe prioritize Conveyor which is already in a chain?
previousConveyor.nextConveyor = this; previousConveyor.SetNextConveyor(this);
previousConveyor.UpdateChain();
break; break;
} }
} }
} }
// If there is a possible next conveyor and this conveyor is able to have a nextConveyor
if (possibleNextConveyors.Count > 0 && nextConveyor == null) if (possibleNextConveyors.Count > 0 && nextConveyor == null)
{ {
Debug.Log("Next " + possibleNextConveyors.Count); foreach (ConveyorPO otherConveyor in possibleNextConveyors)
foreach (ConveyorPO c 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 = otherConveyor; // Maybe prioritize Conveyor which is already in a chain?
nextConveyor.previousConveyor = this; nextConveyor.SetPreviousConveyor(this);
nextConveyor.UpdateChain();
break; break;
} }
} }

View File

@@ -5,18 +5,19 @@ public abstract class ItemObject : MonoBehaviour
public int maxStackSize = 5; public int maxStackSize = 5;
public int stackSize = 1; public int stackSize = 1;
public ItemSO itemSO; public ItemSO itemSO;
public float timeAlive = 0.0f; [SerializeField] private float timeAlive = 0.0f;
public float maxTimeAlive = 600.0f; [SerializeField] private float maxTimeAlive = 600.0f;
public float maxTimeWithoutConveyor = 10.0f; [SerializeField] private float maxTimeWithoutConveyor = 10.0f;
public float timeWithoutConveyor = 0.0f; [SerializeField] private float timeWithoutConveyor = 0.0f;
private ConveyorPO lastTouchedConveyor; // current conveyor but not set to null [SerializeField] private ConveyorPO lastTouchedConveyor; // current conveyor but not set to null
private float conveyorSpeed = 0.0f; [SerializeField] private float conveyorSpeed = 0.0f;
private Vector2 conveyorDirection = Vector2.zero; [SerializeField] private Vector2 conveyorDirection = Vector2.zero;
[SerializeField] private ConveyorPO currentConveyor;
public abstract void OnSpawn(); public abstract void OnSpawn();
private ConveyorPO currentConveyor;
private Rigidbody2D rb; private Rigidbody2D rb;
@@ -53,107 +54,101 @@ public abstract class ItemObject : MonoBehaviour
} }
} }
// Gets the temporary pivot point based on the conveyor direction
private Vector3 GetTempPivotPosition(Vector2 conveyorDirection)
{
Vector3 tempPivotPosition = transform.position;
if (conveyorDirection == Vector2.up) // Up
{
Debug.Log("Up");
tempPivotPosition = transform.position + new Vector3(transform.localScale.x / 2, 0, 0);
}
if (conveyorDirection == Vector2.right) // Right
{
Debug.Log("Right");
tempPivotPosition = transform.position + new Vector3(0, transform.localScale.y / 2, 0);
}
if (conveyorDirection == Vector2.down) // Down
{
Debug.Log("Down");
tempPivotPosition = transform.position + new Vector3(transform.localScale.x / 2, transform.localScale.y, 0);
}
if (conveyorDirection == Vector2.left) // Left
{
Debug.Log("Left");
tempPivotPosition = transform.position + new Vector3(transform.localScale.x, transform.localScale.y / 2, 0);
}
return tempPivotPosition;
}
void Update() void Update()
{ {
GridBuildingSystem gridBuildingSystem = GridBuildingSystem.instance;
# region TimeAlive
timeAlive += Time.deltaTime; timeAlive += Time.deltaTime;
if (timeAlive > maxTimeAlive) if (timeAlive > maxTimeAlive)
{ {
Despawn(); Despawn();
} }
#endregion
// Problem: Pivot point of an item is bottom-left, so its not proberly aligned with the conveyor #region ConveyorBehaviour and Movement
GridBuildingSystem gridBuildingSystem = GridBuildingSystem.instance; // TODO Freeze rigibody rotations based on direction
//* Position has to change depending on which direction the item is heading, in order to stay on the conveyor //! DEBUG LINE TO SEE TEMP PIVOT POSITION
Vector3 tempPivotPosition = transform.position; Debug.DrawLine(GetTempPivotPosition(conveyorDirection), Vector3.zero, Color.red);
if (currentConveyor != null)
{
// TODO Freeze rigibody rotations based on direction
//* KEEP IN MIND -> PIVOT POINT IS BOTTOM-LEFT
// If direction changes
if (conveyorDirection != currentConveyor.GetDirection())
{
// Realign
gridBuildingSystem.buildingGrid.GetXY(lastTouchedConveyor.gameObject.transform.position, out int x, out int y);
transform.position = gridBuildingSystem.buildingGrid.GetWorldPosition(x, y);
conveyorDirection = currentConveyor.GetDirection();
}
if (conveyorDirection.y > 0) // Top
{
Debug.Log("Heading top");
tempPivotPosition = transform.position + new Vector3(transform.localScale.x / 2, 0, 0);
}
if (conveyorDirection.x > 0) // Right
{
Debug.Log("Heading right");
tempPivotPosition = transform.position + new Vector3(0, transform.localScale.y / 2, 0);
}
if (conveyorDirection.y < 0) // Bottom
{
Debug.Log("Heading bottom");
tempPivotPosition = transform.position + new Vector3(transform.localScale.x / 2, transform.localScale.y, 0);
}
if (conveyorDirection.x < 0) // Left
{
Debug.Log("Heading left");
tempPivotPosition = transform.position + new Vector3(transform.localScale.x, transform.localScale.y / 2, 0);
}
Debug.DrawLine(tempPivotPosition, Vector3.zero, Color.red);
}
gridBuildingSystem.buildingGrid.GetXY(tempPivotPosition, out int tempPivotX, out int tempPivotY);
gridBuildingSystem.buildingGrid.GetXY(GetTempPivotPosition(conveyorDirection), out int tempPivotX, out int tempPivotY);
GridBuildingSystem.GridObject gridObject = gridBuildingSystem.buildingGrid.GetGridObject(tempPivotX, tempPivotY); GridBuildingSystem.GridObject gridObject = gridBuildingSystem.buildingGrid.GetGridObject(tempPivotX, tempPivotY);
if (gridObject.GetPlacedObject() != null) if (gridObject.GetPlacedObject() != null) // If there is a placed object
{ {
if (gridObject.GetPlacedObject().gameObject.GetComponent<ConveyorPO>()) if (gridObject.GetPlacedObject().gameObject.GetComponent<ConveyorPO>()) // If this placed object is conveyor
{ {
timeWithoutConveyor = 0.0f; timeWithoutConveyor = 0.0f; // reset time without conveyor
currentConveyor = gridObject.GetPlacedObject().gameObject.GetComponent<ConveyorPO>(); currentConveyor = gridObject.GetPlacedObject().gameObject.GetComponent<ConveyorPO>(); // set current conveyor to this conveyor
lastTouchedConveyor = currentConveyor; lastTouchedConveyor = currentConveyor; // set last touched conveyor to this conveyor
conveyorSpeed = currentConveyor.GetSpeed(); conveyorSpeed = currentConveyor.GetSpeed(); // set conveyor speed to this conveyor
//* If direction changes
if (conveyorDirection != currentConveyor.GetDirection())
{
// Realign the item to the grid/conveyor
gridBuildingSystem.buildingGrid.GetXY(lastTouchedConveyor.gameObject.transform.position, out int x, out int y);
transform.position = gridBuildingSystem.buildingGrid.GetWorldPosition(x, y);
conveyorDirection = currentConveyor.GetDirection();
}
} }
else else // Not on a conveyor
{ {
UpdateWithoutConveyor(); UpdateWithoutConveyor();
} }
} }
else else // Not even on a placed object -> not on a conveyor
{ {
UpdateWithoutConveyor(); UpdateWithoutConveyor();
} }
#endregion
} }
private void UpdateWithoutConveyor() private void UpdateWithoutConveyor() // Handles the item when it is not on a conveyor
{ {
currentConveyor = null; currentConveyor = null;
timeWithoutConveyor += Time.deltaTime; timeWithoutConveyor += Time.deltaTime;
if (timeWithoutConveyor > maxTimeWithoutConveyor) if (timeWithoutConveyor > maxTimeWithoutConveyor)
{ {
Debug.Log("Not on conveyor -> DESPAWN");
Despawn(); Despawn();
} }
} }
void FixedUpdate() void FixedUpdate()
{ {
if (currentConveyor != null) rb.MovePosition(rb.position + (conveyorDirection * conveyorSpeed) * Time.fixedDeltaTime);
{
rb.MovePosition(rb.position + (conveyorDirection * conveyorSpeed) * Time.fixedDeltaTime);
}
else
{
// TODO Interrupt all movement
}
} }
} }