// 响应工作线程发出的AI移动棋子事件,用ITWEEN移动棋子game obj到对应的位置上 void OnAiMove(EventBase eb) { AIMoveEvent aIMoveEvent = eb.eventValue as AIMoveEvent; Debug.Log(string.Format("事件回调:{0}, from={1}, to={2}", AIMoveEvent.AI_MOVE_EVNET, aIMoveEvent.from, aIMoveEvent.to)); if (aIMoveEvent != null) { // 得到下标检测球GameObject GameObject indexSphereGo = IndexCtrlBehaviour.instance.getIndexSphereGo(aIMoveEvent.from); if (indexSphereGo == null) { Debuger.LogError(string.Format("该256下标{0} indexSphereGo is null", aIMoveEvent.from)); return; } // 得到下标检测球上的IndexTriger脚本 IndexTrigerBehaviour indexTriger = indexSphereGo.GetComponent <IndexTrigerBehaviour>( ); if (indexTriger != null) { // 如果该检测球上有棋子,则移动棋子到该次下棋的终点的检测球位置 if (indexTriger.HasQiZi) { Debug.Log(string.Format("AI下棋,将棋子从256数组下标{0}移到下标{1}", aIMoveEvent.from, aIMoveEvent.to)); // 用ITWEEN移动棋子game obj TweenUtil.MoveTo(indexTriger.QiZiGameObject, IndexCtrlBehaviour.instance.getIndexSphereGo(aIMoveEvent.to).GetComponent <Transform>( ).localPosition, m_moveTime); Action finishAction = AiOnceMoveFinish; StartCoroutine(DelayToInvokeUtil.DelayToInvokeDo(finishAction, m_moveTime)); } } else { Debug.LogError("indexTriger is null !!"); } } else { Debug.LogError("aIMoveEvent is null !!"); } }
public void AiOnceMove( ) { // 如果正在进行电脑下棋 if (!isValidTouch) { return; } //...省略Android前面的代码 // 电脑走棋 // 启动一个线程进行电脑下棋 Thread aiThread = new Thread(new ThreadStart(() => { Debuger.LogWarning("GameView onTouchEvent函数,电脑走棋线程开始"); ViewConstant.endTime = ViewConstant.zTime; // 时间初始化 isRedPlayChess = true; // 正在下棋 isValidTouch = false; // 正在下棋标志 draw( ); // 重绘方法 // 电脑走棋 AiMoveSearch.SearchMain( ); int sqSrc = Chess_LoadUtil.SRC(AiMoveSearch.mvResult); // 得到起始位置的数组下标 int sqDst = Chess_LoadUtil.DST(AiMoveSearch.mvResult); // 得到目标位置的数组下标 int pcCaptured = ucpcSquares[sqDst]; // 得到目的格子的棋子 Debuger.LogWarning(string.Format("GameView onTouchEvent函数,走法起点={0},走法终点={1}, 得到目的格子的棋子={2}", sqSrc, sqDst, pcCaptured)); // 线程安全事件管理器,分发事件,到UI线程处理,棋子的移动 // AI移动棋子(U3D API),移动到 256数组【sqDst】 AIMoveEvent aIMoveEvent = new AIMoveEvent( ); aIMoveEvent.from = sqSrc; aIMoveEvent.to = sqDst; // 派发事件,AI移动棋子事件 EventDispatcher.Instance( ).DispatchEvent(AIMoveEvent.AI_MOVE_EVNET, aIMoveEvent); Debuger.Log("GameView onTouchEvent函数->派发事件,AI移动棋子事件"); // 电脑走一步棋 AiMoveSearch.MakeMove(AiMoveSearch.mvResult, 0); StackPlayChess stackplayChess = new StackPlayChess(AiMoveSearch.mvResult, pcCaptured); // 下棋步骤入栈 stack.Push(stackplayChess); initArrays( );// 数组操作 // 如果电脑赢了 if (AiMoveSearch.IsMate( )) { AiMoveSearch.Startup( ); // 初始化棋盘 initArrays( ); // 初始化数组 ViewConstant.shuJMflag = true; //father.playSound(5, 1);// b播放声音,输了 } else { //father.playSound(2, 1);// b播放声音,电脑下棋了 isValidTouch = true;// 下完棋子,玩家可以操控了。 } isRedPlayChess = false; ViewConstant.endTime = ViewConstant.zTime; draw( );// 重绘方法 Debuger.LogWarning("电脑走棋线程执行完毕!!"); })); //aiThread.IsBackground = true; // 加入线程管理,并启动线程 ThreadManager.Instance.addWorkThread(aiThread); }