Beispiel #1
0
    /// <summary>
    /// I'm sure someone's done it better.  Don't think about it too hard.
    /// </summary>
    public void FixedUpdate()
    {
        if (rotationState == Rotation.Vertical)
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] == null)
                {
                    break;
                }
                Vector3 temp   = items[i].transform.position;
                float   deltaX = temp.x - this.transform.position.x;
                if (deltaX > 2 * conveyorBeltSpeed)
                {
                    temp.x -= 2 * conveyorBeltSpeed;
                }
                else if (deltaX < -2 * conveyorBeltSpeed)
                {
                    temp.x += 2 * conveyorBeltSpeed;
                }
                else
                {
                    temp.x = this.transform.position.x;
                }
                temp.y += conveyorBeltSpeed * velocityMultiplier;

                if (nextBelt == null && velocityMultiplier * (temp.y - this.transform.position.y) >= beltSpacing)
                {
                    temp.y = this.transform.position.y + velocityMultiplier * beltSpacing;
                }
                else if (Mathf.Abs(temp.y - this.transform.position.y) > 0.5f) //will do nothing if item can't be inserted to next belt
                {
                    if (nextBelt.GetOutputOrientation() != this.outputOrientation && nextBelt.TryInsertHorizontal(items[i], temp.x))
                    {
                        items[i].transform.position = temp;
                        for (int j = i; j < items.Length - 1; j++)
                        {
                            items[j] = items[j + 1];
                        }
                        items[items.Length - 1] = null;
                        heldItems--;
                    }
                    if (nextBelt.TryInsertVertical(items[i], temp.y))
                    {
                        items[i].transform.position = temp;
                        for (int j = i; j < items.Length - 1; j++)
                        {
                            items[j] = items[j + 1];
                        }
                        items[items.Length - 1] = null;
                        heldItems--;
                    }
                }
                else if ((i == 0 && (nextBelt == null || !nextBelt.CheckCollisionVertical(temp.y))) ||
                         (i - 1 >= 0 && Mathf.Abs(temp.y - items[i - 1].transform.position.y) >= beltSpacing)) //moves item forward if it isn't outside the belt (need to check against items in front)
                {
                    items[i].transform.position = temp;
                }
            }
        }
        else
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] == null)
                {
                    break;
                }
                Vector3 temp   = items[i].transform.position;
                float   deltaY = temp.y - this.transform.position.y;
                if (deltaY > 2 * conveyorBeltSpeed)
                {
                    temp.y -= 2 * conveyorBeltSpeed;
                }
                else if (deltaY < -2 * conveyorBeltSpeed)
                {
                    temp.y += 2 * conveyorBeltSpeed;
                }
                else
                {
                    temp.y = this.transform.position.y;
                }
                temp.x += conveyorBeltSpeed * velocityMultiplier;

                if (nextBelt == null && velocityMultiplier * (temp.x - this.transform.position.x) >= beltSpacing)
                {
                    temp.x = this.transform.position.x + velocityMultiplier * beltSpacing;
                }
                else if (Mathf.Abs(temp.x - this.transform.position.x) > 0.5f) //will do nothing if item can't be inserted to next belt
                {
                    if (nextBelt.GetOutputOrientation() != this.outputOrientation && nextBelt.TryInsertVertical(items[i], temp.y))
                    {
                        items[i].transform.position = temp;
                        for (int j = i; j < items.Length - 1; j++)
                        {
                            items[j] = items[j + 1];
                        }
                        items[items.Length - 1] = null;
                        heldItems--;
                    }
                    if (nextBelt.TryInsertHorizontal(items[i], temp.x))
                    {
                        items[i].transform.position = temp;
                        for (int j = i; j < items.Length - 1; j++)
                        {
                            items[j] = items[j + 1];
                        }
                        items[items.Length - 1] = null;
                        heldItems--;
                    }
                }
                else if ((i == 0 && (nextBelt == null || !nextBelt.CheckCollisionHorizontal(temp.x))) ||
                         (i - 1 >= 0 && Mathf.Abs(temp.x - items[i - 1].transform.position.x) >= beltSpacing)) //moves item forward if it isn't outside the belt (need to check against items in front)
                {
                    items[i].transform.position = temp;
                }
            }
        }
        if (bufferedInput != null)
        {
            items[heldItems] = bufferedInput;
            heldItems++;
            bufferedInput = null;
        }
    }