Bridge Component - UQdeco2800/2021-ext-studio-1 GitHub Wiki
Almost all other feature teams are dependent on map generation. It is crucial in creating map content (such as obstacles and power ups) and coding enemies and player movement into the game. Thereby, map generation is one of the most important components of Ragnarok Racers.
This feature will see the development of the Bifrost. This is the vibrant rainbow bridge in which the main character of the game races across. This feature will also create the starry backdrop of the Bifrost. This will be achieved by creating textures for the map in Photoshop and creating various methods to replicate the map design in Java code.
Furthermore, this feature will introduce several new classes. Namely, it will introduce Bridge and Lane classes. These will segment the map into smaller chunks, allowing for more complex functionalities to be built into the game. For example, these classes will be vital in establishing bounds for player movement and creating methods which allow users to move between lanes.
You can access the rainbow bridge directly under
source/core/src/main/com/deco2800/game/screens/RagnorakRacer.java
public class RagnorakRacer extends ScreenAdapter {
private final GdxGame game;
private final Renderer renderer;
/** The Bridge instance is available here under Ragnorak Racer */
private Bridge rainbowBridge;
private static final Vector2 CAMERA_POSITION = new Vector2(7.5f, 7.5f);
public RagnorakRacer(GdxGame game) {
this.game = game;
ServiceLocator.registerInputService(new InputService());
ServiceLocator.registerResourceService(new ResourceService());
ServiceLocator.registerEntityService(new EntityService());
ServiceLocator.registerRenderService(new RenderService());
this.renderer = RenderFactory.createRenderer();
this.renderer.getCamera().getEntity().setPosition(CAMERA_POSITION);
createUI();
TerrainFactory terrainFactory = new TerrainFactory(renderer.getCamera());
RainbowBridge rainbowBridge = new RainbowBridge(terrainFactory);
rainbowBridge.create();
/** The rainbowBridge attribute is initialized once the terrain is created */
this.rainbowBridge = rainbowBridge.getRainbowBridge();
}
...
A Bridge class holds several Lane classes, which can be accessed from a List<bridge>
The bridge is created as a component and can be found under source\core\src\main\com\deco2800\game\components\bridge
The javadoc in Bridge.java & Lane.java explains exactly how the two work together to create the rainbow bridge
source/core/src/main/com/deco2800/game/components/bridge/Bridge.java
public class Bridge {
/**
* Offsets the location of the bridge to be
* Used to change where the bridge is drawn on-screen
* */
private int offset;
private int width;
/**
* Constant value for y range from the bottom of the grid to the top
*/
private float yGridBot = 2f;
private float yGridTop = 26f;
/**
* Sets a custom y range from the bottom to the top of the screen
* Used to change where the bridge is drawn on-screen
* */
private float yViewBot;
private float yViewTop;
protected Entity player;
/** A list of lanes on a bridge */
private List<Lane> lanes;
...
...
/**
* Returns the top of the bridge from the user's perspective
* TerrainFactory fills tiles from the bottom up
* @return a y-coordinate
*/
public float getTop() {
return rescaleYGridToYView(this.getLastLane().getTop());
}
/**
* Returns the bottom of the bridge from the user's perspective
* TerrainFactory fills tiles from the bottom up
* @return a y-coordinate
*/
public float getBot() {
return rescaleYGridToYView(this.getLanes().get(0).getBot());
}
/**
* Returns the center of the bridge from the user's perspective
* TerrainFactory fills tiles from the bottom up
* @return a y-coordinate
*/
public float getMid() {
return rescaleYGridToYView((this.getTop() + this.getBot()) / 2);
}
...
source/core/src/main/com/deco2800/game/components/bridge/Bridge.java
public class Lane implements Location {
private float y1;
private float y2;
private float mid;
private Bridge bridge;
/**
* Set 2 y-coordinate that defines the bounds of a lane
* @param top top y coordinate
* @param bot bottom y coordinate
*/
public Lane(float top, float bot, Bridge bridge) {
if (top >= bot) {
throw new IllegalArgumentException("top y-coordinate cannot be bigger or equal to bottom y-coordinate");
} else {
this.y1 = top;
this.y2 = bot;
this.mid = (top + bot) / 2;
this.bridge = bridge;
}
}
...
...
/**
* returns the center y-coordinate of the lane from the user's perspective
* TerrainFactory fills tiles from the bottom up
* @return a y-coordinate
*/
public float getMid() {
return this.bridge.rescaleYGridToYView(this.mid);
}
/**
* Returns the top of the lane from the user's perspective
* TerrainFactory fills tiles from the bottom up
* @return a y-coordinate
*/
public float getTop() {
return this.bridge.rescaleYGridToYView(this.y2);
}
/**
* Returns the bottom of the lane from the user's perspective
* TerrainFactory fills tiles from the bottom up
* @return a y-coordinate
*/
public float getBot() {
return this.bridge.rescaleYGridToYView(this.y1);
}
...
source/core/src/main/com/deco2800/game/components/bridge/Location.java
public interface Location {
float getTop();
float getMid();
float getBot();
}
To correctly obtain the y-coordinate of each lane, you must set Bridge.yViewTop and Bridge.yViewBot to the top and bottom viewable part of the game screen
For example
float top = 13f;
float bot = 0.5f;
Bridge bridge = RagnorakRacer.rainbowBridge;
/** Setting top and bottom y-coordinate of the viewable screen
bridge.setYViews(top, bot);
// or
bridge.setYViewTop(top)
bridge.setYViewBot(bot)
/** Obtaining lanes */
List<Lane> lanes = bridge.getLanes()
/** Obtaining y-coordinates */
Lane lane = lanes.get(0)
lane.getBot() // --> some y-coordinate
lane.getMid() // --> some y-coordinate
lane.getTop() // --> some y-coordinate