Ejemplo n.º 1
0
    // レベルデータを読み込む
    public void Load(string fLevel)
    {
        // レイヤー生成.
        _layer = new Layer2D();
        // レベルデータ取得.
        TextAsset tmx = Resources.Load(fLevel) as TextAsset;

        // XML解析開始.
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(tmx.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");

        foreach (XmlNode map in mapList)
        {
            XmlNodeList childList = map.ChildNodes;
            foreach (XmlNode child in childList)
            {
                if (child.Name != "layer")
                {
                    continue;
                }                                       // layerノード以外は見ない.

                // マップ属性を取得.
                XmlAttributeCollection attrs = child.Attributes;
                int w = int.Parse(attrs.GetNamedItem("width").Value);  // 幅を取得.
                int h = int.Parse(attrs.GetNamedItem("height").Value); // 高さを取得.
                // レイヤー生成.
                _layer.Create(w, h);
                XmlNode node = child.FirstChild; // 子ノードは<data>のみ.
                XmlNode n    = node.FirstChild;  // テキストノードを取得.
                string  val  = n.Value;          // テキストを取得.
                // CSV(マップデータ)を解析.
                int y = 0;
                foreach (string line in val.Split('\n'))
                {
                    if (line == "")
                    {
                        continue;
                    }                                                // 空文字は除外.
                    int x = 0;
                    foreach (string s in line.Split(','))
                    {
                        int v = 0;
                        // ","で終わるのでチェックが必要.
                        if (int.TryParse(s, out v) == false)
                        {
                            continue;
                        }
                        // 値を設定.
                        _layer.Set(x, y, v);
                        x++;
                    }
                    y++;
                }
            }
        }
    }
Ejemplo n.º 2
0
    // レベルデータを読み込む
    void Start()
    {
        // レイヤー生成
        Layer2D layer = new Layer2D();
        // レベルデータ取得
        TextAsset tmx = Resources.Load("Resources/samplemap_2017_9_6") as TextAsset;

        // XML解析開始
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(tmx.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");

        foreach (XmlNode map in mapList)
        {
            XmlNodeList childList = map.ChildNodes;
            foreach (XmlNode child in childList)
            {
                if (child.Name != "layer")
                {
                    continue;
                }                                        // layerノード以外は見ない

                // マップ属性を取得
                XmlAttributeCollection attrs = child.Attributes;
                int w = int.Parse(attrs.GetNamedItem("width").Value);  // 幅を取得
                int h = int.Parse(attrs.GetNamedItem("height").Value); // 高さを取得
                // レイヤー生成
                layer.Create(w, h);
                XmlNode node = child.FirstChild; // 子ノードは<data>のみ
                XmlNode n    = node.FirstChild;  // テキストノードを取得
                string  val  = n.Value;          // テキストを取得
                // CSV(マップデータ)を解析
                int y = 0;
                foreach (string line in val.Split('\n'))
                {
                    int x = 0;
                    foreach (string s in line.Split(','))
                    {
                        int v = 0;
                        // ","で終わるのでチェックが必要
                        if (int.TryParse(s, out v) == false)
                        {
                            continue;
                        }
                        // 値を設定
                        layer.Set(x, y, v);
                        x++;
                    }
                    y++;
                }
            }
        }

        // デバッグ出力
        layer.Dump();
    }
Ejemplo n.º 3
0
    public List <Point2> GetShortestPath(Vector2 start, Vector2 goal, int[,] map, bool allowdiag = false)
    {
        List <Point2> ret   = new List <Point2>();
        Layer2D       layer = new Layer2D();

        layer.Create(Constant.MAPSIZE, Constant.MAPSIZE); // TODO: 配列メソッドで取得する方法
        for (int i = 0; i < Constant.MAPSIZE; i++)
        {
            for (int j = 0; j < Constant.MAPSIZE; j++)
            {
                layer.Set(j, i, map[i, j]);
            }
        }

        var   mgr  = new ANodeMgr(layer, (int)goal.x, (int)goal.y, allowdiag);
        ANode node = mgr.OpenNode((int)start.x, (int)start.y, 0, null);

        if (node == null)
        {
            Debug.Log("node is null start" + start);
        }
        int cnt = 0;

        while (cnt < 1000)
        {
            mgr.RemoveOpenList(node);
            // 周囲を開く
            mgr.OpenAround(node);
            // 最小スコアのノードを探す.
            node = mgr.SearchMinScoreNodeFromOpenList();
            if (node == null)
            {
                // 袋小路なのでおしまい.
                // Debug.Log("Not found path.");
                break;
            }
            if (node.X == (int)goal.x && node.Y == (int)goal.y)
            {
                // ゴールにたどり着いた.
                // Debug.Log("Success.");
                mgr.RemoveOpenList(node);
                node.DumpRecursive();
                // パスを取得する
                node.GetPath(ret);
                // 反転する
                ret.Reverse();
                break;
            }
        }

        return(ret);
    }
Ejemplo n.º 4
0
    // レベルデータを読み込む
    public void Load(string fLevel)
    {
        // レイヤー生成.
        _layer = new Layer2D();
        // レベルデータ取得.
        TextAsset tmx = Resources.Load(fLevel) as TextAsset;

        // XML解析開始.
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(tmx.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");
        foreach (XmlNode map in mapList)
        {
          XmlNodeList childList = map.ChildNodes;
          foreach (XmlNode child in childList)
          {
        if (child.Name != "layer") { continue; } // layerノード以外は見ない.

        // マップ属性を取得.
        XmlAttributeCollection attrs = child.Attributes;
        int w = int.Parse(attrs.GetNamedItem("width").Value); // 幅を取得.
        int h = int.Parse(attrs.GetNamedItem("height").Value); // 高さを取得.
        // レイヤー生成.
        _layer.Create(w, h);
        XmlNode node = child.FirstChild; // 子ノードは<data>のみ.
        XmlNode n = node.FirstChild; // テキストノードを取得.
        string val = n.Value; // テキストを取得.
        // CSV(マップデータ)を解析.
        int y = 0;
        foreach (string line in val.Split('\n'))
        {
          // 空白文字を削除
          var line2 = line.Trim();
          if (line2 == "") { continue; } // 空文字は除外.
          int x = 0;
          foreach (string s in line2.Split(','))
          {
            int v = 0;
            // ","で終わるのでチェックが必要.
            if (int.TryParse(s, out v) == false) { continue; }
            // 値を設定.
            _layer.Set(x, y, v);
            x++;
          }
          y++;
        }
          }
        }
    }
Ejemplo n.º 5
0
    // レベルデータを読み込む
    void Start()
    {
        // レイヤー生成
        Layer2D layer = new Layer2D();
        // レベルデータ取得
        TextAsset tmx = Resources.Load ("Levels/test2") as TextAsset;

        // XML解析開始
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml (tmx.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");
        foreach(XmlNode map in mapList) {
            XmlNodeList childList = map.ChildNodes;
            foreach(XmlNode child in childList) {
                if(child.Name != "layer") { continue; } // layerノード以外は見ない

                // マップ属性を取得
                XmlAttributeCollection attrs = child.Attributes;
                int w = int.Parse(attrs.GetNamedItem("width").Value); // 幅を取得
                int h = int.Parse(attrs.GetNamedItem("height").Value); // 高さを取得
                // レイヤー生成
                layer.Create(w, h);
                XmlNode node = child.FirstChild; // 子ノードは<data>のみ
                XmlNode n = node.FirstChild; // テキストノードを取得
                string val = n.Value; // テキストを取得
                // CSV(マップデータ)を解析
                int y = 0;
                foreach(string line in val.Split('\n')) {
                    int x = 0;
                    foreach(string s in line.Split(',')) {
                        int v = 0;
                        // ","で終わるのでチェックが必要
                        if(int.TryParse(s, out v) == false) { continue; }
                        // 値を設定
                        layer.Set (x, y, v);
                        x++;
                    }
                    y++;
                }
            }
        }

        // デバッグ出力
        layer.Dump();
    }
Ejemplo n.º 6
0
    // レベルデータを読み込む
    public Layer2D Load(string mapname)
    {
        Layer2D layer = new Layer2D();
        // レベルデータ取得
        TextAsset tmx = Resources.Load("Data/" + mapname) as TextAsset;

        if (tmx == null)
        {
            Debug.LogError("mapdata is nothing");
        }

        // XML解析開始
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(tmx.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");

        foreach (XmlNode map in mapList)
        {
            XmlNodeList childList = map.ChildNodes;
            foreach (XmlNode child in childList)
            {
                if (child.Name != "layer")
                {
                    continue;
                }                                        // layerノード以外は見ない



                if (child.Name == "layer")
                {
                    Debug.Log("layerLoadNow");
                    // マップ属性を取得
                    XmlAttributeCollection attrs = child.Attributes;
                    int w = int.Parse(attrs.GetNamedItem("width").Value);      // 幅を取得
                    int h = int.Parse(attrs.GetNamedItem("height").Value) + 1; // 高さを取得
                                                                               // レイヤー生成
                    layer.Create(w, h);
                    XmlNode node = child.FirstChild;                           // 子ノードは<data>のみ
                    XmlNode n    = node.FirstChild;                            // テキストノードを取得
                    string  val  = n.Value;                                    // テキストを取得
                                                                               // CSV(マップデータ)を解析
                    int y = 0;
                    foreach (string line in val.Split('\n'))
                    {
                        int x = 0;
                        foreach (string s in line.Split(','))
                        {
                            int v = 0;
                            // ","で終わるのでチェックが必要
                            if (int.TryParse(s, out v) == false)
                            {
                                continue;
                            }
                            // 値を設定
                            layer.Set(x, y, v);
                            x++;
                        }
                        y++;
                    }
                }
            }
        }
        Debug.Log(layer.width);
        Debug.Log(layer.height);
        return(layer);
    }
Ejemplo n.º 7
0
    public static int[,] Read(TextAsset path /*string path*/)
    {
        //レイヤーの設定
        Layer2D layer = new Layer2D();
        //データのload
        TextAsset mapDate = path /*Resources.Load (path)as TextAsset*/;

        //Xmlの解析開始
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(mapDate.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");

        foreach (XmlNode map in mapList)
        {
            XmlNodeList childList = map.ChildNodes;
            foreach (XmlNode child in childList)
            {
                if (child.Name != "layer")
                {
                    continue;
                }                                                       // layerノード以外は見ない

                // マップ属性を取得
                XmlAttributeCollection attrs = child.Attributes;
                int w = int.Parse(attrs.GetNamedItem("width").Value);                 // 幅を取得
                int h = int.Parse(attrs.GetNamedItem("height").Value);                // 高さを取得
                // レイヤー生成
                layer.Create(h, w);
                XmlNode node = child.FirstChild;      // 子ノードは<data>のみ
                XmlNode n    = node.FirstChild;       // テキストノードを取得
                string  val  = n.Value;               // テキストを取得
                // CSV(マップデータ)を解析
                string[] line = val.Split('\n');
                int      y    = 0;        //書き込み場所確認用
                bool     lineRead;
                int      x = 0;
                string   debu;
                for (int i = 0; i < line.Length; i++)
                {
                    lineRead = false;
                    debu     = i + "行目:";
                    string[] s = line [i].Split(',');
                    for (int j = 0; j < layer.width; j++)
                    {
                        int v = 0;
                        if (j >= s.Length)
                        {
                            break;
                        }
                        if (int.TryParse(s[j], out v) == false)
                        {
                            continue;
                        }
                        v--;                        //空を示すデータが0なのに0番からマップチップを用意しているので、全部-1する。-1が空。
                        layer.Set(y, x, v);
                        debu += v + ",";
                        x++;
                        lineRead = true;

                        /*if (v == 1)
                         *      Debug.Log ("1");*/
                    }
                    if (lineRead)
                    {
                        y++;
                        x = 0;
                    }
                    //Debug.Log (debu);
                }

                /*
                 * //Debug.Log ("実際のデータ(と思しきもの)");
                 * for (int i = 0; i < layer.height; i++) {
                 *      string debu2 = i + "行目:";
                 *      for (int j = 0; j < layer.width; j++) {
                 *              debu2 += layer.Get (j, i).ToString () + ",";
                 *      }
                 *      Debug.Log (debu2);
                 * }
                 */
            }
        }

        //縦,横 に変換(しないことになりました)
        //int[,] mapChip = new int[layer.height, layer.width];
        int[,] mapChip = new int[layer.width, layer.height];
        for (int i = 0; i < layer.width; i++)
        {
            for (int j = 0; j < layer.height; j++)
            {
                //mapChip [j, i] = layer.Get (i, j);
                mapChip [i, j] = layer.Get(i, j);
            }
        }

        return(mapChip);
    }
Ejemplo n.º 8
0
    /// レベルデータを読み込む.
    public bool Load(string fLevel)
    {
        // レベルデータ取得.
        TextAsset tmx = Resources.Load(fLevel) as TextAsset;

        if (tmx == null)
        {
            // 読み込み失敗
            Debug.LogErrorFormat("File not found. '{0}'", fLevel);
            return(false);
        }

        // レイヤーディクショナリ生成.
        _layers = new Dictionary <string, Layer2D>();

        // XML解析開始.
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(tmx.text);
        XmlNodeList mapList = xmlDoc.GetElementsByTagName("map");

        foreach (XmlNode map in mapList)
        {
            XmlNodeList childList = map.ChildNodes;
            foreach (XmlNode child in childList)
            {
                if (child.Name != "layer")
                {
                    continue;
                }                                // layerノード以外は見ない.

                // マップ属性を取得.
                XmlAttributeCollection attrs = child.Attributes;
                string name = attrs.GetNamedItem("name").Value;              // 名前を取得.
                int    w    = int.Parse(attrs.GetNamedItem("width").Value);  // 幅を取得.
                int    h    = int.Parse(attrs.GetNamedItem("height").Value); // 高さを取得.
                // レイヤー生成.
                var layer = new Layer2D();
                layer.Create(w, h);
                XmlNode node = child.FirstChild; // 子ノードは<data>のみ.
                XmlNode n    = node.FirstChild;  // テキストノードを取得.
                string  val  = n.Value;          // テキストを取得.
                // CSV(マップデータ)を解析.
                int y = 0;
                foreach (string line in val.Split('\n'))
                {
                    // 空白文字を削除.
                    var line2 = line.Trim();
                    if (line2 == "")
                    {
                        continue;
                    }                    // 空文字は除外.
                    int x = 0;
                    foreach (string s in line2.Split(','))
                    {
                        int v = 0;
                        // ","で終わるのでチェックが必要.
                        if (int.TryParse(s, out v) == false)
                        {
                            continue;
                        }
                        // 値を設定.
                        layer.Set(x, y, v);
                        x++;
                    }
                    y++;
                }

                // ディクショナリに登録
                _layers[name] = layer;
            }
        }

        // 読み込み成功
        return(true);
    }