Badminton-Scoreboard/assets/Script/AvatarPanel/AvatarPanel.ts

148 lines
4.7 KiB
TypeScript
Raw Permalink Normal View History

2022-05-07 02:54:33 +00:00
import { Badminton } from "../Badminton/Badminton";
import { AvatarColor, AvatarData, MemberData } from "../Badminton/MemberData";
2022-05-02 13:04:23 +00:00
import UIPanel from "../Engine/Component/UIPanel/UIPanel";
2022-05-07 02:54:33 +00:00
import { RandomEx } from "../Engine/Utils/Number/RandomEx";
2022-05-02 13:04:23 +00:00
const { ccclass, property } = cc._decorator;
/** AvatarPanel */
@ccclass
export default class AvatarPanel extends UIPanel {
2022-05-07 02:54:33 +00:00
//#region property
2022-05-02 13:04:23 +00:00
2022-05-07 02:54:33 +00:00
@property({ type: cc.Prefab })
public BtnItem: cc.Prefab = null;
@property({ type: cc.Node })
public ItemContent: cc.Node = null;
@property({ type: cc.Node })
public InfoView: cc.Node = null;
@property({ type: cc.Sprite })
public BG: cc.Sprite = null;
//#endregion
//#region public
public Main: Badminton = null;
//#endregion
//#region private
private _m_currentMember: MemberData = null;
private _avatars: AvatarData[] = null;
private _m_currentIndex: number = null;
//#endregion
//#region Lifecycle
protected ImplementInitial(...initData: any[]): void {
let self: this = this;
this.Main = initData[0];
2022-05-08 12:36:56 +00:00
this._avatars = this.Main.Config.Avatars;
2022-05-07 02:54:33 +00:00
let btnItem: cc.Node = cc.instantiate(this.BtnItem);
btnItem.active = false;
// 載入兔兔設定 更新UI
let container: cc.Node = this.ItemContent;
for (let i: number = container.childrenCount - 1; i >= 0; i--) {
let obj: cc.Node = container.children[i];
if (obj !== btnItem) {
obj.destroy();
}
}
for (let i: number = 0; i < this._avatars.length; i++) {
let data: AvatarData = this._avatars[i];
if (data.color !== AvatarColor.Pink && data.color !== AvatarColor.None) {
continue;
}
let item: cc.Node = container.ExAddChild(btnItem);
item.Find("Avatar/Pic").getComponent(cc.Sprite).spriteFrame = data.Pic;
item.Find("Avatar/BG").getComponent(cc.Sprite).spriteFrame = data.BG;
item.Find("Avatar").on("click", () => { self._showAvatarInfoById(i); });
item.active = true;
}
}
protected *ImplementReadyShow(...param: any[]): IterableIterator<any> {
this._m_currentMember = param[0];
}
protected *ImplementShow(): IterableIterator<any> {
if (this._m_currentMember != null) {
let index: number = 0;
for (let i: number = 0; i < this._avatars.length; i++) {
if (this._m_currentMember.AvatarId === this._avatars[i].ID) {
index = i;
break;
}
}
this._showAvatarInfoById(index);
}
this.Main.Lobby.Close();
}
protected *ImplementHide(...param: any[]): IterableIterator<any> {
yield* this.Main.Lobby.Show();
}
//#endregion
//#region Custom
private _getNextColorAvatarIndex(index: number): number {
if (index < 0 || index >= this._avatars.length) {
return 0;
}
let cur: AvatarData = this._avatars[index];
for (let i: number = 0; i < this._avatars.length; i++) {
let data: AvatarData = this._avatars[i];
if (data.Model === cur.Model && data.color !== cur.color) {
return i;
}
}
return 0;
}
private _showAvatarInfoById(index: number): void {
if (index < 0 || index >= this._avatars.length) {
return;
}
let data: AvatarData = this._avatars[index];
this.BG.spriteFrame = data.BG;
this.InfoView.Find("Image").getComponent(cc.Sprite).spriteFrame = data.BigPic != null ? data.BigPic : data.Pic;
this.InfoView.Find("Title/Text").getComponent(cc.Label).string = data.Name;
this.InfoView.Find("Text").getComponent(cc.Label).string = data.Desc;
this._m_currentIndex = index;
}
public OnClickNextColor(): void {
this._showAvatarInfoById(this._getNextColorAvatarIndex(this._m_currentIndex));
}
public OnClickConfirm(): void {
let data: AvatarData = this._avatars[this._m_currentIndex];
if (data.Model <= 0) {
// let list = this._avatars.FindAll(a => a.model > 0);
// let list: any[] = this._avatars.map(a => {
// if (a.Model > 0) {
// return Object.values(a)[0];
// }
// }).filter(item => item);
let list: AvatarData[] = this._avatars.filter(item => item.Model > 0);
data = list[RandomEx.GetInt(0, list.length)];
}
this._m_currentMember.AvatarId = data.ID;
this.Main.ReloadUI();
this.Close();
}
2022-05-02 13:04:23 +00:00
//#endregion
}