Ejemplo n.º 1
0
    PackCellData LoadFromFile(string filePath)
    {
        PackCellData cells = new PackCellData();

        filePath = Path.ChangeExtension(filePath, ".cel");
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        if (fs != null)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                br.ReadString();                     // SceneName
                cells.CellSize    = br.ReadSingle(); // CellSize
                cells.HeightLimit = br.ReadSingle(); // HeightLimit
                Vector2 sp;
                sp.x = br.ReadSingle();
                sp.y = br.ReadSingle();
                cells.StartingPoint = sp;
                int width  = br.ReadInt32();
                int height = br.ReadInt32();
                cells.Create(width, height);

                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        cells[y, x].Height = br.ReadSingle();
                        cells[y, x].Type   = EnumHelper.Parse <CellType, int>(br.ReadInt32());
                    }
                }
            }
            fs.Close();
        }
        return(cells);
    }
Ejemplo n.º 2
0
    void OnGUI()
    {
        EditorGUILayout.BeginVertical();

        #region 1st Step - Map Scan
        EditorGUILayout.BeginVertical("box");
        EditorGUILayout.LabelField("1st Step - Scan");
        EditorGUILayout.EndVertical();
        _cells.CellSize    = EditorGUILayout.FloatField("CellSize", _cells.CellSize);
        _cells.HeightLimit = EditorGUILayout.FloatField("height Limit", _cells.HeightLimit);
        if (GUILayout.Button("Scan the map"))
        {
            _cells.Clear();
            _testMode         = false;
            _testClickedIndex = new Point();

            _cells.MapBound = ScanMapSize();

            int layerMask = 1 << 8;

            int height = (int)((_cells.MapBound.yMax - _cells.MapBound.yMin) / _cells.CellSize);
            int width  = (int)((_cells.MapBound.xMax - _cells.MapBound.xMin) / _cells.CellSize);

            _cells.Create(width, height);
            _cells.StartingPoint = new Vector2(_cells.MapBound.xMin, _cells.MapBound.yMax);

            foreach (CellData ele in _cells)
            {
                Vector3    rayStart = _cells.GetPosVec3(ele.Index, 100f);
                RaycastHit hit;
                if (Physics.Raycast(rayStart, Vector3.down, out hit, 2000f, layerMask))
                {
                    ele.Type   = CellType.Normal;
                    ele.Height = hit.point.y;

                    if (ele.Height > 0.1f || ele.Height < -0.1f)
                    {
                        ele.Type = CellType.CantMove;
                    }
                }
                else
                {
                    ele.Type = CellType.CantMove;
                }

                if (Physics.Raycast(rayStart, Vector3.down, 2000f, (1 << 0)))
                {
                    ele.Type = CellType.CantMove;
                }
            }

            AStar.Instance.SetMap(_cells);
            JPS.Instance.SetMap(_cells);
            _asPath = null;
        }
        #endregion

        EditorGUILayout.Separator();

        if (!_cells.IsEmpty)
        {
            #region View Options
            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("View Options");
            _showGrid     = EditorGUILayout.Toggle("- Show Grid", _showGrid);
            _showIndex    = EditorGUILayout.Toggle("- Show Index", _showIndex);
            _viewDistance = EditorGUILayout.Slider("- View Distance", _viewDistance, 10f, 100f);
            EditorGUILayout.LabelField("- Map Index Width", ((int)(_cells.MapBound.width / _cells.CellSize)).ToString());
            EditorGUILayout.LabelField("- Map Index height", ((int)(_cells.MapBound.height / _cells.CellSize)).ToString());
            EditorGUILayout.EndVertical();
            #endregion

            EditorGUILayout.Separator();

            #region 2nd Step - Test & Review
            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("2nd Step - Test & Review");
            EditorGUILayout.EndVertical();
            bool pastTestMode = _testMode;
            _testMode = EditorGUILayout.Toggle("Test Mode", _testMode);
            if (_testMode != pastTestMode && _testMode)
            {
                foreach (CellData element in _cells)
                {
                    if (element.Type == CellType.Normal)
                    {
                        continue;
                        //_testAsHero = _cells.GetPosVec3(element.Index, element.Y);
                        //_testJpsHero = _cells.GetPosVec3(element.Index, element.Y);
                        //break;
                    }
                }
            }
            if (_testMode)
            {
                _testShowAsPath = EditorGUILayout.Toggle("- Show A* Line2D", _testShowAsPath);
                if (_testShowAsPath)
                {
                    _testShowAClosedPoint = EditorGUILayout.Toggle("-- Show ClosedPoint", _testShowAClosedPoint);
                    _testShowAValue       = EditorGUILayout.Toggle("-- Show A Star Value", _testShowAValue);
                }
                _testShowJumpPointPath = EditorGUILayout.Toggle("- Show JumpPoint Line2D", _testShowJumpPointPath);
                if (_testShowJumpPointPath)
                {
                    _testShowJumpPoint    = EditorGUILayout.Toggle("-- Show JumpPoint", _testShowJumpPoint);
                    _testShowJClosedPoint = EditorGUILayout.Toggle("-- Show ClosedPoint", _testShowJClosedPoint);
                    _testShowJValue       = EditorGUILayout.Toggle("-- Show JumpPoint Value", _testShowJValue);
                }
                _testHeroMoveMode = EditorGUILayout.Toggle("- Hero Move", _testHeroMoveMode);
                if (_testHeroMoveMode)
                {
                    _testHeroSpeed = EditorGUILayout.Slider("- Hero Speed", _testHeroSpeed, 0f, 10f);
                }
            }
            #endregion

            EditorGUILayout.Separator();

            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("3rd Step - Save");
            EditorGUILayout.EndVertical();
            EditorGUILayout.LabelField("- File Path : " + PATH_CELLSETTINGS);
            EditorGUILayout.EndVertical();

            if (GUILayout.Button("Save"))
            {
                SaveToXmlFile();
                SaveToFile();
            }
        }
    }