ui_interact_puzzle.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { _decorator, Component, EventTouch, instantiate, Layout, Node, Size, Sprite, UITransform, Vec2, Vec3 } from 'cc';
  2. import { ui_base } from './ui_base';
  3. import { interact_puzzle_data } from '../../../data/data';
  4. import { tools } from '../../tools';
  5. import { gameManager } from '../gameManager';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('ui_interact_puzzle')
  8. export class ui_interact_puzzle extends ui_base {
  9. @property(Node) puzzle1:Node = null;
  10. @property(Node) puzzle2:Node = null;
  11. @property(Node) puzzle3:Node = null;
  12. @property(Node) puzzle4:Node = null;
  13. @property(Node) puzzle5:Node = null;
  14. @property(Node) puzzle6:Node = null;
  15. @property(Node) content:Node = null;
  16. @property(Node) actionNode:Node = null;
  17. private answer_list:string[] = []
  18. private input_answer_list:string[] = []
  19. private mPuzzleList:Node[] = [];
  20. private mInterctPuzzle:interact_puzzle_data = null;
  21. private mCurSelectDragPuzzle:Node = null;
  22. private mOriginPos:Vec3 = Vec3.ZERO;
  23. protected init(): void {
  24. this.mInterctPuzzle = this.mTopData._interact_puzzle_data;
  25. if(this.mInterctPuzzle===null){
  26. return tools.showToast("设置拼图错误!")
  27. }
  28. this.answer_list= this.mInterctPuzzle.answer.split(",")
  29. this.initPuzzleView()
  30. }
  31. initPuzzleView(){
  32. this.loadBg(this.mInterctPuzzle.bg)
  33. this.content.getComponent(UITransform).setContentSize(new Size(this.mInterctPuzzle.content.width,this.mInterctPuzzle.content.height))
  34. this.content.position = new Vec3(this.mInterctPuzzle.content.x,this.mInterctPuzzle.content.y)
  35. this.puzzle1.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle1.res)
  36. this.puzzle2.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle2.res)
  37. this.puzzle3.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle3.res)
  38. this.puzzle4.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle4.res)
  39. this.puzzle5.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle5.res)
  40. this.puzzle6.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle6.res)
  41. this.actionNode.getComponent(UITransform).contentSize = this.content.getComponent(UITransform).contentSize;
  42. this.mPuzzleList.push(this.puzzle1)
  43. this.mPuzzleList.push(this.puzzle2)
  44. this.mPuzzleList.push(this.puzzle3)
  45. this.mPuzzleList.push(this.puzzle4)
  46. this.mPuzzleList.push(this.puzzle5)
  47. this.mPuzzleList.push(this.puzzle6)
  48. for (let index = 0; index < this.mPuzzleList.length; index++) {
  49. const puzzle = this.mPuzzleList[index];
  50. puzzle.getComponent(UITransform).setContentSize(this.content.getComponent(UITransform).contentSize)
  51. }
  52. this.content.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
  53. if(this.actionNode.children.length>1){
  54. if(this.mCurSelectDragPuzzle!=null){
  55. this.mCurSelectDragPuzzle.position = this.mOriginPos;
  56. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true;
  57. }
  58. this.mCurSelectDragPuzzle = null;
  59. this.actionNode.removeAllChildren()
  60. return
  61. }
  62. this.mCurSelectDragPuzzle = this.checkTouStartWho(et.getUILocation())
  63. if(this.mCurSelectDragPuzzle!=null){
  64. let item = instantiate(this.mCurSelectDragPuzzle)
  65. item.parent = this.actionNode;
  66. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = false;
  67. this.mOriginPos = new Vec3(this.mCurSelectDragPuzzle.position.x, this.mCurSelectDragPuzzle.position.y)
  68. }
  69. })
  70. this.content.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  71. if(this.mCurSelectDragPuzzle!=null){
  72. if(this.actionNode.children.length>0){
  73. let n_p = this.actionNode.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(et.getUILocation().x,et.getUILocation().y))
  74. this.actionNode.children[0].position = new Vec3(this.actionNode.children[0].position.x,n_p.y)
  75. }
  76. }
  77. })
  78. this.content.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
  79. if(this.mCurSelectDragPuzzle!=null){
  80. let drag_item = this.checkTouStartWho(et.getUILocation())
  81. if(drag_item!=null){
  82. let drag_pos = new Vec3(drag_item.position.x,drag_item.position.y)
  83. drag_item.position = this.mOriginPos;
  84. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true;
  85. this.mCurSelectDragPuzzle.position = drag_pos;
  86. // else{
  87. // this.onFialEvent()
  88. // }
  89. }else{
  90. this.mCurSelectDragPuzzle.position = this.mOriginPos;
  91. }
  92. this.mCurSelectDragPuzzle = null;
  93. this.actionNode.removeAllChildren()
  94. let isFinish = this.sortPuzzle()
  95. if(isFinish){
  96. this.onFinishEvent()
  97. return
  98. }
  99. }
  100. })
  101. this.content.on(Node.EventType.TOUCH_CANCEL,()=>{
  102. if(this.mCurSelectDragPuzzle!=null){
  103. this.mCurSelectDragPuzzle.position = this.mOriginPos;
  104. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true;
  105. }
  106. this.mCurSelectDragPuzzle = null;
  107. this.actionNode.removeAllChildren()
  108. })
  109. }
  110. checkTouStartWho(point:Vec2){
  111. for (let index = 0; index < this.mPuzzleList.length; index++) {
  112. const puzzle = this.mPuzzleList[index];
  113. if(puzzle.getComponent(UITransform).getBoundingBoxToWorld().contains(point)){
  114. return puzzle
  115. }
  116. }
  117. return null;
  118. }
  119. //最终按照y坐标排序 计算出是否正确
  120. sortPuzzle(){
  121. let sort_list = this.mPuzzleList.sort((a,b)=>{
  122. return b.position.y- a.position.y
  123. })
  124. this.input_answer_list = []
  125. for (let index = 0; index < sort_list.length; index++) {
  126. const puzzle =sort_list[index];
  127. this.input_answer_list.push(puzzle.name.substring(6,puzzle.name.length))
  128. }
  129. console.log("this.input_answer_list",this.answer_list,this.input_answer_list)
  130. return JSON.stringify(this.input_answer_list)===JSON.stringify(this.answer_list);
  131. }
  132. }