//used in edit mode only, re-allocate the spawn tile on spawnInfo based on the infor recorded on RecordSpawnTilePos(), and deployableTileList too
        //called when the grid has been regenerated
        public void SetStartingTileListBaseOnPos(float tileSize = 1)
        {
            for (int i = 0; i < spawnInfoList.Count; i++)
            {
                spawnInfoList[i].SetStartingTileListBaseOnPos(tileSize, ID);
            }

            GridManager gridManager = GridManager.GetInstance();

            deployableTileList = new List <Tile>();
            for (int i = 0; i < deployTilePosList.Count; i++)
            {
                int count = 0;
                for (int n = 0; n < gridManager.grid.tileList.Count; n++)
                {
                    float dist = Vector3.Distance(gridManager.grid.tileList[n].GetPos(), deployTilePosList[i]);
                    if (dist < tileSize / 2 && !deployableTileList.Contains(gridManager.grid.tileList[n]))
                    {
                        count += 1;
                        gridManager.grid.tileList[n].deployAreaID = ID;
                        deployableTileList.Add(gridManager.grid.tileList[n]);
                    }
                }
            }

            deployTilePosList = new List <Vector3>();
        }
        public void SetStartingTileListBaseOnPos(float tileSize = 1, int factionID = 0)
        {
            GridManager gridManager = GridManager.GetInstance();

            startingTileList = new List <Tile>();
            for (int i = 0; i < startingTilePosList.Count; i++)
            {
                int count = 0;
                for (int n = 0; n < gridManager.grid.tileList.Count; n++)
                {
                    float dist = Vector3.Distance(gridManager.grid.tileList[n].GetPos(), startingTilePosList[i]);
                    if (dist < tileSize / 2 && !startingTileList.Contains(gridManager.grid.tileList[n]))
                    {
                        count += 1;
                        gridManager.grid.tileList[n].spawnAreaID = factionID;
                        startingTileList.Add(gridManager.grid.tileList[n]);
                    }
                }
            }

            startingTilePosList = new List <Vector3>();
        }
        //used in editor only
        public Tile GetNeighbourInDir(Tile tile, float angle)
        {
            int ID = tileList.IndexOf(tile);

            GridManager gridManager = GridManager.GetInstance();

            if (gridManager.tileType == _TileType.Square)
            {
                float distTH = gridManager.tileSize * gridManager.gridToTileRatio * 1.1f;

                if (angle == 90)
                {
                    if (ID + 1 < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[ID + 1].GetPosG()) <= distTH)
                    {
                        return(tileList[ID + 1]);
                    }
                }
                else if (angle == 180)
                {
                    ID -= gridManager.length;
                    if (ID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[ID].GetPosG()) <= distTH)
                    {
                        return(tileList[ID]);
                    }
                }
                else if (angle == 270)
                {
                    if (ID - 1 >= 0 && Vector3.Distance(tile.GetPosG(), tileList[ID - 1].GetPosG()) <= distTH)
                    {
                        return(tileList[ID - 1]);
                    }
                }
                else if (angle == 0)
                {
                    ID += gridManager.length;
                    if (ID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[ID].GetPosG()) <= distTH)
                    {
                        return(tileList[ID]);
                    }
                }
            }
            else if (gridManager.tileType == _TileType.Hex)
            {
                float distTH = GridGenerator.spaceZHex * gridManager.tileSize * gridManager.gridToTileRatio * 1.1f;

                if (angle == 90)
                {
                    if (ID + 1 < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[ID + 1].GetPosG()) <= distTH)
                    {
                        return(tileList[ID + 1]);
                    }
                }
                else if (angle == 150)
                {
                    ID -= gridManager.length - 1;
                    if (ID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[ID].GetPosG()) <= distTH)
                    {
                        return(tileList[ID]);
                    }
                }
                else if (angle == 210)
                {
                    ID -= gridManager.length;
                    if (ID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[ID].GetPosG()) <= distTH)
                    {
                        return(tileList[ID]);
                    }
                }
                else if (angle == 270)
                {
                    if (ID - 1 > 0 && Vector3.Distance(tile.GetPosG(), tileList[ID - 1].GetPosG()) <= distTH)
                    {
                        return(tileList[ID - 1]);
                    }
                }
                else if (angle == 330)
                {
                    ID += gridManager.length - 1;
                    if (ID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[ID].GetPosG()) <= distTH)
                    {
                        return(tileList[ID]);
                    }
                }
                else if (angle == 30)
                {
                    ID += gridManager.length;
                    if (ID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[ID].GetPosG()) <= distTH)
                    {
                        return(tileList[ID]);
                    }
                }
            }

            return(null);
        }
        public void Init()
        {
            for (int i = 0; i < tileList.Count; i++)
            {
                tileList[i].Init();
            }

            GridManager gridManager = GridManager.GetInstance();

            //setup the neighbour of each tile
            if (gridManager.tileType == _TileType.Square)
            {
                float distTH = gridManager.tileSize * gridManager.gridToTileRatio * 1.1f;
                for (int i = 0; i < tileList.Count; i++)
                {
                    Tile tile = tileList[i];
                    tile.aStar = new TileAStar(tile);

                    List <Tile> neighbourList = new List <Tile>();

                    int nID = i + 1;
                    if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i + gridManager.length;
                    if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i - 1;
                    if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i - gridManager.length;
                    if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    //tile.aStar.straightNeighbourCount=neighbourList.Count;

                    //diagonal neighbour, not in used
                    if (GridManager.EnableDiagonalNeighbour())
                    {
                        nID = (i + 1) + gridManager.length;
                        if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f)
                        {
                            neighbourList.Add(tileList[nID]);
                        }

                        nID = (i + 1) - gridManager.length;
                        if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f)
                        {
                            neighbourList.Add(tileList[nID]);
                        }

                        nID = (i - 1) + gridManager.length;
                        if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f)
                        {
                            neighbourList.Add(tileList[nID]);
                        }

                        nID = (i - 1) - gridManager.length;
                        if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f)
                        {
                            neighbourList.Add(tileList[nID]);
                        }
                    }

                    tile.aStar.SetNeighbourList(neighbourList);
                }
            }
            else if (gridManager.tileType == _TileType.Hex)
            {
                float distTH = GridGenerator.spaceZHex * gridManager.tileSize * gridManager.gridToTileRatio * 1.1f;
                for (int i = 0; i < tileList.Count; i++)
                {
                    Tile tile = tileList[i];
                    tile.aStar = new TileAStar(tile);

                    List <Tile> neighbourList = new List <Tile>();

                    int nID = i + 1;
                    if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i + gridManager.length;
                    if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i + gridManager.length - 1;
                    if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i - 1;
                    if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i - gridManager.length;
                    if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    nID = i - gridManager.length + 1;
                    if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH)
                    {
                        neighbourList.Add(tileList[nID]);
                    }

                    tile.aStar.SetNeighbourList(neighbourList);

                    //tile.aStar.straightNeighbourCount=neighbourList.Count;
                }
            }

            //setup the wall
            for (int i = 0; i < tileList.Count; i++)
            {
                tileList[i].InitWall();
            }

            if (GridManager.EnableDiagonalNeighbour())
            {
                for (int i = 0; i < tileList.Count; i++)
                {
                    tileList[i].CheckDiagonal();
                }
            }

            //setup the cover
            if (GameControl.EnableCover())
            {
                for (int i = 0; i < tileList.Count; i++)
                {
                    CoverSystem.InitCoverForTile(tileList[i]);
                }
            }
        }
