Other Related Coder for Obstacles - UQdeco2800/2021-ext-studio-2 GitHub Wiki
Table of Contents
Unlimited generation
Rendering order
Sound
Unlimited generation
All functions in ForestGameArea.java that require endless generation will be called by render()
in MainGameScreen.java.
This judging function is written in render()
by the members of team6 to generate endless terrain. Thank them very much for their efforts, and we have used their logic to achieve the function of endless generation
// Centralize the screen to player
Vector2 screenVector = player.getPosition();
screenVector.y = 7f;
renderer.getCamera().getEntity().setPosition(screenVector);
// infinite loop for terrain and obstacles
if (screenVector.x > (2 * counter + 1) * 10) {
counter += 1;
forestGameArea.showScrollingBackground(counter);
forestGameArea.spawnTerrainRandomly((int) (screenVector.x + 2));
// Generate obstacles
forestGameArea.spawnObstacles();
// Generate meteorites
forestGameArea.spawnMeteorites(0, 1, 2, 1, 1, 2);
// Generate enemies
forestGameArea.spawnFlyingMonkey();
}
Rendering order
When an entity needs to be generated on top of other entities, the engine's rendering order for these entities needs to be adjusted.
For example, this rendering sequence can solve the bug of thorns below the ground.
In entity.java, the zIndex
of entity can be determined by setting the following function.The larger the zIndex
, the lower the rendering order.
public void setZIndex(int zIndex)
zIndex
works in RenderComponent.java.
/**
* Set the zIndex of the entity according to the y coordinate of the entity and the original zIndex.
*
* If zIndex is not set originally, then The smaller the Y value, the higher the Z index, so that closer entities
* are drawn in front.
*
* If zIndex has been set, zIndex will not change.
*
* @return The drawing priority of the current entity
*/
@Override
public float getZIndex() {
// The smaller the Y value, the higher the Z index, so that closer entities are drawn in front
if (this.getEntity().getZIndex() == 0) {
return -entity.getPosition().y;
} else {
return entity.getZIndex();
}
Sound
SoundComponent.java When creating an entity, you can add a sound component to it. This component is used to add different listeners according to different entity types. This separates sound from other functions, which is conducive to tests and a more optimized code structure.
public void create() {
switch (obstacleType) {
case Spaceship:
entity.getEvents().addListener("spaceshipSound", this::spaceshipSound);
break;
case SmallMissile:
entity.getEvents().addListener("missileSound", this::missileSound);
break;
case FlyingMonkey:
entity.getEvents().addListener("roarSound", this::roarSound);
break;
default:
logger.debug("No corresponding sound.");
}
}