TileItem.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. isMoving: boolean = false
  9. auto_rotation = false;
  10. collider: Collider[] = [];
  11. rigid: RigidBody = null;
  12. removed:boolean=false
  13. removedPos:Vec3//消除的时候做一个向中间靠拢的动画
  14. playRmovedEff:boolean=false
  15. private static starttime = Date.now();
  16. private static getY() {
  17. let n = Date.now() - this.starttime;
  18. return n / 10;
  19. }
  20. start() {
  21. this.collider = this.node.children[0].getComponents(Collider);
  22. }
  23. //禁用碰撞
  24. _enableCollider: boolean = false;
  25. set enableCollider(b) {
  26. let worldRotation = this.node.children[0].worldRotation.clone();
  27. this.node.worldPosition = this.node.children[0].worldPosition.clone();
  28. this.node.children[0].position = v3();
  29. this.node.children[0].worldRotation = worldRotation;
  30. this.rigid.useGravity = b;
  31. }
  32. get enableCollider() {
  33. return this._enableCollider;
  34. }
  35. destoryCollider() {
  36. if (!this.node || !this.node.isValid) return;
  37. if (!this.node.children[0]) return;
  38. this.node.children[0].position = v3();
  39. this.node.children[0].eulerAngles = v3();
  40. if (this.collider) {
  41. this.collider.forEach(a => {
  42. if (a && a.isValid) a.enabled = false;
  43. });
  44. }
  45. if (this.rigid && this.rigid.isValid) {
  46. this.rigid.destroy();
  47. }
  48. }
  49. addCollider(): RigidBody {
  50. let _render = this.node.children[0].getComponent(MeshRenderer);
  51. this.rigid = _render.node.addComponent(RigidBody);
  52. this.collider.map(a => a.enabled = true)
  53. return this.rigid;
  54. }
  55. update() {
  56. if(this.auto_rotation) {
  57. this.node.setRotationFromEuler(0, TileItem.getY(), 0);
  58. }
  59. }
  60. }