GBoardChess.ts 54 KB

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