Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Newtonsoft.Json.Linq;
- // Displays the player's ships and lets them pick one.
- public class PlayerShipSelector : MonoBehaviour {
- // Invoked when the player chooses a ship. name is the ship name, def is
- // the serialized AssembledShip.
- public delegate void OnShipSelected(string name, JObject def);
- // Prefab that handles the layout of the ship selector.
- // Must have a ShipSelector script.
- public GameObject selectorPrefab;
- // ScrollView containing the menu, to enable scrolling to show the selected
- // item.
- public ScrollRect scroller;
- public OnShipSelected callback;
- private int selectedIndex;
- private List<string> shipNames = new List<string>();
- private List<JObject> shipDefs = new List<JObject>();
- private List<ShipInfoBox> selectors = new List<ShipInfoBox>();
- private JObject playerShips;
- void Start () {
- selectedIndex = 0;
- playerShips = SaveData.Retrieve(SaveData.PlayerShipsKey);
- foreach (var def in playerShips) {
- shipNames.Add(def.Key);
- var shipData = def.Value.ToObject<JObject>();
- shipDefs.Add(shipData);
- var prefab = Instantiate(selectorPrefab);
- prefab.transform.SetParent(gameObject.transform, false);
- var selector = prefab.GetComponent<ShipInfoBox>();
- selectors.Add(selector);
- selector.ImportDef(def.Key, shipData);
- }
- InputDirector.PushListener(this, true);
- }
- public void OnDestroy() {
- InputDirector.PopListener(this);
- }
- void Update () {
- Debug.Log("Callback for " + gameObject.name + " is " + callback);
- if (InputDirector.GetButtonDown(this, "Select")) {
- if (callback == null) {
- throw new System.Exception("Must provide a callback to ShipSelector!");
- }
- callback(shipNames[selectedIndex], shipDefs[selectedIndex]);
- Destroy(gameObject);
- }
- var dy = InputDirector.GetButtonDown(this, "Menu Up") ? -1 : InputDirector.GetButtonDown(this, "Menu Down") ? 1 : 0;
- if (dy != 0) {
- selectedIndex = Op.Mod(selectedIndex + dy, selectors.Count);
- scroller.verticalNormalizedPosition = 1 - ((float)selectedIndex) / selectors.Count;
- for (int i = 0; i < selectors.Count; ++i) {
- var s = selectors[i];
- s.shipName.color = (i == selectedIndex) ? Color.cyan : Color.grey;
- }
- }
- }
- // Instantiate the dialog, and destroy it and invoke the provided callback
- // when a selection is made.
- public static void InstantiateWithCallback(GameObject prefab, Transform parent, OnShipSelected callback) {
- Debug.Log("Instantiator invoked with callback " + callback);
- var dialog = Instantiate(prefab);
- dialog.transform.SetParent(parent, false);
- Debug.Log("There are " + prefab.GetComponentsInChildren<PlayerShipSelector>().Length + " selectors");
- var selector = prefab.GetComponentInChildren<PlayerShipSelector>();
- Debug.Log("Found selector " + selector);
- selector.callback = (string name, JObject def) => {
- Destroy(prefab);
- callback(name, def);
- };
- Debug.Log("Set callback for " + selector.gameObject.name + " to " + selector.callback.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement