Skip to content

TerrainFactory

Mohamad Dabboussi edited this page Oct 2, 2023 · 3 revisions

Introduction

The TerrainFactoy is an essential facet of game development that focuses on the creation of terrains and their visual representation. These terrains might contain invisible lanes, which are instrumental in defining paths for game entities or organizing various gameplay zones. This guide will delve into the construction and application of game terrains and the creation of invisible lanes.

Terrain Factory

In the provided code, a TerrainFactory class is utilized to create different terrains. This factory pattern allows the creation of varying terrain types with different orientations and characteristics.

public TerrainComponent createTerrain(TerrainType terrainType) {
    switch (terrainType) {
        case ALL_DEMO:
            TextureRegion orthogonal = new TextureRegion(resourceService.getAsset("images/terrain_use.png", Texture.class));
            return createTerrain(1f, orthogonal);
        default:
            return null;
    }
}

This method, createTerrain, facilitates the creation of a terrain based on a specified type. The switch statement can be expanded upon to accommodate more terrain types as the game evolves.

Creating Invisible Lanes

Within the TerrainFactory class, there's a method to create tiles. In this illustration, we constructed a TiledMapTileLayer(Grid) measuring 20 tiles in width by 6 tiles in height. We then add it to a TiledMap which saves multiple MapLayers:

private TiledMap createTiles(GridPoint2 tileSize, TextureRegion terrain) {
    TiledMap tiledMap = new TiledMap();
    TiledMapTileLayer Layer = new TiledMapTileLayer(20, 6, tileSize.x, tileSize.y);
    fillInvisibleTiles(Layer, new GridPoint2(20, 6), terrain);
    tiledMap.getLayers().add(Layer);
    return tiledMap;
}

Here, the fillInvisibleTiles function plays a pivotal role in filling a given layer with invisible tiles, essentially creating the so-called lanes:

private void fillInvisibleTiles(TiledMapTileLayer layer, GridPoint2 mapSize, TextureRegion terrain) {
    for (int x = 0; x < mapSize.x; x++) {
        for (int y = 0; y < mapSize.y; y++) {
            TerrainTile tile = new TerrainTile(terrain);
            Cell cell = new Cell();
            cell.setTile(tile);
            layer.setCell(x, y, cell);
        }
    }
}

This function populates the tile layer with TerrainTile instances using the provided terrain texture region.

Test Plan

TerrainFactory has been tested according to TerrainFactoryTest

Conclusion

The construction and rendering of game terrains are central to defining the game world's structure and visuals. This mechanism provides game developers the flexibility to devise multiple terrains, including invisible lanes or paths, to guide gameplay and entity movement. Integrating such systems can significantly improve gameplay mechanics and the player's overall experience.

Clone this wiki locally