TileItem.ts 2.8 KB

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