Android Open Source - Castle-Invaders World Renderer






From Project

Back to project page Castle-Invaders.

License

The source code is released under:

GNU General Public License

If you think the Android project Castle-Invaders listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.noobygames.castleinvaders;
//www.j a  v a 2s .c o m
import com.badlogic.gdx.Gdx;
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.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.noobgygames.castleinvaders.ui.TextureElement;
import com.noobygames.castleinvaders.World.Species;
import com.noobygames.castleinvaders.World.States;
import com.noobygames.nerzal.castleinvaders.spells.Spells;
import com.noobygames.utils.ui.Table;

/** @author Tobi **/
public class WorldRenderer {

  static final float FRUSTUM_WIDTH = 1280;
  static final float FRUSTUM_HEIGTH = 768;
  final CastleInvaders game;
  public WorldRenderer renderer;
  World world;
  private TextureRegion keyFrame;
  States state;
  Species species;
  BitmapFont font;
  public OrthographicCamera camera;
  private Player player;
  private Table table;

  public WorldRenderer(World world, final CastleInvaders gam, Player player,
      Table table) {
    this.world = world;
    game = gam;
    Assets.load(game);
    font = new BitmapFont();
    font.setScale(3);
    this.player = player;
    this.table = table;
    camera = new OrthographicCamera();
    camera.setToOrtho(false, FRUSTUM_WIDTH, FRUSTUM_HEIGTH);
    camera.update();
    game.batch.setProjectionMatrix(camera.combined);

  }

  public void render() {
    Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
    renderBackground();
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
    game.batch.begin();

    renderMobs(World.mobs);
    renderMobs(World.enemyEntities);
    renderSpells(World.spells);
    renderProjectiles(World.projectiles);
    // renderGui();
    renderTable();
    if (World.win && World.levelEnd)
      drawWin();
    else if (!World.win && World.levelEnd)
      drawWin();
    // drawGolemSelection(x,y);
    font.draw(game.batch, "Level:" + player.getLevel(), 70, 695);
    font.draw(game.batch, "Mana: " + Math.round(world.mana) + "/"
        + (int) world.maxMana, 70, 730);
    font.draw(game.batch, "HP: " + World.hp + "/100", 70, 765);
    game.batch.end();

  }

  private void renderTable() {
    table.draw(game.batch);
  }

  private void renderProjectiles(Array<Projectile> projectiles) {
    for (Projectile obj : projectiles) {
      if (!obj.isHitTarget()) {
        switch (obj.species) {
        default:
          if (!obj.isHitTarget())
            game.batch.draw(Assets.arrow, obj.getCenterX(),
                obj.position.y, 30, 60);
          break;
        }
      }
    }

  }

  private void renderSpells(Array<Spells> spells) {
    for (Spells i : spells) {
      try {
        switch (i.getSpell()) {
        case fireBolt:
          if (!i.isHitTarget()) {
            keyFrame = Assets.fireBolt.getKeyFrame(
                i.getStateTime(), true);
            game.batch.draw(keyFrame, i.position.x, i.position.y,
                25, 25);

            keyFrame = Assets.sparklets.getKeyFrame(
                i.getStateTime(), true);
            game.batch.draw(keyFrame, i.getCenterX(), i.position.y,
                30, 30);
            break;
          } else {
            keyFrame = Assets.fireBolt.getKeyFrame(
                i.getStateTime(), true);
            if (i.getTarget() != null)
              game.batch.draw(keyFrame, i.getTarget()
                  .getCenterX(), i.position.y, 35, 35);
            break;
          }
        case grumblingIce:
          if (!i.isHitTarget()) {
            keyFrame = Assets.grumblingIce.getKeyFrame(
                i.getStateTime(), true);
            game.batch.draw(keyFrame, i.position.x,
                i.position.y + 10, 100, 90);
            break;
          } else {
            keyFrame = Assets.iceSpike;
            if (i.getTarget() != null)
              game.batch.draw(keyFrame, i.getTarget().position.x,
                  i.getTarget().position.y, 60, 130);
          }
          break;
        }

      } catch (IndexOutOfBoundsException e) {
        System.out
            .println("Indezes for Spells ran out of exceptions!! Oh noes!");
      }
    }
  }

