PinComponent.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { _decorator, BoxCollider2D, CCBoolean, CircleCollider2D, Collider2D, Color, Node, Component, Contact2DType, Input, IPhysics2DContact, PhysicsSystem2D, PolygonCollider2D, RigidBody2D, Sprite, Rect, Enum, HingeJoint2D, Vec2 } from 'cc';
  2. import { CarColorHex, CarColorLog, CarColors } from '../CarColorsGlobalTypes';
  3. import { HoleComponent } from './HoleComponent';
  4. import { LayerAction } from '../LayerAction';
  5. import { UnitAction } from '../UnitAction';
  6. import { GameUtil } from '../GameUtil';
  7. const { ccclass, property, executeInEditMode } = _decorator;
  8. /** 钉子组件*/
  9. @ccclass('PinComponent')
  10. @executeInEditMode
  11. export class PinComponent extends Component {
  12. @property({ type: Enum(CarColors) })
  13. get pin_color() {
  14. return this._pin_color
  15. }
  16. set pin_color(value) {
  17. this._pin_color = value
  18. this.changeColor()
  19. }
  20. @property({ type: Enum(CarColors) })
  21. private _pin_color: CarColors = CarColors.Purple
  22. isBlocked: boolean = false;
  23. isProcessing: boolean = false;
  24. pos_hole: HoleComponent = null;
  25. start() {
  26. // this.checkBlocking();
  27. }
  28. // 获取当前 pin 所属的 layer 节点
  29. private getParentLayer(node: Node): Node | null {
  30. let current = node.parent;
  31. while (current) {
  32. if (current.parent?.getComponent(UnitAction)) {
  33. return current;
  34. }
  35. current = current.parent!;
  36. }
  37. return null;
  38. }
  39. // 获取当前层以上的所有 layer
  40. private getHigherLayers(currentLayer: Node): Node[] {
  41. const higherLayers: Node[] = [];
  42. const unitNode = currentLayer.parent; // 当前 layer 的父节点是 unit
  43. if (!unitNode) return higherLayers;
  44. // 获取当前 layer 的索引
  45. const currentLayerIndex = this.getLayerIndex(currentLayer.name);
  46. if (isNaN(currentLayerIndex)) return higherLayers;
  47. // 遍历 unit 下的所有 layer,找到比当前层高的 layer
  48. unitNode.children.forEach((layer) => {
  49. const layerIndex = this.getLayerIndex(layer.name);
  50. if (!isNaN(layerIndex) && layerIndex > currentLayerIndex) {
  51. higherLayers.push(layer);
  52. }
  53. });
  54. return higherLayers;
  55. }
  56. // 从 layer 名称中提取索引
  57. private getLayerIndex(layerName: string): number {
  58. const match = layerName.match(/layer_(\d+)/);
  59. return match ? parseInt(match[1], 10) : NaN;
  60. }
  61. // 获取当前 pin 的包围盒
  62. private getWorldBoundingBox(): Rect | null {
  63. const collider = this.node.getComponent(CircleCollider2D);
  64. if (!collider) return null;
  65. const aabb = collider.worldAABB;
  66. return new Rect(aabb.xMin, aabb.yMin, aabb.width, aabb.height);
  67. }
  68. // 检查是否被遮挡
  69. checkBlocking() {
  70. const circleCollider2D = this.node.getComponent(CircleCollider2D);
  71. if (!circleCollider2D) {
  72. // console.error('钉子节点缺少 CircleCollider2D 组件');
  73. return;
  74. }
  75. // 获取当前 pin 所在的 layer
  76. const currentLayer = this.getParentLayer(this.node);
  77. if (!currentLayer) {
  78. console.warn('无法获取当前钉子所在的 layer');
  79. return;
  80. }
  81. // 获取当前层以上的所有 layer
  82. const higherLayers = this.getHigherLayers(currentLayer);
  83. if (higherLayers.length === 0) {
  84. // this.isBlocked = false; // 没有上层 layer,钉子未被阻挡
  85. return;
  86. }
  87. // 检测钉子是否与上层 layer 中的 element 节点碰撞体相交
  88. this.isBlocked = higherLayers.some((layer) => {
  89. const elements = layer.getComponentsInChildren(PolygonCollider2D);
  90. return elements.some((elementCollider) => {
  91. return GameUtil.isPolygonAndCircleIntersecting(elementCollider, circleCollider2D);
  92. });
  93. });
  94. // console.log('钉子颜色:', CarColorLog[this.pin_color], '是否被阻挡:', this.isBlocked);
  95. if (this.isBlocked) {
  96. // console.log(`${CarColorLog[this.pin_color]} 被遮挡了`);
  97. } else {
  98. // console.log(`${CarColorLog[this.pin_color]} 未被遮挡了, 可以溜`);
  99. }
  100. }
  101. public init_date(group_id: number, pin_color: CarColors, hole: HoleComponent) {
  102. this.pos_hole = hole;
  103. this.node.getComponent(RigidBody2D).group = group_id;
  104. this._pin_color = pin_color;
  105. //set color
  106. this.reset_img();
  107. }
  108. reset_img() {
  109. if (!this._pin_color) {
  110. console.log(`被return的颜色${this._pin_color}`);
  111. return;
  112. }
  113. this.changeColor();
  114. // this.pin_img.getComponent(Sprite).color = new Color().fromHEX(CarColorHex[this.pin_color]);
  115. }
  116. changeColor() {
  117. this.node.children.forEach(child => {
  118. if (child.name === CarColors[this._pin_color]) {
  119. child.active = true
  120. } else {
  121. child.active = false
  122. }
  123. })
  124. }
  125. protected onDestroy(): void {
  126. }
  127. }