GBoardChess.ts 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. import ADSDK from '../../gcommon/AdSdk/ADSDK';
  2. import AudioMng from '../../gcommon/AudioMng';
  3. import { UIButton } from '../../gcommon/UIButton';
  4. import GameUtil from '../../gcommon/GameUtil';
  5. import { ResMng } from '../../gcommon/ResMng';
  6. import UISetting from '../../gcommon/UISetting';
  7. import UITips from '../../gcommon/UITips';
  8. import { GameMng, quit_chess_show_view } from '../../GameMng';
  9. import CanjuModel from '../ChessCanJuSel/CanjuModel';
  10. import ChessMng, { ChessType } from '../DiffSel/ChessMng';
  11. import UIDiffSel from '../DiffSel/UIDiffSel';
  12. import ChessPos from '../ChessPos';
  13. import Search from '../Search';
  14. import UIEndChess from './UIEndChess';
  15. import ChessRBSel from './ChessRBSel';
  16. import { Vec3,tween,UITransform ,_decorator ,Component,Label,Node,instantiate,Animation,Prefab,Sprite, director, Director, SpriteFrame, size} from "cc";
  17. import { UIjiesuan } from '../UIjiesuan';
  18. import { ChessThink } from './ChessThink';
  19. import { ClientEvent } from '../../clientEvent';
  20. import { Constant, robot_ai_level, room_type } from '../../constant';
  21. import { roomData } from '../../UserData/roomData';
  22. import { UIManager } from '../../gcommon/UIManager';
  23. import { waitFirend } from '../waitFirend';
  24. import UIDialog from '../UIDialog';
  25. import ScenceMng from '../../gcommon/ScenceMng';
  26. import { setupData } from '../../UserData/setupData';
  27. import { msgManager } from '../../socket/msgManager';
  28. import { settlementData } from '../../UserData/settlementData';
  29. import { Tools } from '../../Tools';
  30. import { timeCountData } from '../../UserData/timeCountData';
  31. import { waitView } from '../waitView';
  32. import { waitJifen } from '../waitJifen';
  33. const {ccclass, property} = _decorator;
  34. var LIMIT_DEPTH = 64;
  35. ///
  36. var MATE_VALUE = 10000;
  37. var WIN_VALUE = MATE_VALUE - 200;
  38. var PIECE_KING=0;
  39. //////
  40. var RESULT_UNKNOWN = 0;
  41. var RESULT_WIN = 1;
  42. var RESULT_DRAW = 2;
  43. var RESULT_LOSS = 3;
  44. var BOARD_WIDTH = 965;
  45. var BOARD_HEIGHT = 1080;
  46. var SQUARE_SIZE = 106;
  47. var SQUARE_LEFT = (BOARD_WIDTH - SQUARE_SIZE * 9) >> 1;
  48. var SQUARE_TOP = (BOARD_HEIGHT - SQUARE_SIZE * 10) >> 1;
  49. var THINKING_SIZE = 32;
  50. var THINKING_LEFT = (BOARD_WIDTH - THINKING_SIZE) >> 1;
  51. var THINKING_TOP = (BOARD_HEIGHT - THINKING_SIZE) >> 1;
  52. var MAX_STEP = 8;
  53. var PIECE_NAME = [
  54. "oo", null, null, null, null, null, null, null,
  55. "hj", "hs", "hx", "hm", "hc", "hp", "hz", null,
  56. "bj", "bs", "bx", "bm", "bc", "bp", "bz", null,
  57. ];
  58. var IN_CHESS_BOARD = [
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  63. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  64. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  65. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  66. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  67. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  68. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  69. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  70. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  71. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75. ];
  76. function IS_INBOARD(sq) {
  77. return IN_CHESS_BOARD[sq] != 0;
  78. }
  79. function COORD_XY(x, y) {
  80. return x + (y << 4);
  81. }
  82. function FILE_X(sq) {
  83. return sq & 15;
  84. }
  85. function RANK_Y(sq) {
  86. return sq >> 4;
  87. }
  88. function SQ_X(sq) {
  89. return SQUARE_LEFT + (FILE_X(sq) - 3) * SQUARE_SIZE;
  90. }
  91. function SQ_Y(sq) {
  92. return -(SQUARE_TOP + (RANK_Y(sq) - 3) * SQUARE_SIZE);
  93. }
  94. function MOVE_PX(src, dst, step) {
  95. return Math.floor((src * step + dst * (MAX_STEP - step)) / MAX_STEP + .5);
  96. }
  97. function alertDelay(message) {
  98. setTimeout(function () {
  99. alert(message);
  100. }, 250);
  101. }
  102. function SRC(mv) {
  103. return mv & 255;
  104. }
  105. function DST(mv) {
  106. return mv >> 8;
  107. }
  108. function SQUARE_FLIP(sq) {
  109. return 254 - sq;
  110. }
  111. function SIDE_TAG(sd) {
  112. return 8 + (sd << 3);
  113. }
  114. function MOVE(sqSrc, sqDst) {
  115. return sqSrc + (sqDst << 8);
  116. }
  117. @ccclass
  118. export default class GBoardChess extends Component {
  119. isComputerMoveing:boolean=false;
  120. // @property(Label)
  121. // huihelabel:Label=null;
  122. // @property(Label)
  123. // timelabel:Label=null;
  124. // @property(Label)
  125. // bushulabel:Label=null;
  126. @property(Node)
  127. bottom_node:Node=null;
  128. // @property(Animation)
  129. // animation:Animation=null;
  130. // @property(Node)
  131. // Juesha:Node=null;
  132. @property(Prefab)
  133. JueshaAniPrefab:Node=null;
  134. @property(Prefab)
  135. FaceKingPrefab:Prefab=null;
  136. @property(Prefab)
  137. startGameAniPrefab:Prefab=null;
  138. @property(Prefab)
  139. chiAniPrefab:Prefab=null;
  140. // @property(Label)
  141. // titlelabel:Label=null;
  142. @property(Node)
  143. GBoardChess:Node=null;
  144. @property(Node)
  145. chessNode: Node = null;
  146. @property(Node)
  147. chess_nor_Node: Node = null;
  148. @property(Node)
  149. wait_haoyou: Node = null;
  150. @property(Node)
  151. wait_jifen: Node = null;
  152. @property(Node)
  153. wait_renji: Node = null;
  154. @property(Node)
  155. chatNode: Node = null;
  156. @property(SpriteFrame)
  157. select_chess_icon: SpriteFrame = null;
  158. @property(Node)
  159. jiesuan_node:Node=null;
  160. @property(Node)
  161. headNode: Node = null;
  162. @property(Node)
  163. uiLayer: Node = null;
  164. @property(Node)
  165. chat_view: Node = null;
  166. pos:ChessPos=null;
  167. search:Search=null;
  168. imgSquares:Node[]=[];
  169. sqSelected:number=0; //当前被选中棋子的位置。
  170. mvLast:number=0;
  171. millis:number=0;
  172. computer:number=0;
  173. result:number=0;
  174. busy:boolean=false;
  175. animated:boolean=false;
  176. static instance:GBoardChess=null;
  177. moveNode:Node=null;
  178. isStartGame:boolean = false;
  179. static g_isStartFirstGame:boolean = true;
  180. isStartFirstGame:boolean = true;
  181. selfLaseSelectNode:Node=null;
  182. wait_rival_status:number=0; // 0等待,1好友已加入
  183. wait_rival_startGame_status:number = 0;//0,都没准备,1,好友未准备,2,自己未准备,3,开始下棋
  184. //temp test ai
  185. tempsqSelected:number=0;
  186. tempsqDes:number=0;
  187. gTime:number=0;
  188. isend:boolean=false;
  189. bushu:number=0;
  190. result_pos:any[] =[]
  191. last_settlementData:settlementData= null;
  192. who_round:number = 0 //1 自己回手,0对方回合
  193. round_id:number = 0 //回合者的id
  194. moveCallfun:any = null
  195. rund_number:number = 0 //当前第多少步
  196. heqi_number:number = 0
  197. huiqi_number:number = 0
  198. huiqi_setup:number = 0;
  199. moveList:any[] = []
  200. mv_rival_last:number=0;
  201. start () {
  202. GBoardChess.instance=this;
  203. this.moveNode=instantiate(this.chessNode);
  204. //this.moveNode.removeAllChildren();
  205. //this.huihelabel.string="开始游戏";
  206. this.initNeed();
  207. this.setTitlelabel();
  208. this.startGame();
  209. this.regMsg();
  210. }
  211. regMsg(){
  212. ClientEvent.on(Constant.EVENT_TYPE.MSG_ROOM_STATUS,this.recv_room_status,this)
  213. ClientEvent.on(Constant.EVENT_TYPE.MSG_DISMISS_ROOM,this.recv_room_dismiss,this)
  214. ClientEvent.on(Constant.EVENT_TYPE.MSG_OWN_PLAYCHESS,this.recv_own_playchess,this)
  215. ClientEvent.on(Constant.EVENT_TYPE.MSG_RIVAL_PLAYCHESS,this.recv_rival_playchess,this)
  216. ClientEvent.on(Constant.EVENT_TYPE.MSG_SETTLEMENT,this.recv_settlement,this)
  217. ClientEvent.on(Constant.EVENT_TYPE.MSG_QUIT_ROOM,this.recvQuit,this)
  218. ClientEvent.on(Constant.EVENT_TYPE.MSG_PVP_TIME,this.recv_time_count,this)
  219. ClientEvent.on(Constant.EVENT_TYPE.MSG_HEQI_JIESHOU,this.recv_jieshou_heqi_game,this)
  220. ClientEvent.on(Constant.EVENT_TYPE.MSG_HUIQI_JIESHOU,this.recv_jieshou_huiqi_game,this)
  221. ClientEvent.on(Constant.EVENT_TYPE.MSG_HEQI_TONGZHI,this.recv_tongzhi_heqi_game,this)
  222. ClientEvent.on(Constant.EVENT_TYPE.MSG_HUIQI_TONGZHI,this.recv_tongzhi_huiqi_game,this)
  223. ClientEvent.on(Constant.EVENT_TYPE.MSG_START_HUIQI,this.recv_start_huiqi,this)
  224. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HEQI,this.onBtnHeQi,this)
  225. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HUIQI,this.onBtnHuiQi,this)
  226. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_SHOW_CHAT,this.onBtnShowChat,this)
  227. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HIDE_CHAT,this.onBtnHideChat,this)
  228. }
  229. onDestroy(){
  230. GBoardChess.instance=null;
  231. ClientEvent.off(Constant.EVENT_TYPE.MSG_ROOM_STATUS,this.recv_room_status,this)
  232. ClientEvent.off(Constant.EVENT_TYPE.MSG_DISMISS_ROOM,this.recv_room_dismiss,this)
  233. ClientEvent.off(Constant.EVENT_TYPE.MSG_OWN_PLAYCHESS,this.recv_own_playchess,this)
  234. ClientEvent.off(Constant.EVENT_TYPE.MSG_RIVAL_PLAYCHESS,this.recv_rival_playchess,this)
  235. ClientEvent.off(Constant.EVENT_TYPE.MSG_SETTLEMENT,this.recv_settlement,this)
  236. ClientEvent.off(Constant.EVENT_TYPE.MSG_QUIT_ROOM,this.recvQuit,this)
  237. ClientEvent.off(Constant.EVENT_TYPE.MSG_PVP_TIME,this.recv_time_count,this)
  238. ClientEvent.off(Constant.EVENT_TYPE.MSG_HEQI_JIESHOU,this.recv_jieshou_heqi_game,this)
  239. ClientEvent.off(Constant.EVENT_TYPE.MSG_HUIQI_JIESHOU,this.recv_jieshou_huiqi_game,this)
  240. ClientEvent.off(Constant.EVENT_TYPE.MSG_HUIQI_TONGZHI,this.recv_tongzhi_huiqi_game,this)
  241. ClientEvent.off(Constant.EVENT_TYPE.MSG_START_HUIQI,this.recv_start_huiqi,this)
  242. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HEQI,this.onBtnHeQi,this)
  243. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HUIQI,this.onBtnHuiQi,this)
  244. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_SHOW_CHAT,this.onBtnShowChat,this)
  245. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HIDE_CHAT,this.onBtnHideChat,this)
  246. }
  247. onBtnShowChat(){
  248. this.chat_view.active = true;
  249. }
  250. onBtnHideChat(){
  251. this.chat_view.active = false;
  252. }
  253. onBtnHuiQi(){
  254. msgManager.qingqiu_huiqi_game()
  255. waitView.Show("等待对方是否同意悔棋。")
  256. }
  257. onBtnHeQi(){
  258. msgManager.qingqiu_heqi_game()
  259. waitView.Show("等待对方是否同意和棋。")
  260. }
  261. checkWaitStatus(){
  262. if(GameMng._userData.room_info.chess!=null){
  263. if(GameMng._userData.room_info.chess.wait_select_result!=null&&GameMng._userData.room_info.chess.wait_select_result!=undefined){
  264. switch(GameMng._userData.room_info.chess.wait_select_result){
  265. case 0:
  266. {
  267. waitView.Show("等待对方是否同意悔棋。")
  268. break;
  269. }
  270. case 1:
  271. {
  272. waitView.Show("等待对方是否同意和棋。")
  273. break;
  274. }
  275. case 2:
  276. {
  277. this.recv_jieshou_huiqi_game()
  278. break;
  279. }
  280. case 3:
  281. {
  282. this.recv_jieshou_heqi_game()
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. recv_start_huiqi(setup:setupData){
  290. var move_data = MOVE(setup.src_pos,setup.dec_pos)
  291. // var src = COORD_XY(FILE_X(setup.src_pos),15-RANK_Y(setup.src_pos))
  292. // var des = COORD_XY(FILE_X(setup.dec_pos),15-RANK_Y(setup.dec_pos))
  293. // move_data = MOVE(des,src)
  294. this.huiqi_setup = setup.huiqi_setup+1;
  295. this.pos.pushMvList(move_data)
  296. if(setup.userid!=GameMng._userData.userid){ //说明不是自己
  297. console.log("开始对方开始悔棋",setup)
  298. this.huiqi(true)
  299. }else{
  300. this.huiqi(false)
  301. console.log("开始我方开始悔棋",setup)
  302. }
  303. console.log("move_data",move_data)
  304. this.who_round = 1 - this.who_round
  305. if(this.isStartGame===true){
  306. this.headNode.getComponent(ChessThink).startThink(true)
  307. }
  308. }
  309. recv_tongzhi_heqi_game(obj:any){
  310. if(obj.type === 0){
  311. UITips.show("对方拒绝和棋");
  312. }else if(obj.type === 1){
  313. UITips.show("协商和棋,对局结束");
  314. }
  315. UIManager.removeWaitViewLayer()
  316. this.heqi_number-=1;
  317. }
  318. recv_tongzhi_huiqi_game(obj:any){
  319. console.log("recv_tongzhi_huiqi_game",obj.type)
  320. if(obj.type === 0){
  321. UITips.show("对方拒绝悔棋");
  322. this.huiqi_number-=1;
  323. }else if(obj.type === 1){
  324. UITips.show("对方同意悔棋");
  325. this.huiqi_number-=1;
  326. }else if(obj.type === 2){
  327. UITips.show("悔棋请求超时!");
  328. }
  329. UIManager.removeWaitViewLayer()
  330. }
  331. recv_jieshou_heqi_game(){
  332. UIDialog.Show(()=>{
  333. msgManager.tongzhi_heqi_game(1)
  334. },()=>{
  335. msgManager.tongzhi_heqi_game(0)
  336. },"对方请求提和,是否同意",null,true)
  337. }
  338. recv_jieshou_huiqi_game(){
  339. // this.who_round===1&&
  340. if(!this.computerMove()){
  341. UIDialog.Show(()=>{
  342. msgManager.tongzhi_huiqi_game(1)
  343. },()=>{
  344. msgManager.tongzhi_huiqi_game(0)
  345. },"对方请求悔棋,是否同意",null,true)
  346. }else{
  347. msgManager.tongzhi_huiqi_game(2)
  348. }
  349. }
  350. recv_time_count(time_count_data:timeCountData){
  351. GameMng._userData.room.time_count_data = time_count_data;
  352. if(this.round_id!=time_count_data.round_id){
  353. this.round_id = time_count_data.round_id
  354. }else{
  355. }
  356. this.headNode.getComponent(ChessThink).startThink(false)
  357. if(time_count_data.round_id===GameMng._userData.userid){
  358. console.log("我的回合",time_count_data)
  359. this.who_round = 1;
  360. }else{
  361. this.who_round = 0;
  362. console.log("对方回合",time_count_data)
  363. }
  364. this.updateRound()
  365. }
  366. recvQuit(obj:any={type:0}){
  367. if(obj.type===0){
  368. }else{
  369. GameMng.Instance.quit_view = quit_chess_show_view.beitichu;
  370. }
  371. ChessMng.Instance.chesstype = ChessType._null;
  372. GameMng._userData.room = null;
  373. AudioMng.Instance.PlayBGM(GameMng.Instance.mainbgm);
  374. ScenceMng.Instance.load('GameLaunch');
  375. }
  376. recv_own_playchess(setup:setupData){
  377. console.log("自己完成下棋",setup.userid)
  378. // this.rund_number = setup.rund_number
  379. this.who_round = 0;
  380. this.updateRound();
  381. this.rund_number = setup.rund_number
  382. if(this.isStartGame===true){
  383. if(setup.pvp_time==null||setup.pvp_time==undefined){
  384. }else{
  385. GameMng._userData.room.time_count_data = setup.pvp_time;
  386. }
  387. this.headNode.getComponent(ChessThink).startThink(true,true)
  388. }
  389. }
  390. recv_settlement(settlement:settlementData){
  391. if(GameMng._userData.room==null){
  392. }else{
  393. UIManager.removeDialogViewLayer()
  394. UIManager.removeWaitViewLayer()
  395. this.headNode.getComponent(ChessThink).stopThink()
  396. console.log("结算",settlement)
  397. this.last_settlementData = settlement
  398. if(this.moveList.length>0){
  399. this.moveCallfun = ()=>{
  400. console.log("移动完,开始结算!")
  401. this.settlement(settlement)
  402. }
  403. }else{
  404. this.settlement(settlement)
  405. }
  406. this.isStartGame = false;
  407. if(GameMng._userData.room.rival==null){
  408. }
  409. let self = this;
  410. this.scheduleOnce(()=>{
  411. if(ChessMng.Instance.isHaoyouRoom()){
  412. this.wait_haoyou.active = true;
  413. self.wait_haoyou.getComponent(waitFirend).recvAgainRound(GameMng._userData.room)
  414. }else if(ChessMng.Instance.isJiFenRoom()){
  415. this.showWaitJiFen()
  416. self.wait_jifen.getComponent(waitJifen).recvAgainRound(GameMng._userData.room)
  417. }
  418. },3)
  419. GameMng._userData.room.time_count_data = null
  420. }
  421. }
  422. recv_rival_playchess(setup:setupData){
  423. this.who_round = 1;
  424. this.updateRound()
  425. // console.log("self.tempsqDes",self.tempsqDes)
  426. // console.log("self.tempsqSelected",self.tempsqSelected)
  427. if(this.isStartGame ==true){
  428. var self = this;
  429. this.moveList.push("0")
  430. // var src = COORD_XY(FILE_X(setup.src_pos),15-RANK_Y(setup.src_pos))
  431. // var des = COORD_XY(FILE_X(setup.dec_pos),15-RANK_Y(setup.dec_pos))
  432. // console.log("src ",src);
  433. // console.log("des ",des);
  434. // console.log("des row ",RANK_Y(des));
  435. this.mv_rival_last = MOVE(setup.src_pos, setup.dec_pos)
  436. director.once(Director.EVENT_AFTER_DRAW,function(){
  437. self.addMove(MOVE(setup.src_pos, setup.dec_pos),true);
  438. })
  439. console.log("对手下棋了",setup)
  440. this.rund_number = setup.rund_number
  441. if(this.isStartGame===true){
  442. if(setup.pvp_time==null||setup.pvp_time==undefined){
  443. }else{
  444. GameMng._userData.room.time_count_data = setup.pvp_time;
  445. }
  446. this.headNode.getComponent(ChessThink).startThink(true,true)
  447. }
  448. ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_MENU_STATUS)
  449. }
  450. }
  451. recv_room_dismiss(room:roomData){
  452. GameMng._userData.room = null;
  453. UIDialog.Show(()=>{
  454. AudioMng.Instance.PlayBGM(GameMng.Instance.mainbgm);
  455. ScenceMng.Instance.load('GameLaunch');
  456. },()=>{
  457. },"房间已解散",null,false)
  458. }
  459. recv_room_status(room:roomData){
  460. UIManager.removeLoadingLayer()
  461. GameMng.updateRoomData(room)
  462. this.headNode.getComponent(ChessThink).StatusShow()
  463. this.onRecvStartGame(room)
  464. }
  465. onRecvStartGame(room:roomData){
  466. if(ChessMng.Instance.isHaoyouRoom()){
  467. this.wait_haoyou.getComponent(waitFirend).recvReady(room)
  468. }else if(ChessMng.Instance.isJiFenRoom()){
  469. this.wait_jifen.getComponent(waitJifen).recvReady(room)
  470. }
  471. console.log("this.isStartGame== ",this.isStartGame,room.wait_rival_startGame_status,room.rival)
  472. if(room.wait_rival_startGame_status === 3 &&!this.isStartGame){
  473. this.isStartGame = true;
  474. this.userReadyStartGame()
  475. this.headNode.getComponent(ChessThink).startThink(true)
  476. if(room.isfirst_start===1){
  477. this.onstartGameAni()
  478. }
  479. }
  480. if(room.rival===undefined||room.rival===null){
  481. //this.hideAllChess()
  482. }
  483. }
  484. private startGame(){
  485. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  486. this.ShowRoleSel();
  487. this.isStartGame = true;
  488. }else{
  489. this.initChessBoard();
  490. }
  491. }
  492. countTime(){
  493. if(this.isend)return;
  494. this.gTime++;
  495. //this.timelabel.string="耗时:"+GameUtil.FormatTime_HHMMSS(this.gTime*1000)
  496. }
  497. ShowRoleSel(){
  498. //this.uirbselect.active=true;
  499. GBoardChess.instance.initChessBoard()
  500. }
  501. updateWaitStatus(){
  502. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  503. this.isStartGame = true;
  504. this.onstartGameAni()
  505. }else{
  506. this.wait_rival_status = GameMng._userData.room.wait_rival_status
  507. this.wait_rival_startGame_status = GameMng._userData.room.wait_rival_startGame_status
  508. this.onRecvStartGame(GameMng._userData.room);
  509. }
  510. this.headNode.getComponent(ChessThink).StatusShow()
  511. // if(this.wait_rival_status==0){ //房间只有自己还在等待
  512. // }else{//对手已加入房间
  513. // if(this.wait_rival_startGame_status===0){ //0,都没准备
  514. // }else if(this.wait_rival_startGame_status===1){ //1,好友未准备
  515. // }else if(this.wait_rival_startGame_status===2){ //2,自己未准备
  516. // }else if(this.wait_rival_startGame_status===3){ //3 都准备好开始下棋
  517. // }
  518. // }
  519. }
  520. setTitlelabel(){
  521. // if(ChessMng.Instance.chesstype==ChessType.renjiboyi)
  522. // this.titlelabel.string="难度:"+UIDiffSel.lvArr[UIDiffSel.selectIndex];
  523. // else if(ChessMng.Instance.chesstype==ChessType.canju)
  524. // this.titlelabel.string=CanjuModel.Instance.canjuData.name;
  525. }
  526. initChessInfo(){
  527. this.isComputerMoveing=false;
  528. this.busy=false;
  529. this.isend=false;
  530. this.huiqi_setup = 0;
  531. this.gTime=0;
  532. this.millis = this.getDiff();
  533. this.bushu=0;
  534. this.setTitlelabel();
  535. this.hideAllTrack();
  536. this.hideAllPath();
  537. this.result = RESULT_UNKNOWN;
  538. this.round_id = -1;
  539. this.rund_number = 0;
  540. this.heqi_number = 0;
  541. this.huiqi_number = 0;
  542. console.log("initChessInfo")
  543. }
  544. hideAllChess(){
  545. if(this.imgSquares.length>0){
  546. for (let index = 0; index < this.imgSquares.length; index++) {
  547. const element = this.imgSquares[index];
  548. if(element!=null){
  549. element.active =false;
  550. }
  551. }
  552. }
  553. }
  554. showAllChess(){
  555. if(this.imgSquares.length>0){
  556. for (let index = 0; index < this.imgSquares.length; index++) {
  557. const element = this.imgSquares[index];
  558. if(element!=null){
  559. element.active =true;
  560. }
  561. }
  562. }
  563. }
  564. //初始化棋盘
  565. initChessBoard(){
  566. this.initChessInfo()
  567. // this.isComputerMoveing=false;
  568. // this.busy=false;
  569. // this.isend=false;
  570. // this.gTime=0;
  571. // this.millis = this.getDiff();
  572. // this.bushu=0;
  573. // this.unschedule(this.countTime);
  574. // this.schedule(this.countTime, 1);
  575. // this.setTitlelabel();
  576. AudioMng.Instance.PlayBGM(GameMng.Instance.gamebgm)
  577. console.log(this.millis)
  578. if(ChessMng.Instance.chesstype==ChessType.canju){
  579. ChessRBSel.selIndex=0;
  580. }
  581. this.showHuiheLabel(ChessRBSel.selIndex == 0);
  582. // AudioMng.Instance.PlaySoundByName(GameMng.Instance.startclip)
  583. this.refreshBushulabel();
  584. //this.timelabel.string='耗时:00:00';
  585. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  586. this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
  587. this.computer = 1 - ChessRBSel.selIndex;
  588. this.flushBoard();
  589. }else if(ChessMng.Instance.chesstype==ChessType.canju){
  590. this.pos.fromFen(CanjuModel.Instance.canjuData.fen);
  591. }else{
  592. if(ChessMng.Instance.isHaoyouRoom()){
  593. this.showWaitHaoYou();
  594. }else if(ChessMng.Instance.isJiFenRoom()){
  595. this.showWaitJiFen();
  596. }
  597. }
  598. this.updateWaitStatus();
  599. setTimeout(() => {
  600. this.response();
  601. }, 500);
  602. }
  603. userReadyStartGame(){
  604. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  605. }else{
  606. if(ChessMng.Instance.isHaoyouRoom()){
  607. this.showAllChess();
  608. this.wait_haoyou.active = false;
  609. } else if(ChessMng.Instance.isJiFenRoom()){
  610. this.showAllChess();
  611. this.wait_jifen.active = false;
  612. }
  613. }
  614. //ChessRBSel.selIndex = 1-GameMng._userData.room.firstid
  615. //console.log("this.compute",this.computer)
  616. if(this.isStartFirstGame){
  617. }else{
  618. console.log("第二次初始化棋盘")
  619. this.initChessInfo()
  620. }
  621. this.computer = GameMng._userData.room.firstid;
  622. this.busy = GameMng._userData.room.firstid ==0;
  623. this.who_round = GameMng._userData.room.firstid;
  624. this.updateRound();
  625. this.heqi_number = GameMng._userData.room.heqi_number;
  626. this.huiqi_number = GameMng._userData.room.huiqi_number;
  627. this.huiqi_setup = GameMng._userData.room.huiqi_setup;
  628. if(GameMng._userData.room.chess!=null&&GBoardChess.g_isStartFirstGame){
  629. if(GameMng._userData.room_info.chess.chess_whole===""||GameMng._userData.room_info.chess.chess_whole===undefined||GameMng._userData.room_info.chess.chess_whole===null){
  630. this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
  631. }else{
  632. this.pos.fromFen(GameMng._userData.room_info.chess.chess_whole);
  633. this.pos.pushMvList(MOVE(GameMng._userData.room_info.chess.src_pos, GameMng._userData.room_info.chess.dec_pos))
  634. this.drawOldAndNewSelect(GameMng._userData.room_info.chess.src_pos, GameMng._userData.room_info.chess.dec_pos,true)
  635. }
  636. this.rund_number = GameMng._userData.room_info.chess.rund_number;
  637. this.heqi_number = GameMng._userData.room_info.chess.heqi_number;
  638. this.huiqi_number = GameMng._userData.room_info.chess.huiqi_number;
  639. this.checkWaitStatus()
  640. }else{
  641. this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
  642. }
  643. this.flushBoard();
  644. this.isStartFirstGame = false;
  645. GBoardChess.g_isStartFirstGame = false;
  646. }
  647. showWaitHaoYou(){
  648. if(this.isStartFirstGame){
  649. this.bottom_node.active = false;
  650. this.wait_haoyou.active = true;
  651. this.chatNode.active = true;
  652. }
  653. }
  654. showWaitJiFen(){
  655. this.bottom_node.active = false;
  656. this.wait_haoyou.active = false;
  657. this.chatNode.active = true;
  658. this.wait_jifen.active = true;
  659. }
  660. //重新开始
  661. restartGame(){
  662. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  663. if(ChessMng.Instance.chesstype===ChessType.renjiboyi){
  664. this.wait_renji.active = false;
  665. }
  666. this.startGame();
  667. }
  668. //悔棋
  669. retract(){
  670. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  671. if (this.busy) {
  672. return;
  673. }
  674. if(!this.isStartGame){
  675. return;
  676. }
  677. // ADSDK.Share(null, () => {
  678. // }, null)
  679. this.result = RESULT_UNKNOWN;
  680. if (this.pos.mvList.length > 1) {
  681. this.pos.undoMakeMove();
  682. }
  683. if (this.pos.mvList.length > 1 && this.computerMove()) {
  684. this.pos.undoMakeMove();
  685. }
  686. if(this.mvLast>0){
  687. this.drawSquare(SRC(this.mvLast), false,false);
  688. this.drawSquare(DST(this.mvLast), false,false);
  689. this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
  690. }
  691. this.flushBoard();
  692. this.response();
  693. this.hideAllPath();
  694. console.log("this.mvLast == ",this.mvLast)
  695. }
  696. //提示
  697. tishi(){
  698. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  699. if(this.busy||this.isComputerMoveing)return;
  700. ADSDK.Share(null,()=>{
  701. this.isComputerMoveing=true;
  702. console.log("tishi")
  703. this.search.searchMain(4,400,(a)=>{
  704. if(this.addMove(a,false)){
  705. this.hideAllPath()
  706. }
  707. });
  708. },null)
  709. }
  710. private getDiff() {
  711. return Math.pow(10, UIDiffSel.selectIndex + 1);
  712. }
  713. private initNeed() {
  714. this.animated = true;
  715. this.search = null;
  716. this.imgSquares = [];
  717. this.sqSelected = 0;
  718. this.mvLast = 0;
  719. this.millis = 0;
  720. this.computer = -1;
  721. this.result = RESULT_UNKNOWN;
  722. this.busy = false;
  723. this.pos = new ChessPos();
  724. this.setSearch(16);
  725. for (let sq = 0; sq < 256; sq++) {
  726. if (!IS_INBOARD(sq)) {
  727. this.imgSquares.push(null);
  728. continue;
  729. }
  730. let gnode=instantiate(this.chess_nor_Node);
  731. gnode.position = new Vec3( SQ_X(sq),SQ_Y(sq));
  732. gnode.getComponent(UITransform).setContentSize(SQUARE_SIZE,SQUARE_SIZE);
  733. gnode.parent=this.GBoardChess;
  734. var sp = gnode.getChildByName("chess_bg");
  735. (sp.getComponent(Sprite) as Sprite).enabled=false;
  736. gnode.getChildByName('track1').active=false;
  737. gnode.getChildByName('track2').active=false;
  738. gnode.on('click', () => {
  739. this.clickSquare(sq);
  740. }, this);
  741. this.imgSquares.push(gnode);
  742. }
  743. //this.hideAllTrack();
  744. }
  745. ShowSeting(){
  746. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  747. UISetting.Show();
  748. }
  749. // 点击棋盘的响应函数。点击棋盘(棋子或者空位置),就会调用该函数。sq_是点击的位置。
  750. clickSquare(sq_) {
  751. if(GameMng._userData.room){
  752. console.log(this.isStartGame,sq_,this.busy,this.result,this.who_round,this.sqSelected);
  753. }
  754. if(!this.isStartGame){
  755. return;
  756. }
  757. if (this.busy || this.result != RESULT_UNKNOWN) {
  758. return;
  759. }
  760. var sq = this.flipped(sq_);
  761. var pc = this.pos.squares[sq];
  762. // var col = FILE_X(sq_)-3
  763. // var row = RANK_Y(sq_)-3
  764. // console.log("col ",col);
  765. // console.log("row ",row);
  766. // console.log("pc ",pc,sq);
  767. if(this.sqSelected==sq){
  768. return;
  769. }
  770. if ((pc & SIDE_TAG(this.pos.sdPlayer)) != 0 ) {
  771. console.log("111");
  772. this.hideAllPath()
  773. AudioMng.Instance.PlaySoundByName(GameMng.Instance.pickclip);
  774. // if (this.mvLast != 0) {
  775. // this.drawSquare(SRC(this.mvLast), false,true);
  776. // this.drawSquare(DST(this.mvLast), false,true);
  777. // }
  778. if (this.sqSelected) {
  779. this.drawSquare(this.sqSelected, false,true);
  780. }
  781. this.drawSquare(sq, true,true);
  782. var temp_array = this.findAllCanMove(sq)
  783. this.drawCanMovePath(temp_array)
  784. this.sqSelected = sq;
  785. } else if (this.sqSelected > 0) {
  786. console.log("222");
  787. this.tempsqSelected = this.sqSelected;
  788. this.tempsqDes = sq;
  789. if(this.addMove(MOVE(this.sqSelected, sq), false)){
  790. this.hideAllPath();
  791. }
  792. }
  793. }
  794. private FaceKingWarning() //将军
  795. {
  796. let node = instantiate(this.FaceKingPrefab);
  797. node.parent = this.node;
  798. tween()
  799. .target(node)
  800. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  801. .start();
  802. AudioMng.Instance.PlaySoundByName(GameMng.Instance.kingclip)
  803. }
  804. private onstartGameAni(){
  805. let node = instantiate(this.startGameAniPrefab);
  806. node.parent = this.node;
  807. tween()
  808. .target(node)
  809. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  810. .start();
  811. console.log("onstartGameAni")
  812. }
  813. private onchiGameAni(){
  814. let node = instantiate(this.chiAniPrefab);
  815. node.parent = this.node;
  816. tween()
  817. .target(node)
  818. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  819. .start();
  820. console.log("onchiGameAni")
  821. AudioMng.Instance.PlaySoundByName(GameMng.Instance.eatclip)
  822. }
  823. private onJueShaAni(){
  824. let node = instantiate(this.JueshaAniPrefab);
  825. node.parent = this.node;
  826. tween()
  827. .target(node)
  828. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  829. .start();
  830. console.log("onJueShaAni")
  831. AudioMng.Instance.PlaySoundByName(GameMng.Instance.kill)
  832. }
  833. settlement(data:settlementData){
  834. AudioMng.Instance.PauseBGM();
  835. this.result_pos = Tools.getChessPos()
  836. this.isend=true;
  837. if(ChessMng.Instance.chesstype===ChessType.renjiboyi){
  838. if(!data.isheqi){
  839. //this.animation.play('win');
  840. AudioMng.Instance.PlaySoundByName(GameMng.Instance.kill)
  841. this.onJueShaAni()
  842. setTimeout(() => {
  843. this.jiesuan_node.active = true;
  844. this.jiesuan_node.getComponent(UIjiesuan).setData(data,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu)
  845. //this.endview.active=true;
  846. //let nodets= this.endview.getComponent(UIEndChess);
  847. //nodets.setData(result,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu);
  848. }, 2000);
  849. }
  850. }else {
  851. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  852. switch(data.result_type){
  853. case 0:{ //正常绝杀
  854. this.onJueShaAni()
  855. break;
  856. }
  857. case 1:{ //到时间
  858. if(data.result==1){
  859. UITips.show("对方到时间,您获得胜利!");
  860. }else{
  861. UITips.show("你的到时间,对方获得胜利!");
  862. }
  863. break;
  864. }
  865. case 2:{ //逃跑
  866. if(data.result==1){
  867. UITips.show("对方逃跑,获得胜利");
  868. }else{
  869. UITips.show("你逃跑输掉比赛");
  870. }
  871. break;
  872. }
  873. case 3:{ //认输
  874. if(data.result==1){
  875. UITips.show("对方认输,对局结束");
  876. }else{
  877. UITips.show("我方认输,对局结束");
  878. }
  879. break;
  880. }
  881. case 4:{ //和棋
  882. if(data.result==1){
  883. }else{
  884. }
  885. break;
  886. }
  887. }
  888. this.jiesuan_node.active = true;
  889. this.jiesuan_node.getComponent(UIjiesuan).setData(data,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu)
  890. }
  891. }
  892. }
  893. private gameEnd(result,isheqi:boolean=false)
  894. {
  895. this.isStartGame =false;
  896. if(ChessMng.Instance.chesstype == ChessType.renjiboyi){
  897. let settlement_data = new settlementData()
  898. settlement_data.result = result
  899. settlement_data.isheqi = isheqi
  900. this.wait_renji.active = true;
  901. this.settlement(settlement_data)
  902. }else{
  903. console.log("result == ",result)
  904. if(result === 1){
  905. //msgManager.final_game()
  906. }else{
  907. console.log("result",result)
  908. console.log("isheqi",isheqi)
  909. }
  910. }
  911. }
  912. findAllCanMove(sq){
  913. return this.pos.findAllCanMove(sq)
  914. }
  915. moveAni(mv,computerMove,isRight:boolean=true){
  916. AudioMng.Instance.PlaySoundByName(GameMng.Instance.putclip)
  917. this.busy = true;
  918. if (!this.animated) {
  919. if(computerMove)this.isComputerMoveing=false;
  920. this.postAddMove(mv, computerMove);
  921. return false;
  922. }
  923. console.log("sqSrc",mv)
  924. var sqSrc = this.flipped(SRC(mv));
  925. var xSrc = SQ_X(sqSrc);
  926. var ySrc = SQ_Y(sqSrc);
  927. console.log("sqSrc",xSrc,ySrc,computerMove);
  928. var sqDst = this.flipped(DST(mv));
  929. var xDst = SQ_X(sqDst);
  930. var yDst = SQ_Y(sqDst);
  931. console.log("sqDst",xDst,yDst);
  932. var getNode = this.imgSquares[sqSrc];
  933. // this.moveNode = instantiate(getNode)
  934. var bg = getNode.getChildByName("chess_bg");
  935. var qz = bg.getChildByName("chess_qz");
  936. var _bg = this.moveNode.getChildByName("chess_bg");
  937. var _qz = _bg.getChildByName("chess_qz");
  938. _qz.active = true;
  939. this.moveNode.active=true;
  940. getNode.active =false;
  941. this.moveNode.removeFromParent()
  942. //this.moveNode.getComponent(Sprite).spriteFrame=getNode.getComponent(Sprite).spriteFrame;
  943. this.moveNode.parent=getNode.parent;
  944. var temp_pos = new Vec3(getNode.position.x,getNode.position.y,0)
  945. this.moveNode.position = temp_pos
  946. var step = MAX_STEP - 1;
  947. var this_ = this;
  948. let scale_ = 1.04
  949. let dir = -1
  950. _bg.getComponent(Sprite).spriteFrame=bg.getComponent(Sprite).spriteFrame;
  951. _qz.getComponent(Sprite).spriteFrame=qz.getComponent(Sprite).spriteFrame;
  952. if(computerMove){
  953. //console.log("bg", bg.getComponent(Sprite).spriteFrame.name);
  954. _bg.getComponent(Sprite).spriteFrame=this.select_chess_icon;
  955. _qz.getComponent(Sprite).spriteFrame=qz.getComponent(Sprite).spriteFrame;
  956. }
  957. _bg.getComponent(UITransform).contentSize =size(112,117)
  958. _qz.getComponent(UITransform).contentSize =size(90,83)
  959. //_bg.position = new Vec3(bg.position.x,bg.position.y,bg.position.z)
  960. _qz.position = new Vec3(0,13,qz.position.z)
  961. if(!isRight){
  962. tween(this.moveNode)
  963. .to(0.2, { position:new Vec3(xDst,yDst,0)}).call(()=>{
  964. this.busy = false;
  965. this_.moveNode.active=false;
  966. getNode.active =true;
  967. tween(this.moveNode)
  968. .to(0.1, { position:temp_pos}).start()
  969. }).start()
  970. return
  971. }
  972. if(yDst>ySrc){
  973. }else if(yDst<ySrc){
  974. dir = -1;
  975. }
  976. _bg.scale = new Vec3(scale_,scale_,scale_)
  977. _qz.scale = new Vec3(scale_,scale_,scale_)
  978. tween(this.moveNode)
  979. .to(0.2, { position:new Vec3(xDst,yDst,0)})
  980. .delay(0.1)
  981. .call(()=>{
  982. if(computerMove){
  983. _bg.scale = new Vec3(scale_,scale_,scale_)
  984. _qz.scale = new Vec3(scale_,scale_,scale_)
  985. }else{
  986. _bg.scale = new Vec3(scale_,scale_*dir,scale_)
  987. _qz.scale = new Vec3(scale_,scale_*dir,scale_)
  988. }
  989. }).delay(0.1)
  990. .call(async ()=>{
  991. _bg.scale = new Vec3(1.1,1.1,1.1)
  992. _qz.scale = new Vec3(1.1,1.1,1.1)
  993. if(computerMove)this_.isComputerMoveing=false;
  994. var _status = this_.postAddMove(mv, computerMove)
  995. if(!computerMove){
  996. if(ChessMng.Instance.isJiFenRoom()||ChessMng.Instance.isHaoyouRoom()){
  997. let setup = new setupData
  998. setup.src_pos = SRC(mv)
  999. setup.dec_pos = DST(mv)
  1000. setup.chess_whole = this.pos.toFen()
  1001. setup.is_mate = _status?0:1
  1002. if(GameMng._userData.room.firstid==1){
  1003. setup.des_who = "我是红方"
  1004. }else{
  1005. setup.des_who = "我是黑方"
  1006. }
  1007. if(GameMng._userData.room!=null){
  1008. if(GameMng._userData.room.rival!=null){
  1009. if(GameMng._userData.room.rival.is_robot===1){
  1010. setup.robot_data = await this.robot_search(setup.chess_whole,setup.is_mate)
  1011. console.log("给人机插入数据","给人机插入数据")
  1012. }else{
  1013. console.log("人机数据插入空值","人机数据插入空值")
  1014. }
  1015. }
  1016. }
  1017. this.send_msg_self_move(setup)
  1018. }
  1019. }
  1020. this_.moveList.splice(0,1)
  1021. if(this_.moveCallfun!=null){
  1022. this_.moveCallfun()
  1023. this_.moveCallfun = null
  1024. }
  1025. this_.moveNode.active=false;
  1026. getNode.active =true;
  1027. })
  1028. .start();
  1029. }
  1030. robot_search(chess_whole,isMate) :Promise<setupData>{
  1031. if(isMate===1){
  1032. let robot_data = new setupData
  1033. robot_data.is_mate = 0
  1034. robot_data.chess_whole = chess_whole
  1035. return
  1036. }
  1037. return new Promise((resolve, reject) => {
  1038. this.scheduleOnce(() => {
  1039. var search = new Search()
  1040. var pos = new ChessPos();
  1041. pos.fromFen(chess_whole);
  1042. search.setSearch(pos,1)
  1043. switch(GameMng._userData.room.rival.robot_ai_lv){
  1044. case robot_ai_level.xiaobai:{
  1045. search.xiaoBaiSearchMain((a)=>{
  1046. director.once(Director.EVENT_AFTER_DRAW,function(){
  1047. let robot_data = new setupData
  1048. robot_data.src_pos = SRC(a)
  1049. robot_data.dec_pos = DST(a)
  1050. if(!pos.makeMove(a)){
  1051. robot_data.src_pos = 0
  1052. robot_data.dec_pos = 0
  1053. }
  1054. robot_data.is_mate = pos.isMate()?1:0
  1055. robot_data.chess_whole = pos.toFen()
  1056. resolve(robot_data)
  1057. })
  1058. });
  1059. return;
  1060. }
  1061. }
  1062. search.searchMain(GameMng._userData.room.rival.robot_ai_lv,400,(a)=>{
  1063. var robot_data = new setupData
  1064. robot_data.src_pos = SRC(a)
  1065. robot_data.dec_pos = DST(a)
  1066. if(!pos.makeMove(a)){
  1067. robot_data.src_pos = 0
  1068. robot_data.dec_pos = 0
  1069. }
  1070. robot_data.is_mate = pos.isMate()?1:0
  1071. robot_data.chess_whole = pos.toFen()
  1072. resolve(robot_data)
  1073. });
  1074. }, 0)
  1075. })
  1076. }
  1077. send_msg_self_move(setup:setupData){
  1078. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  1079. msgManager.sendPlayChess(setup)
  1080. }
  1081. }
  1082. /** 判断一步棋是否合法,如果合法,就执行这步棋。 */
  1083. addMove(mv, computerMove) {
  1084. console.log("addMove",mv)
  1085. var is_legalMove = !this.pos.legalMove(mv)
  1086. if (is_legalMove) {
  1087. console.log("不合法",mv)
  1088. this.moveAni(mv, computerMove,false)
  1089. return false;
  1090. }
  1091. var is_makeMove = !this.pos.makeMove(mv)
  1092. if (is_makeMove) {
  1093. UITips.show("不能送将!");
  1094. this.moveAni(mv, computerMove,false)
  1095. return false;
  1096. }
  1097. this.moveAni(mv, computerMove)
  1098. // var timer = setInterval(function() {
  1099. // if (step == 0) {
  1100. // clearInterval(timer);
  1101. // getNode.x = xSrc;
  1102. // getNode.y = ySrc;
  1103. // if(computerMove)this_.isComputerMoveing=false;
  1104. // this_.postAddMove(mv, computerMove);
  1105. // } else {
  1106. // getNode.x = MOVE_PX(xSrc, xDst, step);
  1107. // getNode.y = MOVE_PX(ySrc, yDst, step);
  1108. // step --;
  1109. // }
  1110. // }, 16);
  1111. return true;
  1112. }
  1113. showHuiheLabel(computerMove){
  1114. // if(this.computer==0)
  1115. // {
  1116. // if(computerMove)
  1117. // this.huihelabel.string="黑方回合";
  1118. // else this.huihelabel.string="红方回合";
  1119. // }
  1120. // else{
  1121. // if(computerMove)
  1122. // this.huihelabel.string="红方回合";
  1123. // else this.huihelabel.string="黑方回合";
  1124. // }
  1125. }
  1126. drawOldAndNewSelect(old_sq,new_sq,isShow){
  1127. let old_node = this.imgSquares[this.flipped(old_sq)];
  1128. let new_node = this.imgSquares[this.flipped(new_sq)];
  1129. if(isShow){
  1130. old_node.getChildByName("track1").active = true;
  1131. old_node.getChildByName("track2").active = false;
  1132. new_node.getChildByName("track1").active = false;
  1133. new_node.getChildByName("track2").active = true;
  1134. }else{
  1135. old_node.getChildByName("track1").active = false;
  1136. old_node.getChildByName("track2").active = false;
  1137. new_node.getChildByName("track1").active = false;
  1138. new_node.getChildByName("track2").active = false;
  1139. }
  1140. }
  1141. postAddMove(mv, computerMove):boolean {
  1142. this.showHuiheLabel(computerMove);
  1143. if (this.mvLast > 0) {
  1144. this.drawSquare(SRC(this.mvLast), false,false);
  1145. this.drawSquare(DST(this.mvLast), false,false);
  1146. this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
  1147. }
  1148. this.drawSquare(SRC(mv), true,false);
  1149. this.drawSquare(DST(mv), true,false);
  1150. this.drawOldAndNewSelect(SRC(mv),DST(mv),true)
  1151. this.sqSelected = 0;
  1152. this.mvLast = mv;
  1153. if (this.pos.isMate()) { //被将死
  1154. this.result = computerMove ? RESULT_LOSS : RESULT_WIN;
  1155. var pc = SIDE_TAG(this.pos.sdPlayer) + PIECE_KING;
  1156. var sqMate = 0;
  1157. for (var sq = 0; sq < 256; sq ++) {
  1158. if (this.pos.squares[sq] == pc) {
  1159. sqMate = sq;
  1160. break;
  1161. }
  1162. }
  1163. if (!this.animated || sqMate == 0) {
  1164. this.postMate(computerMove);
  1165. return false;
  1166. }
  1167. this.postMate(computerMove);
  1168. return false;
  1169. // sqMate = this.flipped(sqMate);
  1170. // var style = this.imgSquares[sqMate].style;
  1171. // style.zIndex = 256;
  1172. // var xMate = SQ_X(sqMate);
  1173. // var step = MAX_STEP;
  1174. // var this_ = this;
  1175. // var timer = setInterval(function() {
  1176. // if (step == 0) {
  1177. // clearInterval(timer);
  1178. // style.left = xMate + "px";
  1179. // style.zIndex = 0;
  1180. // this_.imgSquares[sqMate].src = this_.images +
  1181. // (this_.pos.sdPlayer == 0 ? "r" : "b") + "km.gif";
  1182. // this_.postMate(computerMove);
  1183. // } else {
  1184. // style.left = (xMate + ((step & 1) == 0 ? step : -step) * 2) + "px";
  1185. // step --;
  1186. // }
  1187. // }, 50);
  1188. // return;
  1189. }
  1190. // var vlRep = this.pos.repStatus(3);
  1191. // if (vlRep > 0) {
  1192. // vlRep = this.pos.repValue(vlRep);
  1193. // if (vlRep > -WIN_VALUE && vlRep < WIN_VALUE) {
  1194. // UITips.show("双方不变作和");
  1195. // //this.heqi();
  1196. // } else if (computerMove == (vlRep < 0)) {
  1197. // this.result = RESULT_LOSS;
  1198. // UITips.show("长打作负");
  1199. // //this.postMate(true);
  1200. // } else {
  1201. // this.result = RESULT_WIN;
  1202. // UITips.show("长打作负");
  1203. // //this.postMate(false);
  1204. // }
  1205. // return;
  1206. // }
  1207. if (this.pos.captured()) {
  1208. var hasMaterial = false;
  1209. for (var sq = 0; sq < 256; sq ++) {
  1210. if (IS_INBOARD(sq) && (this.pos.squares[sq] & 7) > 2) {
  1211. hasMaterial = true;
  1212. break;
  1213. }
  1214. }
  1215. if (!hasMaterial) {
  1216. UITips.show("双方都没有进攻棋子了,作和!");
  1217. this.heqi();
  1218. return false;
  1219. }
  1220. } else if (this.pos.pcList.length > 100) {
  1221. var captured = false;
  1222. for (var i = 2; i <= 100; i ++) {
  1223. if (this.pos.pcList[this.pos.pcList.length - i] > 0) {
  1224. captured = true;
  1225. break;
  1226. }
  1227. }
  1228. if (!captured) {
  1229. UITips.show("超过自然限着,作和");
  1230. this.heqi();
  1231. return false;
  1232. }
  1233. }
  1234. if (this.pos.inCheck()) {
  1235. this.FaceKingWarning();
  1236. // this.playSound(computerMove ? "check2" : "check");
  1237. } else if (this.pos.captured()) {
  1238. this.onchiGameAni()
  1239. } else {
  1240. // AudioMng.Instance.PlaySoundByName(AudioType.put)
  1241. }
  1242. this.bushu++;
  1243. this.refreshBushulabel();
  1244. this.response();
  1245. return true;
  1246. }
  1247. refreshBushulabel(){
  1248. // this.bushulabel.string='步数:'+this.bushu;
  1249. }
  1250. //和棋做赢
  1251. heqi(){
  1252. this.result = RESULT_DRAW;
  1253. this.busy = false;
  1254. if(ChessType.renjiboyi === ChessMng.Instance.chesstype){
  1255. this.gameEnd(0,true);
  1256. }else{
  1257. if(GameMng._userData.room.firstid==0){
  1258. msgManager.heqi_game()
  1259. }
  1260. }
  1261. }
  1262. //结束
  1263. postMate(computerMove) {
  1264. console.log("移动的是不是电脑",computerMove,this.result)
  1265. if(ChessType.renjiboyi === ChessMng.Instance.chesstype){
  1266. let result=computerMove?1:0;
  1267. this.gameEnd(result);
  1268. }else{
  1269. if(!computerMove){ //说明电脑被将死
  1270. this.postMateNew();
  1271. }
  1272. }
  1273. this.busy = false;
  1274. }
  1275. postMateNew(){
  1276. console.log("result",this.result)
  1277. //this.gameEnd(this.result);
  1278. this.busy = false;
  1279. }
  1280. setSearch (hashLevel) {
  1281. this.search = hashLevel == 0 ? null : new Search();
  1282. this.search.setSearch(this.pos, hashLevel)
  1283. }
  1284. updateRound(){
  1285. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  1286. if(this.who_round===1){
  1287. this.busy = false;
  1288. }else{
  1289. this.busy = true;
  1290. }
  1291. }
  1292. }
  1293. response() {
  1294. if(GameMng._userData.room!=null){
  1295. console.log("谁的回合",GameMng._userData.room.round)
  1296. }
  1297. if (this.search == null || !this.computerMove()) {
  1298. this.busy = false;
  1299. return;
  1300. }
  1301. // this.thinking.style.visibility = "visible";
  1302. var self = this;
  1303. this.busy = true;
  1304. setTimeout(() => {
  1305. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  1306. if(UIDiffSel.selectIndex==0){
  1307. self.search.xiaoBaiSearchMain((a)=>{
  1308. director.once(Director.EVENT_AFTER_DRAW,function(){
  1309. self.addMove(a,true);
  1310. })
  1311. });
  1312. }else{
  1313. self.search.searchMain(UIDiffSel.selectIndex+1,self.millis,(a)=>{
  1314. director.once(Director.EVENT_AFTER_DRAW,function(){
  1315. self.addMove(a,true);
  1316. });
  1317. });
  1318. }
  1319. }else{
  1320. if(ChessMng.Instance.isHaoyouRoom()){
  1321. console.log("轮到好友走棋子")
  1322. }
  1323. }
  1324. }, 200);
  1325. }
  1326. // ab():Promise<any>{
  1327. // return new Promise<any>(async (resolve, reject) => {
  1328. // let a=this.search.searchMain(LIMIT_DEPTH, this.millis);
  1329. // resolve(a);
  1330. // });
  1331. // }
  1332. computerMove() {
  1333. if(GameMng._userData.room===null||GameMng._userData.room===undefined){
  1334. return this.pos.sdPlayer == this.computer;
  1335. }
  1336. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  1337. }else{
  1338. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  1339. return this.pos.sdPlayer == GameMng._userData.room.firstid;
  1340. }
  1341. }
  1342. return this.pos.sdPlayer == this.computer;
  1343. }
  1344. //刷新棋盘,也就是重新显示棋盘上的棋子。
  1345. flushBoard() {
  1346. this.mvLast = this.pos.mvList[this.pos.mvList.length - 1];
  1347. for (var sq = 0; sq < 256; sq ++) {
  1348. if (IS_INBOARD(sq)) {
  1349. this.drawSquare(sq, sq == SRC(this.mvLast) || sq == DST(this.mvLast),false);
  1350. }
  1351. }
  1352. }
  1353. drawCanMovePath(sqList){
  1354. for(var i=0;i<sqList.length;i++){
  1355. var sq = sqList[i];
  1356. var gnodeSp = this.imgSquares[this.flipped(sq)]
  1357. if(gnodeSp){
  1358. gnodeSp.getChildByName('path').active=true;
  1359. }
  1360. }
  1361. }
  1362. hideAllPath(){
  1363. for (let index = 0; index < this.imgSquares.length; index++) {
  1364. const gnodeSp = this.imgSquares[index];
  1365. if(gnodeSp){
  1366. gnodeSp.getChildByName('path').active=false;
  1367. }
  1368. }
  1369. }
  1370. hideAllTrack(){
  1371. for (let index = 0; index < this.imgSquares.length; index++) {
  1372. const gnodeSp = this.imgSquares[index];
  1373. if(gnodeSp){
  1374. gnodeSp.getChildByName('track1').active=false;
  1375. gnodeSp.getChildByName('track2').active=false;
  1376. }
  1377. }
  1378. }
  1379. //显示sq位置的棋子图片。如果该位置没棋子,则显示一张透明的图片。
  1380. drawSquare(sq, selected,isSelfOpe) {
  1381. var node__ = this.imgSquares[this.flipped(sq)];
  1382. var gnodeSp =node__.getChildByName("chess_bg").getComponent(Sprite) as Sprite;
  1383. var sp =gnodeSp.node.getChildByName("chess_qz").getComponent(Sprite) as Sprite;
  1384. let spName=PIECE_NAME[this.pos.squares[sq]]
  1385. var shadow = node__.getChildByName("chess_put_down_shadow");
  1386. var self = this;
  1387. if(spName=="oo"){
  1388. gnodeSp.enabled=false;
  1389. sp.node.active = false;
  1390. shadow.active = false;
  1391. }
  1392. else{
  1393. ResMng.LoadImg(spName,sp,()=>{
  1394. this.moveNode.active=false;
  1395. gnodeSp.enabled=true;
  1396. shadow.active = true;
  1397. sp.node.active = true;
  1398. },this);
  1399. }
  1400. if(selected){
  1401. if(isSelfOpe){
  1402. // tween()
  1403. // .target(gnodeSp.node)
  1404. // .to(0.1,{ scale: new Vec3(1.1, 1.1, 1.1) })
  1405. // .start();
  1406. if(spName=="oo"){
  1407. }
  1408. else{
  1409. ResMng.LoadImg("s_"+spName,sp,()=>{
  1410. sp.node.getComponent(UITransform).width = 90;
  1411. sp.node.getComponent(UITransform).height = 83;
  1412. sp.node.position = new Vec3(sp.node.position.x,13,sp.node.position.z);
  1413. },this);
  1414. }
  1415. gnodeSp.node.getComponent(UITransform).width = 112;
  1416. gnodeSp.node.getComponent(UITransform).height = 117;
  1417. gnodeSp.spriteFrame = self.select_chess_icon;
  1418. }
  1419. //gnodeSp.node.scale = new Vec3(1.1,1.1,1.1);
  1420. }else{
  1421. //gnodeSp.node.scale = new Vec3(1,1,1)
  1422. sp.node.getComponent(UITransform).width = 80;
  1423. sp.node.getComponent(UITransform).height = 79;
  1424. sp.node.position = new Vec3(sp.node.position.x,10,sp.node.position.z);
  1425. gnodeSp.spriteFrame = self.moveNode.getChildByName("chess_bg").getComponent(Sprite).spriteFrame;
  1426. gnodeSp.node.getComponent(UITransform).width = 104;
  1427. gnodeSp.node.getComponent(UITransform).height = 107;
  1428. }
  1429. // node__.getChildByName('selected').active=selected;
  1430. // img.src = this.images + PIECE_NAME[this.pos.squares[sq]] + ".gif";
  1431. // img.style.backgroundImage = selected ? "url(" + this.images + "oos.gif)" : "";
  1432. }
  1433. //此处判定画什么颜色的象棋
  1434. flipped(sq) {
  1435. return this.computer == 0 ? SQUARE_FLIP(sq) : sq;
  1436. }
  1437. huiqi(isReSelf:boolean){
  1438. this.result = RESULT_UNKNOWN;
  1439. if (this.pos.mvList.length > 1) {
  1440. this.pos.undoMakeMove();
  1441. }
  1442. // if(this.computerMove()){
  1443. // console.log("我方悔棋",this.pos.sdPlayer)
  1444. // }else{
  1445. // console.log("对方悔棋",this.pos.sdPlayer)
  1446. // }
  1447. // if (this.pos.mvList.length > 1 && this.computerMove()) {
  1448. // this.pos.undoMakeMove();
  1449. // }
  1450. this.flushBoard();
  1451. this.hideAllPath();
  1452. console.log("isReSelf",isReSelf)
  1453. if (this.mvLast > 0) {
  1454. this.drawSquare(SRC(this.mvLast), false,false);
  1455. this.drawSquare(DST(this.mvLast), false,false);
  1456. this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
  1457. }
  1458. if(!isReSelf){
  1459. if(this.mv_rival_last>0){
  1460. this.mvLast = this.mv_rival_last;
  1461. this.drawOldAndNewSelect(SRC(this.mv_rival_last),DST(this.mv_rival_last),true)
  1462. }
  1463. }
  1464. this.response();
  1465. console.log("this.mvLast == ",this.mvLast)
  1466. }
  1467. }