PinComponent.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 unitNode = currentLayer.parent;
  42. if (!unitNode) return [];
  43. const layers = unitNode.children
  44. .filter((child) => child.name.startsWith('layer_'))
  45. .sort((a, b) => parseInt(a.name.split('_')[1]) - parseInt(b.name.split('_')[1]));
  46. const currentIndex = layers.indexOf(currentLayer);
  47. return layers.slice(currentIndex + 1); // 获取当前层以上的所有层
  48. }
  49. // 获取当前 pin 的包围盒
  50. private getWorldBoundingBox(): Rect | null {
  51. const collider = this.node.getComponent(CircleCollider2D);
  52. if (!collider) return null;
  53. const aabb = collider.worldAABB;
  54. return new Rect(aabb.xMin, aabb.yMin, aabb.width, aabb.height);
  55. }
  56. // 检查是否被遮挡
  57. checkBlocking() {
  58. const pinBoundingBox = this.getWorldBoundingBox();
  59. if (!pinBoundingBox) return;
  60. // 获取当前 pin 所在的 layer
  61. const currentLayer = this.getParentLayer(this.node);
  62. if (!currentLayer) return;
  63. // 获取当前层以上的所有 layer
  64. const higherLayers = this.getHigherLayers(currentLayer);
  65. if (higherLayers.length === 0) return;
  66. // 遍历所有高层 layer 的碰撞组件,判断是否有相交
  67. // this.isBlocked = higherLayers.some((layer) => {
  68. // const colliders = layer.getComponentsInChildren(PolygonCollider2D)!;
  69. // return colliders.some((collider) => {
  70. // const otherBoundingBox = collider.worldAABB;
  71. // return pinBoundingBox.intersects(new Rect(
  72. // otherBoundingBox.xMin,
  73. // otherBoundingBox.yMin,
  74. // otherBoundingBox.width,
  75. // otherBoundingBox.height
  76. // ));
  77. // });
  78. // });
  79. const circleCollider2D = this.node.getComponent(CircleCollider2D);
  80. this.isBlocked = higherLayers.some((layer) => {
  81. const colliders = layer.getComponentsInChildren(PolygonCollider2D)!;
  82. return colliders.some((otherCollider) => {
  83. return GameUtil.isPolygonAndCircleIntersecting(otherCollider, circleCollider2D)
  84. });
  85. });
  86. if (this.isBlocked) {
  87. // console.log(`${CarColorLog[this.pin_color]} 被遮挡了`);
  88. } else {
  89. // console.log(`${CarColorLog[this.pin_color]} 未被遮挡了,可以溜`);
  90. }
  91. }
  92. public init_date(group_id: number, pin_color: CarColors, hole: HoleComponent) {
  93. this.pos_hole = hole;
  94. this.node.getComponent(RigidBody2D).group = group_id;
  95. this._pin_color = pin_color;
  96. //set color
  97. this.reset_img();
  98. }
  99. reset_img() {
  100. if (!this._pin_color) {
  101. console.log(`被return的颜色${this._pin_color}`);
  102. return;
  103. }
  104. this.changeColor();
  105. // this.pin_img.getComponent(Sprite).color = new Color().fromHEX(CarColorHex[this.pin_color]);
  106. }
  107. changeColor() {
  108. this.node.children.forEach(child => {
  109. if (child.name === CarColors[this._pin_color]) {
  110. child.active = true
  111. } else {
  112. child.active = false
  113. }
  114. })
  115. }
  116. protected onDestroy(): void {
  117. }
  118. }