EmptyHoleAction.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { _decorator, Component, EventTouch, Input, Node, tween, UITransform, Vec3 } from 'cc';
  2. import { AudioMgr } from './AudioMgr';
  3. import { Clips } from './Enums';
  4. import { Global } from './Global';
  5. import { PinAction } from './PinAction';
  6. import { Tools } from './Tools';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('EmptyHoleAction')
  9. export class EmptyHoleAction extends Component {
  10. @property({type:Node})
  11. lock:Node;
  12. defalut_lock:boolean = false;//默认不锁
  13. isLocked:boolean = false;
  14. onLoad(): void {
  15. //显示锁头
  16. this.defalut_lock = this.lock.active;
  17. }
  18. start() {
  19. // this.node.on(Input.EventType.TOUCH_START, this.unlock_hole, this);
  20. }
  21. set_lock_unlock(l:boolean){
  22. this.isLocked = l;
  23. if(this.lock){
  24. tween(this.lock)
  25. .to(0.25,{scale:new Vec3(1.2,1.2,1)},Global.our_easing)
  26. .to(0.45,{scale:new Vec3(0.8,0.8,1)},Global.our_easing)
  27. .to(0.25,{scale:new Vec3(1,1,1)},Global.our_easing)
  28. .call(()=>{
  29. this.lock.scale = new Vec3(1,1,1);
  30. this.lock.active = l;
  31. })
  32. .start();
  33. }
  34. }
  35. init_empty_hole(){
  36. this.set_lock_unlock(this.defalut_lock);
  37. this.clear_pins();
  38. }
  39. unlock_hole(e: EventTouch) {
  40. // console.log("开始。。。empty ",this.isLocked);
  41. if(!this.isLocked){
  42. return;
  43. }
  44. this.set_lock_unlock(false);
  45. }
  46. update(deltaTime: number) {
  47. }
  48. /**
  49. * 获取钉子
  50. * @param arr
  51. * @returns
  52. */
  53. public get_pin_arr(arr:PinAction[] = null): PinAction[] {
  54. if(!arr){
  55. arr = [];
  56. }
  57. this.node.children.forEach(element => {
  58. if (element.getComponent(PinAction)) {
  59. arr.push(element.getComponent(PinAction));
  60. }
  61. });
  62. return arr;
  63. }
  64. /**
  65. *
  66. * @param color_id
  67. * @param pin_arr
  68. */
  69. public get_pin_arr_by_color_id(color_id:number,pin_arr:PinAction[]){
  70. this.node.children.forEach(element => {
  71. if (element.getComponent(PinAction)) {
  72. let pin_action = element.getComponent(PinAction);
  73. if(pin_action.pin_color&&pin_action.pin_color.id == color_id){
  74. pin_arr.push(pin_action);
  75. }
  76. }
  77. });
  78. }
  79. /**
  80. *
  81. * @returns
  82. */
  83. private get_temp_position(): Vec3 {
  84. //钉子数量
  85. let pin_arr = this.get_pin_arr();
  86. if(pin_arr.length>0){
  87. //有钉子了
  88. return null;
  89. }
  90. return new Vec3(0,0,0);
  91. }
  92. private clear_pins(){
  93. // this.node.children.forEach(element => {
  94. // if (element.getComponent(PinAction)) {
  95. // Tools.clearUI(element);
  96. // }
  97. // });
  98. Tools.clearFromParent(this.node,PinAction);
  99. }
  100. public can_able_put():boolean{
  101. if(this.isLocked){
  102. return false;
  103. }
  104. let target_hole_pos = this.get_temp_position();
  105. if (target_hole_pos) {
  106. return true;
  107. }else{
  108. return false;
  109. }
  110. }
  111. /**
  112. *
  113. * @param pin
  114. * @returns
  115. */
  116. public put_pin(pin: PinAction):boolean {
  117. let target_hole_pos = this.get_temp_position();
  118. if (!target_hole_pos) {
  119. // 没找到孔的点
  120. // console.log("没找到孔的点");
  121. return false;
  122. }
  123. //
  124. let world_pos = pin.node.getWorldPosition();
  125. let target = this.node.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
  126. this.node.addChild(pin.node);
  127. pin.node.setPosition(target);
  128. // let target = find("Canvas").getComponent(UITransform).convertToNodeSpaceAR(hole_world_pos);
  129. pin.remove_collider();
  130. // Global.pin_prgress_computed(1);
  131. tween(pin.node)
  132. .to(0.25, { position: target_hole_pos },Global.our_easing)
  133. .call(() => {
  134. AudioMgr.ins.playSound(Clips.close_screw);
  135. // if (this.get_temp_hole_num()<=0) {
  136. // //如果已经满了。
  137. // this.full_complete();
  138. // }
  139. })
  140. .start();
  141. return true;
  142. }
  143. }