コード例 #1
0
    public static List <Vector2Int> GeneratePath(int[,] mapValues)
    {
        List <Vector2Int> walkable = new List <Vector2Int>();
        Vector2Int        start    = Vector2Int.zero;
        Vector2Int        end      = Vector2Int.zero;

        for (int x = 0; x < mapValues.GetLength(0); x++)
        {
            for (int y = 0; y < mapValues.GetLength(1); y++)
            {
                int      number = mapValues[x, y];
                TileType type   = TileMethods.TypeById[number];
                if (TileMethods.IsWalkable(type))
                {
                    walkable.Add(new Vector2Int(x, y));
                }
                switch (type)
                {
                case TileType.Start: start = new Vector2Int(x, y); break;

                case TileType.End: end = new Vector2Int(x, y); break;

                default: break;
                }
            }
        }
        AI.IPathFinder pathFinder = new AI.Dijkstra(walkable);
        walkable = (List <Vector2Int>)pathFinder.FindPath(start, end);
        return(walkable);
    }
コード例 #2
0
    private void FindAccessibleTiles(Map map)
    {
        List <Vector2Int> accessibles = new List <Vector2Int>();
        Vector2Int        startPos    = Vector2Int.zero;
        Vector2Int        endPos      = Vector2Int.zero;

        foreach (Tile tile in map.tiles)
        {
            if (TileMethods.IsWalkable(TileMethods.TypeByChar[tile.id]))
            {
                accessibles.Add(new Vector2Int(tile.x, tile.y));

                if (TileMethods.TypeByChar[tile.id] == TileType.Start)
                {
                    startPos = new Vector2Int(tile.x, tile.y);
                }
                else if (TileMethods.TypeByChar[tile.id] == TileType.End)
                {
                    endPos = new Vector2Int(tile.x, tile.y);
                }
            }
        }
        if (startPos == endPos)
        {
            return;
        }

        IPathFinder pathFinder = new Dijkstra(accessibles);

        s_Path = new List <Vector2Int>(pathFinder.FindPath(startPos, endPos));
    }
コード例 #3
0
    private void PlaceTile(TileType tileType, int x, int y, Vector3 worldStart)
    {
        TileData tile = TileMethods.GetTile(tileType);

        if (tile.prefab)
        {
            GameObject newTile = tileDataByPool[tile].Get();
            newTile.SetActive(true);

            if (newTile)
            {
                newTile.transform.localPosition = new Vector3(worldStart.x + x * 2, 0f, worldStart.y - y * 2);

                // Astar
                Astar.AddNode(newTile.transform.localPosition, x, y, tile.walkable);
                if (tileType == TileType.Start)
                {
                    MapData.startTile.x = x;
                    MapData.startTile.y = y;
                }
                else if (tileType == TileType.End)
                {
                    MapData.endTile.x = x;
                    MapData.endTile.y = y;
                }
            }
        }
    }
コード例 #4
0
    public void MoveUnits(GameObject fromTile)
    {
        TileMethods    selectedTileMethods = fromTile.GetComponent <TileMethods>();
        TileInfoHolder selectedTileInfo    = fromTile.GetComponent <TileInfoHolder>();

        if (selectedTileInfo.neighbours.Contains(gameObject))
        {
            if (selectedTileMethods.DeleteUnits(1))
            {
                AddUnits(1);
            }
        }
    }
コード例 #5
0
 void MouseClick()
 {
     if (Input.GetMouseButtonDown(0))
     {
         selectedTile = gameObject;
         Debug.Log("Selected: " + selectedTile.name);
     }
     else if (Input.GetMouseButtonDown(1))
     {
         TileMethods methods = GetComponent <TileMethods>();
         methods.MoveUnits(selectedTile);
     }
 }
コード例 #6
0
    public void CreateGameObject(Class_BoardSquare.BoardSquare tile)
    {
        // Serialization does not exist for unity specific types (vector3, quaternion, so we
        // deserialize as ints, and then convert to vectors and then finally quaternions)
        Vector3    location        = new Vector3(tile.int_location[0], tile.int_location[1], tile.int_location[2]);
        Vector3    vector_rotation = new Vector3(tile.int_rotation[0], tile.int_rotation[1], tile.int_rotation[2]);
        Quaternion rotation        = Quaternion.Euler(vector_rotation);
        //The above three lines are just conversions
        GameObject self = Instantiate(tile0, location, rotation, gameObject.transform);

        self.name = tile.name;
        TileMethods tileMethods = self.GetComponent <TileMethods>();
    }
コード例 #7
0
 private void ReadAcceccibleTiles()
 {
     for (int x = 0; x < m_Tiles.GetLength(0); x++)
     {
         for (int y = 0; y < m_Tiles.GetLength(1); y++)
         {
             if (TileMethods.IsWalkable(m_Tiles[x, y]))
             {
                 m_Accessibles.Add(new Vector2Int(x, y));
             }
         }
     }
 }
コード例 #8
0
    private void Start()
    {
        tileDataByPool = new Dictionary <TileData, IPool <GameObject> >();

        var tileEnumArray = Enum.GetValues(typeof(TileType));

        for (int i = 0; i < tileEnumArray.Length; i++)
        {
            TileType tileType = (TileType)tileEnumArray.GetValue(i);
            TileData tile     = TileMethods.GetTile(tileType);

            tileDataByPool.Add(tile, new TilePool(tile.prefab, transform));
        }
    }
