//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Monday, March 25, 2019
|
//--------------------------------------------------------
|
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
namespace Snxxz.UI
|
{
|
|
public class ReikiRootWin : Window
|
{
|
[SerializeField] List<PropertyBehaviour> m_Propertys;
|
[SerializeField] List<ReikiRootPointBehaviour> m_ReikiRootPoints;
|
[SerializeField] RectTransform m_Container;
|
[SerializeField] NumKeyBoard m_NumKeyboard;
|
[SerializeField] Text m_FreePoint;
|
[SerializeField] Button m_Recommend;
|
[SerializeField] Button m_Confirm;
|
[SerializeField] Button m_Rule;
|
|
Dictionary<int, int> currentPropertyDict = new Dictionary<int, int>();
|
Dictionary<int, int> upPropertyDict = new Dictionary<int, int>();
|
|
int openKeyboardId = 0;
|
|
bool propertyRefreshDirty = false;
|
|
const float refreshInterval = 0.5f;
|
float timer = 0f;
|
|
ReikiRootModel model { get { return ModelCenter.Instance.GetModel<ReikiRootModel>(); } }
|
|
#region Built-in
|
protected override void BindController()
|
{
|
}
|
|
protected override void AddListeners()
|
{
|
m_Recommend.AddListener(OnRecommend);
|
m_Confirm.AddListener(Confirm);
|
m_Rule.AddListener(OnRule);
|
|
m_NumKeyboard.onValueChange.AddListener(OnPointValChange);
|
m_NumKeyboard.onConfirm.AddListener((bool isOk) =>
|
{
|
if (isOk)
|
{
|
m_NumKeyboard.gameObject.SetActive(false);
|
}
|
});
|
}
|
|
protected override void OnPreOpen()
|
{
|
model.cacheFreePoint = model.freePoint;
|
for (int i = 0; i < m_ReikiRootPoints.Count; i++)
|
{
|
if (i < model.reikiRoots.Count)
|
{
|
m_ReikiRootPoints[i].Display(this, model.reikiRoots[i]);
|
}
|
}
|
|
m_NumKeyboard.gameObject.SetActive(false);
|
|
model.onCacheFreePointRefresh += OnCacheFreePointRefresh;
|
PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshEvent;
|
|
Display();
|
}
|
|
protected override void OnAfterOpen()
|
{
|
}
|
|
protected override void OnPreClose()
|
{
|
model.onCacheFreePointRefresh -= OnCacheFreePointRefresh;
|
PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshEvent;
|
for (int i = 0; i < m_ReikiRootPoints.Count; i++)
|
{
|
m_ReikiRootPoints[i].Dispose();
|
}
|
}
|
|
protected override void OnAfterClose()
|
{
|
}
|
|
protected override void LateUpdate()
|
{
|
timer += Time.deltaTime;
|
if (timer >= refreshInterval)
|
{
|
timer = 0f;
|
if (propertyRefreshDirty)
|
{
|
DisplayProperty();
|
}
|
}
|
}
|
#endregion
|
|
void Display()
|
{
|
DisplayFreePoint();
|
DisplayProperty();
|
}
|
|
void DisplayFreePoint()
|
{
|
m_FreePoint.text = model.cacheFreePoint.ToString();
|
}
|
|
void DisplayProperty()
|
{
|
currentPropertyDict.Clear();
|
upPropertyDict.Clear();
|
|
foreach (var reikiRootPoint in m_ReikiRootPoints)
|
{
|
var id = reikiRootPoint.id;
|
var point = model.GetReikiRootPoint(id);
|
model.GetReikiRootProperty(id, point, ref currentPropertyDict);
|
|
var cachePoint = point + reikiRootPoint.point;
|
model.GetReikiRootProperty(id, cachePoint, ref upPropertyDict);
|
}
|
|
var index = 0;
|
foreach (var property in currentPropertyDict.Keys)
|
{
|
if (index < m_Propertys.Count)
|
{
|
m_Propertys[index].gameObject.SetActive(true);
|
var value = UIHelper.GetPropertyValue((PropertyType)property);
|
var compareValue = currentPropertyDict[property];
|
var add = upPropertyDict.ContainsKey(property) ?
|
(upPropertyDict[property] - compareValue) : 0;
|
m_Propertys[index].DisplayReiki(property, value, add);
|
}
|
index++;
|
}
|
for (int i = index; i < m_Propertys.Count; i++)
|
{
|
m_Propertys[i].gameObject.SetActive(false);
|
}
|
}
|
|
public void OpenKeyboard(ReikiRootPointBehaviour behaviour)
|
{
|
openKeyboardId = behaviour.id;
|
m_NumKeyboard.gameObject.SetActive(true);
|
m_NumKeyboard.max = (uint)model.cacheFreePoint + (uint)behaviour.point;
|
m_NumKeyboard.min = 0;
|
var trans = behaviour.openKeyboardBottom;
|
|
bool up = m_ReikiRootPoints.IndexOf(behaviour) == m_ReikiRootPoints.Count - 1;
|
|
var worldpos = trans.TransformPoint(new Vector3(0, (up ? 1 : -1) * trans.rect.height / 2, 0));
|
var localpos = m_Container.InverseTransformPoint(worldpos);
|
m_NumKeyboard.transform.localPosition = localpos;
|
}
|
|
private void OnPointValChange()
|
{
|
var behaviour = m_ReikiRootPoints.Find((x) =>
|
{
|
return x.id == openKeyboardId;
|
});
|
|
var cache = behaviour.point;
|
|
int num = int.Parse(m_NumKeyboard.Value);
|
if (model.cacheFreePoint + cache - num < 0)
|
{
|
num = model.cacheFreePoint + cache;
|
}
|
|
behaviour.point = num;
|
model.cacheFreePoint = model.cacheFreePoint - num + cache;
|
|
m_NumKeyboard.Value = num.ToString();
|
}
|
|
private void OnRecommend()
|
{
|
var recommends = model.GetReikiRootRecommendPoint(model.freePoint);
|
if (recommends != null)
|
{
|
foreach (var reikiRootPoint in m_ReikiRootPoints)
|
{
|
var point = 0;
|
var list = recommends.FindAll((x) =>
|
{
|
return x.id == reikiRootPoint.id;
|
});
|
if (list != null)
|
{
|
foreach (var recommend in list)
|
{
|
point += recommend.value;
|
}
|
}
|
reikiRootPoint.point = point;
|
}
|
if (model.cacheFreePoint == 0)
|
{
|
OnCacheFreePointRefresh();
|
}
|
model.cacheFreePoint = 0;
|
}
|
}
|
|
private void Confirm()
|
{
|
if (model.freePoint != model.cacheFreePoint)
|
{
|
var ids = new byte[m_ReikiRootPoints.Count];
|
var values = new ushort[m_ReikiRootPoints.Count];
|
for (int i = 0; i < m_ReikiRootPoints.Count; i++)
|
{
|
ids[i] = (byte)m_ReikiRootPoints[i].id;
|
values[i] = (ushort)m_ReikiRootPoints[i].point;
|
}
|
model.SendAddPoint(ids, values);
|
}
|
}
|
|
private void OnRule()
|
{
|
ModelCenter.Instance.GetModel<HowToPlayModel>().ruleId = 44;
|
WindowCenter.Instance.Open<ReikiRootRuleWin>();
|
}
|
|
private void OnCacheFreePointRefresh()
|
{
|
DisplayFreePoint();
|
DisplayProperty();
|
}
|
|
private void PlayerDataRefreshEvent(PlayerDataType dataType)
|
{
|
if (dataType == PlayerDataType.FreePoint)
|
{
|
if (model.cacheFreePoint == model.freePoint)
|
{
|
OnCacheFreePointRefresh();
|
}
|
model.cacheFreePoint = model.freePoint;
|
}
|
else
|
{
|
propertyRefreshDirty = true;
|
}
|
}
|
}
|
|
}
|
|
|
|
|