Java Roguelike notes (3/13): How to place random items and enemies in our dungeon

The code for both methods (place items and place enemies) is very similar, but contains some differences:

  • We create a reduced sublist from listOfFloorTilePositions , this is our list with all Floor Positions in our map, also works as our list of available positions to spawn something on top.
  • Shuffle and randomize the sublist, then instantiate a new Tile at that positions, and modify each list accordingly (like adding a new item to the item list, and removing this available tile from the general list)
  • Edit the draw(Graphics g) method to repaint the new elements

setItems()

    public void setItems(){
        System.out.println("TODO [GamePanel]: Setting up Items.");
        // 0. Create a sublist based on available floor positions, then get random coordinates from there and create objects.
        Collections.shuffle(listOfFloorTilePositions);
        int randomSeriesLength = 5;
        List<Point2D> randomSeries = listOfFloorTilePositions.subList(0, randomSeriesLength);
        for (int i=0; i<randomSeries.size();i++){
            System.out.println("random loc " + i + ": " + randomSeries.get(i));
            Tile _initialItemLocation = new Tile();
            _initialItemLocation.tilePosition = helperUnitMultiplier(randomSeries.get(i).getX(), randomSeries.get(i).getY()); //.Double(_loc.getX(),_loc.getY());
            listOfOccupiedTilePositions.add(_initialItemLocation.tilePosition);
            listOfFloorTilePositions.remove(_initialItemLocation.tilePosition); // We remove this position from the List, so is reserved for generating items later, we don't want to spawn something else here.
            listOfItemTiles.add(_initialItemLocation);
        }
    }

setEnemies()

    public void setEnemies(){
        System.out.println("TODO [GamePanel]: Setting up Enemies.");
        Collections.shuffle(listOfFloorTilePositions);
        int randomSeriesLength = 5;
        List<Point2D> randomSeries = listOfFloorTilePositions.subList(0, randomSeriesLength);
        for (int i=0; i<randomSeries.size();i++){
            //System.out.println("random loc " + i + ": " + randomSeries.get(i));
            Tile _initialEnemyLocation = new Tile();
            _initialEnemyLocation.tilePosition = helperUnitMultiplier(randomSeries.get(i).getX(), randomSeries.get(i).getY()); //.Double(_loc.getX(),_loc.getY());
            listOfOccupiedTilePositions.add(_initialEnemyLocation.tilePosition);
            listOfFloorTilePositions.remove(_initialEnemyLocation.tilePosition); // We remove this position from the List, so is reserved for generating items later, we don't want to spawn something else here.
            listOfEnemyTiles.add(_initialEnemyLocation);
        }
    }

Caveats:

  • Random() may act funny here: As we’re getting a random element of the same list for different types of Tiles (listOfFloorTilePositions) we’re also using the same seed for this random number generator, which means is not random anymore and Tiles will be instantiated following the same pattern. We would need to share the same Random() instance across the whole class instead of creating a new Random object each time we call the function, however I decided to shuffle sublists instead as fits better for this specific case.
  • Most likely this can be merged into an unique method, as the Exit tile and others will also follow the same logic. I’ll leave this to the latest stages of the project though.
  • At the moment Walls, Floors, Enemies, and Items are the same object, a Tile, just when we render the graphics and draw() methods we apply differences. This will change soon.

by

Comments

One response to “Java Roguelike notes (3/13): How to place random items and enemies in our dungeon”

  1. […] Place random items and enemies: Link to Post […]

Leave a Reply

Discover more from Home

Subscribe now to keep reading and get access to the full archive.

Continue reading