Android Open Source - ssniper-andengine Level Loader






From Project

Back to project page ssniper-andengine.

License

The source code is released under:

Apache License

If you think the Android project ssniper-andengine 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.cladophora.ssniper;
/* w ww.j  a va 2 s  . c  o m*/
import android.content.Context;
import com.cladophora.ssniper.scene.GameScene;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by jmar on 1/12/14.
 *
 * Reads json level configuration file and returns level metadata
 */
public class LevelLoader {

    private String readJSON(Context c) {
        try {
            InputStream is = c.getAssets().open("level/arcadeLevels.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            final String jsonContents = new String(buffer);
            return jsonContents;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private JSONArray getLevelArray(Context c) {

        final String jsonContents = readJSON(c);
        if (jsonContents == null) {
            return null;
        }
        try {
            final JSONObject json = new JSONObject(jsonContents);
            return json.getJSONArray("arcadeLevels");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject getLevel(final Context c, final int levelID) {
        try {
            JSONArray levelArray = getLevelArray(c);
            if (GameScene.FINAL_LEVEL < 0) {
                GameScene.FINAL_LEVEL = levelArray.length();
            }
            return getLevelArray(c).getJSONObject(levelID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public float getMinWait(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return Float.valueOf(level.getString("minWait"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public float getMaxWait(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return Float.valueOf(level.getString("maxWait"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public float getKillBonus(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return Float.valueOf(level.getString("killBonus"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public float getHeadShotBonus(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return Float.valueOf(level.getString("headShotBonus"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int getAmmo(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return level.getInt("ammo");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int getEnemyCount(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return level.getInt("enemyCount");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public float getInitialTime(final JSONObject level) {
        if (level == null) {
            return 0;
        }
        try {
            return Float.valueOf(level.getString("initialTime"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

}




Java Source Code List

com.cladophora.ssniper.BaseActivity.java
com.cladophora.ssniper.DeviceUtil.java
com.cladophora.ssniper.EnemyPool.java
com.cladophora.ssniper.GameEffects.java
com.cladophora.ssniper.GameEvents.java
com.cladophora.ssniper.GameLoopUpdateHandler.java
com.cladophora.ssniper.GameUtil.java
com.cladophora.ssniper.HUDManager.java
com.cladophora.ssniper.HoverListener.java
com.cladophora.ssniper.ItemPool.java
com.cladophora.ssniper.LevelLoader.java
com.cladophora.ssniper.PlantPool.java
com.cladophora.ssniper.SPenHoverView.java
com.cladophora.ssniper.ScopeEngine.java
com.cladophora.ssniper.SoundManager.java
com.cladophora.ssniper.SpriteManager.java
com.cladophora.ssniper.andengine.IRandomTimerCallBack.java
com.cladophora.ssniper.andengine.RandomTimerHandler.java
com.cladophora.ssniper.entity.EnemyLayer.java
com.cladophora.ssniper.entity.Enemy.java
com.cladophora.ssniper.entity.ItemLayer.java
com.cladophora.ssniper.entity.Item.java
com.cladophora.ssniper.entity.PlantLayer.java
com.cladophora.ssniper.entity.Plant.java
com.cladophora.ssniper.entity.Reticle.java
com.cladophora.ssniper.entity.Rifle.java
com.cladophora.ssniper.entity.SPen.java
com.cladophora.ssniper.scene.GameScene.java
com.cladophora.ssniper.scene.InstructionScene.java
com.cladophora.ssniper.scene.IntroScene.java
com.cladophora.ssniper.scene.MainMenuScene.java
com.cladophora.ssniper.scene.PauseScene.java
com.cladophora.ssniper.scene.ResultScene.java
com.cladophora.ssniper.scene.ShopScene.java
com.cladophora.ssniper.scene.SplashScene.java
com.cladophora.ssniper.scene.SurvivorIntroScene.java
com.cladophora.ssniper.scene.SurvivorScene.java
com.cladophora.ssniper.scene.TutorialScene.java
com.cladophora.ssniper.scene.WeaponInfoScene.java