ui_interact_puzzle.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.puzzle1,this.mInterctPuzzle.puzzle1.res)
  36. this.puzzle2.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.puzzle2,this.mInterctPuzzle.puzzle2.res)
  37. this.puzzle3.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.puzzle3,this.mInterctPuzzle.puzzle3.res)
  38. this.puzzle4.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.puzzle4,this.mInterctPuzzle.puzzle4.res)
  39. this.puzzle5.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName( this.puzzle5,this.mInterctPuzzle.puzzle5.res)
  40. this.puzzle6.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.puzzle6,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.off(Node.EventType.TOUCH_START)
  53. this.content.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
  54. if(this.actionNode.children.length>1){
  55. if(this.mCurSelectDragPuzzle!=null){
  56. this.mCurSelectDragPuzzle.position = this.mOriginPos;
  57. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true;
  58. }
  59. this.mCurSelectDragPuzzle = null;
  60. this.actionNode.removeAllChildren()
  61. return
  62. }
  63. this.mCurSelectDragPuzzle = this.checkTouStartWho(et.getUILocation())
  64. if(this.mCurSelectDragPuzzle!=null){
  65. let item = instantiate(this.mCurSelectDragPuzzle)
  66. item.parent = this.actionNode;
  67. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = false;
  68. this.mOriginPos = new Vec3(this.mCurSelectDragPuzzle.position.x, this.mCurSelectDragPuzzle.position.y)
  69. }
  70. })
  71. this.content.off(Node.EventType.TOUCH_MOVE)
  72. this.content.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  73. if(this.mCurSelectDragPuzzle!=null){
  74. if(this.actionNode.children.length>0){
  75. let n_p = this.actionNode.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(et.getUILocation().x,et.getUILocation().y))
  76. this.actionNode.children[0].position = new Vec3(this.actionNode.children[0].position.x,n_p.y)
  77. }
  78. }
  79. })
  80. this.content.off(Node.EventType.TOUCH_END)
  81. this.content.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
  82. if(this.mCurSelectDragPuzzle!=null){
  83. let drag_item = this.checkTouStartWho(et.getUILocation())
  84. if(drag_item!=null){
  85. let drag_pos = new Vec3(drag_item.position.x,drag_item.position.y)
  86. drag_item.position = this.mOriginPos;
  87. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true;
  88. this.mCurSelectDragPuzzle.position = drag_pos;
  89. // else{
  90. // this.onFialEvent()
  91. // }
  92. }else{
  93. this.mCurSelectDragPuzzle.position = this.mOriginPos;
  94. }
  95. this.mCurSelectDragPuzzle = null;
  96. this.actionNode.removeAllChildren()
  97. let isFinish = this.sortPuzzle()
  98. if(isFinish){
  99. this.onFinishEvent()
  100. return
  101. }
  102. }
  103. })
  104. this.content.off(Node.EventType.TOUCH_CANCEL)
  105. this.content.on(Node.EventType.TOUCH_CANCEL,()=>{
  106. if(this.mCurSelectDragPuzzle!=null){
  107. this.mCurSelectDragPuzzle.position = this.mOriginPos;
  108. this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true;
  109. }
  110. this.mCurSelectDragPuzzle = null;
  111. this.actionNode.removeAllChildren()
  112. })
  113. }
  114. checkTouStartWho(point:Vec2){
  115. for (let index = 0; index < this.mPuzzleList.length; index++) {
  116. const puzzle = this.mPuzzleList[index];
  117. if(puzzle.getComponent(UITransform).getBoundingBoxToWorld().contains(point)){
  118. return puzzle
  119. }
  120. }
  121. return null;
  122. }
  123. //最终按照y坐标排序 计算出是否正确
  124. sortPuzzle(){
  125. let sort_list = this.mPuzzleList.sort((a,b)=>{
  126. return b.position.y- a.position.y
  127. })
  128. this.input_answer_list = []
  129. for (let index = 0; index < sort_list.length; index++) {
  130. const puzzle =sort_list[index];
  131. this.input_answer_list.push(puzzle.name.substring(6,puzzle.name.length))
  132. }
  133. console.log("this.input_answer_list",this.answer_list,this.input_answer_list)
  134. return JSON.stringify(this.input_answer_list)===JSON.stringify(this.answer_list);
  135. }
  136. }