How to Make Android 2D car game Tutorial 7

Hello what's going on guys I'm back for this another post of android 2d
car game tutorial series, this will be tutorial number 7 and with in this
tutorial we will finally create our GameRenderer Class. So for what this
class means in our game?.. well this class is intentionally created to
organize classes that is means for rendering in e..g
BitmapFont,Textures,Particles,Animation and others that related to
rendering class. so since this class will hold some methods and
attributes that use for rendering properties. so the class attributes that we
need are the "Viewport" as usual since we cannot render without
camera then we need"OrthographicCamera","SpriteBatch","AssetManager", this common classes are often use in libgdx when creating games. Other Attributes are
Enemy Object's that will use Texture as for their template so we can
determine different types of car that will be added to our World. After
all declaring Attribute classes as field, It's about time to start
creating our methods, our first method that will run will be the
Constructor's our Renderer Constructor hold two types of classes as
parameter, the first params will be our first Generated class or
"GdxGame" and the next params will be the "GameController", We pass
GameController as parameter.. So The GameRenderer will Know what
are the Game object's to be initialize and since all of our game object's
are place there not including graphics related classes. then therefore
GameRenderer look for that class before we can assign specific
textures for that particular class.Other Methods can be seen by the
codes below. if you want more info just watch my video above and
please subscribe to my youtube channel to get more update of new
video.

package game;
import utilities.CameraHelper;
import utilities.ViewportUtils;
import assets.AssetsDescriptors;
import assets.RegionNames;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.Logger;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.ninal.game.GdxGame;
import config.GameConfig;
import entities.Ambulance;
import entities.BlackViper;
import entities.MiniVan;
import entities.Player;
import entities.Police;
import entities.Taxi;
import entities.Truck;

