示例#1
0
    // 旋转图形
    static public SceneShapePolygon rotate(SceneShapeRect rect, Vector2 pos, float angles)
    {
        SceneShapePolygon shape = new SceneShapePolygon();
        Vector3           lt    = new Vector3(rect.mLeft, 0.0f, rect.mTop);
        Vector3           rt    = new Vector3(rect.mRight, 0.0f, rect.mTop);
        Vector3           lb    = new Vector3(rect.mLeft, 0.0f, rect.mBottom);
        Vector3           rb    = new Vector3(rect.mRight, 0.0f, rect.mBottom);

        Quaternion q = new Quaternion();

        q.eulerAngles = new Vector3(0, angles, 0);

        Vector3 center = new Vector3(pos.x, 0.0f, pos.y);

        Vector3 ltnew = (q * (lt - center)) + center;
        Vector3 rtnew = (q * (rt - center)) + center;
        Vector3 lbnew = (q * (lb - center)) + center;
        Vector3 rbnew = (q * (rb - center)) + center;

        shape.PushbackVector2(new Vector2(ltnew.x, ltnew.z));
        shape.PushbackVector2(new Vector2(rtnew.x, rtnew.z));
        shape.PushbackVector2(new Vector2(rbnew.x, rbnew.z));
        shape.PushbackVector2(new Vector2(lbnew.x, lbnew.z));

        return(shape);
    }
    // 解析Zones
    private void ParseZones(XmlNode node)
    {
        XmlNodeList nodeList = node.ChildNodes;

        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlNode childNode = nodeList[i];
            if (childNode != null && childNode.Name == "Zone")
            {
                int type = System.Convert.ToInt32(childNode.Attributes["type"].Value);
                if ((Zone.ZoneType)type == Zone.ZoneType.ZoneType_Invalid || !System.Enum.IsDefined(typeof(Zone.ZoneType), type))
                {
                    continue;
                }

                Zone zone = new Zone();
                zone.type = (Zone.ZoneType)type;
                zone.name = childNode.Attributes["name"].Value;

                ShapeType shapetype = (ShapeType)(System.Convert.ToInt32(childNode.Attributes["shapeType"].Value));
                if (shapetype == ShapeType.ShapeType_Round)
                {
                    zone.shape = new SceneShapeRound();
                    SceneShapeRound round = zone.shape as SceneShapeRound;
                    round.mCenter.x = (float)System.Convert.ToDouble(childNode.Attributes["x"].Value);
                    round.mCenter.y = (float)System.Convert.ToDouble(childNode.Attributes["y"].Value);
                    round.mRadius   = (float)System.Convert.ToDouble(childNode.Attributes["r"].Value);
                }
                else if (shapetype == ShapeType.ShapeType_Rect)
                {
                    zone.shape = new SceneShapeRect();
                    SceneShapeRect rect = zone.shape as SceneShapeRect;
                    rect.mLeft   = (float)System.Convert.ToDouble(childNode.Attributes["x1"].Value);
                    rect.mTop    = (float)System.Convert.ToDouble(childNode.Attributes["y1"].Value);
                    rect.mRight  = (float)System.Convert.ToDouble(childNode.Attributes["x2"].Value);
                    rect.mBottom = (float)System.Convert.ToDouble(childNode.Attributes["y2"].Value);
                }
                else if (shapetype == ShapeType.ShapeType_Polygon)
                {
                    zone.shape = new SceneShapePolygon();
                    SceneShapePolygon polygon = zone.shape as SceneShapePolygon;

                    int          index   = 1;
                    XmlAttribute attribX = null;
                    XmlAttribute attribY = null;

                    while ((attribX = childNode.Attributes["x" + index.ToString()]) != null && (attribY = childNode.Attributes["y" + index.ToString()]) != null)
                    {
                        polygon.PushbackVector2(new Vector2((float)System.Convert.ToDouble(attribX.Value), (float)System.Convert.ToDouble(attribY.Value)));
                        index++;
                    }
                }

                mZones.Add(zone.name, zone);
            }
        }
    }
示例#3
0
    // 根据形状参数、位置、弧度 创建形状
    static public SceneShape Create(SceneShapeParam param, Vector2 pos, float radians)
    {
        if (param.mType == ShapeType.ShapeType_Round)
        {
            if (param == null || param.mParams == null || param.mParams.Count < 1)
            {
                return(null);
            }

            return(new SceneShapeRound(pos, param.mParams[0]));
        }
        else if (param.mType == ShapeType.ShapeType_Rect)
        {
            if (param == null || param.mParams == null || param.mParams.Count < 2)
            {
                return(null);
            }

            if ((int)(radians * 100000) % (int)(Mathf.PI * 100000 * 2) == 0)
            {
                if ((int)(radians * 100000) % (int)(Mathf.PI * 100000) == 0)
                {
                    return(new SceneShapeRect(pos, param.mParams[0], param.mParams[1]));
                }

                return(new SceneShapeRect(pos, param.mParams[1], param.mParams[0]));
            }
            else
            {
                SceneShapeRect rect = new SceneShapeRect(pos, param.mParams[0], param.mParams[1]);

                return(rotate(rect, pos, radians * Mathf.Rad2Deg));
            }
        }
        else if (param.mType == ShapeType.ShapeType_Polygon)
        {
            if (param == null || param.mParams == null || param.mParams.Count < 2 || param.mParams.Count % 2 != 0)
            {
                return(null);
            }

            SceneShapePolygon shape = new SceneShapePolygon();
            for (int i = 0; i < (int)(param.mParams.Count * 0.5); ++i)
            {
                shape.PushbackVector2(new Vector2(param.mParams[2 * i], param.mParams[2 * i + 1]));
            }

            return(rotate(ref shape, pos, radians * Mathf.Rad2Deg));
        }

        GameDebug.LogError("SceneShape.Creat() 未知的图形类别");

        return(null);
    }