Example #1
0
    /// <summary>
    /// Saves the current squaretilemap as a data asset file.
    /// </summary>
    public void SaveMap()
    {
        string filePath = Application.dataPath + "/Resources/SquareTileMaps/";

        if (!Directory.Exists(filePath))
        {
            CreateSaveDirectory();
        }

        SquareTileMapData savedSquareTileMap = ScriptableObject.CreateInstance <SquareTileMapData>();

        // Populate data values.
        savedSquareTileMap.SquareTiles = new List <Vector3>(tiles.Count);
        foreach (SquareTile t in tiles.Values)
        {
            savedSquareTileMap.SquareTiles.Add(new Vector3(t.Pos.x, t.Height, t.Pos.y));
        }
        savedSquareTileMap.MinMapSize         = minMapSize;
        savedSquareTileMap.MaxMapSize         = maxMapSize;
        savedSquareTileMap.ObstaclePercent    = obstaclePercent;
        savedSquareTileMap.ObstaclePlaceSeed  = obstaclePlaceSeed;
        savedSquareTileMap.MinObstacleHeight  = minObstacleHeight;
        savedSquareTileMap.MaxObstacleHeight  = maxObstacleHeight;
        savedSquareTileMap.ForegroundColor    = foregroundColor;
        savedSquareTileMap.BackgroundColor    = backgroundColor;
        savedSquareTileMap.TileSize           = tileSize;
        savedSquareTileMap.TileOutlinePercent = tileOutlinePercent;
        savedSquareTileMap.tiles     = tiles;
        savedSquareTileMap.obstacles = obstacles;

        string fileName = string.Format("Assets/Resources/SquareTileMaps/{1}.asset", filePath, mapName);

        AssetDatabase.CreateAsset(savedSquareTileMap, fileName);
    }
Example #2
0
        private void UpdateElements()
        {
            IGraphData <Tile> data  = cachedContext.GetGraphData();
            SquareTileMapData sData = data as SquareTileMapData;

            if (tiles == null || tiles.Length == 0)
            {
                tiles = new UISquareTileElement[sData.RangeX, sData.RangeY];
                for (int i = 0; i < sData.RangeX; ++i)
                {
                    for (int j = 0; j < sData.RangeY; ++j)
                    {
                        UISquareTileElement tile = Instantiate <UISquareTileElement>(template, this.tilesRoot);
                        tile.Init(i, j, cachedContext);
                        (tile.transform as RectTransform).anchoredPosition = new Vector2((i + 0.5f) * 60f, (j + 0.5f) * 60f);
                        tiles[i, j] = tile;
                        tile.UpdateView();
                    }
                }
            }
            else
            {
                foreach (var cord in dirtyElements)
                {
                    UISquareTileElement tile = this.tiles[cord.x, cord.y];
                    tile.UpdateView();
                }
                dirtyElements.Clear();
            }
        }
Example #3
0
        private void UpdateTileCost(int x, int y, float cost)
        {
            IGraphData <Tile> data  = cachedContext.GetGraphData();
            SquareTileMapData sData = data as SquareTileMapData;

            sData.SetCost(x, y, cost);
            SetTileDirty(x, y);
        }
Example #4
0
        private void GetGHRhs(int x, int y, out float g, out float h, out float rhs)
        {
            SquareTileMapData sData = cachedContext.GetGraphData() as SquareTileMapData;

            g   = 0;
            h   = 0;
            rhs = 0;
            Tile tile = sData.GetTile(x, y);

            switch (cachedContext.TryGetNode(tile))
            {
            case NodeWrapperEx <Tile> nodeWrapperEx:
                g   = nodeWrapperEx.g;
                rhs = nodeWrapperEx.rhs;
                h   = nodeWrapperEx.h;
                break;
            }
        }
Example #5
0
        public void OnDrag(PointerEventData eventData)
        {
            switch (UISquareTileCtrl.Instance.opMode)
            {
            case UISquareTileCtrl.OperationMode.ModifyCost:
                Vector2           delta    = eventData.position - eventData.pressPosition;
                float             dy       = delta.y;
                SquareTileMapData sData    = cachedContext.GetGraphData() as SquareTileMapData;
                float             newValue = dragBeginValue + dy * 0.01f;
                newValue = Mathf.Clamp01(newValue);
                float cost = newValue * sData.CostScale;

                UISquareTileCtrl.Instance.ModifyTileCost(x, y, cost);
                break;

            case UISquareTileCtrl.OperationMode.ClickToSetStart:
            case UISquareTileCtrl.OperationMode.ClickToSetTarget:
                break;
            }
        }
