Android Open Source - Castle-Invaders Store






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.store;
/*w w  w. j  a v a 2  s.c  om*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.noobygames.castleinvaders.Player;

/** @author Nerzal **/
public class Store {
  private Array<StoreObject> items;
  protected Player player;
  private Json json = new Json();
  private File file;
  private BufferedWriter out;
  private BufferedReader in;

  public Store(Player player) {
    this.player = player;

    items = new Array<StoreObject>();
    items.add(new StoreObject(10, 1, 15, true, false, true, "kurzer test",
        "Ich bin der erste Json test (:"));
    items.add(new StoreObject(5, 2, 15, true, false, true,
        "l?ngerer Test undso!", "Das ist ein langer Test! :)"));
    
    file = new File("src/storeObjects.txt");
    saveToFile(this.file);
    //loadFromFile();

  }

  /**Writes all StoreObjects into the given File **/
  private boolean saveToFile(File file) {
    try {
      if (!file.exists())
        file.createNewFile();
      out = new BufferedWriter(new FileWriter(file));
      for (StoreObject obj : items) {
        out.write(json.prettyPrint(obj));
        //  out.write(json.toJson(obj));
          //out.newLine();
      }
      out.flush();
      return true;

    } catch (IOException e) {

      e.printStackTrace();
      return false;
    }
  }

  /**Loads all JSon Objects out of a File and adds it onto items
   * @return true If successfully loaded else false**/
  private boolean loadFromFile() {
    String s;
    try{
      in = new BufferedReader(new FileReader(file));
      
      while((s = in.readLine()) != null){
        json.fromJson(StoreObject.class, s); 
        System.out.println(s);
      }
      
      
      //
    }catch (IOException e){
      e.printStackTrace();
      return false;
    }
    
    return false;

  }

  public boolean buyObject(StoreObject obj){
    if(obj.isCostCoins())
      return buyObjectCoins(obj);
    else 
      return buyObjectGems(obj);
  }
  
  private boolean buyObjectCoins(StoreObject obj) {
    if (player.getCoins() >= obj.getPrice()
        && obj.getObjectLevel() < obj.getObjectMaxLevel()
        && obj.isAvailable() && !obj.isOwned()) {
      obj.setObjectLevel(obj.getObjectLevel() + 1);
      player.addCoins(-obj.getPrice());
      return true;

    }
    return false;
  }

  private boolean buyObjectGems(StoreObject obj) {
    if (player.getGems() >= obj.getPrice()
        && obj.getObjectLevel() < obj.getObjectMaxLevel()
        && obj.isAvailable() && !obj.isOwned()) {
      obj.setObjectLevel(obj.getObjectLevel() + 1);
      player.addGems(-obj.getPrice());
      return true;
    }
    return false;
  }

  // TODO Alle storeItems adden - wird nun per JavaIO gemacht - danke JSON !
  public void addStoreItem() {
    //items.add(new StoreObject(1, 0, 0, false, false, null, null));
  }
  
  public Array<StoreObject> getItems(){
    return items;
  }

}




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