How to Make Android 2D car game Tutorial 2

Class Attributes:
AssetManager,ShapeRenderer,Viewport,OrthographicCamera, SpriteBatch,TextureRegion,BitmapFont

Class Description:
AssetManager: Loads and stores assets like textures, bitmap fonts, tile maps, sounds, music, and so on.
ShapeRenderer: Renders points, lines, shape outlines, and filled shapes. By default, a 2D orthographic projection with the origin in the lower-left corner is used and units are specified in screen pixels. This can be changed by configuring the projection matrix, usually using the matrix. If the screen resolution changes, the projection matrix may need to be updated. Shapes are rendered in batches to increase performance.

The standard usage pattern looks as follows:
camera.update(); shapeRenderer.setProjectionMatrix(camera.combined); shapeRenderer.begin(ShapeType.Line); shapeRenderer.setColor(1, 1, 0, 1); shapeRenderer.line(x, y, x2, y2); shapeRenderer.rect(x, y, width, height); shapeRenderer.circle(x, y, radius); shapeRenderer.end(); shapeRenderer.begin(ShapeType.Filled); shapeRenderer.setColor(0, 1, 0, 1); shapeRenderer.rect(x, y, width, height); shapeRenderer.circle(x, y, radius); shapeRenderer.end();

ShapeRenderer has a second matrix called the transformation matrix which is used to rotate, scale and translate shapes in a more flexible manner. The following example shows how to rotate a rectangle around its center using the z-axis as the rotation axis and placing its center at (20, 12, 2):

shapeRenderer.begin(ShapeType.Line); 
shapeRenderer.identity(); 
shapeRenderer.translate(20, 12, 2); 
shapeRenderer.rotate(0, 0, 1, 90); 
shapeRenderer.rect(-width / 2, -height / 2, width, height); 
shapeRenderer.end();


Matrix operations all use postmultiplication and work just like glTranslate, glScale, and glRotate. The last transformation specified will be the first that is applied to a shape (rotate then translate in the above example).

The projection and transformation matrices are a state of the ShapeRenderer, just like the color, and will be applied to all shapes until they are changed.

Viewport: Manages a Camera and determines how world coordinates are mapped to and from the screen.

3 Types of Viewport

FitViewport: A ScalingViewport that uses Scaling.fit so it keeps the aspect ratio by scaling the world up to fit the screen, adding black bars (letterboxing) for the remaining space.

FillViewport: A ScalingViewport that uses Scaling.fill so it keeps the aspect ratio by scaling the world up to take the whole screen (some of the worlds may be off-screen).

ExtendViewport: A viewport that keeps the world aspect ratio by extending the world in one direction. The world is first scaled to fit within the viewport, then the shorter dimension is lengthened to fill the viewport. Maximum size can be specified to limit how much the world is extended and black bars (letterboxing) are used for any remaining space.

OrthographicCamera: A camera with orthographic projection.

SpriteBatch: Draws batched quads using indices.

TextureRegion: Defines a rectangular area of a texture. The coordinate system used has its origin in the upper left corner with the x-axis pointing to the right and the y axis pointing downwards.

BitmapFont: Renders bitmap fonts. The font consists of 2 files: an image file or containing the glyphs and a file in the AngleCode BMFont text format that describes where each glyph is on the image.

Text is drawn using a Batch. Text can be cached in a BitmapFontCache for faster rendering of static text, which saves needing to compute the location of each glyph in each frame.

The texture for a BitmapFont loaded from a file is managed. dispose() must be called to free the texture when no longer needed. A BitmapFont loaded using a TextureRegion is managed if the region’s texture is managed. Disposing of the BitmapFont disposes of the region’s texture, which may not be desirable if the texture is still being used elsewhere.

package screens;
 import utilities.CameraHelper; 