Example #6
0
        public void SetDirty()
        {
            if (cachedContext == null)
            {
                return;
            }

            IGraphData <Tile> data  = cachedContext.GetGraphData();
            SquareTileMapData sData = data as SquareTileMapData;

            for (int i = 0; i < sData.RangeX; ++i)
            {
                for (int j = 0; j < sData.RangeY; ++j)
                {
                    dirtyElements.Add(new Vector2Int(i, j));
                }
            }

            dirty = true;
        }
Example #7
0
        public void OnPointerClick(PointerEventData eventData)
        {
            switch (UISquareTileCtrl.Instance.opMode)
            {
            case UISquareTileCtrl.OperationMode.ClickToSetStart:
                UISquareTileCtrl.Instance.setStartDelegate?.Invoke(x, y);
                break;

            case UISquareTileCtrl.OperationMode.ClickToSetTarget:
                UISquareTileCtrl.Instance.setTargetDelegate?.Invoke(x, y);
                break;

            case UISquareTileCtrl.OperationMode.ModifyCost:
                SquareTileMapData sData = cachedContext.GetGraphData() as SquareTileMapData;
                float             cost  = sData.GetCost(x, y);
                // 0 <-> 1 flip
                cost = sData.CostScale - cost;
                UISquareTileCtrl.Instance.ModifyTileCost(x, y, cost);
                break;
            }
        }
Example #8
0
 public void LoadData()
 {
     tileData = SerializeHelper.Load <SquareTileMapData>(assetFilePath);
 }
Example #9
0
        public void UpdateView()
        {
            SquareTileMapData sData = cachedContext.GetGraphData() as SquareTileMapData;

            switch (UISquareTileCtrl.Instance.colorMode)
            {
            case UISquareTileCtrl.ColorMode.TileCost:
                float cost  = sData.GetCost(x, y);
                float color = cost / sData.CostScale;
                SetColor(new Color(color, color, color));
                break;

            case UISquareTileCtrl.ColorMode.GHValue:
            {
                GetGH(x, y, out float g, out float h);
                float c = Mathf.Atan(g + h);
                SetColor(new Color(c, c, c));
            }
            break;

            case UISquareTileCtrl.ColorMode.OpenOrClose:
            {
                float c    = 0f;
                Tile  tile = sData.GetTile(x, y);
                NodeWrapper <Tile> nodeWrapper = cachedContext.TryGetNode(tile) as NodeWrapper <Tile>;
                if (nodeWrapper != null)
                {
                    if (nodeWrapper.state == NodeState.Open)
                    {
                        c = 0.75f;
                    }
                    else
                    {
                        c = 1.0f;
                    }
                }

                SetColor(new Color(c, c, c));
            }
            break;

            case UISquareTileCtrl.ColorMode.GValue:
            {
                GetGH(x, y, out float g, out float h);
                float c = Mathf.Atan(g);
                SetColor(new Color(c, c, c));
            }
            break;

            case UISquareTileCtrl.ColorMode.NodeConsistency:
            {
                float c    = 0f;
                Tile  tile = sData.GetTile(x, y);
                NodeWrapperEx <Tile> nodeWrapper = cachedContext.TryGetNode(tile) as NodeWrapperEx <Tile>;
                if (nodeWrapper != null)
                {
                    if (nodeWrapper.GetConsistency() == NodeConsistency.Consistent)
                    {
                        c = 0.75f;
                    }
                    else if (nodeWrapper.GetConsistency() == NodeConsistency.Overconsistent)
                    {
                        c = 1.0f;
                    }
                }

                SetColor(new Color(c, c, c));
            }
            break;
            }

            switch (UISquareTileCtrl.Instance.textMode)
            {
            case UISquareTileCtrl.TextMode.TileCost:
                float cost = sData.GetCost(x, y);
                this.label.text = $"{cost:f2}";
                break;

            case UISquareTileCtrl.TextMode.GHValue:
            {
                GetGH(x, y, out float g, out float h);
                this.label.text = $"{g:f2}\n{h:f2}";
            }
            break;

            case UISquareTileCtrl.TextMode.GHRhsValue:
            {
                GetGHRhs(x, y, out float g, out float h, out float rhs);
                this.label.text = $"{g:f2}\n{rhs:f2}";
            }
            break;
            }
        }
Example #10
0
        public void OnBeginDrag(PointerEventData eventData)
        {
            SquareTileMapData sData = cachedContext.GetGraphData() as SquareTileMapData;

            dragBeginValue = sData.GetCost(x, y) / sData.CostScale;
        }