266 lines
7.4 KiB
C#
266 lines
7.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SelectorBehavior : BaseInputBehavior
|
|
{
|
|
|
|
#region private fields
|
|
List<Transform> transports;
|
|
int columnCount;
|
|
Transform currentTransport;
|
|
object selectionLock;
|
|
private bool _selected;
|
|
private Image _gameObjectImage;
|
|
#endregion
|
|
|
|
#region private properties
|
|
public int CurrentTransportIndex
|
|
{
|
|
get
|
|
{
|
|
return transports != null && currentTransport != null ? transports.IndexOf(currentTransport) : -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Transform CurrentTransport
|
|
{
|
|
get { return currentTransport; }
|
|
set
|
|
{
|
|
lock (selectionLock)
|
|
{
|
|
currentTransport = value;
|
|
AlignSelectorWithTransport(currentTransport);
|
|
UpdatePlayerUIElements();
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Selected
|
|
{
|
|
get
|
|
{
|
|
return _selected;
|
|
}
|
|
set
|
|
{
|
|
_selected = value;
|
|
lastInput = DateTime.Now;
|
|
SelectedChanged();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region public fields
|
|
public Image PlayerPanelImage;
|
|
public Text PlayerPanelText;
|
|
public bool Player2;
|
|
//public int InputDelay = 200;
|
|
public int Padding;
|
|
public Transform TransportsPanel;
|
|
public Color NormalColor = new Color(235, 239, 179, 255);
|
|
//public Transform PlayerPanel;
|
|
public Sprite SelectorImage;
|
|
public Color SelectorColor;
|
|
public Sprite SelectionImage;
|
|
public string XAxisName
|
|
{
|
|
get { return !Player2 ? GameManager.Instance.XAxisP1 : GameManager.Instance.XAxisP2; }
|
|
}
|
|
public string YAxisName
|
|
{
|
|
get { return !Player2 ? GameManager.Instance.YAxisP1 : GameManager.Instance.YAxisP2; }
|
|
}
|
|
public string SubmitButtonName
|
|
{
|
|
get { return !Player2 ? GameManager.Instance.SubmitP1 : GameManager.Instance.SubmitP2; }
|
|
}
|
|
public string CancelButtonName
|
|
{
|
|
get { return !Player2 ? GameManager.Instance.CancelP2 : GameManager.Instance.CancelP2; }
|
|
}
|
|
#endregion
|
|
|
|
#region Behavior overrided methods
|
|
// Use this for initialization
|
|
void Start()
|
|
{
|
|
selectionLock = new object();
|
|
lastInput = DateTime.Now.AddSeconds(1.5);
|
|
//3E00FFFF
|
|
InitGameObjectImage();
|
|
}
|
|
private void OnRenderObject()
|
|
{
|
|
if (transports == null) InitializedTransportsList();
|
|
if (PlayerPanelImage != null && PlayerPanelText != null)
|
|
{
|
|
UpdatePlayerUIElements();
|
|
}
|
|
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
HandleInputs();
|
|
}
|
|
#endregion
|
|
|
|
private void InitGameObjectImage()
|
|
{
|
|
_gameObjectImage = transform.GetChild(0).gameObject.GetComponent<Image>();
|
|
if (_gameObjectImage != null)
|
|
{
|
|
_gameObjectImage.sprite = SelectorImage;
|
|
_gameObjectImage.color = SelectorColor;
|
|
}
|
|
}
|
|
|
|
private void InitializedTransportsList()
|
|
{
|
|
if (TransportsPanel != null)
|
|
{
|
|
transports = new List<Transform>();
|
|
for (int i = 0; i < TransportsPanel.childCount; i++)
|
|
transports.Add(TransportsPanel.GetChild(i));
|
|
columnCount = TransportsPanel.GetComponent<GridLayoutGroup>().constraintCount;
|
|
CurrentTransport = transports[Player2 ? transports.Count / 2 : 0];
|
|
}
|
|
}
|
|
|
|
#region Ui
|
|
|
|
private void AlignSelectorWithTransport(Transform transport)
|
|
{
|
|
if (transport != null)
|
|
{
|
|
transform.SetPositionAndRotation(transport.position, transform.rotation);
|
|
var temp = transport.GetComponent<RectTransform>();
|
|
transform.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(temp.sizeDelta.x + 2 * Padding, temp.sizeDelta.y + 2 * Padding);
|
|
}
|
|
}
|
|
|
|
private void UpdatePlayerUIElements()
|
|
{
|
|
if (CurrentTransport != null)
|
|
{
|
|
|
|
if (PlayerPanelImage != null)
|
|
{
|
|
//Resources.Load<Sprite>("suit_life_meter_2");
|
|
var imageName = CurrentTransport.GetChild(0).gameObject.GetComponent<Image>().mainTexture.name;
|
|
imageName = imageName.Substring(0, imageName.IndexOf('_')) + "_320x320";
|
|
Debug.Log(imageName);
|
|
PlayerPanelImage.sprite = Resources.Load<Sprite>("Images/"+imageName);
|
|
PlayerPanelImage.color = Selected ? SelectorColor : NormalColor;
|
|
PlayerPanelImage.preserveAspect = true;
|
|
}
|
|
if (PlayerPanelText != null)
|
|
PlayerPanelText.text = currentTransport.gameObject.name;
|
|
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
// Update is called once per frame
|
|
private void HandleInputs()
|
|
{
|
|
if (IsSpawned || transports == null || LaunchGameScript.Current.Launching) return;
|
|
if (!string.IsNullOrEmpty(YAxisName) && !Selected)
|
|
{
|
|
var verticalAxis = Input.GetAxis(YAxisName);
|
|
if (Math.Abs(verticalAxis) == 1)
|
|
{
|
|
bool? move = IsPositiveMove(verticalAxis);
|
|
if (move.HasValue)
|
|
{
|
|
MoveVertical(move.Value);
|
|
}
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(XAxisName) && !Selected)
|
|
{
|
|
var horizontalAxis = Input.GetAxis(XAxisName);
|
|
if (Math.Abs(horizontalAxis) == 1)
|
|
{
|
|
bool? move = IsPositiveMove(horizontalAxis);
|
|
if (move.HasValue)
|
|
{
|
|
MoveHorizontal(move.Value);
|
|
}
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(SubmitButtonName))
|
|
{
|
|
if (Input.GetButton(SubmitButtonName))
|
|
{
|
|
Selected = true;
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(CancelButtonName))
|
|
{
|
|
if (Input.GetButton(CancelButtonName))
|
|
{
|
|
if (Selected)
|
|
Selected = false;
|
|
else if (StartupScript.Current != null)
|
|
StartupScript.Current.Display(0, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Move selector
|
|
private static bool? IsPositiveMove(float axis)
|
|
{
|
|
bool? move = null;
|
|
if (axis == 1) move = true;
|
|
else if (axis == -1) move = false;
|
|
return move;
|
|
}
|
|
|
|
private void MoveVertical(bool positive)
|
|
{
|
|
var value = CurrentTransportIndex;
|
|
if (value - (transports.Count / 2) >= 0)
|
|
value -= (transports.Count / 2);
|
|
else value += (transports.Count / 2);
|
|
//var value = GetCurrentTransportIndex;
|
|
//if (positive) value += 1;
|
|
//else value -= 1;
|
|
//if (value > transports.Count - 1) value -= transports.Count - 1;
|
|
lastInput = DateTime.Now;
|
|
CurrentTransport = transports[value];
|
|
}
|
|
|
|
private void MoveHorizontal(bool positive)
|
|
{
|
|
var value = CurrentTransportIndex;
|
|
|
|
if (positive) value++;
|
|
else value--;
|
|
if (value < 0) value = transports.Count - 1;
|
|
if (value > transports.Count - 1) value = 0;
|
|
|
|
lastInput = DateTime.Now;
|
|
CurrentTransport = transports[value];
|
|
}
|
|
#endregion
|
|
|
|
private void SelectedChanged()
|
|
{
|
|
if (_gameObjectImage == null) return;
|
|
//_gameObjectImage.sprite = Selected ? SelectionImage : SelectorImage;
|
|
_gameObjectImage.gameObject.SetActive(!Selected);
|
|
|
|
LaunchGameScript.Current.SelectionChanged();
|
|
}
|
|
|
|
}
|