import { _decorator, Component,Node, RigidBody, Collider, v3, MeshRenderer, Vec3, MeshCollider } from 'cc'; const { ccclass, property } = _decorator; @ccclass('TileItem') export class TileItem extends Component { static TileItem: any; oScale: Vec3 = new Vec3(1,1,1); auto_rotation = false; collider: Collider[] = []; rigid: RigidBody = null; removed:boolean=false removedPos:Vec3//消除的时候做一个向中间靠拢的动画 playRmovedEff:boolean = false private static starttime = Date.now(); private static getY() { let n = Date.now() - this.starttime; return n / 10; } protected onLoad(): void { this.oScale = this.node.scale.clone(); this.collider = this.node.children[0].getComponents(Collider); } //禁用碰撞 _enableCollider: boolean = false; set enableCollider(b) { let child: Node = this.node.children[0]; let worldRotation = child.worldRotation.clone(); this.node.worldPosition = child.worldPosition.clone(); child.position = v3(); child.worldRotation = worldRotation; if(!this.rigid){ this.rigid = child.addComponent(RigidBody); this.rigid.type = RigidBody.Type.DYNAMIC; this.rigid.mass = 1; // 适当的质量 this.rigid.linearDamping = 0.1; // 线性阻尼防止过度弹跳 this.rigid.angularDamping = 0.1; // 角阻 } this.rigid.useGravity = b; } get enableCollider() { return this._enableCollider; } destoryCollider() { if (!this.node || !this.node.isValid) return; if (!this.node.children[0]) return; this.node.children[0].position = v3(); this.node.children[0].eulerAngles = v3(); if(this.collider) { this.collider.forEach(a => { if (a && a.isValid) a.enabled = false; }); } this.rigid.enabled = false; } addCollider(): RigidBody { this.collider.map(a => a.enabled = true); //但该节点包含以下不支持的碰撞体形状之一 地形(Terrain) 平面(Plane) 非凸面网格(Non-convex Mesh) let child: Node = this.node.children[0]; let mesh:MeshCollider = child.getComponent(MeshCollider); if(mesh){ mesh.convex = true; } let render = child.getComponent(MeshRenderer); render.shadowCastingMode = MeshRenderer.ShadowCastingMode.ON; if(!this.rigid){ this.rigid = child.addComponent(RigidBody); this.rigid.enabled = true; this.rigid.type = RigidBody.Type.DYNAMIC; this.rigid.mass = 1; // 适当的质量 this.rigid.linearDamping = 0.1; // 线性阻尼防止过度弹跳 this.rigid.angularDamping = 0.1; // 角阻 } this.rigid.useGravity = true; return this.rigid; } //统一的倾斜角度 旋转 private static readonly BASE_TILT = new Vec3(50, 0, 10); update() { if(this.auto_rotation) { this.node.setRotationFromEuler( TileItem.BASE_TILT.x, TileItem.getY(), TileItem.BASE_TILT.z ); //this.node.setRotationFromEuler(0, TileItem.getY(), 0); } } }