Android Open Source - Castle-Invaders World






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;
//  w  w  w  .  jav  a  2 s  .  c  o m
import java.util.Random;

import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;
import com.noobygames.castleinvaders.mobs.Croco;
import com.noobygames.castleinvaders.mobs.FireDragon;
import com.noobygames.castleinvaders.mobs.FireTroll;
import com.noobygames.castleinvaders.mobs.IceDragon;
import com.noobygames.castleinvaders.mobs.IceTroll;
import com.noobygames.castleinvaders.mobs.Murloc;
import com.noobygames.castleinvaders.mobs.Orc;
import com.noobygames.castleinvaders.mobs.EarthDragon;
import com.noobygames.castleinvaders.mobs.Skeleton;
import com.noobygames.castleinvaders.mobs.GreyTroll;
import com.noobygames.nerzal.castleinvaders.spells.Spells;

/**
 * Updates&Spawns all Objects in the World
 * 
 * @author Noobygames T.Theel
 * **/
public class World {
  public static final int elementEarth = 1;
  public static final int elementLava = 2;
  public static final int elementIce = 3;
  public static int elementState = World.elementEarth;
  public FPSLogger fps;
  public float maxMana = 2000;
  public float mana = maxMana;
  public static int hp = 100;
  Timer timer;
  Random random;

  int orccount;
  public static boolean golemSelect = false;
  private int maxEnemies;
  private int level = 1;
  private int enemyCounter;
  private float spawnTimer;

  public static boolean win;
  public static boolean levelEnd;
  private int wave;

  public static Array<GameLiving> enemyEntities;
  public static Array<GameLiving> mobs;
  public static Array<Spells> spells;
  public static Array<Projectile> projectiles;
  


  public static int earthDragonXP = 0;
  public static int iceDragonXP = 0;
  public static int fireDragonXP = 0;

  // private boolean debug;
  public enum Species {
    greyTroll, lavaTroll, iceTroll, orc, fireBolt, grumblingIce, spell, skeleton, earthDragon, iceDragon, fireDragon, croco, murloc, arrow
  };

  public enum States {
    attack, walk, kwalk, die, run, hit, rangeAttack, look, sleep, read
  };


  /**
   * Returns the count of all enemies spawned
   * 
   * @return Number of enemies
   **/
  public int getEnemyCounter() {
    return enemyCounter;
  }

  /**
   * Adds the given amount to the earthdragons xp
   * 
   * @param xp
   *            int Amount of experience points to add
   **/
  public static void addEarthDragonXP(int xp) {
    earthDragonXP += xp;
    /**
     * Adds the given amount to the icedragons xp
     * 
     * @param xp
     *            int Amount of experience points to add
     **/
  }

  public static void addIceDragonXP(int xp) {
    iceDragonXP += xp;
  }

  /**
   * Adds the given amount to the firedragons xp
   * 
   * @param xp
   *            int Amount of experience points to add
   **/
  public static void addFireDragonXP(int xp) {
    fireDragonXP += xp;
  }

  /**
   * Initialises the World -> Creates a new Array for Trolls (own Mobs) and
   * another list for enemy mobs, creates a timer, fps counter and random()
   * **/
  public World(int level) {
    mobs = new Array<GameLiving>();
    enemyEntities = new Array<GameLiving>();
    spells = new Array<Spells>();
    projectiles = new Array<Projectile>();

    this.timer = new Timer();
    this.fps = new FPSLogger();
    this.random = new Random();
    this.level = level;

    enemyCounter = 0;
    maxEnemies = level * 6;
    spawnTimer = 2;
    wave = 1;
    World.levelEnd = false;
    // this.debug = false;
  }

  /**
   * Spawns waves of mobs till maxEnemies is reached
   * 
   * @param delta
   *            Actual deltaTime like Gdx.graphics.getDeltaTime();
   * **/

  private void generateLevel(float delta) {
    int lanes[] = { 0, 128, 256, 384, 512, 640 };
    if (enemyCounter < maxEnemies) {
      for (int i = 0; i < lanes.length; i++) {
        if (wave % 2 != 0 && wave % 3 != 0)
          spawnMob(1280, lanes[i], Species.orc, level, i);
        else if (wave % 2 == 0)
          spawnMob(1200, lanes[i], Species.skeleton, level, i);
        else if (wave % 3 == 0)
          spawnMob(1200, lanes[i], Species.croco, level, i);
        else
          spawnMob(1200, lanes[i], Species.murloc, level, i);

        enemyCounter++;
      }
      wave++;
      spawnTimer += 10;
    } else
      World.levelEnd = true;
  }

  private void generateTestLevel(float delta) {
    if (enemyEntities.size == 0 && enemyCounter <= 2)
      spawnMob(1200, 384, Species.skeleton, level, 3);
  }

