com.garciagars.Game.java Source code

Java tutorial

Introduction

Here is the source code for com.garciagars.Game.java

Source

package com.garciagars;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.files.FileHandle;
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.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.garciagars.Class.Bullet;
import com.garciagars.Class.Enemy;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//Code and logic by Jan Michael S. Garcia
//
//This file is part of FinalsApp.
//
//FinalsApp is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Foobar is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FinalsApp.  If not, see <http://www.gnu.org/licenses/>.
public class Game extends com.badlogic.gdx.Game implements InputProcessor {

    public interface GameCallback {
        void onGameOver(float score);
    }

    public Game(GameCallback gameCallback) {
        this.gameCallback = gameCallback;
    }

    GameCallback gameCallback;
    OrthographicCamera camera;
    SpriteBatch batch;

    BitmapFont lblHealth;
    BitmapFont lblScore;

    Texture bg;
    Texture danger;
    Rectangle dangerCollision;
    int height, width;
    float dropRate = 1f;
    float heaven = 0f;
    float dangerDistance = 1000f;
    float scorePerKill = 1f;
    float score = 0;

    Texture ship;
    float shipW = 200f;
    float shipH = 200f;
    float x = 0f;
    float y = 0f;
    float shipLife = 100f;
    float damage = 0.5f;

    List<Bullet> bullets;
    List<Bullet> bulletDispose;
    float bulletLimit = 0f;
    boolean shootNow = true;

    List<Enemy> enemies;
    List<Enemy> enemyDispose;
    float enemyLife = 100f;
    float enemyW = 100f;
    float enemyH = 100f;
    int enemySizeLimit = 10;
    float enemyDamage = 10;

    @Override
    public void create() {
        enemies = new ArrayList();
        enemyDispose = new ArrayList();

        bullets = new ArrayList();
        bulletDispose = new ArrayList();

        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        height = Gdx.graphics.getHeight();
        width = Gdx.graphics.getWidth();
        Gdx.input.setInputProcessor(this);
        bg = new Texture(Gdx.files.internal("bg.bmp"));
        ship = new Texture(Gdx.files.internal("ship.png"));
        danger = new Texture(Gdx.files.internal("danger.png"));

        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("arcade.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.flip = true;
        parameter.size = 45;
        parameter.color = Color.WHITE;

        lblHealth = generator.generateFont(parameter);
        lblScore = generator.generateFont(parameter);

        generator.dispose();

        //      setX to be centered
        x = width - shipW / 2;
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch = new SpriteBatch();

        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        DrawBG(batch);
        SpawnWeapon();
        RenderBullet(batch);
        SpawnShip(batch);
        GenerateEnemy();
        EnemyLogic(enemies, batch);

        lblHealth.draw(batch, shipLife + "%", 50f, 50f);
        lblScore.draw(batch, score + "pts", width - 250f, 50f);

        batch.end();
    }

    void DrawBG(SpriteBatch batch) {
        batch.draw(bg, 0f, 0f, width, height);
        TextureRegion dangerTexture = new TextureRegion(danger);
        dangerTexture.flip(false, true);

        batch.draw(dangerTexture, 0f, dangerDistance, width, 50f);
        dangerCollision = new Rectangle(0f, dangerDistance, width, 50f);
    }

    void SpawnWeapon() {
        if (shootNow) {
            if (bullets.size() < bulletLimit || bulletLimit == 0f) {
                bullets.add(new Bullet(damage, 10f, x + 75f, height - ship.getHeight() / 2, 50f, 80f));
            }
        }
        shootNow = !shootNow;
    }

    void RenderBullet(SpriteBatch batch) {
        for (Bullet bullet : bullets) {
            if (bullet.getY() < 50f) {
                if (!bulletDispose.contains(bullet)) {
                    bulletDispose.add(bullet);
                    bullet.dispose();
                }
            } else {
                bullet.setY(bullet.getY() - bullet.getSpeed());
                batch.draw(bullet.getTexture(), bullet.getX(), bullet.getY(), bullet.getW(), bullet.getH());
            }
        }
    }

    void SpawnShip(SpriteBatch batch) {
        batch.draw(ship, x, height - ship.getHeight() / 2, shipW, shipH);
    }

    void EnemyLogic(java.util.List<Enemy> enemies, SpriteBatch batch) {
        for (Enemy enemy : enemies) {
            if (Intersector.intersectRectangles(enemy.getCollision(), dangerCollision, new Rectangle())) {
                if (!enemyDispose.contains(enemy)) {
                    enemyDispose.add(enemy);
                    shipLife -= enemy.getDamage();

                    if (shipLife <= 0f) {
                        //gameoverrrrrrrrrrr
                        gameCallback.onGameOver(score);
                    }
                }
            } else {
                enemy.setY(enemy.getY() + dropRate);
                batch.draw(enemy.getTexture(), enemy.getX(), enemy.getY(), enemy.getW(), enemy.getH());
            }
            for (Bullet bullet : bullets) {
                if (Intersector.intersectRectangles(enemy.getCollision(), bullet.getCollision(), new Rectangle())) {
                    enemy.setLife(enemy.getLife() - bullet.getDamage());
                    //                    if (!enemyDispose.contains(enemy)){
                    if (enemy.getLife() <= 0f) {
                        //                            killed
                        score += scorePerKill;
                        enemyDispose.add(enemy);
                        bulletDispose.add(bullet);
                    }
                    //                    }
                }
            }
        }
        CleanUp();
    }

    void GenerateEnemy() {
        if (enemies.size() <= enemySizeLimit) {
            if (new Random().nextBoolean()) {
                if (new Random().nextBoolean()) {
                    if (new Random().nextBoolean()) {
                        int randomPos = (int) (width * Math.random());
                        enemies.add(new Enemy(enemyLife, randomPos, heaven, enemyH, enemyW, enemyDamage));
                    }
                }
            }
        }
    }

    void CleanUp() {
        for (int i = 0; i < bulletDispose.size(); i++) {
            bullets.remove(bulletDispose.get(i));
            bulletDispose.remove(bulletDispose.get(i));
        }

        for (int i = 0; i < enemyDispose.size(); i++) {
            enemies.remove(enemyDispose.get(i));
            enemyDispose.remove(enemyDispose.get(i));
        }
    }

    @Override
    public void dispose() {
        batch.dispose();
        danger.dispose();
        ship.dispose();
        lblHealth.dispose();
        lblScore.dispose();
        CleanUp();
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        x = screenX - shipW / 2;
        //      y = Math.abs(screenY);
        return false;
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}