Map System Code Implementation - UQdeco2800/2022-studio-2 GitHub Wiki
Introduction
This wiki describes how to create a new map, and load the base map and related entities into the game. The code are Implemented by @rlybgithub
UML Class Diagram
Considering no such diagrams were developed for Sprint 1, Ishaan decided to contribute more towards the coding side of the team-work by learning how the engine functions in accordance with the level 2 game area. So he decided to have an attempt at making a UML Class diagram to give a more visual representation as to how the key classess interact with each other that are mostly responsibly for the initialization and display of the Game area, this is the first iteration and can be further improved by indulging further and exploring the rendering classes as well but due to time constraints, the below image is what he was able to achieve.
To create a map the following 4 main files will be used.
TerrainFactory.java
-This class uses to compose the terrain type. The step as below:
1. Name a new terrain type to create.
` public enum TerrainType {
FOREST_DEMO,
FOREST_DEMO_ISO,
FOREST_DEMO_HEX,
LEVEL_ONE,
}`
2. Make a method of creating TerrainComponent, set the tmx file to load(Created from Tile Map Editor), the tileWordSize allows you to expand the map by resizing the tiles.
` private TerrainComponent createLevelOne(
float tileWorldSize, TextureRegion gold_cobble) {
GridPoint2 tilePixelSize = new GridPoint2(gold_cobble.getRegionWidth(), gold_cobble.getRegionHeight());
TiledMap tiledMap = new TmxMapLoader().load("images/level_1_tiledmap/level_1.tmx");
TiledMapRenderer renderer = createRenderer(tiledMap, tileWorldSize / tilePixelSize.x);
return new TerrainComponent(camera, tiledMap, renderer, orientation, tileWorldSize);
}`
3. Set the case of the new Terrain Type under the CreatTerrain() method.
` case LEVEL_ONE:
TextureRegion gold_cobble =
new TextureRegion(resourceService.getAsset("images/level_1_tiledmap/32x32/gold_cobble.png", Texture.class));
return createLevelOne(1f, gold_cobble);
`
4. You can set the map size with the parameter as below:
private static final GridPoint2 MAP_SIZE = new GridPoint2(30, 30);
ObstacleFactory.java
-This class allows you to create the entities which will show on the map. Simply assign the needed components to the entity and set the parameters. Then return the entity.
` public static Entity createColumn() {
Entity column =
new Entity()
.addComponent(new TextureRenderComponent("images/Map_assets/sprint_1/column.png"))
.addComponent(new PhysicsComponent())
.addComponent(new ColliderComponent().setLayer(PhysicsLayer.OBSTACLE));
column.getComponent(PhysicsComponent.class).setBodyType(BodyType.StaticBody);
column.getComponent(TextureRenderComponent.class).scaleEntity();
column.scaleHeight(2f);
PhysicsUtils.setScaledCollider(column, 0.9f, 0.9f);
return column;
} `
ForestGameArea.java
-This class uses to create the UI components for each entity and place them on the map.
1. You can first write the spawn methods which allow you to place the items.
` private void spawnColumn(int x, int y) {
Entity column = ObstacleFactory.createColumn();
spawnEntityAt(column, new GridPoint2(x, y), false, false);
}`
2. Load the background terrain under the spawnTerrain() method, also you can place the static components and walls in this method.
` private void spawnTerrain() {
// Background terrain
terrain = terrainFactory.createTerrain(TerrainType.LEVEL_ONE);
spawnEntity(new Entity().addComponent(terrain));
// Terrain walls
float tileSize = terrain.getTileSize();
GridPoint2 tileBounds = terrain.getMapBounds(0);
Vector2 worldBounds = new Vector2(tileBounds.x * tileSize, tileBounds.y * tileSize);
`
3. Import the images on the forestTextures variables.
`private static final String[] forestTextures = {
"images/atlantis_citizen_gym_bro.png",
"images/box_boy_leaf.png",
"images/tree.png",
}; `
4. Spawn the terrain and other elements on the map with create() method.
public void create() {
loadAssets();
displayUI();
spawnTerrain();
}
UndergroundGameArea.java
-This is the map for level two, the mechanism is similar as the ForestGameArea.java.
MainGameScreen.java
-The class allow you to control the Main Game Screen you can load your map here.
1. Create a method to load the map.
` private ForestGameArea loadLevelOneMap() {
TerrainFactory terrainFactory = new TerrainFactory(renderer.getCamera());
ForestGameArea forestGameArea = new ForestGameArea(terrainFactory);
forestGameArea.create();
return forestGameArea;
}`
2. Load the map under the MainGameScreen(GdxGame game) method.
` public MainGameScreen(GdxGame game) {
this.game = game;
ForestGameArea map = loadLevelOneMap();
player = map.getPlayer(); // you can also get your entities if you want.
} `
Extra: You can also control the camera in this class. For example, I want my camera always follow the player's position, the approach is to set the camera position and put it into the render() method, then every time when the game is rendering, it will refresh the camera position and bind to the player's position
` private void cameraTracePlayer() {
renderer.getCamera().getEntity().setPosition(player.getPosition());
}`
` public void render(float delta) {
cameraTracePlayer();
}`