public class GameRenderer implements Disposable {
private static final Logger log = new Logger(GameRenderer.class.getSimpleName(), Logger.DEBUG);
private final SpriteBatch batch;
private final AssetManager manager;
private final GameController controller;
private BitmapFont font;
private Viewport viewport;
private Viewport hudViewport;
private OrthographicCamera hudCamera;
private TextureRegion roadRegion;
private TextureRegion playerRegion;
private Animation ambulanceAnimation;
private TextureRegion taxiRegion;
private Animation policeAnimation;
private TextureRegion blackViperRegion;
private TextureRegion truckRegion;
private TextureRegion vanRegion;
private OrthographicCamera camera;
private ShapeRenderer renderer;
private CameraHelper cameraHelper;

public GameRenderer(GdxGame carAudi,GameController controller) {
this.batch = carAudi.getBatch();
this.manager = carAudi.getManager();
this.controller = controller;
init();
}

private void init() {
camera = new OrthographicCamera();
viewport = new FitViewport(GameConfig.WORLD_WIDTH,GameConfig.WORLD_HEIGHT, camera);
hudCamera = new OrthographicCamera();
hudViewport = new FitViewport(GameConfig.HUD_WIDTH, GameConfig.HUD_HEIGHT,hudCamera);
font = manager.get(AssetsDescriptors.FONT);
Texture texture = new Texture(Gdx.files.internal(“road.png”));
roadRegion = new TextureRegion(texture);
TextureAtlas gameAtlas = manager.get(AssetsDescriptors.GAME_ATLAS);
playerRegion = gameAtlas.findRegion(RegionNames.AUDI);
blackViperRegion = gameAtlas.findRegion(RegionNames.BLACK_VIPER);
taxiRegion = gameAtlas.findRegion(RegionNames.TAXI);
truckRegion = gameAtlas.findRegion(RegionNames.TRUCK);
vanRegion = gameAtlas.findRegion(RegionNames.MINI_VAN);
ambulanceAnimation = new Animation(0.1f, gameAtlas.findRegions(RegionNames.AMBULANCE), PlayMode.LOOP_PINGPONG);
policeAnimation = new Animation(0.1f,gameAtlas.findRegions(RegionNames.POLICE ),PlayMode.LOOP_PINGPONG);
renderer = new ShapeRenderer();
cameraHelper = new CameraHelper();
cameraHelper.setstartPosition(GameConfig.WORD_CENTER_X,GameConfig.WORLD_CENTER_Y);
}
public void render(float dt){
Gdx.gl20.glClearColor(0, 0, 0, 0);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
cameraHelper.handleInputKeys(dt);
cameraHelper.applyTo(camera);
ViewportUtils.drawGridLines(viewport, renderer);
hudViewport.apply();
viewport.apply();

if(Gdx.input.isTouched() && !controller.isGameOver()){
Vector2 screenTouch = new Vector2(Gdx.input.getX(), Gdx.input.getY());
Vector2 worldTouch = viewport.unproject(screenTouch);
System.out.println("screenTouch = "  +screenTouch + "worldTouch = " +worldTouch);

}
batch.setProjectionMatrix(camera.combined);
batch.begin();
drawRoadTexture(dt);
drawambulanceRegion(dt);
drawPlayerParticle(batch, dt);
drawEnemyParticle(batch, dt);
drawPlayerTexture(dt);
drawPoliceRegion(dt);
drawTaxiRegion(dt);
drawBlackViperRegion(dt);
drawTruckRegion(dt);
drawMiniVanRegion(dt);

batch.end();
drawHud(hudViewport);
renderer.setProjectionMatrix(camera.combined);
renderer.begin(ShapeRenderer.ShapeType.Line);

// debugPlayer(renderer);
// debugAmbulance(renderer);
// debugMiniVan(renderer);
// debugTruck(renderer);
// debugPolice(renderer);
// debugTaxi(renderer);
// debugBlackViper(renderer);
renderer.end();
}

private void drawPlayerParticle(SpriteBatch batch, float dt){
ParticleEffect playerParticle = controller.getFlameParticle();
playerParticle.draw(batch, dt);
ParticleEffect playerExplodeEffects = controller.getPlayerExplodeEffects();
if(controller.isGameOver()){
playerExplodeEffects.draw(batch, dt);
}

}

private void drawEnemyParticle(SpriteBatch batch, float dt){
ParticleEffect enemyParticle = controller.getEnemyParticle();
enemyParticle.draw(batch, dt);

// ParticleEffect explodeEffects = controller.getExplodeEffect();
// if(controller.isGameOver()){
// explodeEffects.draw(batch, dt);
// }
}
////== Debug Player==
// private void debugPlayer(ShapeRenderer renderer) {
// Player player = controller.getPlayer();
// renderer.setColor(Color.YELLOW);
// player.renderDebug(renderer);
// }
////== Debug Ambulance ==
// private void debugAmbulance(ShapeRenderer renderer) {
// for(Ambulance ambulance : controller.getAmbulanceArray()){
// renderer.setColor(Color.GREEN);
// ambulance.renderDebug(renderer);
// }
//}
////==Debug MiniVan==
// private void debugMiniVan(ShapeRenderer renderer){
// for(MiniVan van : controller.getMinivanArray()){
// renderer.setColor(Color.BLUE);
// van.renderDebug(renderer);
// }
// }
//
// //==Debug Police==
// private void debugPolice(ShapeRenderer renderer){
// for(Police police : controller.getArrayPolice()){
// renderer.setColor(Color.ORANGE);
// police.renderDebug(renderer);
// }
// }
//
// //==Debug MiniTruck==
// private void debugTruck(ShapeRenderer renderer){
// for(Truck truck : controller.getTruckArray()){
// renderer.setColor(Color.RED);
// truck.renderDebug(renderer);
// }
// }
//
// //==Debug MiniTruck==
// private void debugTaxi(ShapeRenderer renderer){
// for(Taxi taxi : controller.getArrayTaxi()){
// renderer.setColor(Color.RED);
// taxi.renderDebug(renderer);
// }
// }
// //==Debug BlackViper==
// private void debugBlackViper(ShapeRenderer renderer){
// for(BlackViper viper : controller.getBlackViperArray()){
// renderer.setColor(Color.RED);
// viper.renderDebug(renderer);
// }
// }
//== Draw Hud Scores And Life ==
private void drawHud(Viewport viewport){
String Life = “Life : “;
String Score = “Score : “;
batch.setProjectionMatrix(hudCamera.combined);
batch.begin();
font.draw(batch, Life+controller.getLife(), 20, GameConfig.HUD_HEIGHT -20);
font.draw(batch, Score+controller.getDisplayScore(), GameConfig.HUD_WIDTH – 180, GameConfig.HUD_HEIGHT -20);
batch.end();
}
// == draw Road Texture ==
private void drawRoadTexture(float dt){

roadRegion.scroll(0, -controller.getScrollAmount());
batch.draw(roadRegion, 0, 0, GameConfig.WORLD_WIDTH, GameConfig.WORLD_HEIGHT);

}
//== Draw Entity Texture ==
private void drawPlayerTexture(float dt){
Player player = controller.getPlayer();
batch.draw(playerRegion, player.getX(), player.getY() ,player.getWidth(), player.getHeight());
}

private void drawambulanceRegion(float dt){
TextureRegion ambulanceRegion = (TextureRegion)ambulanceAnimation.getKeyFrame(controller.getAnimationTimer());
Array ambulanceArray= controller.getAmbulanceArray();
for(Ambulance ambulance : ambulanceArray ){
batch.draw(ambulanceRegion, ambulance.getX(), ambulance.getY(), ambulance.getWidth(), ambulance.getHeight());
}
}
private void drawPoliceRegion(float dt){
TextureRegion policeRegion = (TextureRegion)policeAnimation.getKeyFrame(controller.getAnimationTimer());
Array policeArray= controller.getArrayPolice() ;
for(Police police : policeArray ){
batch.draw(policeRegion, police.getX(), police.getY(), police.getWidth(), police.getHeight());
}

}
private void drawTaxiRegion(float dt){
Array taxiArray = controller.getArrayTaxi();
for(Taxi taxi : taxiArray){
batch.draw(taxiRegion, taxi.getX(), taxi.getY(), taxi.getWidth(), taxi.getHeight());
}
}

private void drawBlackViperRegion(float dt){
Array blackViperArray = controller.getBlackViperArray();
for(BlackViper truck : blackViperArray){
batch.draw(blackViperRegion, truck.getX(), truck.getY(), truck.getWidth(), truck.getHeight());
}
}

private void drawTruckRegion(float dt){
Array truckArray = controller.getTruckArray();
for(Truck truck : truckArray){
batch.draw(truckRegion, truck.getX(), truck.getY(), truck.getSize(), truck.getSize());
}
}

private void drawMiniVanRegion(float dt){
Array vanArray = controller.getMinivanArray();
for(MiniVan van : vanArray){
batch.draw(vanRegion, van.getX(), van.getY(), van.getSize(), van.getSize());
}
}

public void resize(int width, int height){
viewport.update(width, height, true);
hudViewport.update(width, height, true);
}
public void dispose(){
renderer.dispose();}}

Post a Comment

0 Comments