Example #1
0
    public void BlockMovement(Vector3Int origin)
    {
        //print("Blocking " + origin);
        var movement = movementLookup[origin];

        if (movement.Blocked)
        {
            return;
        }

        // Keep a stack of blocks that need to be checked
        Stack <Vector3Int> originsToBeProcessed = new Stack <Vector3Int>(64);

        originsToBeProcessed.Push(origin);
        movement.Blocked = true;

        // Process all blocks in chain
        while (originsToBeProcessed.Count > 0)
        {
            // Pop movement off the stack
            origin   = originsToBeProcessed.Pop();
            movement = movementLookup[origin];

            // Check for blocking in each direction
            foreach (var dir in DirectionExtensions.Directions())
            {
                // Ignore blocking in the direction we're going
                if (movement.Direction == dir)
                {
                    continue;
                }
                //print("Checking " + (origin + dir.Vector3Int()));
                var behindOrigin = origin + dir.Vector3Int();
                if (movementDependency.ContainsKey(behindOrigin))
                {
                    var behindMovement = movementLookup[behindOrigin];
                    //print("Found dependency: " + behindMovement.Direction + " " + behindMovement.Blocked + " " + dir.Opposite());
                    // If it's moving to us, then it needs to be blocked
                    if (behindMovement.Direction == dir.Opposite() && !behindMovement.Blocked)
                    {
                        //print("Blocking " + behindOrigin);
                        behindMovement.Blocked = true;
                        originsToBeProcessed.Push(behindOrigin);
                    }
                }
            }
        }
    }