package tw.brad.apps.bombking;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View {
int play_level, bombking;
boolean isSound, isLimit, isMusic, isCustom;
String CustomFile;
private Context context;
private Resources res;
private GestureDetector gDetector;
private static SharedPreferences sp;
private static SharedPreferences.Editor speditor;
private File rootPath, savePath;
private int rows = 12;
private int cols = 20;
private int[][] gamemap, prebomb;
private int[] leveldata = { R.raw.bomb000, R.raw.bomb001, R.raw.bomb002,
R.raw.bomb003, R.raw.bomb004, R.raw.bomb005, R.raw.bomb006,
R.raw.bomb007, R.raw.bomb008, R.raw.bomb009, R.raw.bomb010,
R.raw.bomb011, R.raw.bomb012, R.raw.bomb013, R.raw.bomb014,
R.raw.bomb015, R.raw.bomb016, R.raw.bomb017, R.raw.bomb018,
R.raw.bomb019, R.raw.bomb020, R.raw.bomb021, R.raw.bomb022,
R.raw.bomb023, R.raw.bomb024, R.raw.bomb025, R.raw.bomb026,
R.raw.bomb027, R.raw.bomb028, R.raw.bomb029, R.raw.bomb030,
R.raw.bomb031, R.raw.bomb032, R.raw.bomb033, R.raw.bomb034,
R.raw.bomb035, R.raw.bomb036, R.raw.bomb037, R.raw.bomb038,
R.raw.bomb039, R.raw.bomb040, R.raw.bomb041, R.raw.bomb042,
R.raw.bomb043, R.raw.bomb044, R.raw.bomb045, R.raw.bomb046,
R.raw.bomb047, R.raw.bomb048, R.raw.bomb049, R.raw.bomb050,
R.raw.bomb051, R.raw.bomb052, R.raw.bomb053, R.raw.bomb054,
R.raw.bomb055, R.raw.bomb056, R.raw.bomb057, R.raw.bomb058,
R.raw.bomb059, R.raw.bomb060, R.raw.bomb061, R.raw.bomb062,
R.raw.bomb063 };
private Bitmap android, bbbird1, bbbird2, apple, brick_dig, brick_hard,
bomb, bomb2, exit;
private Bitmap android_walk1, android_walk2;
private Bitmap bomb_f1, bomb_f2, bomb_f3, bomb_f4, danger, box, shield,
shields, shieldb;
private Bitmap sound_on, sound_off, music_on, music_off;
private Bitmap bg;
private Bitmap stop, up, down, left, right, bomb_bt;
private Bitmap plank1, plank2, plank3, plank4;
private Bitmap controller;
private boolean isInitGame, isStartGame, isStopRound, isShieldMode;
private int android_r, android_c, android_step_h, android_step_v;
private int viewW, viewH, unitW, unitH;
private Matrix matrix;
private final int STOP = 0;
private final int UP = 1;
private final int DOWN = 2;
private final int LEFT = 3;
private final int RIGHT = 4;
private static final int MAP_ROAD = 0;
private static final int MAP_BRICK = 1;
private static final int MAP_WALL = 2;
private static final int MAP_BOX = 3;
private static final int MAP_EXIT = 4;
private static final int MAP_BOMB2 = 5;
private static final int MAP_BOMB = 6;
private static final int MAP_BBBIRD = 7;
private static final int MAP_ANDROID = 8;
private static final int MAP_APPLE = 9;
private static final int MAP_SHIELD = 10;
private static final int MAP_FLASH_4 = -4;
private static final int MAP_FLASH_3 = -3;
private static final int MAP_FLASH_2 = -2;
private static final int MAP_FLASH_1 = -1;
private static final int BOMB_KING_FOX = 0;
private static final int BOMB_KING_LION = 1;
private static final int BOMB_KING_PANDA = 2;
private int freq = 70;
private int bb_freq = 180;
private int bomb_timeout, shield_timeout = 7000;
private static int bomb_freq = 200;
private List<BBBird> bbs = new ArrayList<BBBird>();
private static int Apple_Qty = 0, Apple_Total = 0;
private static int Bomb_Qty, addBombQty;
private static int Shield_Qty, addShieldQty;
Timer timer;
private Android atask;
private static Paint txtTitlePaint, txtStagePaint, txtApplePaint, bgPaint;
private LinkedList<HashMap<String, Integer>> bricks;
private int exit_r0, exit_c0, exit_r1, exit_c1, exit_r2, exit_c2, iStart;
private int box_count, box_open;
private int shield_count, shield_open;
private SoundPool soundPool;
private int Sound_Bomb, Sound_Apple, Sound_Box, Sound_Put_Bomb,
Sound_Loser, Sound_Winner, Sound_Timer;
private boolean isTouchController;
private int tc, tr;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
gDetector = new GestureDetector(this.context, new MyGesture());
sp = context.getSharedPreferences("gamedata", context.MODE_PRIVATE);
speditor = sp.edit();
rootPath = Environment.getExternalStorageDirectory();
savePath = new File(rootPath, "/Android/data/"
+ context.getPackageName() + "/levels");
if (!savePath.exists())
savePath.mkdirs();
// Load Sound Effect
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
Sound_Bomb = soundPool.load(context, R.raw.bomb2, 1);
Sound_Apple = soundPool.load(context, R.raw.eat_apple, 1);
Sound_Box = soundPool.load(context, R.raw.eat_box, 1);
Sound_Put_Bomb = soundPool.load(context, R.raw.put_bomb, 1);
Sound_Loser = soundPool.load(context, R.raw.loser, 1);
Sound_Winner = soundPool.load(context, R.raw.winner1, 1);
Sound_Timer = soundPool.load(context, R.raw.timer, 1);
bgPaint = new Paint();
bgPaint.setAlpha(192);
res = getResources();
}
void initGame() {
viewW = getWidth();
viewH = getHeight();
unitW = viewW / cols;
unitH = viewH / rows;
txtStagePaint = new Paint();
txtStagePaint.setColor(Color.WHITE);
txtStagePaint.setStrokeWidth((int) (unitH / 30));
txtStagePaint.setTextSize((int) (0.6 * unitH));
txtStagePaint.setStyle(Paint.Style.FILL_AND_STROKE);
txtApplePaint = new Paint();
txtApplePaint.setColor(Color.YELLOW);
txtApplePaint.setStrokeWidth((int) (unitH / 30));
txtApplePaint.setTextSize((int) (0.6 * unitH));
txtApplePaint.setStyle(Paint.Style.FILL_AND_STROKE);
matrix = new Matrix();
switch (bombking) {
default:
case BOMB_KING_FOX:
bg = fitBitmap(R.drawable.bg_01, 0);
android = fitBitmap(R.drawable.android_fox, 1);
android_walk1 = fitBitmap(R.drawable.fox_walk1, 1);
android_walk2 = fitBitmap(R.drawable.fox_walk2, 1);
freq = 70;
bomb_timeout = 2400;
break;
case BOMB_KING_LION:
bg = fitBitmap(R.drawable.bg_02, 0);
android = fitBitmap(R.drawable.android_lion, 1);
android_walk1 = fitBitmap(R.drawable.lion_walk1, 1);
android_walk2 = fitBitmap(R.drawable.lion_walk2, 1);
freq = 100;
bomb_timeout = 2800;
break;
case BOMB_KING_PANDA:
bg = fitBitmap(R.drawable.bg_03, 0);
android = fitBitmap(R.drawable.android_panda, 1);
android_walk1 = fitBitmap(R.drawable.panda_walk1, 1);
android_walk2 = fitBitmap(R.drawable.panda_walk2, 1);
freq = 130;
bomb_timeout = 3200;
break;
}
bbbird1 = fitBitmap(R.drawable.enemy1, 1);
bbbird2 = fitBitmap(R.drawable.enemy2, 1);
apple = fitBitmap(R.drawable.apple, 1);
brick_dig = fitBitmap(R.drawable.brick_dig, 1);
brick_hard = fitBitmap(R.drawable.bricks, 1);
bomb = fitBitmap(R.drawable.bomb, 1);
bomb2 = fitBitmap(R.drawable.bomb2, 1);
bomb_f4 = fitBitmap(R.drawable.brick_bomb1, 1);
bomb_f3 = fitBitmap(R.drawable.brick_bomb2, 1);
bomb_f2 = fitBitmap(R.drawable.brick_bomb3, 1);
bomb_f1 = fitBitmap(R.drawable.brick_bomb4, 1);
exit = fitBitmap(R.drawable.exit, 1);
danger = fitBitmap(R.drawable.danger, 1);
box = fitBitmap(R.drawable.box, 1);
sound_on = fitBitmap(R.