-
Notifications
You must be signed in to change notification settings - Fork 4
MapService
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.
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.
-
Entity with Terrain Component: At the heart of the
MapService
is an entity. This entity is instantiated with aTerrainComponent
: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()
. -
Grid Dimensions: With
MapService
, obtaining the grid's dimensions is a breeze. You can retrieve the grid's height and width using thegetHeight()
andgetWidth()
methods, respectively. -
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
andy
denote the coordinates you wish to inspect.
MapService is tested according to MapServiceTest
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.