Skip to content

MapService

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

MapService Guide

Overview

MapService is a pivotal class in our game development ecosystem, serving as a bridge to interface with the game's grid functionalities. Its primary purpose is to facilitate global access to these grid functionalities, centralizing and simplifying many operations related to the game map.

Getting Started

Before diving into the capabilities and utilities provided by the MapService, one must first register it to the ServiceLocator. Here's how you can do it:

ServiceLocator.registerMapService(new MapService(renderer.getCamera()));

In the above code, renderer refers to an instance created using RenderFactory.createRenderer();. By initializing the MapService with a camera component, it efficiently bridges the gap between rendering and grid functionality.

Key Features

  1. Entity with Terrain Component: At the heart of the MapService is an entity. This entity is instantiated with a TerrainComponent:

    this.entity = new Entity().addComponent(terrainFactory.createTerrain(TerrainFactory.TerrainType.ALL_DEMO));

    This is a quintessential part of the service as it displays the game grid once you register the entity in the EntityService().

  2. Grid Dimensions: With MapService, obtaining the grid's dimensions is a breeze. You can retrieve the grid's height and width using the getHeight() and getWidth() methods, respectively.

  3. Accessing Specific Cells: One practical use-case of the MapService is to determine the existence of a particular cell, which can be crucial when planning to spawn an entity. Here's how you can do it:

    MapService().getComponent().getMap().getLayers().get(0).getCell(x,y);

    Here, x and y denote the coordinates you wish to inspect.

Test Plan

MapService is tested according to MapServiceTest

Conclusion

The MapService class is an embodiment of design efficiency, offering a streamlined approach to interacting with the game's grid functionalities. Whether you're looking to fetch the grid's dimensions, inspect a particular cell, or even interface with the terrain, MapService is your go-to utility.

Clone this wiki locally