218 lines
7.2 KiB
C#
218 lines
7.2 KiB
C#
using System;
|
|
using System.Text;
|
|
using BrewMonster.Network;
|
|
using CSNetwork;
|
|
using CSNetwork.GPDataType;
|
|
using CSNetwork.Protocols;
|
|
using CSNetwork.Protocols.RPCData;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using BrewMonster.Scripts;
|
|
|
|
namespace BrewMonster.UI
|
|
{
|
|
/// <summary>
|
|
/// UI screen for creating a new character.
|
|
/// Equivalent to CDlgCreateGenderName + CDlgCreateProfession in C++.
|
|
/// </summary>
|
|
public class CreateCharacterScreen : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject professionSelectionPanel;
|
|
[SerializeField] private Button[] professionButtons;
|
|
[SerializeField] private Button maleGenderButton;
|
|
[SerializeField] private Button femaleGenderButton;
|
|
[SerializeField] private TMP_InputField nameInputField;
|
|
[SerializeField] private Button confirmButton;
|
|
[SerializeField] private Button cancelButton;
|
|
[SerializeField] private Button backButton;
|
|
|
|
private int _currentProfession = -1;
|
|
private int _currentGender = -1;
|
|
|
|
private Action<RoleInfo> _onCreateComplete;
|
|
private Action _onCancel;
|
|
|
|
private void Start()
|
|
{
|
|
if (confirmButton != null)
|
|
confirmButton.onClick.AddListener(OnConfirmClicked);
|
|
if (cancelButton != null)
|
|
cancelButton.onClick.AddListener(OnCancelClicked);
|
|
if (backButton != null)
|
|
backButton.onClick.AddListener(OnCancelClicked);
|
|
|
|
if (maleGenderButton != null)
|
|
maleGenderButton.onClick.AddListener(() => OnGenderSelected(GENDER.GENDER_MALE));
|
|
if (femaleGenderButton != null)
|
|
femaleGenderButton.onClick.AddListener(() => OnGenderSelected(GENDER.GENDER_FEMALE));
|
|
|
|
// Setup profession buttons
|
|
if (professionButtons != null)
|
|
{
|
|
for (int i = 0; i < professionButtons.Length && i < 12; i++)
|
|
{
|
|
int prof = i; // Capture for closure
|
|
if (professionButtons[i] != null)
|
|
professionButtons[i].onClick.AddListener(() => OnProfessionSelected(prof));
|
|
}
|
|
}
|
|
|
|
if (nameInputField != null)
|
|
{
|
|
nameInputField.onSubmit.AddListener((text) => { if (CanConfirm()) OnConfirmClicked(); });
|
|
}
|
|
}
|
|
|
|
public void Show(Action<RoleInfo> onCreateComplete, Action onCancel)
|
|
{
|
|
_onCreateComplete = onCreateComplete;
|
|
_onCancel = onCancel;
|
|
_currentProfession = -1;
|
|
_currentGender = -1;
|
|
|
|
gameObject.SetActive(true);
|
|
|
|
if (nameInputField != null)
|
|
{
|
|
nameInputField.text = "";
|
|
nameInputField.Select();
|
|
}
|
|
|
|
UpdateConfirmButtonState();
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnProfessionSelected(int profession)
|
|
{
|
|
if (profession < 0 || profession >= (int)Profession.NUM_PROFESSION)
|
|
return;
|
|
|
|
_currentProfession = profession;
|
|
|
|
// Update UI to show selected profession
|
|
if (professionButtons != null)
|
|
{
|
|
for (int i = 0; i < professionButtons.Length; i++)
|
|
{
|
|
if (professionButtons[i] != null)
|
|
{
|
|
// Visual feedback for selected profession
|
|
var colors = professionButtons[i].colors;
|
|
colors.normalColor = (i == profession) ? Color.yellow : Color.white;
|
|
professionButtons[i].colors = colors;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Auto-select first available gender for this profession if not selected
|
|
if (_currentGender == -1)
|
|
{
|
|
// Default to male if available, otherwise female
|
|
OnGenderSelected(GENDER.GENDER_MALE);
|
|
}
|
|
|
|
UpdateConfirmButtonState();
|
|
}
|
|
|
|
private void OnGenderSelected(int gender)
|
|
{
|
|
if (gender != GENDER.GENDER_MALE && gender != GENDER.GENDER_FEMALE)
|
|
return;
|
|
|
|
_currentGender = gender;
|
|
|
|
// Update UI to show selected gender
|
|
if (maleGenderButton != null)
|
|
{
|
|
var colors = maleGenderButton.colors;
|
|
colors.normalColor = (gender == GENDER.GENDER_MALE) ? Color.yellow : Color.white;
|
|
maleGenderButton.colors = colors;
|
|
}
|
|
|
|
if (femaleGenderButton != null)
|
|
{
|
|
var colors = femaleGenderButton.colors;
|
|
colors.normalColor = (gender == GENDER.GENDER_FEMALE) ? Color.yellow : Color.white;
|
|
femaleGenderButton.colors = colors;
|
|
}
|
|
|
|
UpdateConfirmButtonState();
|
|
}
|
|
|
|
private void OnConfirmClicked()
|
|
{
|
|
if (!CanConfirm())
|
|
return;
|
|
|
|
string characterName = nameInputField != null ? nameInputField.text : "";
|
|
if (string.IsNullOrWhiteSpace(characterName))
|
|
{
|
|
Debug.LogWarning("Character name cannot be empty");
|
|
return;
|
|
}
|
|
|
|
// Create RoleInfo using helper method
|
|
RoleInfo roleInfo = GameSession.CreateNewRoleInfo(characterName, _currentProfession, _currentGender);
|
|
|
|
// Create role via network
|
|
Debug.Log($"Calling CreateRoleAsync for character: {characterName}, profession: {_currentProfession}, gender: {_currentGender}");
|
|
UnityGameSession.CreateRoleAsync(roleInfo, new Octets(), (createdRole) =>
|
|
{
|
|
if (createdRole != null)
|
|
{
|
|
Debug.Log($"Character created successfully: {characterName}, RoleID: {createdRole.roleid}");
|
|
_onCreateComplete?.Invoke(createdRole);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Failed to create character: {characterName}. Check GameSession logs for error details.");
|
|
// TODO: Show error message to user
|
|
}
|
|
});
|
|
}
|
|
|
|
private void OnCancelClicked()
|
|
{
|
|
Hide();
|
|
_onCancel?.Invoke();
|
|
}
|
|
|
|
private bool CanConfirm()
|
|
{
|
|
if (_currentProfession < 0 || _currentProfession >= (int)Profession.NUM_PROFESSION)
|
|
return false;
|
|
|
|
if (_currentGender != GENDER.GENDER_MALE && _currentGender != GENDER.GENDER_FEMALE)
|
|
return false;
|
|
|
|
string name = nameInputField != null ? nameInputField.text : "";
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
private void UpdateConfirmButtonState()
|
|
{
|
|
if (confirmButton != null)
|
|
{
|
|
confirmButton.interactable = CanConfirm();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Update confirm button state in case name changes
|
|
if (nameInputField != null && nameInputField.isFocused)
|
|
{
|
|
UpdateConfirmButtonState();
|
|
}
|
|
}
|
|
}
|
|
}
|