squidGame/tgx-games-client/assets/module_arean/scripts/GamePlayerInfo.ts

152 lines
4.8 KiB
TypeScript

import { _decorator, Component, Node, Label, Sprite, director, SpriteFrame } from 'cc';
import { SubGameMgr } from '../../module_basic/scripts/SubGameMgr';
import { UserMgr } from '../../module_basic/scripts/UserMgr';
import { RoomEvent } from '../../module_basic/scripts/GameMgr';
import {ModuleDef} from "db://assets/scripts/ModuleDef";
import {resLoader} from "db://assets/core_tgx/base/utils/ResLoader";
import {EMusicDefine} from "db://assets/module_basic/Define/MusicDefine";
import {tgxAudioMgr} from "db://assets/core_tgx/tgx";
import {MsgUserDataChangedPush} from "db://assets/module_basic/shared/protocols/public/MsgUserDataChangedPush";
const { ccclass, property } = _decorator;
const aiLevelStr = ['初级', '中级', '高级'];
@ccclass('GamePlayerInfo')
export class GamePlayerInfo extends Component {
@property(Label)
userId: Label;
@property(Sprite)
icon: Sprite
@property(Sprite)
iconBg: Sprite
@property(Sprite)
danIcon: Sprite
@property(Node)
readyState: Node;
@property(Node)
mask: Node;
private _defaultIcon;
public _userId: string='';
public seatIndex:number =0;
/** 英雄选择界面回调 */
private _fun : Function = null;
onLoad() {
this._defaultIcon = this.icon.spriteFrame;
}
onDestroy(){
}
public setSeatIndex(index:number){
this.seatIndex = index;
}
protected start(): void {
director.getScene().on(RoomEvent.USER_DATA_CHANGED, (msg: MsgUserDataChangedPush) => {
if (msg.uid != this._userId) {
return;
}
if (!msg.isOnline) {
this.readyState.active = true;
this.readyState.getComponent(Label).string = '离线';
}
else {
this.readyState.active = (!SubGameMgr.gameMgr.isPlaying && SubGameMgr.gameMgr.getUser(this._userId).ready);
if(msg.ready){
this.readyState.getComponent(Label).string = '准备';
}
}
});
}
setAILevel(level: number) {
this.userId.string = '电脑-' + (aiLevelStr[level] || aiLevelStr[0]);
this.icon.spriteFrame = this._defaultIcon;
this.readyState.active = false;
}
setUserId(userId: string) {
this._userId = userId;
console.log("当前房间的玩家id == ",this._userId);
if (!userId) {
this.userId.string = '等待加入...';
this.icon.spriteFrame = this._defaultIcon;
this.readyState.active = false;
if (SubGameMgr.gameMgr.isWatcher) {
this.readyState.active = true;
this.readyState.getComponent(Label).string = '加入';
}
}
else {
UserMgr.inst.setUserIconAndName(userId, this.icon, this.userId, ModuleDef.BASIC).then();
UserMgr.inst.setDanIcon(userId, this.node.getChildByName("left_icon").getChildByName("dan").getComponent(Sprite), ModuleDef.Arean).then();
UserMgr.inst.setTitle(userId,this.node.getChildByName("left_userName").getComponent(Label)).then();
this.readyState.active = false;
this.readyState.active = (!SubGameMgr.gameMgr.isPlaying && SubGameMgr.gameMgr.getUser(userId).ready);
if (this.readyState.active) {
this.readyState.getComponent(Label).string = '准备';
}
}
}
async onClicked() {
// if(UserLocalCache.inst.fightModel != EFightModel.Room){
// return;
// }
// if(this._userId != UserMgr.inst.uid){
// return;
// }
// if (this._userId) {
// let info = await UserMgr.inst.rpc_GetUserInfo(this._userId);
// if (info) {
// tgxUIMgr.inst.showUI(UIUserInfo,null,null,info);
// return;
// }
// }
// if (SubGameMgr.gameMgr.isPlayer) {
// return;
// }
// let ret = await SubGameMgr.gameMgr.rpc_JoinGame();
// if (!ret.isSucc) {
// tgxUIAlert.show("无法加入");
// return;
// }
// tgxSceneUtil.reloadScene();
// SubGameMgr.gameMgr.rpc_Ready();
// console.log("准备1");
}
update(deltaTime: number) {
}
public updateBg() : void{
let sp : SpriteFrame = resLoader.getSpriteFrame("res/RoomBg/cardBg_3",ModuleDef.Arean);
if(sp) this.iconBg.getComponent(Sprite).spriteFrame = sp;
}
public updateHomeOwnerActive(show : boolean) : void{
let img = this.iconBg.node.getChildByName("homeOwner");
if(img) img.active = show;
}
public setOpenChooseHero(fun : Function) : void{
this._fun = fun;
}
public openChooseHero() : void{
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_OPEN);
this._fun && this._fun();
}
}