TileItem.ts 3.4 KB

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