PinAction.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { _decorator, BoxCollider2D, CircleCollider2D, Color, Component, EventTouch, find, Input, instantiate, Node, PolygonCollider2D, Rect, RigidBody2D, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { ElementAction } from './ElementAction';
  3. import { LayerRootAction } from './LayerRootAction';
  4. import { Clips } from './Enums';
  5. import { AudioMgr } from './AudioMgr';
  6. import { LayerCatAction } from './LayerCatAction';
  7. import { ColorDate } from './ColorDate';
  8. import { Global } from './Global';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('PinAction')
  11. export class PinAction extends Component {
  12. pin_color: ColorDate = null;
  13. start() {
  14. this.node.on(Input.EventType.TOUCH_START, this.touch_start, this);
  15. }
  16. update(deltaTime: number) {
  17. }
  18. /**
  19. *
  20. * @param group_id
  21. * @param pin_color
  22. */
  23. public init_date(group_id: number, pin_color: ColorDate) {
  24. this.node.getComponent(RigidBody2D).group = group_id;
  25. this.node.getComponents(BoxCollider2D).forEach(element => {
  26. element.group = group_id;
  27. });
  28. this.node.getComponents(CircleCollider2D).forEach(element => {
  29. element.group = group_id;
  30. });
  31. this.node.getComponents(PolygonCollider2D).forEach(element => {
  32. element.group = group_id;
  33. });
  34. this.pin_color = pin_color;
  35. //set color
  36. this.reset_img();
  37. }
  38. /**
  39. *
  40. * @param e
  41. * @returns
  42. */
  43. touch_start(e: EventTouch) {
  44. AudioMgr.ins.playSound(Clips.open_screw);
  45. // ret?boolean ,return put result,true=success,false=failure
  46. if (Global.layer_cat_action.put_pin(this)) {
  47. // 需要检查,关卡是否结束
  48. return;
  49. }
  50. Global.layer_empty_action.put_pin(this);
  51. if (Global.layer_empty_action.is_pin_full()) {
  52. //TODO 游戏结束,这里有个bug,用延迟处理,后面处理
  53. tween(this.node).delay(0.75).call(() => {
  54. if (Global.layer_empty_action.is_pin_full()) {
  55. Global.game_over_action.open();
  56. } else {
  57. //console.log("$FFFFFFFFFFFFF");
  58. }
  59. }).start();
  60. }
  61. }
  62. public remove_collider() {
  63. this.node.getComponent(RigidBody2D).enabled = false;
  64. this.node.getComponent(CircleCollider2D).enabled = false;
  65. this.node.off(Input.EventType.TOUCH_START);
  66. }
  67. /**
  68. *
  69. * @returns void
  70. */
  71. reset_img() {
  72. if (!this.pin_color) {
  73. return;
  74. }
  75. let img = this.node.getComponent(Sprite);
  76. img.color = new Color(this.pin_color.r, this.pin_color.g, this.pin_color.b, 255);
  77. let holering = this.node.getChildByName("holering")
  78. if (holering) {
  79. tween(holering)
  80. .to(0.35, { scale: new Vec3(1.2, 1.2, 1) }, Global.our_easing)
  81. .to(0.35, { scale: new Vec3(0.8, 0.8, 1) }, Global.our_easing)
  82. .to(0.25, { scale: new Vec3(1, 1, 1) }, Global.our_easing)
  83. .call(() => {
  84. holering.scale = new Vec3(1, 1, 1);
  85. })
  86. .start();
  87. }
  88. }
  89. // get_pin_color(arr:ColorDate[]){
  90. // if(!this.pin_color){
  91. // return;
  92. // }
  93. // arr.push(this.pin_color);
  94. // }
  95. }