ElementAction.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.children.forEach(pin_node => {
  77. // pin_node.getComponent(PinComponent)?.get_pin_color(arr);
  78. let pin_action = pin_node.getComponent(PinComponent)
  79. if (pin_action && pin_action.pin_color) {
  80. arr.push(pin_node.getComponent(PinComponent));
  81. }
  82. });
  83. }
  84. public flash_img(t: number = 0.3) {
  85. this.node.children.forEach(element => {
  86. if (element.name == "img") {
  87. if (element.getComponent(UIOpacity)) {
  88. let t: number = 0.3
  89. let opc_1 = 100;
  90. let opc_2 = 200;
  91. element.getComponent(UIOpacity).opacity = 255;
  92. tween(element.getComponent(UIOpacity))
  93. .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
  94. .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
  95. .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
  96. .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
  97. .to(t, { opacity: 255 }, { easing: 'quadInOut' })
  98. .call(() => {
  99. element.getComponent(UIOpacity).opacity = 255;
  100. })
  101. // .start();
  102. }
  103. }
  104. });
  105. }
  106. private set_img_color(_color: CarColors, a: number) {
  107. this.node.children.forEach(element => {
  108. if (element.name == "img" && _color) {
  109. let img = element.getComponent(Sprite);
  110. img.color = new Color().fromHEX(CarColorHex[_color]);
  111. }
  112. });
  113. }
  114. // this this layer is black color,but don't show pin for shows
  115. public set_layer_bg_or_orgin(is_bg: boolean) {
  116. if (is_bg) {
  117. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Static;
  118. this.node.children.forEach(element => {
  119. //默认都不显示
  120. element.active = false;
  121. if (element.name == "img") {
  122. element.active = true;
  123. //只设置图片颜色
  124. this.set_img_color(CarColors.Black, 180);
  125. }
  126. });
  127. } else {
  128. this.node.children.forEach(element => {
  129. //默认都不显示
  130. element.active = true;
  131. });
  132. this.set_img_color(CarColors.SpriteWhite, 190);
  133. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;
  134. }
  135. }
  136. protected onDestroy(): void {
  137. EventDispatcher.instance.off(GameEvent.EVENT_CHECK_ELEMENT_CHILDREN, this.checkElementChildren);
  138. }
  139. }