RelSnapToGrid() public static méthode

public static RelSnapToGrid ( Vector2 pos ) : Vector2
pos Vector2
Résultat Vector2
Exemple #1
0
    void PositionPartsOffShip(Vector2 pos, Vector2 aimPos, float partAngle)
    {
        float angle     = 0.0f;
        float refPAngle = 0.0f;        //reference block angle

        //current island angle as point of reference
        while (refPAngle > angle + 45)
        {
            refPAngle -= 90.0f;
        }
        while (refPAngle < angle - 45)
        {
            refPAngle += 90.0f;
        }

        Vector2 mouseAim  = aimPos - pos;
        float   mouseDist = Mathf.Min(mouseAim.magnitude, max_build_distance);

        mouseAim.Normalize();
        aimPos = pos + mouseAim * mouseDist;    //position of the 'buildpart' pointer
        aimPos = ShipFunctions.RelSnapToGrid(aimPos);
        Vector2 cursor_pos = aimPos;            //position of snapped build part

        //rotate and position parts
        for (int i = 0; i < parts.Count; ++i)
        {
            GameObject part   = parts[i];
            Vector2    offset = part.GetComponent <Part_Info>().Offset;
            offset = RotateVector2(offset, parts_angle);
            offset = RotateVector2(offset, refPAngle);

            part.transform.position    = cursor_pos + offset;           //align to ship grid
            part.transform.eulerAngles = new Vector3(0, 0, (refPAngle + parts_angle) % 360.0f);

            //SetDisplay( block, color_white, RenderStyle::additive, 560.0f );
        }
    }
Exemple #2
0
    void PositionParts(Vector2 pos, Vector2 aimPos, float partAngle, GameObject centerPart, GameObject refPart)
    {
        if (centerPart == null)
        {
            Debug.Log("PositionParts: center part not found");
            return;
        }

        Vector2 ship_pos  = centerPart.transform.position;
        float   angle     = centerPart.transform.eulerAngles.z;
        float   refPAngle = refPart.transform.eulerAngles.z;      //reference block angle

        //current island angle as point of reference
        while (refPAngle > angle + 45)
        {
            refPAngle -= 90.0f;
        }
        while (refPAngle < angle - 45)
        {
            refPAngle += 90.0f;
        }

        //get offset (based on the centerblock) of block we're standing on
        Vector2 refPartPos2D = refPart.transform.position;
        Vector2 refPOffset   = refPartPos2D - ship_pos;
        float   gridSize     = 0.16f;
        float   halfGridSize = gridSize / 2.0f;

        refPOffset   = RotateVector2(refPOffset, -refPAngle);
        refPOffset.x = refPOffset.x % gridSize;
        refPOffset.y = refPOffset.y % gridSize;
        //not really necessary
        if (refPOffset.x > halfGridSize)
        {
            refPOffset.x -= gridSize;
        }
        else if (refPOffset.x < -halfGridSize)
        {
            refPOffset.x += gridSize;
        }
        if (refPOffset.y > halfGridSize)
        {
            refPOffset.y -= gridSize;
        }
        else if (refPOffset.y < -halfGridSize)
        {
            refPOffset.y += gridSize;
        }
        refPOffset = RotateVector2(refPOffset, refPAngle);

        ship_pos += refPOffset;
        Vector2 mouseAim  = aimPos - pos;
        float   mouseDist = Mathf.Min(mouseAim.magnitude, max_build_distance);

        mouseAim.Normalize();
        aimPos = pos + mouseAim * mouseDist;        //position of the 'buildpart' pointer
        Vector2 shipAim = aimPos - ship_pos;        //ship to 'buildpart' pointer

        shipAim = RotateVector2(shipAim, -refPAngle);
        shipAim = shipFunctions.RelSnapToGrid(shipAim);
        shipAim = RotateVector2(shipAim, refPAngle);
        Vector2 cursor_pos = ship_pos + shipAim;        //position of snapped build part

        //rotate and position parts
        for (int i = 0; i < parts.Count; ++i)
        {
            GameObject part   = parts[i];
            Vector2    offset = part.GetComponent <Part_Info>().Offset;
            offset = RotateVector2(offset, parts_angle);
            offset = RotateVector2(offset, refPAngle);

            part.transform.position    = cursor_pos + offset;           //align to ship grid
            part.transform.eulerAngles = new Vector3(0, 0, (refPAngle + parts_angle) % 360.0f);

            //SetDisplay( block, color_white, RenderStyle::additive, 560.0f );
        }
    }