Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Newtonsoft.Json.Linq;
  6.  
  7. // Displays the player's ships and lets them pick one.
  8. public class PlayerShipSelector : MonoBehaviour {
  9.  
  10. // Invoked when the player chooses a ship. name is the ship name, def is
  11. // the serialized AssembledShip.
  12. public delegate void OnShipSelected(string name, JObject def);
  13.  
  14. // Prefab that handles the layout of the ship selector.
  15. // Must have a ShipSelector script.
  16. public GameObject selectorPrefab;
  17. // ScrollView containing the menu, to enable scrolling to show the selected
  18. // item.
  19. public ScrollRect scroller;
  20. public OnShipSelected callback;
  21.  
  22. private int selectedIndex;
  23. private List<string> shipNames = new List<string>();
  24. private List<JObject> shipDefs = new List<JObject>();
  25. private List<ShipInfoBox> selectors = new List<ShipInfoBox>();
  26. private JObject playerShips;
  27.  
  28. void Start () {
  29. selectedIndex = 0;
  30. playerShips = SaveData.Retrieve(SaveData.PlayerShipsKey);
  31. foreach (var def in playerShips) {
  32. shipNames.Add(def.Key);
  33. var shipData = def.Value.ToObject<JObject>();
  34. shipDefs.Add(shipData);
  35. var prefab = Instantiate(selectorPrefab);
  36. prefab.transform.SetParent(gameObject.transform, false);
  37. var selector = prefab.GetComponent<ShipInfoBox>();
  38. selectors.Add(selector);
  39. selector.ImportDef(def.Key, shipData);
  40. }
  41. InputDirector.PushListener(this, true);
  42. }
  43.  
  44. public void OnDestroy() {
  45. InputDirector.PopListener(this);
  46. }
  47.  
  48. void Update () {
  49. Debug.Log("Callback for " + gameObject.name + " is " + callback);
  50. if (InputDirector.GetButtonDown(this, "Select")) {
  51. if (callback == null) {
  52. throw new System.Exception("Must provide a callback to ShipSelector!");
  53. }
  54. callback(shipNames[selectedIndex], shipDefs[selectedIndex]);
  55. Destroy(gameObject);
  56. }
  57. var dy = InputDirector.GetButtonDown(this, "Menu Up") ? -1 : InputDirector.GetButtonDown(this, "Menu Down") ? 1 : 0;
  58. if (dy != 0) {
  59. selectedIndex = Op.Mod(selectedIndex + dy, selectors.Count);
  60. scroller.verticalNormalizedPosition = 1 - ((float)selectedIndex) / selectors.Count;
  61. for (int i = 0; i < selectors.Count; ++i) {
  62. var s = selectors[i];
  63. s.shipName.color = (i == selectedIndex) ? Color.cyan : Color.grey;
  64. }
  65. }
  66. }
  67.  
  68. // Instantiate the dialog, and destroy it and invoke the provided callback
  69. // when a selection is made.
  70. public static void InstantiateWithCallback(GameObject prefab, Transform parent, OnShipSelected callback) {
  71. Debug.Log("Instantiator invoked with callback " + callback);
  72. var dialog = Instantiate(prefab);
  73. dialog.transform.SetParent(parent, false);
  74. Debug.Log("There are " + prefab.GetComponentsInChildren<PlayerShipSelector>().Length + " selectors");
  75. var selector = prefab.GetComponentInChildren<PlayerShipSelector>();
  76. Debug.Log("Found selector " + selector);
  77. selector.callback = (string name, JObject def) => {
  78. Destroy(prefab);
  79. callback(name, def);
  80. };
  81. Debug.Log("Set callback for " + selector.gameObject.name + " to " + selector.callback.ToString());
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement