Skip to content

Tile System

Lucas Hicks edited this page Aug 27, 2023 · 18 revisions

Introduction

As this is a farming game, players must be able to change the game world around them, creating and modifying objects in the game as they please. For example, a player will be able to create a crop tile in the world through the use of a hoe (see tool wiki), or could water a plant (see plant wiki) with a bucket.

Some entities that the player creates or modifies will be arranged in a tesselating square-grid pattern. The tile system provides the factories and components of such entities, and is intended to work seamlessly with map generation, player tools, and plant behaviors.

Process Flow

Terrain Entities

Certain entities, such as trees, boulders, plots of land meant for planting crops, etc, are unmoving in the scene, while other entities, such as the player or npcs, are able to move freely through the game area. To simplify the farming and building processes, the placement of unmoving entities are bound to one or more square ‘cells’ within the terrain (these entities will be referred to as ‘terrain entities’).

These terrain entities will share a common entity creation method signature (in sprint 1, this is simply a Vector2 or two floats representing the location at which to spawn the entity), which is responsible for instantiating an entity at a given grid cell location. The behaviour of certain terrain entities can depend on the type of terrain on which the entity was spawned. For instance, the soil quality of crop tiles depend on what TileType the crop tile was spawned on (e.g., certain types of terrain tiles will improve the growth rate of certain plants - note this is not implemented as of sprint 1, but is a project for future sprints).

Since terrain entities are bound to one or more grid cells in the game area, the Map (see Map Generation) will need to know which cells of the map are occupied by terrain entities. When a terrain entity is spawned, the grid cells it occupies are marked as such. Likewise, before spawning a terrain entity, the map’s grids need to be checked to see if they are occupied.

Usage

To create a CropTile at position (x,y), use the following:

TerrainCropTileFactory.createTerrainEntity(x,y);

This will create and register the Crop Tile entity.

Factory Design

As this game engine uses ECS, almost every game object is an Entity, which has components added to it that add functionality. As most entities have several components attached to them, the factory design pattern is used to collect all relevant information and return a single instance of a class. This significantly simplifies our methods as classes that require the creation of entities only need to provide simple arguments (eg: x and y position) without worrying about ECS related tasks (eg: registering the entity with the EntityService.

The TerrainCropTileFactory is an example of such a factory. This factory creates entities that have the CropTileComponent (see Crop Tiles), which are tiles that house plant entities.

Clone this wiki locally