171 lines
5.8 KiB
C#
171 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BrewMonster.Network;
|
|
using CSNetwork.Protocols;
|
|
using CSNetwork.Protocols.RPCData;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BrewMonster.UI
|
|
{
|
|
/// <summary>
|
|
/// Login Flow:
|
|
/// 1. Enter username and password
|
|
/// 2. Click login button
|
|
/// 3. Login success, get the list of characters
|
|
/// 4. Open the select character screen
|
|
/// </summary>
|
|
public class LoginScreenUI : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_InputField _usernameInputField;
|
|
[SerializeField] private TMP_InputField _passwordInputField;
|
|
[SerializeField] private Button _loginButton;
|
|
[SerializeField] private SelecScreenCharacter _selectCharacterScreen;
|
|
|
|
private List<RoleInfo> _roleInfos;
|
|
bool isDoneWorldRender = false;
|
|
bool isDoneNPCRender = false;
|
|
private SynchronizationContext context;
|
|
|
|
void Start()
|
|
{
|
|
_loginButton.onClick.AddListener(OnLoginButtonClicked);
|
|
context = SynchronizationContext.Current;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (_roleInfos != null)
|
|
{
|
|
_selectCharacterScreen.InitScreen(_roleInfos, OnClickSelectCharacter);
|
|
_roleInfos = null;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
if (Input.GetKeyUp(KeyCode.LeftAlt))
|
|
{
|
|
_usernameInputField.text = "test025";
|
|
_passwordInputField.text = "123456";
|
|
}
|
|
#endif
|
|
}
|
|
|
|
public async void OnLoginButtonClicked()
|
|
{
|
|
Logger.Log("OnLoginButtonClicked");
|
|
string username = _usernameInputField.text;
|
|
string password = _passwordInputField.text;
|
|
UnityGameSession.SetConnectionInfo("103.182.22.52", 29000);
|
|
await UnityGameSession.Login(username, password, OnLoginComplete);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Callback when the login is complete.
|
|
/// Then get the list of characters
|
|
/// </summary>
|
|
private void OnLoginComplete(bool result)
|
|
{
|
|
if (!result)
|
|
{
|
|
Logger.LogError("Login failed");
|
|
return;
|
|
}
|
|
UnityGameSession.GetRoleListAsync(OnGetRoleListComplete);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Callback when the list of characters is retrieved.
|
|
/// Then move to the select character screen
|
|
/// </summary>
|
|
private void OnGetRoleListComplete(List<RoleInfo> roleInfos)
|
|
{
|
|
Logger.Log($"OnGetRoleListComplete {roleInfos.Count}");
|
|
|
|
_roleInfos = roleInfos;
|
|
}
|
|
|
|
private void OnClickSelectCharacter(RoleInfo roleInfo)
|
|
{
|
|
Logger.Log($"OnClickSelectCharacter {roleInfo.name}");
|
|
UnityGameSession.SelectRoleAsync(roleInfo, OnSelectRoleComplete);
|
|
}
|
|
|
|
private void OnSelectRoleComplete(RoleInfo roleInfo)
|
|
{
|
|
context.Post(_ =>
|
|
{
|
|
isDoneWorldRender = false;
|
|
isDoneNPCRender = false;
|
|
Action actLoadChar = () =>
|
|
{
|
|
Debug.Log(" isDoneNPCRender || !isDoneWorldRende.isDoneNPCRender || !isDoneWorldRende(");
|
|
if (!isDoneNPCRender || !isDoneWorldRender)
|
|
{
|
|
return;
|
|
}
|
|
};
|
|
string nameScene = "NPCRender";
|
|
UnityGameSession.Instance.LoadScene(nameScene, LoadSceneMode.Single, (value) =>
|
|
{
|
|
isDoneNPCRender = value;
|
|
actLoadChar?.Invoke();
|
|
});
|
|
UnityGameSession.Instance.LoadScene("MonsterRender", LoadSceneMode.Additive, (value) =>
|
|
{
|
|
isDoneNPCRender = value;
|
|
actLoadChar?.Invoke();
|
|
});
|
|
nameScene = "WorldRender";
|
|
UnityGameSession.Instance.LoadScene(nameScene, LoadSceneMode.Additive, (value) =>
|
|
{
|
|
isDoneWorldRender = value;
|
|
actLoadChar?.Invoke();
|
|
UnityGameSession.EnterWorldAsync(roleInfo, OnEnterWorldComplete);
|
|
|
|
});
|
|
}, null);
|
|
|
|
}
|
|
|
|
private async void OnEnterWorldComplete()
|
|
{
|
|
|
|
await Task.Delay(2000);
|
|
// Request all known packages: 0=Inventory,1=Equipment,2=Task
|
|
UnityGameSession.RequestAllInventoriesAsync(() => { Logger.Log("Sent Inventory Detail Requests (all packs)"); }, 0, 1, 2);
|
|
UnityGameSession.c2s_CmdGetAllData(true, true, false);
|
|
}
|
|
|
|
//private void OnInventoryReceived(List<InventoryItem> inventoryData)
|
|
//{
|
|
// _inventoryUI.DisplayInventory(inventoryData);
|
|
//}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnValidate()
|
|
{
|
|
if (_usernameInputField == null)
|
|
{
|
|
// find childrend with name "username"
|
|
_usernameInputField = transform.Find("username").GetComponent<TMP_InputField>();
|
|
}
|
|
if (_passwordInputField == null)
|
|
{
|
|
// find childrend with name "password"
|
|
_passwordInputField = transform.Find("password").GetComponent<TMP_InputField>();
|
|
}
|
|
if (_loginButton == null)
|
|
{
|
|
// find childrend with name "LoginBtn"
|
|
_loginButton = transform.Find("LoginBtn").GetComponent<Button>();
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
}
|