Multiple Maps Explanation - UQdeco2800/2021-ext-studio-2 GitHub Wiki
Overview
In sprint 3 we decided to add the multiple maps feature to the game to improve the game playability. We decided to add a portal after winning the spaceship in order to provide an entrance to the bonus map. In the bonus map, there will be no monsters and player are free to collect more and more gold before hitting the exit portal. Background image for the bonus map is here. BGM for the bonus map is here.
Code implementation
Rock road
Under the com/deco2800/game/areas/terrain/TerrainFactory.java
file, we added a new terrainType ROCK_ROAD
:
case ROCK_ROAD:
TextureRegion rockRoad =
new TextureRegion(resourceService.getAsset("images/rock.jpg", Texture.class));
return createRockRoadTerrain(1, rockRoad);
In order to create rock road terrain and tiles, two private functions have been created:
private TerrainComponent createRockRoadTerrain(float tileWorldSize, TextureRegion road) {
GridPoint2 tilePixelSize = new GridPoint2(road.getRegionWidth(), road.getRegionHeight());
TiledMap tiledMap = createRockRoadTiles(tilePixelSize, road);
TiledMapRenderer renderer = createRenderer(tiledMap, tileWorldSize / tilePixelSize.x);
return new TerrainComponent(camera, tiledMap, renderer, orientation, tileWorldSize);
}
private TiledMap createRockRoadTiles(GridPoint2 tileSize, TextureRegion road) {
TiledMap tiledMap = new TiledMap();
TerrainTile grassTile = new TerrainTile(road);
TiledMapTileLayer newLayer = new TiledMapTileLayer(MAP_SIZE.x, MAP_SIZE.y + 47, tileSize.x, tileSize.y);
fillTiles(newLayer, MAP_SIZE, grassTile, 49);
tiledMap.getLayers().add(newLayer);
return tiledMap;
}
After that, we added one parameter int vertical
into fillTiles()
function, so that the tiles can be filled in the given vertical height.
Based on the above code, some public functions have been created in order to be called under com/deco2800/game/screens/MainGameScreen.java
to realize the scrolling terrain:
switch (newMapStatus) {
case Off:
...
case On:
// Render terrains on new map
if (screenVector.x > (2 * counter + 1) * 10) {
counter += 1;
forestGameArea.spawnTerrainRandomly((int) (screenVector.x + 2), TerrainFactory.TerrainType.ROCK_ROAD);
}
Background image
In order to draw image on certain vertical height. We added an global variable private float vertical = 0;
into com/deco2800/game/rendering/BackgroundRenderComponent.java
, and created a public function to manually adjust the height of background:
/**
* Set the vertical start point for the background texture
* @param y vertical value
*/
public void setVertical(float y) {
vertical = y;
}
@Override
public void draw(SpriteBatch batch) {
batch.draw(texture, -30, 0, 30, 15);
batch.draw(texture, horizontal, 0, 30, 15);
batch.draw(texture, -30, vertical, 30, 15);
batch.draw(texture, horizontal, vertical, 30, 15);
}
After that, under com/deco2800/game/areas/ForestGameArea.java
, we created the following function to show new map scrolling background image on given height:
public void showNewMapScrollingBackground(int counter, float vertical) {
Entity gameBg = new Entity();
BackgroundRenderComponent newBg = new BackgroundRenderComponent("images/background_2.png");
newBg.setHorizontal(30f * counter);
newBg.setVertical(vertical);
gameBg.addComponent(newBg);
spawnEntity(gameBg);
}
Last but not least, call showNewMapScrollingBackground
and set its vertical parameter according to the player current position under com/deco2800/game/screens/MainGameScreen.java
.
Switch BGM
Under com/deco2800/game/areas/ForestGameArea.java
, created four public functions, which can be called to play or stop corresponding BGM from other class.
public void playMusic() {
Music music = ServiceLocator.getResourceService().getAsset(BACKGROUNDMUSIC, Music.class);
music.setLooping(true);
music.setVolume(0.3f);
music.play();
}
public void playNewMapMusic() {
Music newMusic = ServiceLocator.getResourceService().getAsset(NEWMAP_BACKGROUNDMUSIC, Music.class);
newMusic.setLooping(true);
newMusic.setVolume(0.3f);
newMusic.play();
}
public void stopMusic() {
ServiceLocator.getResourceService().getAsset(BACKGROUNDMUSIC, Music.class).stop();
}
public void stopNewMapMusic() {
ServiceLocator.getResourceService().getAsset(NEWMAP_BACKGROUNDMUSIC, Music.class).stop();
}
After creating above functions, we can call these functions under com/deco2800/game/screens/MainGameScreen.java
to realize switching BGM between regular map and bonus map.
switch (newMapStatus) {
case Off:
forestGameArea.stopNewMapMusic();
forestGameArea.playMusic();
...
case On:
forestGameArea.stopMusic();
forestGameArea.playNewMapMusic();
...
Rocks and woods piling up
Overview
In "sprint 3" we decided to arrange various shapes of obstacles such as pyramids, lines, columns, etc. Here are examples of obstacles:
Code implementation
Line of rock flying in the air
Create the game "rock" obstacle
A row of 5 rocks in a straight line suspended in the air
At coordinates y= 50, the rocks will be on the ground, so at position y=55, the rocks will appear in the air and become an obstacle when the character moves.
Create a loop to create x-coordinates for 5 consecutive stones standing next to each other.
The spawnRocksone function will be called at MainGameScreen.java and will appear when switching to a new map.
"spawnEntityAt" function helps us choose the coordinate position of the center of the rock, from there using rock.png and will scale the size of the rock horizontally and vertically.
public void spawnRocksone(int xValue) {
for (int i = 0; i < 5; i++) {
GridPoint2 pos = new GridPoint2(xValue + 2 +i, 55);
Entity rock = ObstacleFactory.createRock();
spawnEntityAt(rock, pos, true, false);
Create rock pyramid type 1
Create the game "rock" obstacle
Form a rocks pyramid with six rocks stacked on top of each other, a total of 6 rocks needed
Since the pyramid consists of three floors from the ground, the y coordinate of the rocks will be from 50 to 52, since y=50 the co-ordinate of the rocks will be on the ground
The x coordinate will depend on the location of the rocks.
The spawnRockstwo function will be called at MainGameScreen.java and will appear when switching to a new map.
"spawnEntityAt" function helps us choose the coordinate position of the center of the rock, from there using rock.png and will scale the size of the rock horizontally and vertically.
public void spawnRockstwo(int xValue) {
GridPoint2 Pos = new GridPoint2 ( xValue + 10, 50);
Entity rock = ObstacleFactory.createRock();
spawnEntityAt(rock, Pos, true, false);
GridPoint2 PosTwo = new GridPoint2 ( xValue + 11, 51);
Entity rockTwo = ObstacleFactory.createRock();
spawnEntityAt(rockTwo, PosTwo, true, false);
GridPoint2 PosThree = new GridPoint2 ( xValue + 12, 52);
Entity rockThree = ObstacleFactory.createRock();
spawnEntityAt(rockThree, PosThree, true, false);
GridPoint2 PosFour = new GridPoint2 ( xValue + 12, 51);
Entity rockFour = ObstacleFactory.createRock();
spawnEntityAt(rockFour, PosFour, true, false);
GridPoint2 PosFive = new GridPoint2 ( xValue + 12, 50);
Entity rockFive = ObstacleFactory.createRock();
spawnEntityAt(rockFive, PosFive, true, false);
GridPoint2 PosSix = new GridPoint2 ( xValue + 11, 50);
Entity rockSix = ObstacleFactory.createRock();
spawnEntityAt(rockSix, PosSix, true, false);
}
Create rock pyramid type 2
Create the game "rock" obstacle
Form a rocks pyramid with six rocks stacked on top of each other, a total of 6 rocks needed
Since the pyramid consists of three floors from the ground, the y coordinate of the rocks will be from 50 to 52, since y=50 the co-ordinate of the rocks will be on the ground
The x coordinate will depend on the location of the rocks.
The spawnRocksthree function will be called at MainGameScreen.java and will appear when switching to a new map.
"spawnEntityAt" function helps us choose the coordinate position of the center of the rock, from there using rock.png and will scale the size of the rock horizontally and vertically.
public void spawnRocksthree(int xValue) {
GridPoint2 Pos = new GridPoint2 ( xValue + 14, 52);
Entity rock = ObstacleFactory.createRock();
spawnEntityAt(rock, Pos, true, false);
GridPoint2 PosTwo = new GridPoint2 ( xValue + 14, 51);
Entity rockTwo = ObstacleFactory.createRock();
spawnEntityAt(rockTwo, PosTwo, true, false);
GridPoint2 PosThree = new GridPoint2 ( xValue + 14, 50);
Entity rockThree = ObstacleFactory.createRock();
spawnEntityAt(rockThree, PosThree, true, false);
GridPoint2 PosFour = new GridPoint2 ( xValue + 15, 51);
Entity rockFour = ObstacleFactory.createRock();
spawnEntityAt(rockFour, PosFour, true, false);
GridPoint2 PosFive = new GridPoint2 ( xValue + 15, 50);
Entity rockFive = ObstacleFactory.createRock();
spawnEntityAt(rockFive, PosFive, true, false);
GridPoint2 PosSix = new GridPoint2 ( xValue + 16, 50);
Entity rockSix = ObstacleFactory.createRock();
spawnEntityAt(rockSix, PosSix, true, false);
}
Create rock rocks collum from the ground up
Arow of 5 rocks in a straight line suspended in the air
Since it is a column, the x coordinate will stay the same, so we set x=18
Loop the y coordinate to form 5 rock stacked on top of each other forming a straight column, with y= 50 rock will be on the ground facing up
The spawnRocksfour function will be called at MainGameScreen.java and will appear when switching to a new map.
"spawnEntityAt" function helps us choose the coordinate position of the center of the rock, from there using rock.png and will scale the size of the rock horizontally and vertically.
public void spawnRocksfour(int xValue) {
for (int i = 0; i < 5; i++) {
GridPoint2 pos = new GridPoint2(xValue + 18, 50+i);
Entity rock = ObstacleFactory.createRock();
spawnEntityAt(rock, pos, true, false);
}
}
Create rocks collum from the sky down
Create the game "rock" obstacle
A row of 5 rocks in a straight line suspended in the air
Since it is a column, the x coordinate will stay the same, so we set x=20
Loop the y coordinate to form 5 rock stacked on top of each other forming a straight column, with y start from 57
The spawnRocksfive function will be called at MainGameScreen.java and will appear when switching to a new map.
"spawnEntityAt" function helps us choose the coordinate position of the center of the rock, from there using rock.png and will scale the size of the rock horizontally and vertically.
public void spawnRocksfive(int xValue) {
for (int i = 0; i < 4; i++) {
GridPoint2 pos = new GridPoint2(xValue + 20, 57+i);
Entity rock = ObstacleFactory.createRock();
spawnEntityAt(rock, pos, true, false);
}
}
Create large rock pyramid
Create the game "rock" obstacle
Form a rocks larger pyramid with ten rocks stacked on top of each other, a total of 6 rocks needed
Since the pyramid consists of four floors from the ground, the y coordinate of the rocks will be from 50 to 53, since y=50 the co-ordinate of the rocks will be on the ground
The x coordinate will depend on the location of the rocks.
The spawnRocksssixfunction will be called at MainGameScreen.java and will appear when switching to a new map.
"spawnEntityAt" function helps us choose the coordinate position of the center of the rock, from there using rock.png and will scale the size of the rock horizontally and vertically.
public void spawnRockssix(int xValue) {
GridPoint2 Pos = new GridPoint2 ( xValue + 22, 53);
Entity rock = ObstacleFactory.createRock();
spawnEntityAt(rock, Pos, true, false);
GridPoint2 PosTwo = new GridPoint2 ( xValue + 22, 52);
Entity rockTwo = ObstacleFactory.createRock();
spawnEntityAt(rockTwo, PosTwo, true, false);
GridPoint2 PosThree = new GridPoint2 ( xValue + 22, 51);
Entity rockThree = ObstacleFactory.createRock();
spawnEntityAt(rockThree, PosThree, true, false);
GridPoint2 PosFour = new GridPoint2 ( xValue + 22, 50);
Entity rockFour = ObstacleFactory.createRock();
spawnEntityAt(rockFour, PosFour, true, false);
GridPoint2 PosFive = new GridPoint2 ( xValue + 23, 52);
Entity rockFive = ObstacleFactory.createRock();
spawnEntityAt(rockFive, PosFive, true, false);
GridPoint2 PosSix = new GridPoint2 ( xValue + 23, 51);
Entity rockSix = ObstacleFactory.createRock();
spawnEntityAt(rockSix, PosSix, true, false);
GridPoint2 PosSeven = new GridPoint2 ( xValue + 23, 50);
Entity rockSeven = ObstacleFactory.createRock();
spawnEntityAt(rockSeven, PosSeven, true, false);
GridPoint2 PosEight = new GridPoint2 ( xValue + 24, 51);
Entity rockEight = ObstacleFactory.createRock();
spawnEntityAt(rockEight, PosEight, true, false);
GridPoint2 PosNine = new GridPoint2 ( xValue + 24, 50);
Entity rockNine = ObstacleFactory.createRock();
spawnEntityAt(rockNine, PosNine, true, false);
GridPoint2 PosTen = new GridPoint2 ( xValue + 25, 50);
Entity rockTen = ObstacleFactory.createRock();
spawnEntityAt(rockTen, PosTen, true, false);
}
UML diagram
Overview
BackgroundRenderComponent & MainGameScreen
ForestGameArea
Sequence diagram
ForestGameArea_spawnWoods()
ForestGameArea_spawnRocksone (Generate line 5 rocks in the new map)
ForestGameArea_showNewMapBackground
ForestGameArea_spawnInvisibleCeiling
ForestGameArea_spawnGoldNewMap
ForestGameArea_playNewMapMusic