package com.jonathan.survivor.renderers;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.jonathan.survivor.CombatLevel;
import com.jonathan.survivor.Level;
import com.jonathan.survivor.TerrainLevel;
/*
* Draws level geometry. Level instances are passed by this class to their designated Renderer objects to be drawn.
*/
public class LevelRenderer
{
/** Stores the camera where the terrain is drawn. In this case, the world camera. */
private OrthographicCamera worldCamera;
/** Stores the SpriteBatcher used to draw the level's elements. */
private SpriteBatch batcher;
/** Holds the renderer used to draw the background. */
private BackgroundRenderer backgroundRenderer;
/** Stores the renderer used to draw terrain. */
private TerrainRenderer terrainRenderer;
/** Holds the renderer used to draw the combat level's terrain. */
private CombatRenderer combatRenderer;
public LevelRenderer(SpriteBatch batcher, OrthographicCamera worldCamera)
{
//Stores the constructor arguments in their respective member variables.
this.batcher = batcher;
this.worldCamera = worldCamera;
//Instantiates the BackgroundRenderer, used to render the Background, and update it so that it follows the player's position.
//backgroundRenderer = new BackgroundRenderer(batcher, worldCamera);
//Creates a TerrainRenderer used to draw terrain geometry. Passes the world camera, where the terrain's lines should be drawn.
terrainRenderer = new TerrainRenderer(worldCamera);
//Instantiates a CombatRenderer used to draw the terrain of a combat level. Passes the camera where the lines should be drawn.
combatRenderer = new CombatRenderer(worldCamera);
}
/** Renders a level's geometry. This method is a helper method which delegates the level instance to a more specific renderer. */
public void render(Level level)
{
//Updates the background's position and draws it to the screen.
//backgroundRenderer.render(Gdx.graphics.getDeltaTime());
//If the level we want to draw is a TerrainLevel
if(level instanceof TerrainLevel)
//Delegate the rendering to the TerrainRenderer.
terrainRenderer.render((TerrainLevel)level);
//Else, if the level to draw is a CombatLevel
else if(level instanceof CombatLevel)
//Delegate the draw call to the CombatRenderer.
combatRenderer.render((CombatLevel)level);
}
/** Called whenever the screen is resized. The argument contains the factor by which the screen had to be scaled compared to the target
* resolution. We have to rescale our lines by this factor for the lines to look the same size no matter the screen. */
public void resize(float screenScale)
{
//Resizes the terrain lines by the amount we had to scale the default frustum to fit the target device.
terrainRenderer.resize(screenScale);
}
}