Beispiel #1
0
        private void generateRandomMap(float percOpen = .65f,float percMonsters = .05f)
        {
            int numTiles = (x - 2) * (y - 2);
            int numToPopulate = (int)(numTiles * (percOpen));
            int numPopulated = 0;
            int direction;
            int numMonsters = (int)(numToPopulate * percMonsters);

            Vector2D position;
            Vector2D nextPosition;
            start = new Vector2D(rand.Next(1,x-1), rand.Next(1,y-1));

            playerPosition = start;
            position = start;

            layout[start.x][start.y].type = TileType.start;
            layout[start.x][start.y].playerIsHere = true;

            List<Vector2D> populatedTiles = new List<Vector2D>();

            if (numToPopulate > numTiles)
                throw new Exception("number of tiles to populate is too great");


            while (numPopulated < numToPopulate)
            {
                direction = rand.Next(1, 5);
                switch (direction)
                {
                    case 1:
                        nextPosition = new Vector2D(position.x, position.y - 1);
                        break;
                    case 2:
                        nextPosition = new Vector2D(position.x + 1, position.y);
                        break;
                    case 3:
                        nextPosition = new Vector2D(position.x, position.y +1);
                        break;
                    case 4:
                        nextPosition = new Vector2D(position.x-1, position.y);
                        break;
                    default:
                        throw new Exception("Direction selection is broken");
                }

                if ((nextPosition.x > 1 && nextPosition.x < x - 1) && (nextPosition.y > 1 && nextPosition.y < y - 1))
                {
                    position = nextPosition;
                    if (layout[position.x][position.y].type == TileType.closed)
                    {
                        layout[position.x][position.y].visible = false;
                        layout[position.x][position.y].type = TileType.open;

                        populatedTiles.Add(new Vector2D(position.x,position.y));
                        numPopulated++;
                    }
                }
            }
            for (int i = 0; i < numMonsters; i++)
            {
                int monsterToBeInt =  rand.Next(numToPopulate - i);
                Vector2D monsterToBe = populatedTiles[monsterToBeInt];
                populatedTiles.Remove(populatedTiles[monsterToBeInt]);
                layout[monsterToBe.x][monsterToBe.y].type = TileType.monster;
                layout[monsterToBe.x][monsterToBe.y].monsterIsHere = true;
                layout[monsterToBe.x][monsterToBe.y].visible = false;
            }
            numToPopulate -= numMonsters;
            int exitInt = rand.Next(numToPopulate);
            Vector2D exit = populatedTiles[exitInt];
            layout[exit.x][exit.y].type = TileType.end;
            layout[exit.x][exit.y].visible = false;

            fogOfWar();
        }
Beispiel #2
0
 private void generateTestMap()
 {
     for (int i = 0; i < x; i++)
     {
         for (int j = 0; j < y; j++)
         {
             layout[i][j].visible = true;
             layout[i][j].type  = TileType.open;
         }
     }
     //generates the closed tiles
     foreach (Vector2D v in mapCoords.closedTiles)
     {
         layout[v.x][v.y].type = TileType.closed;
     }
     foreach (Vector2D v in mapCoords.exitTiles)
     {
         layout[v.x][v.y].type = TileType.end;
     }
     foreach (Vector2D v in mapCoords.monsterTiles)
     {
         layout[v.x][v.y].type = TileType.monster;
     }
     foreach (Vector2D v in mapCoords.trapTiles)
     {
         layout[v.x][v.y].type = TileType.trap;
     }
     //closes the outer most walls
     for (int i = 0; i < x; i++)
     {
         layout[0][i].type = TileType.closed;
         layout[i][0].type = TileType.closed;
         layout[19][i].type = TileType.closed;
         layout[i][19].type = TileType.closed;
     }
     layout[x / 2][y / 2].type = TileType.start;
     playerPosition = new Vector2D(x / 2, y / 2);
     layout[x / 2][y / 2].playerIsHere = true;
 }