Android Open Source - Castle-Invaders Game Screen






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.screens;
// w  w w.  j a va2s  .com



import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.noobgygames.castleinvaders.ui.DragonUltiButton;
import com.noobgygames.castleinvaders.ui.ElementSwitcherButton;
import com.noobgygames.castleinvaders.ui.TextureElement;
import com.noobygames.castleinvaders.Assets;
import com.noobygames.castleinvaders.CastleInvaders;
import com.noobygames.castleinvaders.Player;
import com.noobygames.castleinvaders.World;
import com.noobygames.castleinvaders.World.Species;
import com.noobygames.castleinvaders.WorldRenderer;
import com.noobygames.utils.exceptions.OutOfBoundingException;
import com.noobygames.utils.ui.Button;
import com.noobygames.utils.ui.ClickableElement;
import com.noobygames.utils.ui.Table;


/**
 * @author Tobi
 * **/

public class GameScreen implements Screen, InputProcessor {
  final CastleInvaders game;

  static final int GAME_READY = 0;
  static final int GAME_RUNNING = 1;
  static final int GAME_PAUSED = 2;
  static final int GAME_LEVEL_END = 3;
  static final int GAME_OVER = 4;

  
  int state;
  Vector3 touchPoint;
  World world;
  Player player;
  WorldRenderer renderer;
  Rectangle pauseBounds;
  Rectangle resumeBounds;
  String scoreString;
  TextureElement stickToMouse;
  private int id;
  //private final Window window = new Window();
  private final Table table;

  private boolean isHonoured;

  private float switchTimer = 0;

  private boolean start;
  int lanes[] = { 0, 128, 256, 384, 512, 640, 768 };


  public static final Button elementSwitcher = new ElementSwitcherButton(new Rectangle(1000, 5, 80, 80), Assets.eleSwitchEarth);
  public static final Button dragonUlti = new DragonUltiButton(new Rectangle(880, 5, 80, 80), Assets.earthDragonSwitch);
  //ClickableElement greyTrollSelect = new TextureElement(new Rectangle(), Assets.greyTrollSelect);
  Array<TextureElement> selectButtons = new Array<TextureElement>();

  private boolean sticked;

  private int lane;
  
  

  public GameScreen(final CastleInvaders gam) throws OutOfBoundingException {
    this.game = gam;
    sticked = false;
    id = 0; // One unique id for each element
    dragonUlti.setId(id+2);
    elementSwitcher.setId(id+1);
    state = GAME_READY; 
    player = new Player();
    touchPoint = new Vector3();
    world = new World(player.getLevel());
    stickToMouse = new TextureElement(null,null,null);
    touchPoint.z = 0;
    player.updatePrefs(game);
    isHonoured = false;
    this.stickToMouse = null;
    
    table = new Table(new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()), null);
    table.setId(id);
    table.addElement(new TextureElement(new Rectangle(400, 5, 65, 65), Assets.greyTrollSelect, Species.greyTroll));
    table.addElement(new TextureElement(new Rectangle(460, 5, 65, 65), Assets.lavaTrollSelect, Species.lavaTroll));
    table.addElement(new TextureElement(new Rectangle(520, 5, 65, 65), Assets.iceTrollSelect, Species.iceTroll));
    
