Package com.palepail.TestGame.View

Source Code of com.palepail.TestGame.View.WorldRender

package com.palepail.TestGame.View;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeIn;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeOut;

import java.io.IOException;
import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
import com.badlogic.gdx.utils.Array;
import com.palepail.TestGame.TestGame;
import com.palepail.TestGame.Enemies.Boss;
import com.palepail.TestGame.Enemies.Enemy;
import com.palepail.TestGame.Levels.Level;
import com.palepail.TestGame.Message.Message;
import com.palepail.TestGame.Model.Explosion;
import com.palepail.TestGame.Model.Ship;
import com.palepail.TestGame.Model.Bullet.Bullet;
import com.palepail.TestGame.Model.Items.Item;
import com.palepail.TestGame.Utilities.Configuration;

public class WorldRender {

  World world;
  SpriteBatch batch;
  Ship ship;
  OrthographicCamera cam;

  Level level;
  Stage stage;

  Color color;

  BitmapFont bonzai32;
  BitmapFont bonzai24;

  ParticleEmitter followerParticle;

  ShapeRenderer sr;

  Array<Enemy> enemies;
  Iterator<Enemy> enemyIterator;
  Enemy enemy;

  Array<Bullet> enemyBullets;
  Array<Bullet> bullets;
  Iterator<Bullet> bulletIterator;
  Bullet bullet;

  Array<Explosion> explosions;
  Array<Explosion> secondaryExplosions;
  Iterator<Explosion> explosionIterator;
  Explosion explosion;

  Array<Message> messages;
  Array<Message> infoBoxMessages;
  Iterator<Message> messageIterator;
  Message message;

  Array<TextButton> buttons;
  Iterator<TextButton> buttonIterator;
  TextButton button;

  Array<Item> items;
  Iterator<Item> itemIterator;
  Item item;

  // ==========Animation
  TextureRegion currentFrame;

  Rectangle scissors = new Rectangle();
 

  float width, height;

  public WorldRender(World world) {
    this.world = world;
    this.level = world.level;

    world.setWorldRenderer(this);

    width = Gdx.graphics.getWidth() * Configuration.gameScale / 40;
    height = Gdx.graphics.getHeight() * Configuration.gameScale / 40;

    ship = world.getShip();
    enemies = world.getEnemies();
    enemyBullets = world.getEnemyBullets();
    bullets = world.getBullets();
    explosions = world.getExplosions();
    secondaryExplosions = world.getSecondaryExplosions();
    messages = world.getMessages();
    infoBoxMessages = world.getInfoBoxMessages();
    buttons = world.getButtons();
    items = world.getItems();

    cam = new OrthographicCamera();
    cam.setToOrtho(false, width, height);
    // cam.position.

    // used to change alpha
    color = new Color();

    batch = new SpriteBatch();
    batch.setProjectionMatrix(cam.combined);
    stage = new Stage(width, height, false, batch);

    // ====Fonts
    bonzai32 = Configuration.bonzai32;
    bonzai24 = Configuration.bonzai24;

    // ========

    sr = new ShapeRenderer();

    followerParticle = new ParticleEmitter();

    try {
      followerParticle.load(Gdx.files.internal("data/fx/particle").reader(2024));
    } catch (IOException e) {
      e.printStackTrace();
    }

    stage.addAction(fadeOut(0));

  }

  public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.act(Gdx.graphics.getDeltaTime());
    stage.clear();


    ScissorStack.calculateScissors(cam, batch.getTransformMatrix(), world.getPlayArea(), scissors);
   
    ScissorStack.pushScissors(scissors);

   

    drawBombs();
    drawShip();
    drawEnemies();
    drawEnemyBullets();
    drawBullet();
    drawExplosions();
    drawSecondaryExplosions();
    drawItems();
   
    addMessagesToStage();
   
    stage.draw();
    batch.flush();
    ScissorStack.popScissors();
   
    stage.clear();
   
   
    ScissorStack.calculateScissors(cam, batch.getTransformMatrix(), world.getInfoArea(), scissors);
    ScissorStack.pushScissors(scissors);
    drawTextBox();
    stage.draw();
    ScissorStack.popScissors();
   
    stage.clear();
   
   
    addFrameToStage();
    addButtonsToStage();
    stage.draw();
   
 
    if (world.isGAMEOVER()) {
      Gdx.input.setInputProcessor(stage);
    }

   
    if (TestGame.DEBUG) {
      debug();
    }
   
