1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { _decorator, Component, RigidBody, Collider, v3, MeshRenderer, Vec3, MeshCollider } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('TileItem')
- export class TileItem extends Component {
- static TileItem: any;
- 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;
- }
- start() {
- this.collider = this.node.children[0].getComponents(Collider);
- }
- //禁用碰撞
- _enableCollider: boolean = false;
- set enableCollider(b) {
- let worldRotation = this.node.children[0].worldRotation.clone();
- this.node.worldPosition = this.node.children[0].worldPosition.clone();
- this.node.children[0].position = v3();
- this.node.children[0].worldRotation = worldRotation;
- 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;
- });
- }
- if (this.rigid && this.rigid.isValid) {
- this.rigid.destroy();
- }
- }
-
- addCollider(): RigidBody {
- this.collider.map(a => a.enabled = true);
- //但该节点包含以下不支持的碰撞体形状之一:
- //地形(Terrain) 平面(Plane) 非凸面网格(Non-convex Mesh)
- const meshCollider = this.node.children[0].getComponent(MeshCollider);
- if(meshCollider) {
- meshCollider.convex = true;
- }
- if(this.rigid && this.rigid.isValid) {
- this.rigid.destroy();
- }
- let _render = this.node.children[0].getComponent(MeshRenderer);
- _render.shadowCastingMode = MeshRenderer.ShadowCastingMode.ON;
- this.rigid = _render.node.addComponent(RigidBody);
- this.rigid.type = RigidBody.Type.DYNAMIC;
- this.rigid.mass = 1; // 适当的质量
- this.rigid.linearDamping = 0.1; // 线性阻尼防止过度弹跳
- this.rigid.angularDamping = 0.1; // 角阻
- 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);
- }
- }
- }
|