TileItem.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { _decorator, Component, RigidBody, Collider, v3, MeshRenderer, MeshCollider, CCObject, Vec2, Vec3, ERigidBodyType } from 'cc';
  2. import { GameNode } from './GameNode';
  3. import { Main } from './Main';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('TileItem')
  6. export class TileItem extends Component {
  7. static TileItem: any;
  8. isMoving: boolean = false
  9. auto_rotation = false;
  10. collider: Collider[] = [];
  11. rigid: RigidBody = null;
  12. removed:boolean=false
  13. removedPos:Vec3//消除的时候做一个向中间靠拢的动画
  14. playRmovedEff:boolean=false
  15. private static starttime = Date.now();
  16. private static getY() {
  17. let n = Date.now() - this.starttime;
  18. return n / 10;
  19. }
  20. start() {
  21. this.collider = this.node.children[0].getComponents(Collider);
  22. }
  23. //禁用碰撞
  24. _enableCollider: boolean = false;
  25. set enableCollider(b) {
  26. let worldRotation = this.node.children[0].worldRotation.clone();
  27. this.node.worldPosition = this.node.children[0].worldPosition.clone();
  28. this.node.children[0].position = v3();
  29. this.node.children[0].worldRotation = worldRotation;
  30. this.rigid.useGravity = b;
  31. // this.collider.enabled = b;
  32. // this.rigid.enabled = b;
  33. }
  34. get enableCollider() {
  35. return this._enableCollider;
  36. }
  37. destoryCollider() {
  38. if (!this.node || !this.node.isValid) return;
  39. if (!this.node.children[0]) return;
  40. this.node.children[0].position = v3();
  41. this.node.children[0].eulerAngles = v3();
  42. if (this.collider) {
  43. this.collider.forEach(a => {
  44. if (a && a.isValid) a.enabled = false;
  45. });
  46. }
  47. if (this.rigid && this.rigid.isValid) {
  48. this.rigid.destroy();
  49. }
  50. }
  51. addCollider(): RigidBody {
  52. let _render = this.node.children[0].getComponent(MeshRenderer);
  53. this.rigid = _render.node.addComponent(RigidBody);
  54. this.collider.map(a => a.enabled = true)
  55. return this.rigid;
  56. }
  57. update() {
  58. if(this.auto_rotation) {
  59. this.node.setRotationFromEuler(0, TileItem.getY(), 0);
  60. }
  61. if(this.rigid.isValid){
  62. /*let plate = Main.I._GameNode.plate;
  63. const gooseWorldPos = this.node.worldPosition;
  64. const plateWorldPos = plate.worldPosition;
  65. // 检测是否超出盘子范围(示例:XZ 平面限制)
  66. const maxX = 2, maxZ = 2; // 盘子半径
  67. if (Math.abs(gooseWorldPos.x - plateWorldPos.x) > maxX ||
  68. Math.abs(gooseWorldPos.z - plateWorldPos.z) > maxZ) {
  69. // 施加反向力(推回盘子中心)
  70. const forceDir = new Vec3(
  71. plateWorldPos.x - gooseWorldPos.x,
  72. 0,
  73. plateWorldPos.z - gooseWorldPos.z
  74. ).normalize().multiplyScalar(10); // 力的大小
  75. this.rigid.applyForce(forceDir);
  76. }*/
  77. }
  78. }
  79. }