Skip to content
Jantoom edited this page Oct 19, 2021 · 3 revisions

Introduction

In RETROACTIVE's version of random generation, levels are synonymous with floors. The randomness is expressed in the way individual rooms are generated. These rooms constitute the majority of the floor, which in turn makes the whole floor feel new after each randomization.

Rooms are not expressed in their own JSON files. A room is only usable when part of a Floor JSON file in its room map.

Technical Explanation

Conceptual design of the Room class

Explained in [Floor], an old approach to random generation was to have an algorithm that would randomly designate room types to every room plan in a floor plan. This was scrapped when we was realized that most designations would match the preconceived configuration when making the floor plan.

The purpose of this class is to explicitly declare where rooms should go on a floor, and to provide functionality that is relevant on a room-by-room basis. This includes handling randomizing interiors, and spawning of tiles and entities.

Defining a Room using JSON deserialization

The Room class implements the Json.Serializable interface to allow for de/serialization of Room instances.

type

This string is used for declaring what type of room that this Room is. A list of valid rooms can be found in the validRoomTypes array in Room. A room will fail deserialization if its type is not in this array.

offset

This is a GridPoint2 object describing where the bottom-left corner of the room is in the floorGrid of the Floor JSON file. The coordinates are relative to [0, 0] being the top-left corner. The easiest way to determine this value in a very big floorGrid is to use the libGDX plugin to skin the JSON file as a libGDX JSON. Clicking where the offset is will tell you what that coordinate is in your IDE.

dimensions

This is also a GridPoint2 object. It describes the [horizontal, vertical] dimensions of the room. This should include all overridden grid points in the floorGrid (i.e. overriding an entire wall to not spawn still means that those grid points belong to the overridden room). This value is used when selecting what [Interior] JSON file to inject into the Room instance.

interior

This is a serialization of an Interior object. This value is typically not used when defining a room, but can be defined if the desired interior is known.

Typical JSON file format

{
  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
  }
}

Calling create( ) post-deserialization

When create( ) is called, an Interior will be selected. If the type of the room is "hallway", then no JSON file exists for it so an Interior will have to be explicitly created in the class. If the type of the room is anything else, then the random selection algorithm will pick an Interior at random and inject its variables into the Room instance. At this point, the Room will have defined its tile & entity maps, and its tile & entity grids. After this, subsequent calls to create( ) will do nothing.

Spawning room tiles

The method spawnRoomTiles( ) is called from the Floor that owns this room. It iterates through all elements in the tile grid and spawns the tile mapped to the element's Character. If a map is not found, the tile is assumed to be using the Floor's defaultInteriorTile instead.

Spawning room entities

The method spawnRoomEntities( ) is called from the Floor that owns this room. It iterates through all elements in the entity grid and spawns the entity mapped to the element's Character. If a map is not found, the entity is assumed to not exist and nothing will be spawned.

When the Floor overrides a specific element in its floor grid, the Room will know this and update its entity grid. The character in the floor grid is then reverted to the character used to map the room for post-creation.

Getting spawn locations

Part of the Floor's spawning algorithm is to dynamically choose where entities not defined in any prefabricated JSON files (e.g. the player) are spawned. When asking a Room for valid spawn locations, the room must have a type defined in the validSpawnRooms array. It will return every grid point without a spawned entity, including any overriding entities from the Floor itself.

Clone this wiki locally