ElementAction.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { _decorator, BoxCollider2D, CircleCollider2D, Color, Component, ERigidBody2DType, instantiate, PolygonCollider2D, RigidBody2D, Sprite, tween, UIOpacity, view, Node, isValid, find } from 'cc';
  2. import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
  3. import { CarColorHex, CarColors } from './CarColorsGlobalTypes';
  4. import { HoleComponent } from './Components/HoleComponent';
  5. import { PinComponent } from './Components/PinComponent';
  6. import { GameEvent } from './Enum/GameEvent';
  7. import { ResourcePool } from './ResourcePool';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('ElementAction')
  10. export class ElementAction extends Component {
  11. ele_color: CarColors;
  12. parking: Node;
  13. start() {
  14. this.parking = find('Canvas/Scene/Parkings')!;
  15. EventDispatcher.instance.on(GameEvent.EVENT_CHECK_ELEMENT_CHILDREN, this.checkElementChildren, this);
  16. }
  17. update(deltaTime: number) {
  18. const currentPosition = this.node.getPosition().clone();
  19. const worldPos = this.node.getWorldPosition();
  20. if (worldPos.y <= this.parking.getWorldPosition().y) {
  21. EventDispatcher.instance.emit(GameEvent.EVENT_UPDATE_LAYER);
  22. }
  23. if (currentPosition.y < - view.getVisibleSize().height) {
  24. this.removeElement();
  25. }
  26. }
  27. removeElement() {
  28. if (isValid(this.node)) {
  29. this.node.removeFromParent();
  30. this.node.destroy();
  31. }
  32. }
  33. //检测没有钉子 就变成传感器
  34. public checkElementChildren() {
  35. if (!this.node) return;
  36. const pins = this.node.getComponentsInChildren(PinComponent)!;
  37. // console.log('检测是否还有钉子:', pins.length)
  38. if (pins.length == 0) {
  39. // console.log("没有钉子 Element刚体变成动力学");
  40. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;
  41. }
  42. }
  43. public get_hole_num(): number {
  44. let hole_num: number = 0;
  45. this.node.children.forEach(element => {
  46. if (element.getComponent(HoleComponent)) {
  47. hole_num++;
  48. }
  49. });
  50. return hole_num;
  51. }
  52. public init_element(group_id: number, ele_color: CarColors) {
  53. this.ele_color = ele_color;
  54. this.node.getComponent(RigidBody2D).group = group_id;
  55. this.node.getComponents(PolygonCollider2D).forEach(element => {
  56. element.group = group_id;
  57. });
  58. //set img color
  59. }
  60. public init_pin(group_id: number, color_pin_arr: CarColors[]) {
  61. //获取 hole
  62. this.node.children.forEach(element => {
  63. if (element.getComponent(HoleComponent)) {
  64. // let new_pin = instantiate(ResourcePool.instance.get_prefab("pin"));
  65. // this.node.addChild(new_pin);
  66. // new_pin.setPosition(element.position);
  67. // new_pin.getComponent(PinComponent).init_date(group_id, color_pin_arr.shift(), element.getComponent(HoleComponent));
  68. const pin = element.getComponentInChildren(PinComponent)!;
  69. if (pin) {
  70. pin.init_date(group_id, color_pin_arr.shift(), element.getComponent(HoleComponent));
  71. }
  72. }
  73. });
  74. }
  75. get_pin_color(arr: PinComponent[]) {
  76. this.node.getComponentsInChildren(PinComponent).forEach(pin => {
  77. arr.push(pin);
  78. })
  79. }
  80. public flash_img(t: number = 0.3) {
  81. this.node.children.forEach(element => {
  82. if (element.name == "img") {
  83. if (element.getComponent(UIOpacity)) {
  84. let t: number = 0.3
  85. let opc_1 = 100;
  86. let opc_2 = 200;
  87. element.getComponent(UIOpacity).opacity = 255;
  88. tween(element.getComponent(UIOpacity))
  89. .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
  90. .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
  91. .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
  92. .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
  93. .to(t, { opacity: 255 }, { easing: 'quadInOut' })
  94. .call(() => {
  95. element.getComponent(UIOpacity).opacity = 255;
  96. })
  97. // .start();
  98. }
  99. }
  100. });
  101. }
  102. private set_img_color(_color: CarColors, a: number) {
  103. this.node.children.forEach(element => {
  104. if (element.name == "img" && _color) {
  105. let img = element.getComponent(Sprite);
  106. img.color = new Color().fromHEX(CarColorHex[_color]);
  107. }
  108. });
  109. }
  110. // this this layer is black color,but don't show pin for shows
  111. public set_layer_bg_or_orgin(is_bg: boolean) {
  112. if (is_bg) {
  113. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Static;
  114. this.node.children.forEach(element => {
  115. //默认都不显示
  116. element.active = false;
  117. if (element.name == "img") {
  118. element.active = true;
  119. //只设置图片颜色
  120. this.set_img_color(CarColors.Black, 180);
  121. }
  122. });
  123. } else {
  124. this.node.children.forEach(element => {
  125. //默认都不显示
  126. element.active = true;
  127. });
  128. this.set_img_color(CarColors.SpriteWhite, 190);
  129. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;
  130. }
  131. }
  132. protected onDestroy(): void {
  133. EventDispatcher.instance.off(GameEvent.EVENT_CHECK_ELEMENT_CHILDREN, this.checkElementChildren);
  134. }
  135. }