TileItem.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.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. }
  62. }