示例#1
0
        public static bool HasObstacle(Tile tile1, Tile tile2)
        {
            Vector3 pos1 = tile1.GetPos();
            Vector3 pos2 = tile2.GetPos();

            float   dist      = Vector3.Distance(pos2, pos1);
            Vector3 dir       = (pos2 - pos1).normalized;
            Vector3 dirO      = new Vector3(-dir.z, 0, dir.x).normalized;
            float   posOffset = GridManager.GetTileSize() * GridManager.GetGridToTileSizeRatio() * 0.4f;

            LayerMask mask = 1 << TBTK.GetLayerObstacleFullCover() | 1 << TBTK.GetLayerObstacleHalfCover();

            if (Physics.Raycast(pos1, dir, dist, mask))
            {
                return(true);
            }
            if (Physics.Raycast(pos1 + dirO * posOffset, dir, dist, mask))
            {
                return(true);
            }
            if (Physics.Raycast(pos1 - dirO * posOffset, dir, dist, mask))
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        public void CheckDiagonal()
        {
            LayerMask mask = 1 << TBTK.GetLayerObstacleHalfCover() | 1 << TBTK.GetLayerObstacleFullCover();

            List <Tile> neighbourList = new List <Tile>(aStar.GetNeighbourList());

            for (int n = 0; n < neighbourList.Count; n++)
            {
                Vector3 dir    = neighbourList[n].GetPos() - GetPos();
                float   angleN = Utilities.Vector2ToAngle(new Vector2(dir.x, dir.z));

                if (angleN % 90 != 0)
                {
                    if (Physics.Linecast(GetPos(), neighbourList[n].GetPos(), mask))
                    {
                        aStar.DisconnectNeighbour(neighbourList[n]);
                    }
                }
            }
        }
        public static void InitCoverForTile(Tile tile)
        {
            List <Tile>  neighbourList = tile.GetNeighbourList();
            List <Cover> coverList     = new List <Cover>();

            for (int i = 0; i < tile.GetNeighbourList().Count; i++)
            {
                Vector3 dir  = (neighbourList[i].GetPos() - tile.GetPos()).normalized;
                float   dist = GridManager.GetTileSize() * GridManager.GetGridToTileSizeRatio() * .75f;

                LayerMask  mask = 1 << TBTK.GetLayerObstacleFullCover() | 1 << TBTK.GetLayerObstacleHalfCover();
                RaycastHit hit;
                if (Physics.Raycast(tile.GetPos(), dir, out hit, dist, mask))
                {
                    Cover cover = new Cover();
                    cover.angle = Mathf.Round(Utilities.Vector2ToAngle(new Vector2(dir.x, dir.z)));

                    if (GridManager.GetTileType() == _TileType.Square)                          //when diagonal neighbour is enabled, avoid adding cover to diagonal neighbour
                    {
                        if (cover.angle % 90 != 0)
                        {
                            continue;
                        }
                    }

                    int layer = hit.transform.gameObject.layer;
                    if (layer == TBTK.GetLayerObstacleFullCover())
                    {
                        cover.type = _CoverType.Full;
                        Debug.DrawLine(tile.GetPos(), tile.GetPos() + dir * dist, Color.red, 2);
                    }
                    else if (layer == TBTK.GetLayerObstacleHalfCover())
                    {
                        cover.type = _CoverType.Half;
                        Debug.DrawLine(tile.GetPos(), tile.GetPos() + dir * dist, Color.white, 2);
                    }


                    if (GridManager.GetTileType() == _TileType.Square)
                    {
                        cover.overlayPos = tile.GetPos() + dir * dist * 0.4f;
                    }
                    else if (GridManager.GetTileType() == _TileType.Hex)
                    {
                        cover.overlayPos = tile.GetPos() + dir * dist * 0.35f;
                    }

                    float angleY = cover.angle + 90;
                    if (cover.angle == 30)
                    {
                        angleY = cover.angle + 30;
                    }
                    else if (cover.angle == 150)
                    {
                        angleY = cover.angle - 30;
                    }
                    else if (cover.angle == 210)
                    {
                        angleY = cover.angle + 30;
                    }
                    else if (cover.angle == 330)
                    {
                        angleY = cover.angle - 30;
                    }
                    cover.overlayRot = Quaternion.Euler(0, angleY, 0);

                    coverList.Add(cover);
                }
            }

            tile.coverList = coverList;
        }
示例#4
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);
        }