package wyf.ytl;
import static wyf.ytl.ConstantUtil.CITY_INFO;
import static wyf.ytl.ConstantUtil.DICE_SIZE;
import static wyf.ytl.ConstantUtil.DICE_SPAN;
import static wyf.ytl.ConstantUtil.DICE_START_X;
import static wyf.ytl.ConstantUtil.DICE_START_Y;
import static wyf.ytl.ConstantUtil.DIGIT_SPAN;
import static wyf.ytl.ConstantUtil.GAME_VIEW_SCREEN_COLS;
import static wyf.ytl.ConstantUtil.GAME_VIEW_SCREEN_ROWS;
import static wyf.ytl.ConstantUtil.HERO_ANIMATION_FRAMES;
import static wyf.ytl.ConstantUtil.HERO_ANIMATION_SEGMENTS;
import static wyf.ytl.ConstantUtil.HERO_ARMY_DRAW_X;
import static wyf.ytl.ConstantUtil.HERO_ARMY_DRAW_Y;
import static wyf.ytl.ConstantUtil.HERO_FACE_HEIGHT;
import static wyf.ytl.ConstantUtil.HERO_FACE_START_Y;
import static wyf.ytl.ConstantUtil.HERO_FACE_WIDTH;
import static wyf.ytl.ConstantUtil.HERO_FOOD_DRAW_X;
import static wyf.ytl.ConstantUtil.HERO_FOOD_DRAW_Y;
import static wyf.ytl.ConstantUtil.HERO_HEIGHT;
import static wyf.ytl.ConstantUtil.HERO_MONEY_DRAW_X;
import static wyf.ytl.ConstantUtil.HERO_MONEY_DRAW_Y;
import static wyf.ytl.ConstantUtil.HERO_STRENGTH_DRAW_X;
import static wyf.ytl.ConstantUtil.HERO_STRENGTH_DRAW_Y;
import static wyf.ytl.ConstantUtil.HERO_WIDTH;
import static wyf.ytl.ConstantUtil.HUI_TOU_SHI_AN;
import static wyf.ytl.ConstantUtil.MAP_BUTTON_SIZE;
import static wyf.ytl.ConstantUtil.MAP_BUTTON_START_X;
import static wyf.ytl.ConstantUtil.MAP_BUTTON_START_Y;
import static wyf.ytl.ConstantUtil.MAP_COLS;
import static wyf.ytl.ConstantUtil.MAP_ROWS;
import static wyf.ytl.ConstantUtil.MINI_MAP_SPAN;
import static wyf.ytl.ConstantUtil.MINI_MAP_START_X;
import static wyf.ytl.ConstantUtil.RIGHT;
import static wyf.ytl.ConstantUtil.TILE_SIZE;
import static wyf.ytl.ConstantUtil.WU_ZHONG_SHENG_YOU;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
/**
*
* 该类为游戏的主界面
*
*/
public class GameView extends SurfaceView implements SurfaceHolder.Callback,View.OnTouchListener {
private int status = 0;//绘制时的状态,0正常游戏,1,英雄在走动,2,主菜单出现,3,选择将军出战,4,战斗动画
//5,穷死结束游戏,6,统一中国
//100-人物属性界面,99-武将情报 98-使用计谋 97-城池管理 96-天下局势
HDZGActivity activity;//activity的引用
DrawThread drawThread;//刷帧的线程
Hero hero;//英雄对象
static Bitmap dialogBack;//存放对话框背景
static Bitmap dialogButton;//存放对话框按钮
static Bitmap [][] heroAnimationSegments;//存放英雄所有动画段的图片
static Bitmap [] bmpDice;//存放骰子的图片数组
static Bitmap dashboardBitmap;//下面用于管理的图片
static Bitmap darkBitmap;//黑色外框的效果图
static Bitmap[] smallGameMenuOptions = new Bitmap[6];//装主菜单分割以后的图片
static Bitmap[] bmpDigit;//存放数码管数字的图片
static Bitmap bmpBattleFiled;//战场背景图片
static Bitmap[] bmpHero;//存放英雄的图片,分为静态和动态,代表英雄静止还是走路
int [][] notInMatrix=new int[MAP_ROWS][MAP_COLS];//存放整个大地图的不可通过矩阵
int miniMapStartX = MINI_MAP_START_X; //初始情况下缩略地图开始的X坐标
int miniMapStartY = -160; //初始情况下缩略地图开始的Y坐标
boolean showMiniMap;//是否显示迷你地图
int startRow = 0;//屏幕在大地图中的行数
int startCol = 0;//屏幕在大地图中的列数
int offsetX = 0;//屏幕定位点在大地图上的x方向偏移,用来实现无级滚屏
int offsetY = 0;//屏幕定位点在大地图上的y方向偏移,用来实现无级滚屏
LayerList layerList;//所有的层
MyMeetableDrawable [][] meetableMatrix;//存放大地图的可遇矩阵
MyMeetableDrawable currentDrawable;//记录当前碰到的可遇Drawable对象引用
MyMeetableDrawable previousDrawable;//记录上一个碰到的可遇Drawable对象引用
MeetableLayer meetableChecker;//
GameViewThread gvt;//后台修改数据的线程
BattleField battleField;
int suiXinBu=0;//记录英雄施放随心步时选择的步数
General fightingGeneral = null;//存放当前出战的将军
ArrayList<Skill> skillToLearn;//将要学习的技能
int diceCount=2;//起作用的骰子的个数,学习了骑术会增加,最大到三
int []diceValue = {1,3,5};//存放骰子的值,其实是骰子图片数组的下标,0-5代表1-6的骰子图片
int currentSteps;//记录本次掷骰子需要走几步
ManPanelView manPanelView;//人物属性窗口
WuJiangView wuJiangView;//武将情报窗口
UseSkillView useSkillView;//使用计谋窗口
CityManageView cityManageView;//城池管理窗口
SelectGeneral selectGeneral;//选中出征武将窗口
TianXiaView tianXiaView;//天下局势窗口
GameAlert currentGameAlert;//记录当前的消息提示
ArrayList<CityDrawable> allCityDrawable = new ArrayList<CityDrawable>();//存放所有敌方的城池
ArrayList<General> freeGeneral;//自由的将领,即不属于我方和敌方的
Paint paint;//画笔
public static Resources resources; //声明资源对象引用
MediaPlayer mMediaPlayer;
SoundPool soundPool;//声音
HashMap<Integer, Integer> soundPoolMap;
//构造器
public GameView(HDZGActivity activity) {
super(activity);
resources = this.getResources();
if(activity.loadingView != null){//走进度条
activity.loadingView.process += 30;
}
this.activity = activity;//activity的引用
initSounds();
mMediaPlayer = MediaPlayer.create(activity, R.raw.backsound);
mMediaPlayer.setLooping(true);
if(activity.loadingView != null){//走进度条
activity.loadingView.process += 30;
}
paint = new Paint();
paint.setColor(Color.RED);
if(activity.loadingView != null){//走进度条
activity.loadingView.process += 30;
}
getHolder().addCallback(this);
this.drawThread = new DrawThread(getHolder(), this);//初始化刷帧线程
this.gvt = new GameViewThread(this);//初始化后台数据修改线程
this.battleField = new BattleField(this);
hero = new Hero(this, 3, 3);//创建英雄
hero.initAnimationSegment(heroAnimationSegments);//为英雄初始化动画段列表
hero.setAnimationDirection(RIGHT%4);//初始化英雄朝向右,静态右
hero.startAnimation();//启动英雄动画
initMap();//初始化地图
initClass();//初始化所有用到的类
if(activity.isBackSound){
mMediaPlayer.start();
}
if(activity.loadingView != null){//走进度条
activity.loadingView.process += 40;
}
}
public void initSounds(){
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(getContext(), R.raw.dingdong, 1));
soundPoolMap.put(2, soundPool.load(getContext(), R.raw.battle, 1));
}
public void playSound(int sound, int loop) {
AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
}
//初始化所有用到的类
public void initClass(){
manPanelView = new ManPanelView(this);
wuJiangView = new WuJiangView(this);
useSkillView = new UseSkillView(this);
cityManageView = new CityManageView(this);
selectGeneral = new SelectGeneral(this);
tianXiaView = new TianXiaView(this);
}
//方法:初始化地图
public void initMap(){
layerList = new LayerList(getResources());
this.notInMatrix = layerList.getTotalNotIn();//得到总不可通过矩阵
this.meetableChecker = (MeetableL