Skip to content

Map Generation Logic

parker334 edited this page Sep 14, 2021 · 22 revisions

Overview

Maps are created using the freeware Tiled, this software outputs a JSON, This JSON is then preprocessed prior to importing into the game engine to reduce complexity and data processing on the Game engine's side. A Map JSON consists of tiles and collidables on separate layers. Then the to generate a map into the game engine, the task is split into subtasks:

  • Render Background tiles (each 32x32 and not collidable objects)
  • Render Collidable objects (easier to place if (32k x 32j pixel, where k,j are integers)
  • Render Physical Traps (easier to place if (32k x 32j pixel, where k,j are integers)
  • Render Non Physical Traps (easier to place if (32k x 32j pixel, where k,j are integers)

Data Structure

An example of the JSON file that represents a map can be seen here: Example Map File


This is read into the game engine as a Map object: game/areas/terrain/Map.java, using FileLoader i.e.
Map m = FileLoader.readClass(Map.class, "maps/test_map.json");

Background Placement

Usage

When a new GameArea is loaded, the area's tile textures are red in from the map JSON, so they can be loaded by ResourseServer

    Map map = FileLoader.readClass(Map.class, "maps/test_map.json");
    tileTextures = map.TileRefsArray();

In spawnTerrain(), terrainFactory.createTerrain() is called using the appropriate ENUM and map to be rendered

    terrain = terrainFactory.createTerrain(TerrainType.TEST, map);

Where the rest of the process is handled automatically as long as the appropriate case is handled in TerrainFactory.createTerrain

case TEST:
        String[] tileRefs = map.TileRefsArray();
        ArrayList<TextureRegion> textures = new ArrayList<>();
        for (String s:tileRefs){
          textures.add(new TextureRegion(resourceService.getAsset(s,Texture.class)));
        }
        return createWorldTerrain(0.5f, textures, map.getMapTiles(), map.getDimensions());

Collidable Generation

Usage

Collidable objects (i.e. Walls) are created by reading data from a JSON produced from Tiled.

It works by importing the objects from the JSON and putting them in an array of HashMap.

    Map m = FileLoader.readClass(Map.class, "maps/test_map.json");
    HashMap<String, Float>[] walls = m.getWallObjects();

A loop then iterates through all wall objects and places them accordingly.

for (HashMap<String, Float> wall : walls) {
      int x = wall.get("x").intValue();
      int y = wall.get("y").intValue();
      float width = wall.get("width");
      float height = wall.get("height");

      int unitHeight = (int) ((height/32f));
      spawnEntityAt(
              ObstacleFactory.createWall((width/32f)*0.5f, (height/32f)*0.5f),
              new GridPoint2(x, map.getDimensions().get("n_tiles_height")- (y + unitHeight)),
              false,
              false);
    }

where ObstacleFactory.createWall creates a wall of width(right) and height(up) in world units, GridPoint2 is the point (x, y) on the grid where the wall is placed (Origin is top left). The last 2 booleans are whether the x and y are centered respectively. In walls these should be false.

Physical and Non-Physical Traps

Usage

Physical Traps (currently spikes) and Non-Physical Traps (currently lava) are placed similarly to walls, and are created by reading data from a JSON produced from Tiled, as above except that

HashMap<String, Float>[] spikeTraps = map.getSpikeObjects();

or

HashMap<String, Float>[] lavaTraps = map.getLavaObjects();

are used instead.

A loop then iterates through the respective traps and places them accordingly, as above, except that in spawnEntityAt() instead of ObstacleFactory.createWall(),

ObstacleFactory.createRSPhysicalTrap()

or

ObstacleFactory.createRSNonePhysicalTrap()

are used, for Physical and Non-Physical Traps respectively.

Table of Contents

Home

Design Document

Design Document

Design Document

Game Engine

Getting Started

Entities and Components

Item Drops from Entities

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Map Generation

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally