ElementAction.ts 6.2 KB

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