Skip to content
Snippets Groups Projects
GlobalBehaviour.cs 4.89 KiB
Newer Older
  • Learn to ignore specific revisions
  • MaZiFAU's avatar
    MaZiFAU committed
    using Newtonsoft.Json;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using UnityEngine.Networking;
    
    public class GlobalBehaviour : MonoBehaviour, ISerializationCallbackReceiver
    
    MaZiFAU's avatar
    MaZiFAU committed
        public static GlobalBehaviour Instance
        {
            get => _Instance;
            set
            {
    
    MaZiFAU's avatar
    MaZiFAU committed
                if (_Instance == null)
    
    MaZiFAU's avatar
    MaZiFAU committed
                    _Instance = value;
                else
                    Destroy(value);
            }
        }
        private static GlobalBehaviour _Instance;
    
    
        //Make sure when using RGBA-Colors, the A-value of animationStartColor 
        //and animationEndColor is the same OR try with value = 255
        public static Color hintAnimationStartColor;
        public static Color hintAnimationEndColor;
        public static float hintAnimationDuration;
    
    
        public static Color StageAccomplished;
        public static Color StageNotYetAccomplished;
    
        public static Color StageError;
    
        public static float GadgetLaserDistance;
        public static float GadgetPhysicalDistance;
    
        #region Unity Serialization
        [SerializeField] private Color _hintAnimationStartColor;
        [SerializeField] private Color _hintAnimationEndColor;
        [SerializeField] private float _hintAnimationDuration;
    
        [SerializeField] private Color _StageAccomplished;
        [SerializeField] private Color _StageNotYetAccomplished;
        [SerializeField] private Color _StageError;
    
        [SerializeField] private float _GadgetLaserDistance = 30f;
        [SerializeField] private float _GadgetPhysicalDistance = 2.5f;
    
    
        void ISerializationCallbackReceiver.OnAfterDeserialize()
    
            hintAnimationStartColor = _hintAnimationStartColor;
            hintAnimationEndColor = _hintAnimationEndColor;
            hintAnimationDuration = _hintAnimationDuration;
    
            StageAccomplished = _StageAccomplished;
            StageNotYetAccomplished = _StageNotYetAccomplished;
    
            GadgetLaserDistance = _GadgetLaserDistance;
            GadgetPhysicalDistance = _GadgetPhysicalDistance;
    
        }
    
        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            _hintAnimationStartColor = hintAnimationStartColor;
            _hintAnimationEndColor = hintAnimationEndColor;
            _hintAnimationDuration = hintAnimationDuration;
    
            _StageAccomplished = StageAccomplished;
            _StageNotYetAccomplished = StageNotYetAccomplished;
            _StageError = StageError;
    
            _GadgetLaserDistance = GadgetLaserDistance;
            _GadgetPhysicalDistance = GadgetPhysicalDistance;
    
    MaZiFAU's avatar
    MaZiFAU committed
    
        private void Awake()
        {
            Instance = this;
    
    MaZiFAU's avatar
    MaZiFAU committed
            DontDestroyOnLoad(this);
        }
    
        private void Start()
        {
            PostServerConnection();
        }
    
    
        //TODO: Move where appropiate
    
        public int tryScrollListTimes = 2;
        static public List<Scroll> AvailableScrolls;
    
        private void PostServerConnection()
        {
            StartCoroutine(getScrollsfromServer());
    
            IEnumerator getScrollsfromServer()
            {
                //Try /scroll/listAll endpoint when scroll/list is not working
                UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
                //Postman-Echo-Mock
                //UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get");
    
                for (int i = 0; i < this.tryScrollListTimes; i++)
                {
                    request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
                    request.method = UnityWebRequest.kHttpVerbGET;
    
                    yield return request.SendWebRequest();
    
                    if (request.result == UnityWebRequest.Result.ConnectionError
                     || request.result == UnityWebRequest.Result.ProtocolError)
                    {
                        Debug.LogWarning(request.error);
                        Debug.Log("GET Scroll/list failed. Attempt: " + (i + 1).ToString());
                    }
                    else
                        break;
                }
    
    
                string jsonString = null;
    
                if (request.result == UnityWebRequest.Result.ConnectionError
                 || request.result == UnityWebRequest.Result.ProtocolError)
                {
                    Debug.LogWarning(request.error);
                }
                else
                {
                    CommunicationEvents.ServerRunning = true;
                    jsonString = request.downloadHandler.text;
                }
    
                if (string.IsNullOrEmpty(jsonString)
                 || jsonString.Equals("[]"))
                {
                    jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json");
                    Debug.Log("Using Fallback Scrolls: \n" + jsonString);
                }
    
                System.DateTime startTime = System.DateTime.UtcNow;
                AvailableScrolls = JsonConvert.DeserializeObject<List<Scroll>>(jsonString);
                Debug.Log("Scroll Parsing in: " + (System.DateTime.UtcNow - startTime).TotalMilliseconds + "ms");
            }
    
    MaZiFAU's avatar
    MaZiFAU committed
        }