Beispiel #5
0
        public void AddObstacle(int obsType, float gridSize)
        {
            if (obstacleT != null)
            {
                if (obstacleT.gameObject.layer == TBTK.GetLayerObstacleHalfCover() && obsType == 1)
                {
                    return;
                }
                if (obstacleT.gameObject.layer == TBTK.GetLayerObstacleFullCover() && obsType == 2)
                {
                    return;
                }

                                #if UNITY_EDITOR
                Undo.DestroyObjectImmediate(obstacleT.gameObject);
                                #endif
            }

            if (wallList.Count > 0)
            {
                if (!Application.isPlaying)
                {
                    Grid grid = GridManager.GetInstance().GetGrid();
                    while (wallList.Count > 0)
                    {
                        RemoveWall(wallList[0].angle, grid.GetNeighbourInDir(this, wallList[0].angle));
                    }
                }
                else
                {
                    while (wallList.Count > 0)
                    {
                        RemoveWall(wallList[0].angle, GetNeighbourFromAngle(wallList[0].angle));
                    }
                }
            }

            //float gridSize=GridManager.GetTileSize();

                        #if UNITY_EDITOR
            Transform obsT = (Transform)PrefabUtility.InstantiatePrefab(GridManager.GetObstacleT(obsType));
            Undo.RecordObject(this, "Tile");
            Undo.RecordObject(GetComponent <Renderer>(), "TileRenderer");
            Undo.RegisterCreatedObjectUndo(obsT.gameObject, "Obstacle");
                        #else
            Transform obsT = (Transform)Instantiate(GridManager.GetObstacleT(obsType));
                        #endif



            //~ float offsetY=0;
            //~ if(type==_TileType.Square){
            //~ if(obsType==1)offsetY=obsT.localScale.z*gridSize/4;
            //~ if(obsType==2)offsetY=obsT.localScale.z*gridSize/2;
            //~ }
            //~ else if(type==_TileType.Hex) offsetY=obsT.localScale.z*gridSize/4;

            obsT.position = GetPos();          //+new Vector3(0, offsetY, 0);

            obsT.localScale *= gridSize * GridManager.GetGridToTileSizeRatio();
            obsT.parent      = transform;

            obstacleT = obsT;
            walkable  = false;

            GetComponent <Renderer>().enabled = false;

            //SetState(_TileState.Default);
        }