TileItem.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { _decorator, Component, RigidBody, Collider, v3, MeshRenderer, Vec3, MeshCollider } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('TileItem')
  4. export class TileItem extends Component {
  5. static TileItem: any;
  6. auto_rotation = false;
  7. collider: Collider[] = [];
  8. rigid: RigidBody = null;
  9. removed:boolean=false
  10. removedPos:Vec3//消除的时候做一个向中间靠拢的动画
  11. playRmovedEff:boolean = false
  12. private static starttime = Date.now();
  13. private static getY() {
  14. let n = Date.now() - this.starttime;
  15. return n / 10;
  16. }
  17. start() {
  18. this.collider = this.node.children[0].getComponents(Collider);
  19. }
  20. //禁用碰撞
  21. _enableCollider: boolean = false;
  22. set enableCollider(b) {
  23. let worldRotation = this.node.children[0].worldRotation.clone();
  24. this.node.worldPosition = this.node.children[0].worldPosition.clone();
  25. this.node.children[0].position = v3();
  26. this.node.children[0].worldRotation = worldRotation;
  27. this.rigid.useGravity = b;
  28. }
  29. get enableCollider() {
  30. return this._enableCollider;
  31. }
  32. destoryCollider() {
  33. if (!this.node || !this.node.isValid) return;
  34. if (!this.node.children[0]) return;
  35. this.node.children[0].position = v3();
  36. this.node.children[0].eulerAngles = v3();
  37. if (this.collider) {
  38. this.collider.forEach(a => {
  39. if (a && a.isValid) a.enabled = false;
  40. });
  41. }
  42. if (this.rigid && this.rigid.isValid) {
  43. this.rigid.destroy();
  44. }
  45. }
  46. addCollider(): RigidBody {
  47. this.collider.map(a => a.enabled = true);
  48. //但该节点包含以下不支持的碰撞体形状之一:
  49. //地形(Terrain) 平面(Plane) 非凸面网格(Non-convex Mesh)
  50. const meshCollider = this.node.children[0].getComponent(MeshCollider);
  51. if(meshCollider) {
  52. meshCollider.convex = true;
  53. }
  54. if(this.rigid && this.rigid.isValid) {
  55. this.rigid.destroy();
  56. }
  57. let _render = this.node.children[0].getComponent(MeshRenderer);
  58. _render.shadowCastingMode = MeshRenderer.ShadowCastingMode.ON;
  59. this.rigid = _render.node.addComponent(RigidBody);
  60. this.rigid.type = RigidBody.Type.DYNAMIC;
  61. this.rigid.mass = 1; // 适当的质量
  62. this.rigid.linearDamping = 0.1; // 线性阻尼防止过度弹跳
  63. this.rigid.angularDamping = 0.1; // 角阻
  64. return this.rigid;
  65. }
  66. //统一的倾斜角度 旋转
  67. private static readonly BASE_TILT = new Vec3(50, 0, 10);
  68. update() {
  69. if(this.auto_rotation) {
  70. this.node.setRotationFromEuler(
  71. TileItem.BASE_TILT.x,
  72. TileItem.getY(),
  73. TileItem.BASE_TILT.z
  74. );
  75. //this.node.setRotationFromEuler(0, TileItem.getY(), 0);
  76. }
  77. }
  78. }