ElementAction.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }
  35. }
  36. //检测没有钉子 就变成传感器
  37. public checkElementChildren() {
  38. if (!this.node) return;
  39. const pins = this.node.getComponentsInChildren(PinComponent)!;
  40. // console.log('检测是否还有钉子:', pins.length)
  41. if (pins.length == 0) {
  42. // console.log("没有钉子 Element刚体变成动力学");
  43. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;
  44. }
  45. }
  46. public get_hole_num(): number {
  47. let hole_num: number = 0;
  48. this.node.children.forEach(element => {
  49. if (element.getComponent(HoleComponent)) {
  50. hole_num++;
  51. }
  52. });
  53. return hole_num;
  54. }
  55. public init_element(group_id: number, ele_color: CarColors) {
  56. this.ele_color = ele_color;
  57. this.node.getComponent(RigidBody2D).group = group_id;
  58. this.node.getComponents(PolygonCollider2D).forEach(element => {
  59. element.group = group_id;
  60. });
  61. //set img color
  62. }
  63. public init_pin(group_id: number, color_pin_arr: CarColors[]) {
  64. //获取 hole
  65. this.node.children.forEach(element => {
  66. if (element.getComponent(HoleComponent)) {
  67. const pin = element.getComponentInChildren(PinComponent)!;
  68. if (pin) {
  69. // pin.init_date(group_id, color_pin_arr.shift(), element.getComponent(HoleComponent));
  70. const index = CarColorsGlobalInstance.instance.carSysterm.pinColorIndex++;
  71. pin.init_date(group_id, color_pin_arr[index], element.getComponent(HoleComponent));
  72. }
  73. }
  74. });
  75. }
  76. public change_pin_color(color: CarColors) {
  77. this.node.children.forEach(element => {
  78. if (element.getComponent(HoleComponent)) {
  79. const pin = element.getComponentInChildren(PinComponent)!;
  80. if (pin) {
  81. pin.pin_color = color;
  82. }
  83. }
  84. });
  85. }
  86. get_pin_color(arr: PinComponent[]) {
  87. this.node.getComponentsInChildren(PinComponent).forEach(pin => {
  88. arr.push(pin);
  89. })
  90. }
  91. public flash_img(t: number = 0.3) {
  92. this.node.children.forEach(element => {
  93. if (element.name == "img") {
  94. if (element.getComponent(UIOpacity)) {
  95. let t: number = 0.3
  96. let opc_1 = 100;
  97. let opc_2 = 200;
  98. element.getComponent(UIOpacity).opacity = 255;
  99. tween(element.getComponent(UIOpacity))
  100. .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
  101. .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
  102. .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
  103. .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
  104. .to(t, { opacity: 255 }, { easing: 'quadInOut' })
  105. .call(() => {
  106. element.getComponent(UIOpacity).opacity = 255;
  107. })
  108. // .start();
  109. }
  110. }
  111. });
  112. }
  113. private set_img_color(_color: CarColors, a: number) {
  114. this.node.children.forEach(element => {
  115. if (element.name == "img" && _color) {
  116. let img = element.getComponent(Sprite);
  117. img.color = new Color().fromHEX(CarColorHex[_color]);
  118. }
  119. });
  120. }
  121. // this this layer is black color,but don't show pin for shows
  122. public set_layer_bg_or_orgin(is_bg: boolean) {
  123. if (is_bg) {
  124. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Static;
  125. this.node.children.forEach(element => {
  126. //默认都不显示
  127. element.active = false;
  128. if (element.name == "img") {
  129. element.active = true;
  130. //只设置图片颜色
  131. this.set_img_color(CarColors.Black, 180);
  132. }
  133. });
  134. } else {
  135. this.node.children.forEach(element => {
  136. //默认都不显示
  137. element.active = true;
  138. });
  139. this.set_img_color(CarColors.SpriteWhite, 190);
  140. this.node.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;
  141. }
  142. }
  143. protected onDestroy(): void {
  144. EventDispatcher.instance.off(GameEvent.EVENT_CHECK_ELEMENT_CHILDREN, this.checkElementChildren);
  145. }
  146. }