TraceRacer.WorldObject.getClosestGate C# (CSharp) Method

getClosestGate() public method

Gets the closest gate that intersects with the player.
public getClosestGate ( PlayerObject player ) : Gate
player PlayerObject
return Gate
        public Gate getClosestGate(PlayerObject player)
        {
            Gate closestGate = null;

            float gX, gX2, gY, gY2; // Gate Position X, Y - should be 10px less on Y due to perspective
            float pX, pX2, pY, pY2; // Player Position X,y
            pX = player.Position.X;
            pX2 = player.Position.X + player.Texture.Width;
            pY = player.Position.Y;
            pY2 = player.Position.Y + player.Texture.Height;

            foreach(Gate gate in gateQueue)
            {
                gX = gate.position.X;
                gX2 = gX + gate.myTexture.Width;
                gY = gate.position.Y +20;
                gY2 = gY + gate.myTexture.Height -35;

                if (!gate.isHit &&
                    ((gX <= pX) && (pX <= gX2) ||
                    (gX <= pX2) && (pX2 <= gX2))) // player and gate intersect on X
                {
                    if ((gY <= pY) && (pY <= gY2) ||
                        (gY <= pY2) && (pY2 <= gY2)) // player and gate intersect on Y
                    {
                        closestGate = gate;
                        break;
                    }
                }
                else if (gX > pX2)
                {
                    // This gate is still far, don't look for next ones in this cycle
                    break;
                }
            }

            return closestGate;
        }