TileItem.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { _decorator, Component, RigidBody, Collider, v3, MeshRenderer, Vec3 } from 'cc';
  2. import { NewPackOpenedLayer } from '../gameui/NewPackOpenedLayer';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('TileItem')
  5. export class TileItem extends Component {
  6. static TileItem: any;
  7. //预制体的原始大小
  8. oScale: Vec3 = new Vec3(1,1,1);
  9. isMoving: boolean = false
  10. auto_rotation = false;
  11. collider: Collider[] = [];
  12. rigid: RigidBody = null;
  13. removed:boolean=false
  14. removedPos:Vec3//消除的时候做一个向中间靠拢的动画
  15. playRmovedEff:boolean=false
  16. private static starttime = Date.now();
  17. private static getY() {
  18. let n = Date.now() - this.starttime;
  19. return n / 10;
  20. }
  21. start() {
  22. this.oScale = this.node.scale.clone();
  23. this.collider = this.node.children[0].getComponents(Collider);
  24. }
  25. //禁用碰撞
  26. _enableCollider: boolean = false;
  27. set enableCollider(b) {
  28. let worldRotation = this.node.children[0].worldRotation.clone();
  29. this.node.worldPosition = this.node.children[0].worldPosition.clone();
  30. this.node.children[0].position = v3();
  31. this.node.children[0].worldRotation = worldRotation;
  32. this.rigid.useGravity = 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.rigid.type = RigidBody.Type.DYNAMIC;
  55. this.collider.map(a => a.enabled = true)
  56. return this.rigid;
  57. }
  58. update() {
  59. if(this.auto_rotation) {
  60. this.node.setRotationFromEuler(0, TileItem.getY(), 0);
  61. }
  62. }
  63. }