import assets.AssetsDescriptors; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.ScreenAdapter; 
import com.badlogic.gdx.assets.AssetManager; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.BitmapFont;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion;
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 
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; 
public class LoadingScreen extends ScreenAdapter { private static final Logger log = new Logger(LoadingScreen.class.getSimpleName(), Logger.DEBUG); 
private final GdxGame game;A private final AssetManager manager; 
private float time = 0.60f; 
private float progress; 
private boolean changeScreen; 
private static final float PROGRESS_BAR_WITH = GameConfig.HUD_WIDTH; 
private static final float PROGRESS_BAR_HEIGHT = 50f;
private ShapeRenderer renderer; 
private Viewport viewport; 
private OrthographicCamera camera; 
private CameraHelper cameraHelper;
 private SpriteBatch batch; 
private TextureRegion bgRegion; 
private BitmapFont font;
 private static final float PROGRESS_BAR_X = 475f;
 private static final float PROGRESS_BAR_Y = 700f;
 private static final float OFF_SET_SCREEN_WIDTH = 10f; 

public LoadingScreen(GdxGame game) { this.game = game; this.manager = game.getManager(); } 
@Override public void show() { renderer = new ShapeRenderer();
 camera = new OrthographicCamera();
 viewport = new FitViewport(GameConfig.HUD_WIDTH, GameConfig.HUD_HEIGHT, camera); batch = game.getBatch(); Texture texture = new Texture(Gdx.files.internal("loading-img.png")); bgRegion = new TextureRegion(texture); font = new BitmapFont(Gdx.files.internal("font/oswald-32.fnt")); // cameraHelper = new CameraHelper(); // cameraHelper.setstartPosition(GameConfig.HUD_WIDTH / // 2,GameConfig.HUD_HEIGHT / 2); manager.load(AssetsDescriptors.FONT);
manager.load(AssetsDescriptors.GAME_ATLAS); 
manager.load(AssetsDescriptors.BTN_SKIN); 
manager.load(AssetsDescriptors.BTN_ATLAS); 
manager.load(AssetsDescriptors.UISKIN);
 manager.load(AssetsDescriptors.FLY_SOUNDS); manager.load(AssetsDescriptors.NAVIGATE_SOUND_3); manager.load(AssetsDescriptors.GAME_SOUNDS); manager.load(AssetsDescriptors.CLICK_SOUND); manager.load(AssetsDescriptors.EXPLODE_SOUND); manager.load(AssetsDescriptors.EXPLODE_MINI_SOUND); manager.load(AssetsDescriptors.ENGINE_SOUND); manager.load(AssetsDescriptors.AUDI_EFFECTS); manager.load(AssetsDescriptors.ENEMY_EFFECTS); manager.load(AssetsDescriptors.EXPLODE_EFFECTS); manager.load(AssetsDescriptors.PLAYER_EXPLODE_EFFECTS); 
@Override public void resize(int width, int height) { 
viewport.update(width, height, true); 
}
 @Override public void render(float delta) { 
Gdx.gl20.glClearColor(0, 0, 0, 0);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); 
viewport.apply(); 
update(delta); 
// cameraHelper.handleInputKeys(delta);
 // cameraHelper.applyTo(camera); 
batch.setProjectionMatrix(camera.combined); 
batch.begin();
 String loading = "Loading..."; 
batch.draw(bgRegion, 0, 0, GameConfig.HUD_WIDTH, GameConfig.HUD_HEIGHT); font.setColor(Color.GOLD); font.draw(batch, loading + (manager.getLoadedAssets() + 76), GameConfig.HUD_WIDTH / 2.5f, GameConfig.HUD_HEIGHT / 4);
 batch.end();
 renderer.setProjectionMatrix(camera.combined); 
drawProgressBar();
 if (changeScreen) { game.setScreen(new MenuScreen(game));
 }
 }
 private void update(float dt) { progress = manager.getProgress();
 if (manager.update()) {
 time -= dt; if (time <= 0) {
 changeScreen = true; }
 }
 } private void drawProgressBar() {
 float progressBarX = (GameConfig.HUD_WIDTH - PROGRESS_BAR_X); float progressBarY = (GameConfig.HUD_HEIGHT - PROGRESS_BAR_Y); renderer.begin(ShapeRenderer.ShapeType.Filled);
 renderer.setColor(Color.CYAN); 
renderer.rect(progressBarX, progressBarY, progress * PROGRESS_BAR_WITH - OFF_SET_SCREEN_WIDTH, PROGRESS_BAR_HEIGHT); 
renderer.end();
 drawRectLine();
 }
 private void drawRectLine() {
 float progressBarX = (GameConfig.HUD_WIDTH - PROGRESS_BAR_X);
 float progressBarY = (GameConfig.HUD_HEIGHT - PROGRESS_BAR_Y); renderer.begin(ShapeRenderer.ShapeType.Line); 
renderer.setColor(Color.BLACK); renderer.rect(progressBarX, progressBarY, PROGRESS_BAR_WITH - OFF_SET_SCREEN_WIDTH, PROGRESS_BAR_HEIGHT); renderer.end(); 
}
 @Override public void hide() { 
dispose();
 } 
@Override public void dispose() { 
renderer.dispose();
 } 
}

Post a Comment

0 Comments