com.octa.topdown.TopDown.java Source code

Java tutorial

Introduction

Here is the source code for com.octa.topdown.TopDown.java

Source

// Copyright 2015 Octavio Galland
// 
// This file is part of TopDown
//
// TopDown 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.
//
// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

package com.octa.topdown;

import java.util.ArrayList;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.octa.topdown.armory.Bullet;
import com.octa.topdown.entities.Player;
import com.octa.topdown.entities.Zombie;
import com.octa.topdown.entities.ZombieManager;
import com.octa.topdown.items.Box;
import com.octa.topdown.items.ItemSpawner;
import com.octa.topdown.map.TileMap;
import com.octa.topdown.system.InputTracker;
import com.octa.topdown.system.ResourceHandler;
import com.octa.topdown.system.SinCosTable;

public class TopDown extends ApplicationAdapter {
    SpriteBatch batch;
    OrthographicCamera cam;
    InputTracker input;
    BitmapFont font;

    Player player;
    ArrayList<Bullet> bullets;

    TileMap map;

    FPSLogger log;
    ZombieManager manager;

    ItemSpawner is;

    int wave;

    @Override
    public void create() {
        batch = new SpriteBatch();
        font = new BitmapFont();
        SinCosTable.init();
        ResourceHandler.initResourceHandler();

        cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.setToOrtho(false);

        Zombie.animId = ResourceHandler.createAnimation("Zombie.png", 8, 0, 0, 60, 60);
        Bullet.regId = ResourceHandler.createRegion("atlas.png", 149, 25, 2, 10);
        player = new Player(ResourceHandler.createAnimation("Soldier_atlas.png", 8, 0, 0, 60, 60), 62, -58, 60, 60);

        input = new InputTracker();

        int[][] mapSpec = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 },
                { 1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1 }, { 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1 },
                { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, { 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1 },
                { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, { 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1 },
                { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, { 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1 },
                { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, { 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1 },
                { 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };

        map = new TileMap(mapSpec);

        Gdx.input.setInputProcessor(input);

        manager = new ZombieManager(map);

        wave = 1;

        is = new ItemSpawner();

        log = new FPSLogger();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        float delta = Gdx.graphics.getDeltaTime();

        cam.position.x = player.getX() + (player.getWidth() / 2);
        cam.position.y = player.getY() + (player.getHeight() / 2);
        cam.update();

        if (player.alive()) {
            if (manager.getCount() < wave * 5) {
                manager.spawn();
            }
        }

        if (manager.isEmpty()) {
            wave++;
            manager.resetCount();
            manager.forceSpawn();
        }

        //********************
        // Movement
        //********************
        for (int j = 0; j < manager.zombies.size(); j++) {
            if (manager.zombies.get(j).alive()) {
                if (player.alive())
                    manager.zombies.get(j).setObjective(player.getAABB().x, player.getAABB().y);
                else
                    manager.zombies.get(j).setObjective(null);
                manager.zombies.get(j).update(delta);
            } else {
                is.spawn(manager.zombies.get(j).getX(), manager.zombies.get(j).getY());
                manager.remove(manager.zombies.get(j));
                player.addScore(10);
            }
        }

        if (player.alive()) {
            player.update(delta);
        }
        bullets = player.getWeapon().getBullets();
        player.getWeapon().update(manager.zombies, delta);
        //*******************
        // Collision
        //*******************

        for (int j = 0; j < is.items.size(); j++) {
            if (is.items.get(j).isOverlaping(player.getAABB())) {
                is.items.get(j).pickUp(player);
                is.delete(is.items.get(j));
            }
        }

        for (int j = 0; j < manager.zombies.size(); j++) {
            if (manager.zombies.get(j).alive()) {
                map.collideBounds(manager.zombies.get(j));
            }
        }

        for (int i = 0; i < bullets.size(); i++) {
            if (bullets.get(i).collideBounds(map)) {
                bullets.remove(i);
            }
        }

        for (int j = 0; j < manager.zombies.size(); j++) {
            if (manager.zombies.get(j).alive()) {
                if (manager.zombies.get(j).isOverlaping(player.getAABB()) && player.alive()) {
                    manager.zombies.get(j).forceStop();
                    player.damage(1, -manager.zombies.get(j).getSin(), manager.zombies.get(j).getCos());
                } else {
                    manager.zombies.get(j).forceStart();
                }

                for (int i = 0; i < bullets.size(); i++) {
                    if (bullets.get(i).collideBounds(manager.zombies.get(j).getAABB()))
                        manager.zombies.get(j).damage(bullets.get(i).getArmShooted().getDamage(),
                                bullets.get(i).getArmShooted().getKnockBack());
                }
            }
        }

        map.collideBounds(player);
        //*******************
        // Drawing
        //*******************
        batch.setProjectionMatrix(cam.combined);
        batch.begin();

        map.draw(batch);

        for (Box b : is.items) {
            b.draw(batch);
        }

        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).draw(batch);
        }

        if (player.isDamaged())
            batch.setColor(1.0f, 0.0f, 0.0f, 1.0f);
        if (player.alive())
            player.draw(batch);
        batch.setColor(1.0f, 1.0f, 1.0f, 1.0f);

        for (int j = 0; j < manager.zombies.size(); j++) {
            if (manager.zombies.get(j).alive()) {
                manager.zombies.get(j).draw(batch);
            }
        }

        font.draw(batch, "Wave: " + wave, 330, 120);

        font.draw(batch, "Player:", cam.position.x - 300, cam.position.y + 230);
        font.draw(batch, "Life " + player.getHp(), cam.position.x - 300, cam.position.y + 200);
        font.draw(batch, "Ammo " + player.getAmmo(), cam.position.x - 300, cam.position.y + 170);
        font.draw(batch, "Score " + player.getScore(), cam.position.x - 300, cam.position.y + 140);

        font.draw(batch, "Weapon:", cam.position.x - 300, cam.position.y - 140);
        font.draw(batch, "Damage: " + player.getWeapon().getDamage(), cam.position.x - 300, cam.position.y - 170);
        font.draw(batch, "Rate: " + player.getWeapon().getFireRate(), cam.position.x - 300, cam.position.y - 200);
        batch.end();

        InputTracker.updateState();
        //      log.log();
    }
}