import { BaseEntity, LoopType } from '@uino/thing';
import { Utils } from '../common/Utils';
/**
* @class Door
* 门
* @extends THING.BaseEntity
* @public
*/
class Door extends BaseEntity {
static defaultTagArray = ['Door'];
constructor(param) {
super(param);
this._isOpen = Utils.parseBoolean(param['isOpen'], false);
this._transformCorrection = Utils.parseBoolean(param['transformCorrection'], false);
this._dirIdx = Utils.parseNumber(param['dirIdx'], 0);
}
onLoadComplete() {
super.onLoadComplete();
if (!this._transformCorrection) {
return;
}
const dirIdx = this.external.dirIdx || 0;
const doorDirInfo = _getDoorOpenDirectionInfo(this);
if (!doorDirInfo.isSingle && !doorDirInfo.hasAnimation && !doorDirInfo.hasController) {
this.body.localAngles = [0, 180, 0];
}
if (doorDirInfo.isSingle) {
if (dirIdx == 1) {
this.body.localScale = [-1, 1, 1];
}
else if (dirIdx == 2) {
this.body.localScale = [1, 1, -1];
}
else if (dirIdx == 3) {
this.body.localScale = [-1, 1, -1];
}
}
else {
if (dirIdx == 1) {
this.body.localScale = [-1, 1, -1];
}
}
if (this._isOpen) {
openDoor(this, true);
}
}
/**
* 获取门是否打开
* @type {Boolean}
* @public
*/
get isOpen() {
return this._isOpen;
}
/**
* 开门
* @public
*/
open() {
if (this._isOpen) {
return;
}
openDoor(this, true);
this._isOpen = true;
}
/**
* 关门
* @public
*/
close() {
if (!this._isOpen) {
return;
}
openDoor(this, false);
this._isOpen = false;
}
}
export { Door };
function openDoor(door, isOpen = true) {
let animationName = isOpen ? 'Auto_Open' : 'Auto_Close';
const names = door.animationNames;
if (names.indexOf(animationName) != -1) {
door.playAnimation(animationName);
return;
}
if (isOpen) {
if (names.indexOf('_defaultAnim_') != -1) {
door.playAnimation({ name: "_defaultAnim_", loopType: LoopType.Repeat });
}
}
else {
door.stopAnimation("_defaultAnim_")
}
}
function _getDoorOpenDirectionInfo(door) {
let isSingle = false;
let hasAnimation = false;
let hasController = false;
if (door.animations.length) {
hasAnimation = true;
}
let count = 0;
door.body.traverseNodes((e) => {
if (e.name == 'LeftPiovt' || e.name == 'RightPiovt') {
++count;
}
else if (e.name == 'Controller') {
hasController = true;
}
});
if (count == 1) {
isSingle = true;
}
return { isSingle, hasAnimation, hasController };
}