    renderer = new WorldRenderer(world, game, player, table);
    id+=4;
  }

  @Override
  public void resize(int width, int height) {
    renderer.setSize(width, height);
  }

  @Override
  public void show() {
    player.updatePrefs(game);
    world = new World(player.getLevel());
    renderer = new WorldRenderer(world, game, player, table);
    Gdx.input.setInputProcessor(this);
    Gdx.input.setCatchBackKey(true);
    renderer.camera.update();
    game.batch.setProjectionMatrix(renderer.camera.combined);
    state = GAME_RUNNING;
  }

  @Override
  public void hide() {
    Gdx.input.setInputProcessor(null); // Ressourcen freigeben
  }

  @Override
  public void pause() {
    if (state == GAME_RUNNING)
      state = GAME_PAUSED;
  }

  @Override
  public void resume() {
    if (state == GAME_PAUSED)
      this.state = GAME_RUNNING;
  }

  @Override
  public void dispose() {
    Gdx.input.setInputProcessor(null); // Ressourcen freigeben
  }

  @Override
  public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    switch(World.elementState){
    case World.elementEarth:
      if(dragonUlti.isClicked())
        dragonUlti.setTexRegion(Assets.earthDragonSwitchClicked);
      else
        dragonUlti.setTexRegion(Assets.earthDragonSwitch);
      break;
    case World.elementLava:
      if(dragonUlti.isClicked())
        dragonUlti.setTexRegion(Assets.fireDragonSwitchClicked);
      else
        dragonUlti.setTexRegion(Assets.fireDragonSwitch);
      break;
    case World.elementIce:
      if(dragonUlti.isClicked())
        dragonUlti.setTexRegion(Assets.iceDragonSwitchClicked);
      else
        dragonUlti.setTexRegion(Assets.iceDragonSwitch);
      break;
    }
    world.updateWorld(delta);
    renderer.render();
    if(sticked)
      renderer.drawTex(stickToMouse,touchPoint);
    setStart(true);

    switch (state) {
    case GAME_READY:
      presentReady();
      break;
    case GAME_RUNNING:
      presentRunning();
      break;
    case GAME_PAUSED:
      presentPaused(Gdx.input.getX(), Gdx.input.getY());
      break;
    case GAME_LEVEL_END:
      presentLevelEnd();
      break;
    case GAME_OVER:
      presentGameOver();
      break;
    }
  }

  private void presentGameOver() {
    world.cleanUp();
    if (Gdx.input.isTouched()) {
      game.setScreen(new ScoreScreen(game, player, false,
          World.earthDragonXP, World.fireDragonXP, World.iceDragonXP));
      this.dispose();
    }
    this.dispose();
  }

  private void presentLevelEnd() {
    if (!isHonoured) {
      player.setTotalEnemiesKilled(world.getEnemyCounter());
      player.addCoins(player.getLevel() * 100);

      if (player.getLevel() % 3 == 0)
        player.addGems(1);

      player.addLevel();

      player.setEarthDragonXP(player.getEarthDragonXP()
          + World.earthDragonXP);
      player.setFireDragonXP(player.getFireDragonXP()
          + World.fireDragonXP);
      player.setIceDragonXP(player.getIceDragonXP() + World.iceDragonXP);

      game.prefs.putInteger("totalEnemiesKilled",
          player.getTotalEnemiesKilled());
      game.prefs.putInteger("coins", player.getCoins());
      game.prefs.putInteger("gems", player.getGems());
      game.prefs.putInteger("level", player.getLevel());

      game.prefs.putInteger("earthDragonXP", player.getEarthDragonXP());
      game.prefs.putInteger("iceDragonXP", player.getIceDragonXP());
      game.prefs.putInteger("fireDragonXP", player.getFireDragonXP());
      game.prefs.flush();

      isHonoured = true;
    }

    World.win = true;
    world.cleanUp();

    switchTimer+=Gdx.graphics.getDeltaTime();
    if (switchTimer >= 1.5) {
      game.setScreen(new ScoreScreen(game, player, true,
          World.earthDragonXP, World.fireDragonXP, World.iceDragonXP));
      this.dispose();
    }
  }

  private void presentPaused(float x, float y) {
  }

  private void presentRunning() {
    if (World.hp <= 0) {
      this.state = GAME_OVER;
      World.win = false;
      World.levelEnd = true;
    }
    if (world.checkWin()) {
      this.state = GAME_LEVEL_END;
      World.win = true;
      World.levelEnd = true;
    }

    if (World.hp <= 0) {
      World.levelEnd = true;
      this.state = GAME_OVER;
    }

  }

  private void presentReady() {
  }

  @Override
  public boolean keyDown(int keycode) {

    return false;
  }

  @Override
  public boolean keyUp(int keycode) {
    if (keycode == Keys.BACK) {
      this.pause();
      return true;
    }
    return false;
  }

  @Override
  public boolean keyTyped(char character) {

    return false;
  }

  @Override
  public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touchPoint.x = screenX;
    touchPoint.y = screenY;

    
    return true;
  }

  @Override
  public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    touchPoint.x = screenX;
    touchPoint.y = screenY;

    renderer.camera.unproject(touchPoint);
    //Gdx.app.log("Info", ""+touchPoint.y+" screenY:"+screenY);
    for(int i=0; i<=lanes.length-2;i++){
      if(touchPoint.y >= lanes[i] && touchPoint.y <= lanes[i+1])
        lane = i;
    }
    if(sticked)
      world.spawnMob(screenX, lanes[lane], stickToMouse.getSpecies(), player.getMobLevelForSpecies(stickToMouse.getSpecies()), lane);
    sticked = false;
    if(table.removeElement(stickToMouse))
      id--;
    return true;
  }

  

  @Override
  public boolean touchDragged(int screenX, int screenY, int pointer) {
    touchPoint.x = screenX;
    touchPoint.y = screenY;
  //  Gdx.app.log("INFO", "screenX: "+screenX+" screenY: "+screenY);
    renderer.camera.unproject(touchPoint);
    if(table.checkElementTouched(touchPoint) && !sticked)
      for(ClickableElement obj: table.getElements()){
      if(obj.isClicked()){
        this.stickToMouse = new TextureElement(new Rectangle(touchPoint.x, touchPoint.y, 65, 65),obj.getTex(), ((TextureElement) obj).getSpecies());
        this.stickToMouse.setId(id);
        id++;
        if(table.getElementforID(stickToMouse.getId()) == null)
      //    table.addElement(stickToMouse);
        sticked = true;
        obj.setClicked(false);
      }
    }
  //  Gdx.app.log("INFO", "screenX: "+screenX+" screenY: "+screenY+" sticked?: "+sticked);
    return true;
  }

  @Override
  public boolean mouseMoved(int screenX, int screenY) {

    return false;
  }

  @Override
  public boolean scrolled(int amount) {

    return false;
  }

  /**
   * @return the start
   */
  public boolean isStart() {
    return start;
  }

  /**
   * @param start the start to set
   */
  public void setStart(boolean start) {
    this.start = start;
  }

}




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