//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Tuesday, October 23, 2018
|
//--------------------------------------------------------
|
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
namespace Snxxz.UI
|
{
|
|
public class RolePointWin : Window
|
{
|
[SerializeField] Button m_Close;
|
[SerializeField] Button m_Suggest;
|
[SerializeField] Button m_Confirm;
|
[SerializeField] Text m_Point;
|
[SerializeField] RectTransform m_ContainerRight;
|
[SerializeField] List<PropertyValue> m_PropertyValues;
|
[SerializeField] List<AddPointBehaviour> m_AddPoints;
|
[SerializeField] NumKeyBoard m_NumkeyBoard;
|
|
RolePointModel model { get { return ModelCenter.Instance.GetModel<RolePointModel>(); } }
|
|
bool isSpeedUp = false;
|
int presentAddPoint = 0;
|
int surplusPoint = 0;
|
Dictionary<int, int> allocatPoints = new Dictionary<int, int>();
|
|
static WaitForSeconds waitSpeedUp = new WaitForSeconds(0.5f);
|
static WaitForSeconds waitSpeedFaster = new WaitForSeconds(0.05f);
|
static WaitForSeconds waitSpeedSlow = new WaitForSeconds(0.2f);
|
|
const int WAITCNT = 1;
|
|
|
protected override void BindController()
|
{
|
|
}
|
|
protected override void AddListeners()
|
{
|
m_Close.onClick.AddListener(CloseClick);
|
m_Confirm.onClick.AddListener(Confirm);
|
m_Suggest.onClick.AddListener(Suggest);
|
m_NumkeyBoard.onValueChange.AddListener(OnPointValChange);
|
m_NumkeyBoard.onConfirm.AddListener((bool isOk) =>
|
{
|
if (isOk)
|
{
|
m_NumkeyBoard.gameObject.SetActive(false);
|
}
|
});
|
for (int i = 0; i < m_AddPoints.Count; i++)
|
{
|
m_AddPoints[i].onPreview = OnPreview;
|
m_AddPoints[i].onSub = OnSub;
|
m_AddPoints[i].onAdd = OnAdd;
|
}
|
}
|
|
protected override void OnPreOpen()
|
{
|
PlayerDatas.Instance.PlayerDataRefreshInfoEvent += OnPlayerInfoRefresh;
|
m_NumkeyBoard.gameObject.SetActive(false);
|
Dictionary<int, string> dict = null;
|
if (model.TryGetPointFormula(PlayerDatas.Instance.baseData.Job, out dict))
|
{
|
int i = 0;
|
foreach (var _type in dict.Keys)
|
{
|
m_PropertyValues[i].property = _type;
|
i++;
|
}
|
}
|
OnRefreshProprety();
|
isSpeedUp = false;
|
}
|
|
protected override void OnAfterOpen()
|
{
|
HandleAchievement();
|
}
|
|
protected override void OnPreClose()
|
{
|
PlayerDatas.Instance.PlayerDataRefreshInfoEvent -= OnPlayerInfoRefresh;
|
isSpeedUp = false;
|
StopAllCoroutines();
|
}
|
|
protected override void OnAfterClose()
|
{
|
if (!WindowJumpMgr.Instance.IsJumpState
|
&& !WindowCenter.Instance.IsOpen<RolePanel>()
|
&& !WindowCenter.Instance.IsOpen<MainInterfaceWin>())
|
{
|
WindowCenter.Instance.Open<MainInterfaceWin>();
|
}
|
}
|
|
void OnRefreshProprety()
|
{
|
if (model.propertys != null)
|
{
|
for (int i = 0; i < model.propertys.Length; i++)
|
{
|
var proprety = model.propertys[i];
|
var behaviour = m_AddPoints.Find((x) =>
|
{
|
return x.property == proprety;
|
});
|
var config = PlayerPropertyConfig.Get(proprety);
|
behaviour.propertyName.text = config.Name;
|
behaviour.propertyValue.text = UIHelper.GetPropertyMapPlayerData((AttrEnum)proprety).ToString();
|
allocatPoints[proprety] = 0;
|
behaviour.propertyPreview.text = allocatPoints[proprety].ToString();
|
}
|
}
|
|
surplusPoint = (int)PlayerDatas.Instance.baseData.FreePoint;
|
m_Point.text = surplusPoint.ToString();
|
|
OnRefreshAdd();
|
}
|
|
private void OnRefreshAdd()
|
{
|
Equation.Instance.Clear();
|
foreach (var key in allocatPoints.Keys)
|
{
|
var config = PlayerPropertyConfig.Get(key);
|
Equation.Instance.AddKeyValue(config.Parameter, allocatPoints[key]);
|
}
|
Dictionary<int, string> dict = null;
|
if (model.TryGetPointFormula(PlayerDatas.Instance.baseData.Job, out dict))
|
{
|
foreach (var property in dict.Keys)
|
{
|
float value = Equation.Instance.Eval<float>(dict[property]);
|
GetProperty(property).RefreshAdd(value);
|
}
|
}
|
}
|
|
private PropertyValue GetProperty(int property)
|
{
|
return m_PropertyValues.Find((x) =>
|
{
|
return x.property == property;
|
});
|
}
|
|
private void HandleAchievement()
|
{
|
if (AchievementGoto.achievementType == AchievementGoto.RolePoint)
|
{
|
var _effect = AchievementGuideEffectPool.Require(1);
|
_effect.transform.SetParentEx(m_Suggest.transform, Vector3.zero, Vector3.zero, Vector3.one);
|
AchievementGoto.achievementType = 0;
|
}
|
}
|
|
private void Confirm()
|
{
|
foreach (int key in allocatPoints.Keys)
|
{
|
if (allocatPoints[key] > 0)
|
{
|
model.SendAddPoint(key, allocatPoints[key]);
|
}
|
}
|
}
|
|
private void Suggest()
|
{
|
int points = (int)PlayerDatas.Instance.baseData.FreePoint;
|
int totalpoints = points;
|
int pt = 0;
|
|
Dictionary<int, float> suggestDict = null;
|
model.TryGetPointSuggest(PlayerDatas.Instance.baseData.Job, out suggestDict);
|
Dictionary<int, int> initDict = null;
|
model.TryGetPointInit(PlayerDatas.Instance.baseData.Job, out initDict);
|
|
if (suggestDict == null || initDict == null)
|
{
|
return;
|
}
|
|
foreach (var property in suggestDict.Keys)
|
{
|
totalpoints += ((int)UIHelper.GetPropertyMapPlayerData((AttrEnum)property) - initDict[property]);
|
}
|
|
allocatPoints.Clear();
|
foreach (var property in initDict.Keys)
|
{
|
allocatPoints.Add(property, 0);
|
var behaviour = m_AddPoints.Find((x) =>
|
{
|
return x.property == property;
|
});
|
behaviour.propertyPreview.text = allocatPoints[property].ToString();
|
}
|
|
foreach (var property in suggestDict.Keys)
|
{
|
int expectpt = Mathf.RoundToInt(suggestDict[property] * totalpoints);
|
int presentpt = expectpt - ((int)UIHelper.GetPropertyMapPlayerData((AttrEnum)property) - initDict[property]);
|
if (presentpt > 0)
|
{
|
if (pt + presentpt > points)
|
{
|
presentpt = points - pt;
|
}
|
pt += presentpt;
|
allocatPoints[property] = presentpt;
|
}
|
var behaviour = m_AddPoints.Find((x) =>
|
{
|
return x.property == property;
|
});
|
behaviour.propertyPreview.text = allocatPoints[property].ToString();
|
}
|
|
surplusPoint = points - pt;
|
m_Point.text = surplusPoint.ToString();
|
OnRefreshAdd();
|
}
|
|
private void OnPlayerInfoRefresh(PlayerDataRefresh refreshType)
|
{
|
switch (refreshType)
|
{
|
case PlayerDataRefresh.FreePoint:
|
{
|
OnRefreshProprety();
|
}
|
break;
|
case PlayerDataRefresh.STR:
|
case PlayerDataRefresh.PNE:
|
case PlayerDataRefresh.PHY:
|
case PlayerDataRefresh.CON:
|
{
|
OnRefreshProprety();
|
}
|
break;
|
case PlayerDataRefresh.MAXATK:
|
case PlayerDataRefresh.MINATK:
|
case PlayerDataRefresh.MaxHP:
|
case PlayerDataRefresh.DEF:
|
case PlayerDataRefresh.HIT:
|
case PlayerDataRefresh.AtkInterval:
|
case PlayerDataRefresh.BattleValEx1:
|
{
|
foreach (var property in m_PropertyValues)
|
{
|
property.RefreshValue();
|
}
|
}
|
break;
|
}
|
}
|
|
private void OnPointValChange()
|
{
|
var key = presentAddPoint;
|
surplusPoint += allocatPoints[key];
|
int num = int.Parse(m_NumkeyBoard.Value);
|
if (surplusPoint - num < 0)
|
{
|
num = surplusPoint;
|
}
|
allocatPoints[key] = num;
|
surplusPoint -= num;
|
OnUpdatePoint(key);
|
m_NumkeyBoard.Value = num.ToString();
|
}
|
|
private void OnUpdatePoint(int key)
|
{
|
var behaviour = m_AddPoints.Find((x) =>
|
{
|
return x.property == key;
|
});
|
behaviour.propertyPreview.text = allocatPoints[key].ToString();
|
m_Point.text = surplusPoint.ToString();
|
OnRefreshAdd();
|
}
|
|
private void OnAdd(int property, bool down)
|
{
|
if (!down)
|
{
|
isSpeedUp = false;
|
StopAllCoroutines();
|
return;
|
}
|
isSpeedUp = true;
|
if (allocatPoints.ContainsKey(property))
|
{
|
if (surplusPoint < 1)
|
{
|
return;
|
}
|
allocatPoints[property]++;
|
surplusPoint--;
|
OnUpdatePoint(property);
|
}
|
StartCoroutine(Co_OnSpeedUp(true, property));
|
}
|
|
private void OnSub(int property, bool down)
|
{
|
if (!down)
|
{
|
isSpeedUp = false;
|
StopAllCoroutines();
|
return;
|
}
|
isSpeedUp = true;
|
if (allocatPoints.ContainsKey(property))
|
{
|
if (allocatPoints[property] < 1)
|
{
|
return;
|
}
|
allocatPoints[property]--;
|
surplusPoint++;
|
OnUpdatePoint(property);
|
}
|
StartCoroutine(Co_OnSpeedUp(false, property));
|
}
|
|
IEnumerator Co_OnSpeedUp(bool up, int property)
|
{
|
yield return waitSpeedUp;
|
int upCnt = 0;
|
while (isSpeedUp)
|
{
|
if (allocatPoints.ContainsKey(property))
|
{
|
if (up)
|
{
|
if (surplusPoint < 1)
|
{
|
yield break;
|
}
|
allocatPoints[property]++;
|
surplusPoint--;
|
OnUpdatePoint(property);
|
}
|
else
|
{
|
if (allocatPoints[property] < 1)
|
{
|
yield break;
|
}
|
allocatPoints[property]--;
|
surplusPoint++;
|
OnUpdatePoint(property);
|
}
|
}
|
if (upCnt < WAITCNT)
|
{
|
yield return waitSpeedSlow;
|
}
|
else
|
{
|
yield return waitSpeedFaster;
|
}
|
upCnt++;
|
}
|
}
|
|
private void OnPreview(int property)
|
{
|
presentAddPoint = property;
|
m_NumkeyBoard.gameObject.SetActive(true);
|
m_NumkeyBoard.max = (uint)surplusPoint + (uint)allocatPoints[property];
|
m_NumkeyBoard.min = 0;
|
var behaviour = m_AddPoints.Find((x) =>
|
{
|
return x.property == property;
|
});
|
RectTransform parent = behaviour.pointBottom;
|
var worldpos = parent.TransformPoint(new Vector3(0, -parent.rect.height / 2, 0));
|
var localpos = m_ContainerRight.InverseTransformPoint(worldpos);
|
m_NumkeyBoard.transform.localPosition = localpos;
|
}
|
|
[Serializable]
|
public class PropertyValue
|
{
|
private int m_Property;
|
public int property
|
{
|
get
|
{
|
return m_Property;
|
}
|
set
|
{
|
m_Property = value;
|
Refresh();
|
}
|
}
|
|
[SerializeField] Text m_PropertyName;
|
[SerializeField] Text m_PropertyValue;
|
[SerializeField] Text m_PropertyAdd;
|
|
public void Refresh()
|
{
|
var config = PlayerPropertyConfig.Get(property);
|
if (config != null)
|
{
|
m_PropertyName.text = config.Name;
|
RefreshValue();
|
RefreshAdd(0);
|
}
|
}
|
|
public void RefreshValue()
|
{
|
var config = PlayerPropertyConfig.Get(property);
|
if ((AttrEnum)property == AttrEnum.ATK)
|
{
|
m_PropertyValue.text = StringUtility.Contact(UIHelper.ReplaceLargeNum((ulong)PlayerDatas.Instance.extersion.MINATK),
|
"-", UIHelper.ReplaceLargeNum((ulong)PlayerDatas.Instance.extersion.MAXATK));
|
}
|
else
|
{
|
m_PropertyValue.text = UIHelper.ReplaceLargeNum((ulong)UIHelper.GetPropertyMapPlayerData((AttrEnum)property));
|
}
|
}
|
|
public void RefreshAdd(float _value)
|
{
|
m_PropertyAdd.gameObject.SetActive(_value != 0);
|
if (_value > 0)
|
{
|
m_PropertyAdd.text = StringUtility.Contact("+", _value);
|
}
|
}
|
}
|
}
|
}
|
|
|
|
|