GBoardChess.ts 55 KB

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