Map Generation - UQcsse3200/2024-studio-2 GitHub Wiki

Descirption

The Map for the game makes use of tiling algorithms. It is generated by dividing it into small chunks, each containing terrain tiles. These chunks are then loaded dynamically based on the player’s current location, hereby ensuring only nearby sections are generated and rendered. The tiles within each chunk are selected to fit seamlessly together, creating a coherent terrain. This approach is made to optimize performance by focusing on the areas near the player while simultaneously managing a larger, cohesive map.

The map is generated in chunks and each chunk is represented as a grid consisting of 16x16 tiles. Since the terrain is divided into multiple areas (such as forest, water, fog etc.), the tiles in each chunk are assigned according to the area type. Bitsets are used to track possible tile configurations for each cell in the chunk and iteratively selects and "collapses" tiles until the terrain is finally fully defined.

APIs, code, feature & design used

APIs

LibGDX: Game framework used for graphics, input handling, and sound.

JSON: Used to load and configure levels such as terrain types and spawn points.

Code

Game Logic: Written in Java using object-oriented principles.

Modular Architecture: Terrain, player, and item components are managed using independent classes to improve scalability.

TerrainComponent: Handles terrain rendering, tile selection, and interaction between tiles across different map areas.

TerrainFactory: Generates terrain components with customizable map types and orientations like orthogonal, isometric, or hexagonal.

TerrainChunk: Manages 16x16 terrain chunks, generates terrain tiles using entropy-based algorithms, and ensures area-specific tile placement.

Features

Chunk-Based Generation: Divides the terrain into 16x16 chunks, each handled separately to optimize loading and rendering.

Area-specific Tiles: Dynamically assigning of tiles to chunks based on their location in predefined areas (forest, water, fog, or air).

BitSet-based Tiles: Utilization of bitsets to track possible tiles for each cell, ensuring efficient generation and rendering of the terrain.

Entropy-based Tile Collapsing: Collapsing of tile options by selecting tiles with minimal entropy to ensure a coherent, structured terrain.

Dynamic Map : Continuously updating grid data and terrain based on nearby chunks.

UML

image

This UML class diagram presents four main classes along with their attributes, methods, and relationships: ForestGameArea, GameArea, TerrainLoaderComponent, Component, and TerrainFactory. The UML class diagram provides a detailed description of the collaboration between ForestGameArea and TerrainFactory, showcasing how TerrainLoaderComponent is used to load terrain chunks, thereby illustrating a hierarchical system for creating and managing game areas.

Testing

This test is initialized and it is checked that the grid within the object is properly initialized and not null, confirming that its size is zero. It then verifies that the tile type count map is initialized and not null, ensuring the object starts in a valid state.

    public void setUp() {
        // Create simulated TiledMap and TerrainResource objects
        tiledMap = Mockito.mock(TiledMap.class);
        terrainResource = Mockito.mock(TerrainComponent.TerrainResource.class);

        // Create a mock MapLayers object and set it to the return value of TiledMap
        MapLayers mapLayers = Mockito.mock(MapLayers.class);
        TiledMapTileLayer tileLayer = Mockito.mock(TiledMapTileLayer.class);
        Mockito.when(mapLayers.get(0)).thenReturn(tileLayer);
        Mockito.when(tiledMap.getLayers()).thenReturn(mapLayers);

        // initialise TerrainChunk
        terrainChunk = new TerrainChunk(new GridPoint2(0, 0), tiledMap);
    }

    public void testInitialization() {
        // Check that the grid is initialized
        assertNotNull(terrainChunk.grid);
        assertEquals(0, terrainChunk.grid.size);

        // Check that the tile is initialized
        assertNotNull(terrainChunk.tileTypeCount);
    }