/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); Vector2 firstSquare = new Vector2(Cam.Origin.X / 32, Cam.Origin.Y / 32); int firstX = (int)firstSquare.X; int firstY = (int)firstSquare.Y; Vector2 squareOffset = new Vector2(Cam.Origin.X % 32, Cam.Origin.Y % 32); int offsetX = (int)squareOffset.X; int offsetY = (int)squareOffset.Y; for (int y = 0; y < Cam.sizeY; y++) { for (int x = 0; x < Cam.sizeX; x++) { MapCell currentCell = null; if (myMap.map[x + firstX, y + firstY] == null) { currentCell = new MapCell(0); } else { currentCell = myMap.map[x + firstX, y + firstY]; } spriteBatch.Draw( Tile.TileSetTexture, new Rectangle((x * 32) - offsetX, (y * 32) - offsetY, 32, 32), Tile.GetSourceRectangle(currentCell.TileID), Color.White); } } /*for (int y = 0; y < squaresDown; y++) { for (int x = 0; x < squaresAcross; x++) { spriteBatch.Draw( Tile.TileSetTexture, new Rectangle((x * 32) - offsetX, (y * 32) - offsetY, 32, 32), Tile.GetSourceRectangle(myMap.Rows[y + firstY].Columns[x + firstX].TileID), Color.White); } }*/ spriteBatch.End(); // TODO: Add your drawing code here base.Draw(gameTime); }
public TileMap() { /* Pick between 1 and 6 peaks randomly. Assign each one a random height between 60 and 100 * decrease the height of sorrounding tiles by between 2 and 10 percent to a minimum of -20. * once a tile has been assigned, dont assign to it again, skip it! */ Random randGen = new Random(); List<MapCell> Peaks = new List<MapCell>(); map = new MapCell[MapWidth, MapHeight]; List<Vector2> PeakPositions = new List<Vector2>(); int numberPeaks = randGen.Next(0, 6); // create peaks, assign random positions to the peaks for (int i = 0; i < numberPeaks; i++) { MapCell currentPeak = new MapCell(randGen.Next(70, 100)); Peaks.Add(currentPeak); Vector2 currentPosition = new Vector2(randGen.Next(0, MapWidth), randGen.Next(0, MapHeight)); PeakPositions.Add(currentPosition); map[(int)currentPosition.X, (int)currentPosition.Y] = currentPeak; } // go throught the peak positions. Assign tiles in order around them. Go back and do this until no more unassigned tiles are found. // tiles in a square increase by 8 every one iteration. There are n squares until a change in direction (n*2 for a full side of the square //do one square around every peak, n distance away, until no more assigned tiles are gound. Skip over assigned tiles. for (int n = 1; n < 2; n++)//n should be much larger later { foreach (Vector2 p in PeakPositions) { Random elevationGen = new Random(); int currentPeakIndex = PeakPositions.IndexOf(p); //Peak with position = p in PeakPositions int startingX = (int)p.X; int startingY = (int)p.Y; //we have the peak's position. Now we add the offset (n) to the startingX, and assign mapcells with elevations around in a square. if (n == 1) //first iteration, set the peak tile { map[startingX, startingY] = Peaks[currentPeakIndex]; } //randomize the elevation by subtracting a random amount from an adjacent cell //there are n * 8 tiles in each square: 1, 8, 16, 24.... //need formula for # of squares per side based on n - sequence is 1, 3, 5, 7 - n+2 //first cell is at (x-((n+2)/2 *rounded up* ),y-1 /* [1] [2] [3] <= square index [x-ciel((n+2)/2),y-1] [-] [-] <= x and y * [8] [4] [-] [-] * [7] [6] [5] [-] [-] [-] */ //go row by row for (int squareIndexY = 1; squareIndexY < (n + 2); squareIndexY ++) { //count down the rows on the y axis //if the row is 1 or n+2, then the process is different if (squareIndexY == 1 || squareIndexY == (n + 2)) { // count across and fill in the row } else { double newCellX = 0; double newCellY = 0; double newCellX2 = 0; double newCellY2 = 0; // first cell at (P(x)-ciel((n+2)/2), p(y)-n) // place a new map cell on the left and right side of the square newCellX = startingX - n; newCellY = startingY - n; MapCell NewCell = new MapCell(randGen.Next(50, 70)); map[(int)newCellX, (int)newCellY] = NewCell; newCellX2 = startingX + n; newCellY2 = startingY + n; MapCell NewCell2 = new MapCell(randGen.Next(50, 70)); map[(int)newCellX2, (int)newCellY2] = NewCell2; } } } } }