    if (world.START) {
      triggerFadeIn();
      world.setSTART(false);
    }

  }

  private void addButtonsToStage() {
    buttonIterator = buttons.iterator();
    while (buttonIterator.hasNext()) {
      // Gdx.app.log(TestGame.LOG, "Buttons!!!");
      button = buttonIterator.next();
     
      stage.addActor(button);
    }

  }

  private void addMessagesToStage() {
    messageIterator = messages.iterator();
    while (messageIterator.hasNext()) {
      // Gdx.app.log(TestGame.LOG, "Messages!!!");
      message = messageIterator.next();
   
      stage.addActor(message.label);
    }

  }

  private void drawTextBox() {

    messageIterator = infoBoxMessages.iterator();
    while (messageIterator.hasNext()) {
      // Gdx.app.log(TestGame.LOG, "Messages!!!");
      message = messageIterator.next();
     
      stage.addActor(message.label);
    }
  }

  private void drawBombs() {
   
    Image object = new Image(world.level.getTexture("bomb"));
    object.setPosition(ship.getBomb().x, ship.getBomb().y);
    object.setOrigin(ship.getBomb().getWidth() / 2,ship.getBomb().getHeight() / 2);
    object.setWidth(ship.getBomb().getWidth());
    object.setHeight(ship.getBomb().getHeight());
    object.setScale(1, 1);
    object.setRotation(ship.getBombRotation());
 

   
    stage.addActor(object);

  }

  private void addFrameToStage() {
    Image object = new Image(world.level.getTexture("Frame"));
    object.setPosition(0, 0);
    object.setWidth(width);
    object.setHeight(height);
    stage.addActor(object);

   
  }

  private void drawSecondaryExplosions() {
    explosionIterator = secondaryExplosions.iterator();

    while (explosionIterator.hasNext()) {
      explosion = explosionIterator.next();
     
      Image object = new Image(world.level.getTexture(explosion.getSpriteName()));
      object.setPosition(explosion.getPosition().x, explosion.getPosition().y);
      object.setOrigin(explosion.getWidth() / 2, explosion.getHeight() / 2);
      object.setWidth(explosion.getWidth());
      object.setHeight(explosion.getHeight());
      object.setScale(explosion.getRenderScaleX(), explosion.getRenderScaleY());
      object.setRotation(explosion.getRotation());
      object.setColor(1f, 1f, 1f, explosion.getAlpha());
   
      stage.addActor(object);

    }
 

  }

  private void drawExplosions() {
    explosionIterator = explosions.iterator();

    while (explosionIterator.hasNext()) {
      explosion = explosionIterator.next();

      Image object = new Image(world.level.getTexture(explosion.getSpriteName()));
      object.setPosition(explosion.getPosition().x, explosion.getPosition().y);
      object.setOrigin(explosion.getWidth() / 2, explosion.getHeight() / 2);
      object.setWidth(explosion.getWidth());
      object.setHeight(explosion.getHeight());
      object.setScale(explosion.getRenderScaleX(), explosion.getRenderScaleY());
      object.setRotation(explosion.getRotation());
      object.setColor(1, 1, 1, explosion.getAlpha());
   
      stage.addActor(object);
     
    }
 

  }

  private void drawEnemyBullets() {
    bulletIterator = enemyBullets.iterator();
    while (bulletIterator.hasNext()) {
      bullet = bulletIterator.next();
      currentFrame = world.level.getAnimation(bullet.getSpriteName()).getKeyFrame(bullet.getTime(), true);

      TextureRegion image = new TextureRegion(currentFrame);

      Image object = new Image(image);
      object.setPosition(bullet.getPosition().x - (currentFrame.getRegionWidth() / 2) + bullet.getWidth() / 2f,
          (bullet.getPosition().y - currentFrame.getRegionHeight() / 2) + bullet.getHeight() / 2);
      object.setOrigin(currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2);
      object.setWidth(currentFrame.getRegionWidth());
      object.setHeight(currentFrame.getRegionHeight());
      object.setScale(bullet.getRenderScaleX(), bullet.getRenderScaleY());
      object.setRotation(bullet.getRotation());
     
      stage.addActor(object);

     
    }

  }

  private void drawShip() {

    if (ship.isMOVE_LEFT()) {
      currentFrame = world.level.getAnimation(ship.getSpriteName() + "Move").getKeyFrame(ship.getTime(), true);
      if (currentFrame.isFlipX()) {
        // .flip flips it from its current orientation, no its base
        currentFrame.flip(true, false);
      }
    } else if (ship.isMOVE_RIGHT()) {
      currentFrame = world.level.getAnimation(ship.getSpriteName() + "Move").getKeyFrame(ship.getTime(), true);
      if (!currentFrame.isFlipX()) {
        currentFrame.flip(true, false);
      }

    } else {
      currentFrame = world.level.getAnimation(ship.getSpriteName()).getKeyFrame(ship.getTime(), true);
      currentFrame.flip(false, false);

    }
    TextureRegion image = new TextureRegion(currentFrame);
    Image object = new Image(image);

    object.setPosition(ship.getPosition().x + ship.getWidth() / 3 - currentFrame.getRegionWidth() / 2,
        ship.getPosition().y + ship.getHeight() / 3 - currentFrame.getRegionHeight() / 2);
    object.setOrigin(currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2);
    object.setWidth(currentFrame.getRegionWidth());
    object.setHeight(currentFrame.getRegionHeight());
    object.setScale(ship.getRenderScaleX(), ship.getRenderScaleY());
    object.setRotation(ship.getRotation());
 

    if (ship.isDead()) {
      object.setColor(1, 1, 1, ship.getAlpha());
    }

    stage.addActor(object);

 
    if (ship.isSLOW()) {

      TextureRegion slowSprite = new TextureRegion(world.level.getTexture("shipSlow"));
      Image slow = new Image(slowSprite);

      slow.setPosition(ship.getPosition().x + ship.getWidth() / 2 - slowSprite.getRegionWidth() / 2,
          ship.getPosition().y + ship.getHeight() / 2 - slowSprite.getRegionHeight() / 2);
      slow.setOrigin(slowSprite.getRegionWidth() / 2, slowSprite.getRegionHeight() / 2);
      slow.setWidth(slowSprite.getRegionWidth());
      slow.setHeight(slowSprite.getRegionHeight());
      slow.setScale(ship.getRenderScaleX(), ship.getRenderScaleY());
      slow.setRotation(ship.getSlowEffectRotation());
     
      stage.addActor(slow);
    }

  }

  private void drawItems() {
    itemIterator = items.iterator();
    while (itemIterator.hasNext()) {
      item = itemIterator.next();

      Image object = new Image(world.level.getTexture(item.getTextureName()));
      object.setPosition(item.getPosition().x, item.getPosition().y);
      object.setOrigin(item.getWidth() / 2, item.getHeight() / 2);
      object.setWidth(item.getWidth());
      object.setHeight(item.getHeight());
      object.setScale(item.getRenderScaleX(), item.getRenderScaleY());
      object.setRotation(item.getRotation());
   

      stage.addActor(object);
   

    }

  }

  private void drawEnemies() {
    enemyIterator = enemies.iterator();

    while (enemyIterator.hasNext()) {
      enemy = enemyIterator.next();
      if (enemy instanceof Boss) {
        drawBoss(enemy);

      }else if (enemy.isMOVE_LEFT()) {
        currentFrame = world.level.getAnimation(enemy.getSpriteName() + "Move").getKeyFrame(enemy.getTime(),
            true);

        if (currentFrame.isFlipX()) {
          // .flip flips it from its current orientation, not its base
          currentFrame.flip(true, false);
        }
      } else if (enemy.isMOVE_RIGHT()) {
        currentFrame = world.level.getAnimation(enemy.getSpriteName() + "Move").getKeyFrame(enemy.getTime(),
            true);
        if (!currentFrame.isFlipX()) {
          currentFrame.flip(true, false);
        }

      } else {
        currentFrame = world.level.getAnimation(enemy.getSpriteName()).getKeyFrame(enemy.getTime(), true);
        if (currentFrame.isFlipX()) {
          currentFrame.flip(true, false);
        }
      }
      TextureRegion image = new TextureRegion(currentFrame);

      Image object = new Image(image);
      object.setPosition(enemy.getPosition().x + enemy.getWidth() / 2 - currentFrame.getRegionWidth() / 2,
          enemy.getPosition().y + enemy.getHeight() / 2 - currentFrame.getRegionHeight() / 2);
      object.setOrigin(currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2);
      object.setWidth(currentFrame.getRegionWidth());
      object.setHeight(currentFrame.getRegionHeight());
      object.setScale(enemy.getRenderScaleX(), enemy.getRenderScaleY());
      object.setRotation(enemy.getRotation());
     
      stage.addActor(object);

     
    }

  }
 
  private void drawBoss(Enemy enemy) {

    if (enemy.isMOVE_LEFT()) {
      currentFrame = world.level.getAnimation(enemy.getSpriteName() + "Move").getKeyFrame(enemy.getTime(), true);
      if (currentFrame.isFlipX()) {
        // .flip flips it from its current orientation, no its base
        currentFrame.flip(true, false);
      }
    } else if (enemy.isMOVE_RIGHT()) {
      currentFrame = world.level.getAnimation(enemy.getSpriteName() + "Move").getKeyFrame(enemy.getTime(), true);
      if (!currentFrame.isFlipX()) {
        currentFrame.flip(true, false);
      }

    } else {
      currentFrame = world.level.getAnimation(enemy.getSpriteName() + "Wait").getKeyFrame(enemy.getTime(), true);
      if (currentFrame.isFlipX()) {
        currentFrame.flip(true, false);
      }
    }
    drawBossHealth(enemy);

  }

  private void drawBossHealth(Enemy enemy) {

    Boss boss = (Boss) enemy;

    Image object = new Image(world.level.getTexture(boss.getHBSpriteName()));
    object.setPosition(boss.getHealthBar().getPosition().x, boss.getHealthBar().getPosition().y);
    object.setOrigin(boss.getHealthBar().getHeight() / 2, boss.getHealthBar().getHeight() / 2);
    object.setWidth(boss.getHealthBar().getHeight());
    object.setHeight(boss.getHealthBar().getWidth());
    object.setScale(.5f * Configuration.gameScale / 20, .5f * Configuration.gameScale / 20);
    object.setRotation(boss.getHealthBar().getRotation());

    stage.addActor(object);
 

  }

  private void drawBullet() {

    bulletIterator = bullets.iterator();
    while (bulletIterator.hasNext()) {

      bullet = bulletIterator.next();

      Image object = new Image(world.level.getTexture(bullet.getSpriteName()));
      object.setPosition(bullet.getPosition().x, bullet.getPosition().y);
      object.setOrigin(bullet.getWidth() / 2, bullet.getHeight() / 2);
      object.setWidth(bullet.getWidth());
      object.setHeight(bullet.getHeight());
      object.setScale(bullet.getRenderScaleX(), bullet.getRenderScaleY());
      object.setRotation(bullet.getRotation());
     
      stage.addActor(object);

    }

  }

  private void debug() {
    sr.setProjectionMatrix(cam.combined);
    sr.begin(ShapeType.Rectangle);
    sr.setColor(Color.CYAN);
    sr.rect(ship.getBounds().x, ship.getBounds().y, ship.getBounds().width, ship.getBounds().height);
    sr.setColor(Color.PINK);
    sr.rect(ship.getInfluence().x, ship.getInfluence().y, ship.getInfluence().width, ship.getInfluence().height);

    // play area
    sr.setColor(Color.YELLOW);
    sr.rect(world.getPlayArea().x, world.getPlayArea().y, world.getPlayArea().width, world.getPlayArea().height);

    // info area
    sr.rect(world.getInfoArea().x, world.getInfoArea().y, world.getInfoArea().width, world.getInfoArea().height);

    sr.setColor(Color.YELLOW);
    sr.rect(ship.getBomb().x, ship.getBomb().y, ship.getBomb().width, ship.getBomb().height);

    sr.setColor(Color.BLUE);
    enemyIterator = enemies.iterator();
    while (enemyIterator.hasNext()) {
      enemy = enemyIterator.next();
      sr.rect(enemy.getBounds().x, enemy.getBounds().y, enemy.getBounds().width, enemy.getBounds().height);
      if (enemy instanceof Boss) {
        Boss boss = (Boss) enemy;
        sr.rect(boss.getHealthBar().getPosition().x, boss.getHealthBar().getPosition().y, boss.getHealthBar()
            .getWidth(), boss.getHealthBar().getHeight());
      }

    }
    sr.setColor(Color.YELLOW);
    bulletIterator = bullets.iterator();
    while (bulletIterator.hasNext()) {
      bullet = bulletIterator.next();
      sr.rect(bullet.getBounds().x, bullet.getBounds().y, bullet.getBounds().width, bullet.getBounds().height);
    }
    sr.setColor(Color.ORANGE);
    bulletIterator = enemyBullets.iterator();
    while (bulletIterator.hasNext()) {
      bullet = bulletIterator.next();
      sr.rect(bullet.getBounds().x, bullet.getBounds().y, bullet.getBounds().width, bullet.getBounds().height);
    }

    sr.setColor(Color.GREEN);
    itemIterator = items.iterator();
    while (itemIterator.hasNext()) {
      item = itemIterator.next();
      sr.rect(item.getBounds().x, item.getBounds().y, item.getBounds().width, item.getBounds().height);
    }

    sr.end();

  }

  public OrthographicCamera getCamera() {
    return cam;
  }

  public void dispose() {
    batch.dispose();
    bonzai24.dispose();
    bonzai32.dispose();
    stage.dispose();

    sr.dispose();
  }

  public void triggerFadeOut() {
    stage.addAction(fadeOut(2));
  }
  public void triggerFadeIn() {
    stage.addAction(fadeIn(.5f));
  }
}
TOP

Related Classes of com.palepail.TestGame.View.WorldRender

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.