  /**
   * Calls : generateLevel, updateMobs and regenMana
   * @param delta
   *            Actual delta time (The time between last and actual frame)
   * **/
  public void updateWorld(float delta) {
    if (spawnTimer <= 0 && enemyCounter < maxEnemies)
      generateLevel(delta);
    // generateTestLevel(delta);
    else
      spawnTimer -= delta;

  
    updateMobs(delta);
    updateSpells(delta);
    updateProjectiles(delta);
    regenMana(delta);
  }

  /**
   * This function spawns specific mobs depending on the Species
   * 
   * @param f
   *            x-coord to spawn
   * @param g
   *            y-coord to spawn
   * @param species
   *            species of the object to spawn
   * @param mobLevel
   *            specific level of the mob to spawn
   * 
   * **/
  public void spawnMob(float f, float g, Species species, int mobLevel,
      int lane) {
    switch (species) {
    case greyTroll:
      if (mana >= 20) {
        mobs.add(new GreyTroll(0, g, species, mobLevel, lane));
        mana -= 20;
      }
      break;
    case lavaTroll:
      if (mana >= 30) {
        mobs.add(new FireTroll(0, g, species, mobLevel, lane));
        mana -= 30;
      }
      break;
    case iceTroll:
      if (mana >= 30) {
        mobs.add(new IceTroll(0, g, species, mobLevel, lane));
        mana -= 30;
      }
      break;

    case earthDragon:
      if (mana >= 100) {
        mobs.add(new EarthDragon(0, g, species, mobLevel, lane));
        mana -= 100;
      }
      break;

    case iceDragon:
      if (mana >= 100) {
        mobs.add(new IceDragon(0, g, species, mobLevel, lane));
        mana -= 100;
      }
      break;

    case fireDragon:
      if (mana >= 100) {
        mobs.add(new FireDragon(0, g, species, mobLevel, lane));
        mana -= 100;
      }
      break;

    case orc:
      enemyEntities.add(new Orc(f, g, species, mobLevel, lane));
      break;

    case skeleton:
      enemyEntities.add(new Skeleton(f, g, species, mobLevel, lane));
      break;

    case croco:
      enemyEntities.add(new Croco(f, g, species, mobLevel, lane));
      break;

    case murloc:
      enemyEntities.add(new Murloc(f, g, species, mobLevel, lane));
      break;
    }
  }

  /**
   * Calls mob.update() for every element in the Array. Sets swingTimer = 0 if
   * its <= 0 and substracts delta from swingTimer, if its >= 0
   * 
   * @param delta
   *            Actual delta time
   * **/
  private void updateMobs(float delta) {
    for (GameLiving mob : mobs) {
      mob.update(delta);
      mob.updateTimers(delta);
      
      mob.target = getFirstMobOnLane(enemyEntities, mob.lane);
      
      if (mob.target != null)
        mob.attackTarget();    
    }

    for (GameLiving mob : enemyEntities) {
      mob.updateTimers(delta);
      mob.update(delta);

      mob.target = getFirstMobOnLane(mobs, mob.lane);
      if (mob.target != null)
        mob.attackTarget();    
    }
  }

  /**
   * Adds 2x delta to the players mana
   * 
   * @param delta
   *            time since last frame
   * **/
  private void regenMana(float delta) {
    if (mana < maxMana)
      mana += delta * 2.5f;
  }

  /**
   * calls the update method for each spell
   * 
   * @param delta
   *            Actual delta Time
   **/
  private void updateSpells(float delta) {
    for (Spells spell : spells) {
      if (spell.getTarget() == null || spell.isHitTarget()
          && spell.getHitTimer() >= 0.2f)
        spells.removeValue(spell, true);
      else
        spell.update(delta);
    }
  }

  /**
   * calls the update method for each projectile
   * 
   * @param delta
   *            Actual delta Time
   **/
  private void updateProjectiles(float delta) {
    for (Projectile projectile : projectiles) {
      if (projectile.target == null || projectile.isHitTarget())
        projectiles.removeValue(projectile, true);
      else
        projectile.update(delta);
    }
  }

  /**
   * checks if the player won the stage
   * 
   * @return true if player won
   **/
  public boolean checkWin() {
    if (enemyCounter >= maxEnemies && enemyEntities.size == 0)
      return true;
    else
      return false;
  }

  /**
   * Returns the mob standing on the first position on its line @ return First
   * mob, else null
   **/
  public GameLiving getFirstMobOnLane(Array<GameLiving> list, int lane) {
    GameLiving tmp = null;
    for (GameLiving mob : list) {
      if (mob.lane != lane)
        continue;
      else if (tmp == null)
        tmp = mob;
      else if (mob.velocity.x < 0) {
        if (mob.position.x < tmp.position.x)
          tmp = mob;
      } else if (mob.position.x > tmp.position.x)
        tmp = mob;
    }
    return tmp;
  }

  /** Cleansup **/
  public void cleanUp() {
    enemyEntities.clear();
    mobs.clear();
    spells.clear();
    projectiles.clear();
    enemyCounter = 0;
    wave = 1;
    hp = 100;
    fireDragonXP = 0;
    earthDragonXP = 0;
    iceDragonXP = 0;
    mana = maxMana;
  }

}




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