malialis

SelectScreen

Jun 21st, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.66 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class SelectScreenManager : MonoBehaviour {
  8.  
  9.     public int numberOfPlayers = 1;
  10.     public List<PlayerInterfaces> plInterfaces = new List<PlayerInterfaces>();
  11.     public PortraitInfo[] portraitPrefabs; // all our entries as portraits
  12.     public int maxX; // how many portraits we have on x and y NOTE this is hardcoded
  13.     public int maxY;
  14.     PortraitInfo[,] charGrid; // the grid we are making to select entries
  15.  
  16.     public GameObject portraitCanvas; // canvas that holds all the portraits
  17.  
  18.     bool loadLevel; // if we load the level
  19.     public bool bothPlayersSelected;
  20.  
  21.     CharacterManager charManager;
  22.  
  23.     #region Singleton
  24.     public static SelectScreenManager instance;
  25.     public static SelectScreenManager GetInstance()
  26.     {
  27.         return instance;
  28.     }
  29.    
  30.     // Use this for initialization
  31.     void Awake ()
  32.     {
  33.         instance = this;
  34.     }
  35.     #endregion
  36.  
  37.     void Start()
  38.     {
  39.         //we start by getting the reference to the character manager
  40.         charManager = CharacterManager.GetInstance();
  41.         numberOfPlayers = charManager.numberOfUsers;
  42.  
  43.         //we create the grid
  44.         charGrid = new PortraitInfo[maxX, maxY];
  45.  
  46.         int x = 0;
  47.         int y = 0;
  48.  
  49.         portraitPrefabs = portraitCanvas.GetComponentsInChildren<PortraitInfo>();
  50.         // we need to go into all our portraits
  51.         for(int i = 0; i < portraitPrefabs.Length; i++)
  52.         {
  53.             //assign a grid position
  54.             portraitPrefabs[i].posX += x;
  55.             portraitPrefabs[i].posY += y;
  56.  
  57.             charGrid[x, y] = portraitPrefabs[i];
  58.  
  59.             if(x < maxX - 1)
  60.             {
  61.                 x++;
  62.             }
  63.             else
  64.             {
  65.                 x = 0;
  66.                 y++;
  67.             }
  68.         }
  69.     }
  70.    
  71.  
  72.     // Update is called once per frame
  73.     void Update ()
  74.     {
  75.         if (!loadLevel)
  76.         {
  77.             for (int i = 0; i < plInterfaces.Count; i++)
  78.             {
  79.                 if(i < numberOfPlayers)
  80.                 {
  81.                    /* //to deselect
  82.                     if (Input.GetButtonUp("Fire2" + charManager.players[i].inputID))
  83.                     {
  84.                         plInterfaces[i].playerBase.hasCharacter = false;
  85.                     } */
  86.  
  87.                     if (!charManager.players[i].hasCharacter)
  88.                     {
  89.                         plInterfaces[i].playerBase = charManager.players[i];
  90.  
  91.                         HandleSelectorPosition(plInterfaces[i]);
  92.                         HandleSelectScreenInput(plInterfaces[i], charManager.players[i].inputID);
  93.                         HandleCharacterPreview(plInterfaces[i]);
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     charManager.players[i].hasCharacter = true;
  99.                 }
  100.             }
  101.         }
  102.         if (bothPlayersSelected)
  103.         {
  104.             Debug.Log("loading");
  105.             StartCoroutine("LoadLevel"); //start to load the level
  106.             loadLevel = true;
  107.         }
  108.         else
  109.         {
  110.             if(charManager.players[0].hasCharacter
  111.                 && charManager.players[1].hasCharacter)
  112.             {
  113.                 bothPlayersSelected = true;
  114.             }
  115.         }
  116.     }
  117.  
  118.     void HandleSelectScreenInput(PlayerInterfaces pl, string playerId)
  119.     {
  120.         #region Grid Navigation
  121.         /* to navigate the grid
  122.          * we change the active x and y
  123.          * we limit the input to not move more than 1 time each 1/2 second
  124.          * */
  125.  
  126.         float vertical = Input.GetAxis("Vertical" + playerId);
  127.         if(vertical != 0)
  128.         {
  129.             if (!pl.hitInputOnce)
  130.             {
  131.                 if(vertical > 0)
  132.                 {
  133.                     pl.activeY = (pl.activeY > 0) ? pl.activeY - 1 : maxY - 1;
  134.                 }
  135.                 else
  136.                 {
  137.                     pl.activeY = (pl.activeY < maxY - 1) ? pl.activeY + 1 : 0;
  138.                 }
  139.                 pl.hitInputOnce = true;
  140.             }
  141.         }
  142.         float horizontal = Input.GetAxis("Horizontal" + playerId);
  143.         if(horizontal != 0)
  144.         {
  145.             if (!pl.hitInputOnce)
  146.             {
  147.                 if(horizontal > 0)
  148.                 {
  149.                     pl.activeX = (pl.activeX > 0) ? pl.activeX - 1 : maxX - 1;
  150.                 }
  151.                 else
  152.                 {
  153.                     pl.activeX = (pl.activeX < maxX - 1) ? pl.activeX + 1 : 0;
  154.                 }
  155.                 pl.timerToReset = 0;
  156.                 pl.hitInputOnce = true;
  157.             }
  158.         }
  159.  
  160.         if(vertical == 0 && horizontal == 0)
  161.         {
  162.             pl.hitInputOnce = false;
  163.         }
  164.         if (pl.hitInputOnce)
  165.         {
  166.             pl.timerToReset += Time.deltaTime;
  167.             if(pl.timerToReset > 0.8f)
  168.             {
  169.                 pl.hitInputOnce = false;
  170.                 pl.timerToReset = 0;
  171.             }
  172.         }
  173.  
  174.         #endregion
  175.  
  176.         //if space is pressed select character
  177.         if(Input.GetButtonUp("Fire1" + playerId))
  178.         {
  179.             //make a reaction on the character
  180.             pl.createdCharacter.GetComponentInChildren<Animator>().Play("Victory");
  181.             //pass the character into the characterManager
  182.             pl.playerBase.playerPrefab =
  183.                 charManager.returnCharacterWithID(pl.activePortrait.characterId).prefab;
  184.             pl.playerBase.hasCharacter = true;
  185.         }
  186.        
  187.     }
  188.  
  189.     void HandleSelectorPosition(PlayerInterfaces pl)
  190.     {
  191.         pl.selector.SetActive(true); // enable selector
  192.         pl.activePortrait = charGrid[pl.activeX, pl.activeY]; // find active portrait
  193.         //place selector over portrait position
  194.         Vector2 selectorPosition = pl.activePortrait.transform.localPosition;
  195.         selectorPosition = selectorPosition + new Vector2(portraitCanvas.transform.localPosition.x,
  196.             portraitCanvas.transform.localPosition.y);
  197.  
  198.         pl.selector.transform.localPosition = selectorPosition;
  199.     }
  200.  
  201.     void HandleCharacterPreview(PlayerInterfaces pl)
  202.     {
  203.         //if previews portrait we had is not the same as the active one that means we changed characters
  204.         if(pl.previewPortrait != pl.activePortrait)
  205.         {
  206.             if(pl.createdCharacter != null) // delete the one we have now if we do not have one
  207.             {
  208.                 Destroy(pl.createdCharacter);
  209.             }
  210.             //create an other one
  211.             GameObject go = Instantiate(
  212.                 CharacterManager.GetInstance().returnCharacterWithID(pl.activePortrait.characterId).prefab,
  213.                 pl.charVisPos.position, Quaternion.identity) as GameObject;
  214.  
  215.             pl.createdCharacter = go;
  216.             pl.previewPortrait = pl.activePortrait;
  217.  
  218.             if(!string.Equals(pl.playerBase.playerID, charManager.players[0].playerID))
  219.             {
  220.                 pl.createdCharacter.GetComponent<StateManager>().lookRight = false;
  221.             }
  222.         }
  223.     }
  224.  
  225.     IEnumerator LoadLevel()
  226.     {
  227.         //if any of the players is an ai, then assign random
  228.         for(int i = 0; i < charManager.players.Count; i++)
  229.         {
  230.             if(charManager.players[i].playerType == CharacterManager.PlayerBase.PlayerType.ai)
  231.             {
  232.                 if(charManager.players[i].playerPrefab == null)
  233.                 {
  234.                     int ranValue = Random.Range(0, portraitPrefabs.Length);
  235.  
  236.                     charManager.players[i].playerPrefab =
  237.                         charManager.returnCharacterWithID(portraitPrefabs[ranValue].characterId).prefab;
  238.  
  239.                     Debug.Log(portraitPrefabs[ranValue].characterId);
  240.                 }
  241.             }
  242.         }
  243.  
  244.         yield return new WaitForSeconds(2); // after 2 secs load level
  245.         SceneManager.LoadSceneAsync("level", LoadSceneMode.Single);
  246.     }
  247.  
  248.  
  249.     [System.Serializable]
  250.     public class PlayerInterfaces
  251.     {
  252.         public PortraitInfo activePortrait; // current active portrait for player 1
  253.         public PortraitInfo previewPortrait;
  254.         public GameObject selector; //the select indicator for player1
  255.         public Transform charVisPos; // the visualization position for player 1
  256.         public GameObject createdCharacter; // the created character for player 1
  257.  
  258.         public int activeX; // the active x and y entries for player 1
  259.         public int activeY;
  260.  
  261.         //variables for smoothing out input
  262.         public bool hitInputOnce;
  263.         public float timerToReset;
  264.  
  265.         public CharacterManager.PlayerBase playerBase;
  266.     }
  267.  
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment