Ejemplo n.º 1
0
    private List <Receiver> getConnections(int currentBounces, int maxBounces, ComponentPiece currentPiece, int rotation)
    {
        //passes in a piece and the sees it it connects to a reciever eventually.

        //stores all of the recievers connected to
        List <Receiver> result = new List <Receiver>();


        List <LightComponent> components = this.lightGraph.getAllGraphComponents();

        //default to rotation 0
        Func <ComponentPiece, ComponentPiece, bool> selector = (closestPiece, checkingPiece) =>
        {
            //gets the smallest y value
            bool isCloser = false;
            if (closestPiece == null || (checkingPiece.Rect.y < closestPiece.Rect.y))
            {
                isCloser = true;
            }
            return(isCloser);
        };

        if (rotation == 1)
        {
            selector = (closestPiece, checkingPiece) =>
            {
                //gets the smallest x value
                bool isCloser = false;
                if (closestPiece == null || (checkingPiece.Rect.x < closestPiece.Rect.x))
                {
                    isCloser = true;
                }
                return(isCloser);
            };
        }
        else if (rotation == 2)
        {
            selector = (closestPiece, checkingPiece) =>
            {
                //gets the biggest y value
                bool isCloser = false;
                if (closestPiece == null || (checkingPiece.Rect.y > closestPiece.Rect.y))
                {
                    isCloser = true;
                }
                return(isCloser);
            };
        }
        else if (rotation == 3)
        {
            selector = (closestPiece, checkingPiece) =>
            {
                //gets the biggest x value
                bool isCloser = false;
                if (closestPiece == null || (checkingPiece.Rect.x > closestPiece.Rect.x))
                {
                    isCloser = true;
                }
                return(isCloser);
            };
        }

        Rect ray = getRayRect((Direction)rotation, currentPiece.Rect, new Vector2(.2f, getMaxRayLength()));

        GraphComponent currentClosestComponent = null;
        ComponentPiece currentClosestPiece     = null;

        //gets the closest piece it collides with
        //goes through each component
        for (int c = 0; c < components.Count; c++)
        {
            GraphComponent        currentComponent = components[c];
            List <ComponentPiece> pieces           = currentComponent.ComponentPieces;

            //loops though all the pieces
            for (int p = 0; p < pieces.Count; p++)
            {
                //checks if overlaps with ray and is closer
                if (ray.Overlaps(pieces[p].Rect))
                {
                    if (selector(currentClosestPiece, pieces[p]))
                    {
                        currentClosestPiece     = pieces[p];
                        currentClosestComponent = currentComponent;
                    }
                }
            }
        }

        if (currentClosestPiece != null && currentBounces < LightGraph.maxBounces)
        {
            if (currentClosestPiece.GetType() == typeof(Mirror))
            {
                //reflect off of the mirror
                Direction newDirection = reflect((Direction)rotation, currentClosestComponent.Rotation, currentClosestComponent.Flipped);

                //adds the result from the next beam to this list
                result.AddRange(getConnections(currentBounces++, LightGraph.maxBounces, currentClosestPiece, (int)newDirection));
            }
            else if (currentClosestPiece.GetType() == typeof(PassingMirror))
            {
                //goes through the passing mirror
                result.AddRange(getConnections(currentBounces++, LightGraph.maxBounces, currentClosestPiece, rotation));

                //reflects off the passing mirror
                Direction newDirection = reflect((Direction)rotation, currentClosestComponent.Rotation, currentClosestComponent.Flipped);
                result.AddRange(getConnections(currentBounces++, LightGraph.maxBounces, currentClosestPiece, (int)newDirection));
            }
            else if (currentClosestPiece.GetType().IsSubclassOf(typeof(Receiver)))
            {
                result.Add((Receiver)currentClosestPiece);
            }
        }

        return(result);
    }
Ejemplo n.º 2
0
 private List <Receiver> getConnections(ComponentPiece currentPiece, int rotation)
 {
     //gets the connections for the sender
     return(this.getConnections(0, LightGraph.maxBounces, currentPiece, rotation));
 }