2
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
8.2 KiB
C#

using UnityEngine;
using Game.Common;
namespace Game
{
public class PageDrama : UIPageBase<ViewDrama>
{
private string _nameLeft = "";
private string _nameRight = "";
private const float ShowCharWaitTimeMax = 0.02f;
private float _showCharWaitTime;
private int _textNumNow;
private int _textNumMax;
private string _textNow = "";
private int _choiceNow = 0;
private int _choiceMax = 0;
private bool _isChoiceOpen = false;
public PageDrama()
{
}
protected override void OnCreate()
{
CreateUI(true);
}
protected override void OnDestroy()
{
}
protected override void OnOpen()
{
EventManager.Instance.AddEvent<PDialog>(EEvent.Dialog, OnDialog);
EventManager.Instance.AddEvent<PDialogChoice>(EEvent.DialogChoice, OnDialogChoice);
EventManager.Instance.AddEvent(EEvent.DialogEnd, OnDialogEnd);
Util.SetControl(false);
}
protected override void OnClose()
{
EventManager.Instance.RemoveEvent<PDialog>(EEvent.Dialog, OnDialog);
EventManager.Instance.RemoveEvent<PDialogChoice>(EEvent.DialogChoice, OnDialogChoice);
EventManager.Instance.RemoveEvent(EEvent.DialogEnd, OnDialogEnd);
Util.SetControl(true);
View.m_c1.selectedIndex = 0;
View.m_c4.selectedIndex = 0;
View.m_c2.selectedIndex = 0;
View.m_c3.selectedIndex = 0;
View.m_c5.selectedIndex = 0;
_isChoiceOpen = false;
_choiceMax = 0;
_choiceNow = 0;
_nameLeft = "";
_nameRight = "";
ShowText("");
}
public override void Update()
{
if (_textNumNow == _textNumMax)
{
View.m_content.m_c1.selectedIndex = 1;
return;
}
if (_showCharWaitTime > 0)
{
_showCharWaitTime -= Time.deltaTime;
return;
}
_textNumNow++;
_showCharWaitTime = ShowCharWaitTimeMax;
View.m_content.m_text.text = _textNow.Substring(0, _textNumNow);
}
private void ShowText(string text)
{
_textNow = text;
_showCharWaitTime = 0;
_textNumMax = text.Length;
_textNumNow = 0;
View.m_content.m_c1.selectedIndex = 0;
}
private void RefreshChoiceOpen()
{
View.m_c4.selectedIndex = _isChoiceOpen ? 0 : 1;
View.m_c6.selectedIndex = _isChoiceOpen ? 0 : 1;
View.m_c5.selectedIndex = _isChoiceOpen ? 1 : 0;
}
private void RefreshChoiceSelecet()
{
var choiceList = View.m_choice.GetChildren();
for (int i = 0; i < choiceList.Length; i++)
{
var choice = choiceList[i] as ViewDramaChoiceItem;
choice.m_c1.selectedIndex = i == _choiceNow ? 1 : 0;
}
}
private void OnDialogChoice(PDialogChoice param)
{
var choiceList = View.m_choice;
choiceList.RemoveChildrenToPool();
_choiceMax = param.Choices.Count;
_choiceNow = 0;
_isChoiceOpen = false;
foreach (var item in param.Choices)
{
var choiceItem = choiceList.AddItemFromPool() as ViewDramaChoiceItem;
choiceItem.m_text.text = item;
}
RefreshChoiceSelecet();
}
private void OnDialog(PDialog param)
{
var isRight = false;
var needRefresh = false;
var name = param.Name;
//优先检测已经存在的
if (_nameLeft == name)
{
isRight = false;
}
else if (_nameRight == name)
{
isRight = true;
}
else
{
needRefresh = true;
//检测空位置
if (string.IsNullOrEmpty(_nameLeft))
{
isRight = false;
}
else if (string.IsNullOrEmpty(_nameRight))
{
isRight = true;
}
else
{
//没有空位置 占左边位置
isRight = false;
}
}
if (isRight)
{
_nameRight = name;
}
else
{
_nameLeft = name;
}
var hasLeft = !string.IsNullOrEmpty(_nameLeft);
var hasRight = !string.IsNullOrEmpty(_nameRight);
//左右
View.m_c1.selectedIndex = isRight ? 2 : 1;
//显隐
View.m_c4.selectedIndex = 1;
View.m_c2.selectedIndex = hasLeft ? 1 : 0;
View.m_c3.selectedIndex = hasRight ? 1 : 0;
//刷新文本
View.m_nameLable.m_text.text = param.Name;
ShowText(param.Content);
//刷新立绘
if (needRefresh)
{
if (isRight)
{
View.m_picRight.texture = Texture(param.Pic);
}
else
{
View.m_picLeft.texture = Texture(param.Pic);
}
}
}
private void OnDialogEnd()
{
UIManager.Instance.Close();
}
protected override void OnInput(PPlayerInput context)
{
if (context.Action == EKeyActionType.Press)
{
if (context.Key == EFunctionKey.Menu)
{
Util.EndDrama();
}
if (context.Key == EFunctionKey.Up || context.Key == EFunctionKey.Down)
{
if (!_isChoiceOpen)
{
return;
}
var newIndex = _choiceNow;
if (context.Key == EFunctionKey.Up)
{
newIndex--;
}
else
{
newIndex++;
}
_choiceNow = (newIndex + _choiceMax) % _choiceMax;
RefreshChoiceSelecet();
}
if (context.Key == EFunctionKey.Jump)
{
if (_choiceMax != 0)
{
//选择
if (!_isChoiceOpen)
{
_isChoiceOpen = true;
RefreshChoiceOpen();
var c1Index = View.m_c1.selectedIndex;
if (c1Index != 0)
{
View.m_c1.selectedIndex = c1Index == 1 ? 2 : 1;
}
}
else
{
_choiceMax = 0;
_isChoiceOpen = false;
RefreshChoiceOpen();
Util.NextDramaChoice(_choiceNow);
}
}
else
{
//播放
if (_textNumMax != _textNumNow)
{
_textNumNow = _textNumMax - 1;
_showCharWaitTime = 0;
}
else
{
Util.NextDrama();
}
}
}
if (context.Key == EFunctionKey.Bag)
{
UIManager.Instance.Close();
}
}
}
}
}