Skip to content

Map Generation and Functionality

Sam Behm edited this page Sep 12, 2022 · 13 revisions

Generation and Functionality Overview

The generation and functionality of the terrain has to conform to these parameters:

  1. It has to place tiles in a way that simulates a sandy island being surrounded by water
  2. It has to be a static map (in response to feedback)
  3. The island has to be able to expand upon a condition
  4. The island has to be able to shrink upon a condition
  5. Island boundaries must be generated such that the player cannot walk off of the island
  6. The tile textures must change in response to different times of the day.

Generating a Static Map

To aid in the generation of a static map, PNGToMapFile.java has been created to convert a visually designed map to a file that is able to be interpreted by TerrainFactory.

PNGToMapFile.java Usage

This program converts a variable length .png file to a text file filled with 3-bit integers. Each bit representing a different attribute of the tile. This allows for the overall map shape to be designed visually in a pixel-art program, and then used in the game as a layout for the map.

PNGToMapFile maps the RGB values of a pixel to the corresponding 3-bit integer, readable by TerrainFactory. The mapping is as follows:

Red Value > 0 -> Yes: The tile is a land tile. No: The tile is a water tile.

Green Value > 0 -> Yes: There is an island border on this tile

Blue Value > 0 -> Yes: Enemies can spawn on this tile.

Examples of how a visually designed map may look:

image

Note: the cyan colour surrounding the red island is generated by R: 0 G: 255: B: 255, which implies the tile is a water tile, has a island border on it, and enemies can spawn on it.

image

To convert the .png file to a game-readable file, ensure that the png is in the same directory as PNGToMapFile, and execute:

java PNGToMapFile.java fileName.png

To convert multiple images:

java PNGToMapFile.java fileName1.png fileName2.png fileNameN.png

Example of Generated Island:

level0.txt

Generating the Map from Map Files

On initialisation, TerrainFactory reads in 5 level files, adding them to a list of arrays which contain each 3-bit integer, describing the tile. Then, when the terrain is called to be generated (in ForestGameArea), all levels are generated by looping over array, creating a tiled map layer (TiledMapTileLayer). Each tile in the tile map is generated based on the equivalent integer in the level array (using little-endian notation):

  • Least significant bit (a): Land Bit -> 0 for water, 1 for land
  • a+1: Border bit -> 0 for no border, 1 for border on the tile
  • a+2: Spawnable bit -> 0 for not spawnable, 1 for spawnable

Each layer is added to the TiledMap, which is passed as a parameter to the TerrainComponent for ForestGameArea to handle.

Changing the Map Size

When the Crystal is upgraded, the function incrementMapLvl is called in the TerrainComponent. This function increments the map level counter, which describes what level file is being used to display the map, hides the previous layer, and makes the new layer visible.

After every night, if the crystals health is less than 50%, the map shrinks. This is impl

Table of Contents

Home

How to Play

Introduction

Game Features

Main Character

Enemies
The Final Boss

Landscape Objects

Shop
Inventory
Achievements
Camera

Crystal

Infrastructure

Audio

User Interfaces Across All Pages
Juicy UI
User Interfaces Buildings
Guidebook
[Resource Management](Resource-Management)
Map
Day and Night Cycle
Unified Grid System (UGS)
Polishing

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally