Пример #1
0
    public void MoveItem(Vector3Int origin, Direction2D direction, Transform item)
    {
        ItemMovement2D movement = new ItemMovement2D(origin, direction, item);

        ItemMovements.Add(movement);
        movementLookup[origin] = movement;

        // Check for special case of two conveyors facing each other
        if (movementLookup.TryGetValue(movement.Destination, out var destMovement) && destMovement.Destination == movement.Origin)
        {
            BlockMovement(movement.Origin);
            BlockMovement(movement.Destination);
        }
    }
Пример #2
0
    // Evaluate movements & Write output
    void LateUpdate()
    {
        if (!TickInfo.Ticking)
        {
            return;
        }

        // Evaluate movements
        foreach (var movement in ItemMovements)
        {
            // If there is no belt in the destination, we're blocked
            if (!belts.ContainsKey(movement.Destination))
            {
                BlockMovement(movement.Origin);
                continue;
            }

            // If there is a belt, check for blocking
            if (movementLookup.TryGetValue(movement.Destination, out var aheadMovement))
            {
                if (aheadMovement.Blocked)
                {
                    //print("Ahead is blocked (origin: " + movement.Origin + ")");
                    BlockMovement(movement.Origin);
                }
                else
                {
                    //print("Adding dependency from " + movement.Origin + " to " + movement.Destination);
                    movementDependency.Add(movement.Origin, movement.Destination);
                }
            }

            // Check priority
            if (!movement.Blocked)
            {
                var center     = movement.Destination;
                var centerBelt = belts[center];
                //print($"Center: {center}");

                // Get surrounding positions in order of priority
                var behind           = center + centerBelt.Direction.Opposite().Vector3Int();
                var clockwise        = center + centerBelt.Direction.Clockwise().Vector3Int();
                var counterClockwise = center + centerBelt.Direction.CounterClockwise().Vector3Int();
                var front            = center + centerBelt.Direction.Vector3Int();

                ItemMovement2D[] priorityMovements = new ItemMovement2D[4];

                priorityMovements[0] = movementLookup.ContainsKey(behind) ? movementLookup[behind] : null;
                priorityMovements[1] = movementLookup.ContainsKey(clockwise) ? movementLookup[clockwise] : null;
                priorityMovements[2] = movementLookup.ContainsKey(counterClockwise) ? movementLookup[counterClockwise] : null;
                priorityMovements[3] = movementLookup.ContainsKey(front) ? movementLookup[front] : null;

                bool foundGoodMovement = false;
                for (int i = 0; i < 4; i++)
                {
                    if (priorityMovements[i] == null || priorityMovements[i].Destination != center)
                    {
                        continue;
                    }
                    if (!foundGoodMovement && !priorityMovements[i].Blocked)
                    {
                        foundGoodMovement = true;
                    }
                    else
                    {
                        BlockMovement(priorityMovements[i].Origin);
                    }
                }
            }
        }

        // Write output
        foreach (var movement in ItemMovements)
        {
            var belt = belts[movement.Origin];
            belt.Moving = !movement.Blocked;
        }
    }