  /**
   * This function draws every element depending on the species of the 2 lists
   * onto the framebuffer
   * 
   * @param mobs
   *            List of DynamicGameObjects (mobs) to render
   * **/
  private void renderMobs(Array<GameLiving> mobs) {
    for (GameLiving mob : mobs) {
      if (mob.getAnimation() == null)
        Gdx.app.log("Null", mob.toString());
      game.batch.draw(mob.getAnimation(), mob.position.x, mob.position.y,  mob.width, mob.height);
    }
  }

  private void renderBackground() {
    game.batch.begin();
    game.batch.draw(Assets.background, 0, 0);
    game.batch.end();

  }

  public void setSize(int width, int height) {
    // TODO Auto-generated method stub

  }

  public void drawGolemSelection(int x, int y) {
    Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
    game.batch.begin();
    game.batch.draw(Assets.golemSelectionRegion, x, y);
    game.batch.end();

  }

  public void drawWin() {
    Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
    game.batch.draw(Assets.window, 60, 60, 1160, 648);
    font.draw(game.batch, "Tap to continue", 370, 250);
  }

  public void drawTex(TextureElement stickToMouse, Vector3 touchPoint) {
    game.batch.begin();
    game.batch.draw(stickToMouse.getTex(), touchPoint.x, touchPoint.y, 65,
        65);
    game.batch.end();

  }

}




Java Source Code List

com.noobgygames.castleinvaders.ui.DragonUltiButton.java
com.noobgygames.castleinvaders.ui.ElementSwitcherButton.java
com.noobgygames.castleinvaders.ui.StoreElement.java
com.noobgygames.castleinvaders.ui.TextureElement.java
com.noobygames.castleinvaders.Assets.java
com.noobygames.castleinvaders.CastleInvaders.java
com.noobygames.castleinvaders.DynamicGameObject.java
com.noobygames.castleinvaders.GameLiving.java
com.noobygames.castleinvaders.GameObject.java
com.noobygames.castleinvaders.MainActivity.java
com.noobygames.castleinvaders.Main.java
com.noobygames.castleinvaders.Player.java
com.noobygames.castleinvaders.Projectile.java
com.noobygames.castleinvaders.Settings.java
com.noobygames.castleinvaders.WorldRenderer.java
com.noobygames.castleinvaders.World.java
com.noobygames.castleinvaders.mobs.Croco.java
com.noobygames.castleinvaders.mobs.EarthDragon.java
com.noobygames.castleinvaders.mobs.FireDragon.java
com.noobygames.castleinvaders.mobs.FireTroll.java
com.noobygames.castleinvaders.mobs.GameScreen.java
com.noobygames.castleinvaders.mobs.GreyTroll.java
com.noobygames.castleinvaders.mobs.IceDragon.java
com.noobygames.castleinvaders.mobs.IceTroll.java
com.noobygames.castleinvaders.mobs.Murloc.java
com.noobygames.castleinvaders.mobs.Orc.java
com.noobygames.castleinvaders.mobs.Skeleton.java
com.noobygames.castleinvaders.screens.GameScreen.java
com.noobygames.castleinvaders.screens.MainMenuScreen.java
com.noobygames.castleinvaders.screens.ScoreScreen.java
com.noobygames.castleinvaders.screens.SplashScreen.java
com.noobygames.castleinvaders.screens.StoreScreen.java
com.noobygames.castleinvaders.store.StoreObject.java
com.noobygames.castleinvaders.store.Store.java
com.noobygames.nerzal.castleinvaders.spells.Burning.java
com.noobygames.nerzal.castleinvaders.spells.Freeze.java
com.noobygames.nerzal.castleinvaders.spells.SpellEffect.java
com.noobygames.nerzal.castleinvaders.spells.Spells.java
com.noobygames.utils.ArrayListUtils.java
com.noobygames.utils.ObjectSelectionContainer.java
com.noobygames.utils.OverlapTester.java
com.noobygames.utils.exceptions.OutOfBoundingException.java
com.noobygames.utils.exceptions.SliderOutOfBoundingsException.java
com.noobygames.utils.ui.Button.java
com.noobygames.utils.ui.ClickableElement.java
com.noobygames.utils.ui.DropDownMenu.java
com.noobygames.utils.ui.Element.java
com.noobygames.utils.ui.RadioButton.java
com.noobygames.utils.ui.RadioGroupButton.java
com.noobygames.utils.ui.ScrollableElement.java
com.noobygames.utils.ui.SimpleElement.java
com.noobygames.utils.ui.Slider.java
com.noobygames.utils.ui.Table.java
com.noobygames.utils.ui.TextBox.java
com.noobygames.utils.ui.Text.java
com.noobygames.utils.ui.Window.java