Beispiel #1
0
    private Vector3 GetBoxPositionForAgent(int agentID, Vector2 boxDimensions, bool tryAbove, ref bool positionValid, ref Vector2 tailOffset)
    {
        Vector3   boxPosition     = Vector3.zero;
        BaseAgent agent           = null;
        Vector3   agentPosition   = Vector3.zero;
        float     agentHeight     = 0f;
        float     halfAgentHeight = 0f;
        bool      fitsAbove       = false;
        bool      fitsBelow       = false;
        Vector3   cameraPosition  = Vector3.zero;
        float     cameraTop       = 0f;
        float     cameraBottom    = 0f;
        float     cameraLeft      = 0f;
        float     cameraRight     = 0f;
        float     boxTop          = 0f;
        float     boxBottom       = 0f;
        float     halfBoxWidth    = 0f;
        float     halfBoxHeight   = 0f;

        positionValid = false;
        if (cameraObject != null)
        {
            if (agentID == BaseAgent.INVALID_AGENT_ID)
            {
                /*halmeida - A box for system text display is always shown horizontally centered at the screen and with its
                 * top at a fixed, predetermined position.*/
                cameraPosition = cameraObject.transform.position;
                boxPosition.x  = cameraPosition.x;
                boxPosition.y  = cameraPosition.y + systemBoxTopOffset - boxDimensions.y / 2f;
                positionValid  = true;
            }
            else
            {
                if (stage != null)
                {
                    agent = stage.GetAgent(agentID);
                    if (agent != null)
                    {
                        /*halmeida - gotta place the box at a position that relates to the agent's position, is within the
                         * camera, and tries to respect the above/bellow orientation.*/
                        halfBoxHeight   = boxDimensions.y / 2f;
                        agentPosition   = agent.gameObject.transform.position;
                        agentHeight     = agent.GetHeight();
                        halfAgentHeight = agentHeight / 2f;
                        cameraPosition  = cameraObject.transform.position;
                        cameraTop       = cameraPosition.y + cameraHalfHeight;
                        boxTop          = agentPosition.y + halfAgentHeight + tailHeight + boxDimensions.y;
                        fitsAbove       = !(boxTop > cameraTop);
                        cameraBottom    = cameraPosition.y - cameraHalfHeight;
                        boxBottom       = agentPosition.y - halfAgentHeight - tailHeight - boxDimensions.y;
                        fitsBelow       = !(boxBottom < cameraBottom);
                        if (fitsAbove && fitsBelow)
                        {
                            if (tryAbove)
                            {
                                boxPosition.y = boxTop - halfBoxHeight;
                                tailOffset.y  = -(halfBoxHeight + tailHalfHeight);
                            }
                            else
                            {
                                boxPosition.y = boxBottom + halfBoxHeight;
                                tailOffset.y  = halfBoxHeight + tailHalfHeight;
                            }
                        }
                        else if (fitsAbove)
                        {
                            boxPosition.y = boxTop - halfBoxHeight;
                            tailOffset.y  = -(halfBoxHeight + tailHalfHeight);
                        }
                        else if (fitsBelow)
                        {
                            boxPosition.y = boxBottom + halfBoxHeight;
                            tailOffset.y  = halfBoxHeight + tailHalfHeight;
                        }
                        else
                        {
                            if ((cameraTop - agentPosition.y) >= (agentPosition.y - cameraBottom))
                            {
                                boxPosition.y = cameraTop - halfBoxHeight;
                                tailOffset.y  = -(halfBoxHeight + tailHalfHeight);
                            }
                            else
                            {
                                boxPosition.y = cameraBottom + halfBoxHeight;
                                tailOffset.y  = halfBoxHeight + tailHalfHeight;
                            }
                        }
                        /*halmeida - the y position is set, now we discover the x.*/
                        boxPosition.x = agentPosition.x;
                        halfBoxWidth  = boxDimensions.x / 2f;
                        cameraRight   = cameraPosition.x + cameraHalfWidth;
                        if ((agentPosition.x + halfBoxWidth) > cameraRight)
                        {
                            boxPosition.x = cameraRight - halfBoxWidth;
                        }
                        cameraLeft = cameraPosition.x - cameraHalfWidth;
                        if ((agentPosition.x - halfBoxWidth) < cameraLeft)
                        {
                            boxPosition.x = cameraLeft + halfBoxWidth;
                        }
                        tailOffset.x = agentPosition.x - boxPosition.x;
                        if (((boxPosition.x + tailOffset.x) + tailHalfWidth) > cameraRight)
                        {
                            tailOffset.x = halfBoxWidth - tailHalfWidth;
                        }
                        if (((boxPosition.x + tailOffset.x) - tailHalfWidth) < cameraLeft)
                        {
                            tailOffset.x = -halfBoxWidth + tailHalfWidth;
                        }
                        positionValid = true;
                    }
                }
            }
        }
        return(boxPosition);
    }