EmptyHoleAction.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. public get_pin_arr(arr:PinAction[] = null): PinAction[] {
  50. if(!arr){
  51. arr = [];
  52. }
  53. this.node.children.forEach(element => {
  54. if (element.getComponent(PinAction)) {
  55. arr.push(element.getComponent(PinAction));
  56. }
  57. });
  58. return arr;
  59. }
  60. public get_pin_arr_by_color_id(color_id:number,pin_arr:PinAction[]){
  61. this.node.children.forEach(element => {
  62. if (element.getComponent(PinAction)) {
  63. let pin_action = element.getComponent(PinAction);
  64. if(pin_action.pin_color&&pin_action.pin_color.id == color_id){
  65. pin_arr.push(pin_action);
  66. }
  67. }
  68. });
  69. }
  70. private get_temp_position(): Vec3 {
  71. //钉子数量
  72. let pin_arr = this.get_pin_arr();
  73. if(pin_arr.length>0){
  74. //有钉子了
  75. return null;
  76. }
  77. return new Vec3(0,0,0);
  78. }
  79. private clear_pins(){
  80. // this.node.children.forEach(element => {
  81. // if (element.getComponent(PinAction)) {
  82. // Tools.clearUI(element);
  83. // }
  84. // });
  85. Tools.clearFromParent(this.node,PinAction);
  86. }
  87. public can_able_put():boolean{
  88. if(this.isLocked){
  89. return false;
  90. }
  91. let target_hole_pos = this.get_temp_position();
  92. if (target_hole_pos) {
  93. return true;
  94. }else{
  95. return false;
  96. }
  97. }
  98. public put_pin(pin: PinAction):boolean {
  99. let target_hole_pos = this.get_temp_position();
  100. if (!target_hole_pos) {
  101. // 没找到孔的点
  102. // console.log("没找到孔的点");
  103. return false;
  104. }
  105. //
  106. let world_pos = pin.node.getWorldPosition();
  107. let target = this.node.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
  108. this.node.addChild(pin.node);
  109. pin.node.setPosition(target);
  110. // let target = find("Canvas").getComponent(UITransform).convertToNodeSpaceAR(hole_world_pos);
  111. pin.remove_collider();
  112. // Global.pin_prgress_computed(1);
  113. tween(pin.node)
  114. .to(0.25, { position: target_hole_pos },Global.our_easing)
  115. .call(() => {
  116. AudioMgr.ins.playSound(Clips.close_screw);
  117. // if (this.get_temp_hole_num()<=0) {
  118. // //如果已经满了。
  119. // this.full_complete();
  120. // }
  121. })
  122. .start();
  123. return true;
  124. }
  125. }