Example #1
0
        // Try to build the specified tower at the current tile.
        // Returns true if succeeds
        public bool TryBuild(eTowerType type)
        {
            // Check and make sure we can afford this building
            int BuildCost = 0;

            switch (type)
            {
            case (eTowerType.Projectile):
                BuildCost = Balance.ProjectileTower[0].BuildCost;
                break;

            case (eTowerType.Slow):
                BuildCost = Balance.SlowTower[0].BuildCost;
                break;
            }
            if (BuildCost > Money)
            {
                m_UIGameplay.ShowErrorMessage(Localization.Get().Text("ui_err_money"));
                return(false);
            }

            // Create the tower
            // We don't use SpawnGameObject here because the Tower is a child of its Tile
            Objects.Tower tower = null;
            switch (type)
            {
            case (eTowerType.Projectile):
                tower = new Objects.ProjectileTower(m_Game);
                break;

            case (eTowerType.Slow):
                tower = new Objects.SlowTower(m_Game);
                break;
            }

            tower.Load();

            // Check whether or not there still is a route for enemies to take
            // before building this!
            m_SelectedTile.PreviewBuild(tower);
            if (!Pathfinder.Get().ComputeAStar())
            {
                m_UIGameplay.ShowErrorMessage(Localization.Get().Text("ui_err_block"));

                // Clear out this tower and recompute old path
                m_SelectedTile.ClearTower();

                Pathfinder.Get().ComputeAStar();
                return(false);
            }
            else
            {
                // Any active enemies need to compute new paths, in case the old one is blocked
                foreach (Objects.Enemy e in m_Enemies)
                {
                    Pathfinder.Get().ComputeAStar(e);
                }

                m_SelectedTile.Build(tower, false);
                m_UIGameplay.FinishBuild();
            }

            return(true);
        }
Example #2
0
        public virtual void LoadLevel(string sLevelName)
        {
            m_Tiles.Clear();
            int iWidth  = 10;
            int iHeight = 5;

            int[,] TileData =
            {
                { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 2, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
                { 2, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            };

            // Generate tile array based on width/height
            // (used to setup neighbors)
            Objects.Tile[,] Tiles = new Objects.Tile[iHeight, iWidth];

            float fPerColumn = 1.72f;
            float fPerRow    = 1.5f;

            Objects.Tile BaseTile  = null;
            Objects.Tile SpawnTile = null;

            int   iColCount = 0;
            float fXOffset  = -1.0f * (iWidth / 2) * fPerColumn;

            while (iColCount < iWidth)
            {
                int   iRowCount = 0;
                float fZOffset  = -1.0f * (iHeight / 2) * fPerRow;
                while (iRowCount < iHeight)
                {
                    eTileType    type        = (eTileType)TileData[iRowCount, iColCount];
                    float        fTileHeight = GlobalDefines.fTileHeight;
                    Objects.Tile t;
                    if (iRowCount % 2 == 0)
                    {
                        t = CreateTile(new Vector3(fXOffset, fTileHeight, fZOffset), type);
                    }
                    else
                    {
                        t = CreateTile(new Vector3(fXOffset - 0.875f, fTileHeight, fZOffset), type);
                    }

                    Tiles[iRowCount, iColCount] = t;
                    if (type == eTileType.Base)
                    {
                        BaseTile = t;
                    }
                    else if (type == eTileType.EnemySpawn)
                    {
                        SpawnTile = t;
                    }
                    fZOffset += fPerRow;
                    iRowCount++;
                }

                fXOffset += fPerColumn;
                iColCount++;
            }

            // Now loop through the array of tiles to assign neighbors.
            // Since these are hexagons, the row affects which hexagons are neighbors.
            for (int i = 0; i < iHeight; i++)
            {
                for (int j = 0; j < iWidth; j++)
                {
                    // East/West are same regardless of row modulus
                    // E
                    if (j + 1 < iWidth)
                    {
                        Tiles[i, j].AddNeighbor(Tiles[i, j + 1]);
                    }
                    // W
                    if (j - 1 >= 0)
                    {
                        Tiles[i, j].AddNeighbor(Tiles[i, j - 1]);
                    }

                    if (i % 2 == 0)
                    {
                        // NE
                        if ((i - 1 >= 0) && (j + 1 < iWidth))
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i - 1, j + 1]);
                        }
                        // SE
                        if ((i + 1 < iHeight) && (j + 1 < iWidth))
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i + 1, j + 1]);
                        }
                        // SW
                        if (i + 1 < iHeight)
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i + 1, j]);
                        }
                        // NW
                        if (i - 1 >= 0)
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i - 1, j]);
                        }
                    }
                    else
                    {
                        // NE
                        if (i - 1 >= 0)
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i - 1, j]);
                        }
                        // SE
                        if (i + 1 < iHeight)
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i + 1, j]);
                        }
                        // SW
                        if ((i + 1 < iHeight) && (j - 1 >= 0))
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i + 1, j - 1]);
                        }
                        // NW
                        if ((i - 1 >= 0) && (j - 1 >= 0))
                        {
                            Tiles[i, j].AddNeighbor(Tiles[i - 1, j - 1]);
                        }
                    }
                }
            }

            // These values let the camera know what the maximum scroll area should be.
            GameState.Get().Camera.LevelMin = Tiles[0, 0].Position;
            GameState.Get().Camera.LevelMax = Tiles[iHeight - 1, iWidth - 1].Position;


            // Create the player's base and initial path for enemies
            if (BaseTile != null && SpawnTile != null)
            {
                BaseTile.Build(new Objects.Base(m_Game));

                Pathfinder.Get().GlobalStartTile = SpawnTile;
                Pathfinder.Get().GlobalGoalTile  = BaseTile;
                Pathfinder.Get().ComputeAStar();

                GameState.Get().SetSelected(BaseTile);
            }
        }