Skip to content

Random Map Generation System

Jantoom edited this page Oct 6, 2021 · 2 revisions

Introduction

On creation of a new world, random map assets are loaded to compose a home. These map assets can describe floor plans or room interiors by mapping symbols to individual objects and placing these symbols onto a grid.

The random generation has support for rectangular room interiors, but protrusions can be added in the floor plan.

Classes in codebase

Pre-existing classes

  • FileLoader: API for reading JSON files and reading basic Java classes. Has been extended to read two-dimension grids and object maps.
  • GameArea: Legacy code for spawning and registered entities into the world.
  • MainGameScreen: Responsible for instantiating a Home. Minor dependency injection is expected before calling create() on the instance.

Relevant map classes

  • Home: Defines the main directory for map files. May hold one or more floors to constitute a home. Has functionality to randomize floor plans.
  • Floor: Extends GameArea. Responsible for object instantiation and placement into the world. Constitutes rooms and non-room-related objects relevant to the floor plan.
  • Room: Holds spatial data relevant to the floor it is on. Each room has a type (e.g. Bathroom, Bedroom). Has functionality to randomize room interiors.
  • Interior: Constitutes room-related objects relevant to the room interior. These classes are read by the JSON parser, then its values are injected into the room it belongs to. It is subsequently released in memory.
  • GridObject: Describes a method and parameters related to the creation of an object (either a TerrainTile or an Entity). These methods are independent of grid placement, so methods employing randomization may be used.

Relevant utility classes

  • GridPoint2Utils: Has been extended to support custom JSON deserialization. GridPoint2 objects can now be defined in map files as an int array.
  • MatrixUtils: Allows for non-square matrices to be manipulated. Supports transposing and vertical/horizontal reversing, which in turn allows anti/clockwise rotations.
  • RandomUtils: Allows for pseudo-random generation using seeds. The purpose is to guarantee consistent saving and loading of random maps. This functionality is yet to be expanded upon.

UML Diagram

How to use the Random Map Generation System

The random generation system is mostly in place so that prefabs are plug and play. If the intention is created prefabs, simply follow the format found in existing files, or refer to the custom serialization below.

Serialization Skeletons

Grid Object

{
  class:  // Class that declares the method used for creation
  method: // Method inside the class. This method should not be overloaded
  assets: [
    // String array. Typical creation methods will use the first asset to 
    // create a Texture or Animation RenderComponent, but complex methods
    // can define their own argument parsing. Make sure to apply this here
    // Brackets can be in-lined
  ]
}

Interior

{
  tileMap: {
    ?: {
      // Custom GridObject serialization
    }
    ...
  }
  entityMap: {
    ?: {
      // Custom GridObject serialization
    }
    ...
  }
  tileGrid: [
    // Default JSON Character grid serialization
  ]
  entityGrid: [
    // Default JSON Character grid serialization
  ]
}

Room

{
  type: // One valid room type as a String. Defined in Room.Java
  offset: [y, x] // Offset describes the bottom left corner of the room,
                 // relative to 0,0 in the top left corner of the floor
                 // If you skin your JSON file as a libGDX JSON, then you
                 // can find these numbers in the bottom right of the tab.
                 // Internally converted to x, y when matrix is later
                 // manipulated so that generation is in the same orientation
                 // as the file
  dimensions: [x, y] // Dimensions of the room. Used for matching interiors
                     // Note that these are x, y instead of y, x
  interior: {
    // Optional parameter. If declared, then use custom Interior serialization
  }
}

Floor

{
  defaultInteriorTile: {
    // Custom GridObject serialization
  }
  defaultInteriorWall: {
    // Custom GridObject serialization
  }
  tileMap: {
    ?: {
      // Custom GridObject serialization
    }
    ...
  }
  entityMap: {
    ?: {
      // Custom GridObject serialization
    }
    ...
  }
  roomMap: {
    ?: {
      // Custom Room serialization
    }
    ...
  }
  floorGrid: {
    // Default JSON Character grid serialization. Note that special tile/entity
    // symbols overriding room symbols will not override tile ownership, 
    // only entity ownership. This is to ensure that the correct tile texture 
    // is placed in the room, and the floor plan can decide to place a door
    // instead of a room wall
  }
}
Clone this wiki locally