コード例 #9
0
ファイル: MapCreator.cs プロジェクト: FGEbba/TowerDefence
        private static void SpawnMap(List <MapData> data, int mapWidth)
        {
            int mapLength = theMap.Count;

            for (int iRun = mapLength - 1, i = 0; iRun >= 0; iRun--, i++)
            {
                for (int j = 0; j < mapWidth; j++)
                {
                    GameObject currentTilePrefab = null;
                    TileType   currentTileType;
                    TileMethods.TypeById.TryGetValue(theMap[iRun][j], out currentTileType);
                    foreach (MapData mapData in data)
                    {
                        if (mapData.tileType.Equals(currentTileType))
                        {
                            if (TileMethods.IsWalkable(currentTileType))
                            {
                                CreateEnemyPath(j, i);
                                if (currentTileType == TileType.Start)
                                {
                                    start = new Vector3(j * BlockSize, 0, i * BlockSize);
                                }
                                if (currentTileType == TileType.End)
                                {
                                    end = new Vector3(j * BlockSize, 0, i * BlockSize);
                                }
                            }
                            currentTilePrefab = mapData.tilePrefab;
                            SpawnTile(currentTilePrefab, j, i, currentTileType);
                            break;
                        }
                    }
                    if (currentTilePrefab == null)
                    {
                        throw new NullReferenceException($"Unable to find prefab for {currentTileType}. No reference has been created by the user.");
                    }
                }
            }
        }
コード例 #10
0
    public void MapCreator()
    {
        m_WalkablePath = new List <Vector2Int>();
        for (int lineIndex = m_Lines.Count - 1, rowIndex = 0; lineIndex >= 0; lineIndex--, rowIndex++)
        {
            string line = m_Lines[lineIndex];

            for (int columnIndex = 0; columnIndex < line.Length; columnIndex++)
            {
                char item = line[columnIndex];

                float z = rowIndex * m_cellSize;
                float x = columnIndex * m_cellSize;
                switch (item)
                {
                case m_CharPath:
                    m_CurrentTileChar = m_CharPath;
                    break;

                case m_CharOsbtacle:
                    m_CurrentTileChar = m_CharOsbtacle;
                    break;

                case m_CharStartEnenmyBase:
                    m_CurrentTileChar = m_CharStartEnenmyBase;
                    break;

                case m_CharEndPlayerBase:
                    m_CurrentTileChar = m_CharEndPlayerBase;
                    break;

                case m_CharTowerDefOne:
                    m_CurrentTileChar = m_CharTowerDefOne;
                    break;

                case m_CharTowerDefTwo:
                    m_CurrentTileChar = m_CharTowerDefTwo;
                    break;
                }

                TileType   tileType      = TileMethods.TypeByIdChar[m_CurrentTileChar];
                GameObject currentPrefab = m_prefabById[tileType];
                Vector3    currentPos    = new Vector3(x, m_Offset, z);
                GameObject test          = GameObject.Instantiate(currentPrefab, currentPos, Quaternion.identity);

                if (TileMethods.IsWalkable(tileType))
                {
                    Vector2Int localPos = new Vector2Int((int)test.transform.localPosition.x, (int)test.transform.localPosition.z);

                    if (tileType == TileType.Start)
                    {
                        m_StartTilePos = localPos;
                    }
                    if (tileType == TileType.End)
                    {
                        m_EndTilePos = localPos;
                        m_EndObject  = test;
                    }
                    m_WalkablePath.Add(localPos);
                }
            }
        }
    }
コード例 #11
0
    public void CreateMap()
    {
        for (int row = lines.Count - 1, rowIndex = 0; row >= 0; row--, rowIndex++)
        {
            string line = lines[row];

            for (int column = 0; column < line.Length; column++)
            {
                char  item = line[column];
                float z    = rowIndex * size;
                float x    = column * size;

                switch (item)
                {
                case charPath:
                    currentTileChar = charPath;
                    break;

                case charObstacle:
                    currentTileChar = charObstacle;
                    break;

                case charTowerOne:
                    currentTileChar = charTowerOne;
                    break;

                case charTowerTwo:
                    currentTileChar = charTowerTwo;
                    break;

                case charStartEnemyBase:
                    currentTileChar = charStartEnemyBase;
                    break;

                case charEndPlayerBase:
                    currentTileChar = charEndPlayerBase;
                    break;

                case mapTileEnd:
                    break;

                default:
                    break;
                }

                TileType   type          = TileMethods.TypeByIdChar[currentTileChar];
                GameObject currentPrefab = m_PrefabsById[type];
                GameObject inst          = GameObject.Instantiate(currentPrefab, new Vector3(x, 0, z), Quaternion.identity);

                if (TileMethods.IsWalkable(type))
                {
                    Vector2Int pos = new Vector2Int(Mathf.RoundToInt(inst.transform.position.x),
                                                    Mathf.RoundToInt(inst.transform.position.z));

                    if (type == TileType.Start)
                    {
                        startPos = pos;
                    }
                    else if (type == TileType.End)
                    {
                        endPos = pos;
                    }

                    mapTiles.Add(pos);
                }
            }
        }
    }