Esempio n. 1
0
        /// <summary>
        /// Calculates parameters for tiles with height and creates DrawableTiles from them
        /// </summary>
        private void FillTiles()
        {
            Random random = new Random();
            Dictionary <uint, int> borderTileCounter = new Dictionary <uint, int>();

            for (uint y = 0; y < map.Height; y++)
            {
                if (borderTileCounter.ContainsKey(map.GetTile(0, y).ID) && map.GetTile(0, y).Height == 0)
                {
                    borderTileCounter[map.GetTile(0, y).ID]++;
                }
                else if (map.GetTile(0, y).Height == 0)
                {
                    borderTileCounter.Add(map.GetTile(0, y).ID, 1);
                }

                if (borderTileCounter.ContainsKey(map.GetTile(map.Width - 1, y).ID) && map.GetTile(map.Width - 1, y).Height == 0)
                {
                    borderTileCounter[map.GetTile(map.Width - 1, y).ID]++;
                }
                else if (map.GetTile(map.Width - 1, y).Height == 0)
                {
                    borderTileCounter.Add(map.GetTile(map.Width - 1, y).ID, 1);
                }
            }
            for (uint x = 0; x < map.Width - 1; x++)
            {
                if (borderTileCounter.ContainsKey(map.GetTile(x, 0).ID) && map.GetTile(x, 0).Height == 0)
                {
                    borderTileCounter[map.GetTile(x, 0).ID]++;
                }
                else if (map.GetTile(x, 0).Height == 0)
                {
                    borderTileCounter.Add(map.GetTile(x, 0).ID, 1);
                }

                if (borderTileCounter.ContainsKey(map.GetTile(x, map.Height - 1).ID) && map.GetTile(x, map.Height - 1).Height == 0)
                {
                    borderTileCounter[map.GetTile(x, map.Height - 1).ID]++;
                }
                else if (map.GetTile(x, map.Height - 1).Height == 0)
                {
                    borderTileCounter.Add(map.GetTile(x, map.Height - 1).ID, 1);
                }
            }
            int maxValue = 0; uint maxValueTile = 0;

            foreach (KeyValuePair <uint, int> element in borderTileCounter)
            {
                if (element.Value > maxValue)
                {
                    maxValue     = element.Value;
                    maxValueTile = element.Key;
                }
            }
            //Load floor
            float minX = -10000f; float maxX = 100000f;
            float minY = -100000f; float maxY = 10000f;
            float Xspread = (maxX - minX) / Constants.TILE_SIZE;
            float Yspread = (maxY - minY) / Constants.TILE_SIZE;
            List <VertexPositionNormalTexture> verts = new List <VertexPositionNormalTexture>();

            verts.Add(new VertexPositionNormalTexture(new Vector3(minX, minY, -0.75f), Vector3.UnitZ, Vector2.UnitY * Yspread));
            verts.Add(new VertexPositionNormalTexture(new Vector3(minX, maxY, -0.75f), Vector3.UnitZ, Vector2.Zero));
            verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, minY, -0.75f), Vector3.UnitZ, new Vector2(Xspread, Yspread)));
            verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, maxY, -0.75f), Vector3.UnitZ, Vector2.UnitX * Xspread));
            verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, minY, -0.75f), Vector3.UnitZ, new Vector2(Xspread, Yspread)));
            verts.Add(new VertexPositionNormalTexture(new Vector3(minX, maxY, -0.75f), Vector3.UnitZ, Vector2.Zero));
            VertexGroup floor = new VertexGroup(TileList.GetTile((int)maxValueTile), verts);

            floor.Technique           = RendererAssetPool.UniversalEffect.Techniques.TexturedWrap;
            floor.CastsShadow         = false;
            floor.TransparencyEnabled = false;
            floor.Ready();
            renderer.ActiveScene.Add(floor, 0);
            //Loads the tiles into the background array
            for (uint y = 0; y < map.Height; y++)
            {
                for (uint x = 0; x < map.Width; x++)
                {
                    Tile tempTile = map.GetTile(x, y);
                    // Figure out the height of each tile in each direction relative to this one.

                    int positionNorth = (int)(y - 1);
                    int positionSouth = (int)(y + 1);
                    int positionWest  = (int)(x - 1);
                    int positionEast  = (int)(x + 1);
                    if (positionNorth >= 0)
                    {
                        tempTile.heightN = map.GetTile(x, y - 1).Height;
                    }
                    if (positionSouth < map.Height)
                    {
                        tempTile.heightS = map.GetTile(x, y + 1).Height;
                    }
                    if (positionWest >= 0)
                    {
                        tempTile.heightW = map.GetTile(x - 1, y).Height;
                    }
                    if (positionEast < map.Width)
                    {
                        tempTile.heightE = map.GetTile(x + 1, y).Height;
                    }
                    map.SetTile(x, y, tempTile);

                    Texture2D    texture = TileList.GetTile((int)tempTile.ID);
                    DrawableTile tile    = new DrawableTile(
                        texture, tempTile, (int)x, (int)y,
                        tempTile.heightN, tempTile.heightW, tempTile.heightE, tempTile.heightS);

                    Vector3 pos = new Vector3(
                        x * Constants.TILE_SIZE + (Constants.TILE_SIZE / 2),
                        -(y * Constants.TILE_SIZE + (Constants.TILE_SIZE / 2)),
                        0);
                    if (tempTile.ObjectID != 0)
                    {
                        Model   objectModel = TileList.GetObject(tempTile.ObjectID);
                        Object3 newTileObj  = new Object3(objectModel, pos + (Vector3.UnitZ * tempTile.Height * Constants.TILE_SIZE));
                        newTileObj.TransparencyEnabled = true;
                        Scene.Add(newTileObj, 1);
                    }

                    switch (tempTile.EventID)
                    {
                    case 4:
                    case 5:
                        GameSession.Alliance team = tempTile.EventID == 4 ?
                                                    GameSession.Alliance.RED : GameSession.Alliance.BLUE;
                        if (currentGameMode == VTankObject.GameMode.CAPTURETHEFLAG)
                        {
                            Flags.AddFlag(team, pos);
                        }
                        break;

                    case 8:
                    case 9:
                    case 10:
                        if (currentGameMode == VTankObject.GameMode.CAPTURETHEBASE)
                        {
                            Bases.AddBase(GameSession.Alliance.BLUE, tempTile.EventID, pos);
                        }
                        break;

                    case 11:
                    case 12:
                    case 13:
                        if (currentGameMode == VTankObject.GameMode.CAPTURETHEBASE)
                        {
                            Bases.AddBase(GameSession.Alliance.RED, tempTile.EventID, pos);
                        }
                        break;

                    default:
                        break;
                    }
                    visibleTiles[y * map.Width + x] = tile;
                }
            }


            #region Make Flat Tiles
            for (uint y = 0; y < map.Height; y++)
            {
                for (uint x = 0; x < map.Width; x++)
                {
                    Tile    tempTile = map.GetTile(x, y);
                    Vector3 pos      = new Vector3(x * Constants.TILE_SIZE, (-(y + 1) * Constants.TILE_SIZE), 0);
                    Tiles.AddFloor(pos, tempTile.Height, (int)tempTile.ID);
                }
            }
            #endregion

            #region Make North Walls
            int height = 0;
            int tileID = 0;
            int width  = 0;
            int Hdir   = 0;
            //Make north facing walls
            for (uint y = 0; y < map.Height; y++)
            {
                for (uint x = 0; x < map.Width; x++)
                {
                    Tile tempTile = map.GetTile(x, y);
                    if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightN)) && (x + 1) < map.Width)
                    {
                        height = tempTile.Height;
                        tileID = (int)tempTile.ID;
                        Hdir   = tempTile.heightN;
                        width++;
                    }
                    else
                    {
                        if (height > Hdir)
                        {
                            Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-y * Constants.TILE_SIZE), 0);
                            Tiles.AddWall(pos, Vector3.UnitY, width, height, tileID);
                        }
                        width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightN;
                    }
                }
                width = 0;
            }
            #endregion

            #region Make South Walls
            height = 0;
            tileID = 0;
            width  = 0;
            Hdir   = 0;
            uint startX = 0;
            //Make south facing walls
            for (uint y = 0; y < map.Height; y++)
            {
                for (uint x = 0; x < map.Width; x++)
                {
                    Tile tempTile = map.GetTile(x, y);
                    if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightS)) && (x + 1) < map.Width)
                    {
                        if (width == 0)
                        {
                            startX = x;
                        }
                        height = tempTile.Height;
                        tileID = (int)tempTile.ID;
                        Hdir   = tempTile.heightS;
                        width++;
                    }
                    else
                    {
                        if (height > Hdir)
                        {
                            Vector3 pos = new Vector3(startX * Constants.TILE_SIZE, (-(y + 1) * Constants.TILE_SIZE), 0);
                            Tiles.AddWall(pos, -Vector3.UnitY, width, height, tileID);
                        }
                        width  = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightS;
                        startX = x;
                    }
                }
                width = 0;
            }

            #endregion

            #region Make East Walls
            height = 0;
            tileID = 0;
            width  = 0;
            Hdir   = 0;
            //Make east facing walls
            for (uint x = 0; x < map.Width; x++)
            {
                for (uint y = 0; y < map.Height; y++)
                {
                    Tile tempTile = map.GetTile(x, y);
                    if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightE)) && (y + 1) < map.Height)
                    {
                        height = tempTile.Height;
                        tileID = (int)tempTile.ID;
                        Hdir   = tempTile.heightE;
                        width++;
                    }
                    else
                    {
                        if (height > Hdir)
                        {
                            Vector3 pos = new Vector3((x + 1) * Constants.TILE_SIZE, (-y * Constants.TILE_SIZE), 0);
                            Tiles.AddWall(pos, Vector3.UnitX, width, height, tileID);
                        }
                        width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightE;
                    }
                }
                width = 0;
            }

            #endregion

            #region Make West Walls
            height = 0;
            tileID = 0;
            width  = 0;
            Hdir   = 0;
            uint startY = 0;
            //Make south facing walls
            for (uint x = 0; x < map.Width; x++)
            {
                for (uint y = 0; y < map.Height; y++)
                {
                    Tile tempTile = map.GetTile(x, y);
                    if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightW)) && (y + 1) < map.Height)
                    {
                        if (width == 0)
                        {
                            startY = y;
                        }
                        height = tempTile.Height;
                        tileID = (int)tempTile.ID;
                        Hdir   = tempTile.heightW;
                        width++;
                    }
                    else
                    {
                        if (height > Hdir)
                        {
                            Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-startY * Constants.TILE_SIZE), 0);
                            Tiles.AddWall(pos, -Vector3.UnitX, width, height, tileID);
                        }
                        width  = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightW;
                        startY = y;
                    }
                }
                width = 0;
            }

            #endregion



            Tiles.AllReady();
            ////////////////////////////////////////////
            ///TODO:::: FOR EACH TILE GOING LEFT TO RIGHT
            //
            //   IF height = height  and tileID = tileID
            //       if Hnorth = Hnorth
            //          NorthWidth ++;
            //       else
            //          TexturedTileGroupManager.AddWall(lowerLeftNorth, Vector3.UnitY, NorthWidth, height, tileID)
            //          NorthWidth = 0;
            //          height = -1
            //

            //       if Hsouth = Hsouth
            //          SouthWidth ++;
            //       else
            //          TexturedTileGroupManager.AddWall(lowerLeftSouth, -Vector3.UnitY, SouthhWidth, height, tileID)
            //etc
        }