summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs
blob: 99a2c58d2f1a1e7fe747a04bc680579307398e65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PanelPropBar : PanelBase
{
	public PropWidget m_PropTempalte;

	public UISimpleGrid m_PropGrid;

	public Text m_TextName;

	private List<PropWidget> m_Props = new List<PropWidget>();

	private int m_CurrentIndex = 0;

	public override void Set(object param)
	{
		for(int i = 0; i < PlayerManager.Instance.props.Count; ++i)
		{
			PropWidget widget = MakePropWidget(PlayerManager.Instance.props[i]);
			m_Props.Add(widget);
		}

		SelectPropWidget(0);
	}

	bool SwitchToLeft()
	{
		return Input.GetButtonDown("LeftProp");
	}

	bool SwitchToRight()
	{
		return Input.GetButtonDown("RightProp");
	}

	bool UseProp()
	{
		return Input.GetButtonDown("Fire3");
	}

	PropWidget MakePropWidget(PropBase prop)
	{
		PropWidget widget = Instantiate<PropWidget>(m_PropTempalte);
		widget.transform.SetParent(m_PropGrid.transform);
		widget.gameObject.SetActive(true);
		widget.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
#if UNITY_EDITOR
		widget.name = "prop (" + prop.name + ")";
#endif

		PropWidgetParam param = new PropWidgetParam();
		//param.onSelected = OnSelectPropWidget;
		param.prop = prop;
		widget.Set(param);
		return widget;
	}

	protected override void Update()
	{
		if (SwitchToLeft())
		{
			int newIndex = Mathf.Clamp(m_CurrentIndex - 1, 0, m_Props.Count - 1);
			SelectPropWidget(newIndex);
		}
		if (SwitchToRight())
		{
			int newIndex = Mathf.Clamp(m_CurrentIndex + 1, 0, m_Props.Count - 1);
			SelectPropWidget(newIndex);
		}
		if (UseProp())
		{
			m_Props[m_CurrentIndex].OnUseCallback();
			PlayerManager.Instance.UseProp(m_Props[m_CurrentIndex].prop);
		}
	}

	void SelectPropWidget(int index)
	{
		if(index < 0 || index > m_Props.Count - 1)
		{
			return ;
		}
		m_Props[m_CurrentIndex].OnDeselectCallback();
		m_Props[index].OnSelectCallback();

		m_TextName.text = m_Props[index].prop.name;
		m_TextName.gameObject.SetActive(false);

		m_CurrentIndex = index;
	}

}