116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
|
|
import LocalStorageData from "../Engine/Data/LocalStorageData";
|
|
import { Badminton } from "./Badminton";
|
|
import { AvatarData, AvatarSettings, GameConfig, MemberData } from "./MemberData";
|
|
|
|
/** ConfigManager */
|
|
export default class ConfigManager {
|
|
|
|
//#region get
|
|
|
|
public Main: Badminton = null;
|
|
|
|
//#endregion
|
|
|
|
//#region public
|
|
|
|
public Avatars: AvatarData[] = [];
|
|
|
|
//#endregion
|
|
|
|
//#region Lifecycle
|
|
|
|
constructor(mainControl: Badminton) {
|
|
this.Main = mainControl;
|
|
}
|
|
|
|
public Init(): void {
|
|
this.LoadMemberList();
|
|
this.LoadAvatarSettings();
|
|
}
|
|
|
|
public LoadMemberList(): void {
|
|
let teamMemberList: MemberData[] = this.Main.TeamMemberList;
|
|
if (teamMemberList == null) {
|
|
this.Main.TeamMemberList = teamMemberList = [];
|
|
}
|
|
let request: GameConfig = (<GameConfig>JSON.parse(LocalStorageData.Instance.GameConfig));
|
|
if (request != null) {
|
|
let members: string[] = teamMemberList.map(m => Object.values(m)[0]);
|
|
|
|
for (let i: number = 0; i < request.Members.length; i++) {
|
|
if (!members.includes(request.Members[i])) {
|
|
teamMemberList.push(new MemberData(request.Members[i]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public LoadAvatarSettings(): void {
|
|
let request: AvatarSettings = (<AvatarSettings>JSON.parse(LocalStorageData.Instance.AvatarSettings));
|
|
if (request != null) {
|
|
this.Avatars = request.Avatars;
|
|
}
|
|
}
|
|
|
|
//#endregion
|
|
|
|
//#region Get
|
|
|
|
public GetAvatarPicById(avatarId: number): cc.SpriteFrame {
|
|
for (let i: number = 0; i < this.Avatars.length; i++) {
|
|
if (this.Avatars[i].ID === avatarId) {
|
|
return this.Avatars[i].Pic;
|
|
}
|
|
}
|
|
return this.Avatars[this.Avatars.length - 1].Pic;
|
|
}
|
|
|
|
public GetAvatarPicByName(playerName: string): cc.SpriteFrame {
|
|
let teamMemberList: MemberData[] = this.Main.TeamMemberList;
|
|
let avatarId: number = 0;
|
|
for (let i: number = 0; i < teamMemberList.length; i++) {
|
|
if (teamMemberList[i].Name === playerName) {
|
|
avatarId = teamMemberList[i].AvatarId;
|
|
break;
|
|
}
|
|
}
|
|
for (let i: number = 0; i < this.Avatars.length; i++) {
|
|
if (this.Avatars[i].ID === avatarId) {
|
|
return this.Avatars[i].Pic;
|
|
}
|
|
}
|
|
|
|
return this.Avatars[this.Avatars.length - 1].Pic;
|
|
}
|
|
|
|
public GetMemberDataByName(playerName: string): MemberData {
|
|
let teamMemberList: MemberData[] = this.Main.TeamMemberList;
|
|
for (let i: number = 0; i < teamMemberList.length; i++) {
|
|
if (teamMemberList[i].Name === playerName) {
|
|
return teamMemberList[i];
|
|
}
|
|
}
|
|
return this.Main.defaultMember;
|
|
}
|
|
|
|
public GetAvatarDataByName(playerName: string): AvatarData {
|
|
let teamMemberList: MemberData[] = this.Main.TeamMemberList;
|
|
let avatarId: number = 0;
|
|
for (let i: number = 0; i < teamMemberList.length; i++) {
|
|
if (teamMemberList[i].Name === playerName) {
|
|
avatarId = teamMemberList[i].AvatarId;
|
|
break;
|
|
}
|
|
}
|
|
for (let i: number = 0; i < this.Avatars.length; i++) {
|
|
if (this.Avatars[i].ID === avatarId) {
|
|
return this.Avatars[i];
|
|
}
|
|
}
|
|
|
|
return this.Avatars[0];
|
|
}
|
|
|
|
//#endregion
|
|
} |