/* * Copyright (c) 2013 Jayce Rettob * * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/. */ using System; using System.Collections; using com.jbrettob.core; using com.jbrettob.data; using com.jbrettob.data.api; using com.jbrettob.data.enums; using com.jbrettob.data.vo; using Svelto.Tasks; using UnityEngine; namespace com.jbrettob.core { public class StartUp:CoreObject { public void Start(AbstractPage go, Action onStartupComplete) { if (debug) logDebug("Start()"); SerialTasks startupSequence = new SerialTasks(); startupSequence.Add(initApplication()); startupSequence.Add(initAPI()); startupSequence.Add(initGameModel()); startupSequence.Add(loadItems()); startupSequence.Add(initPlayerModel()); startupSequence.Add(initLoadSaveFile()); startupSequence.Add(this.onStartupComplete(onStartupComplete)); go.StartCoroutine(startupSequence.GetEnumerator()); } private IEnumerator initApplication() { if (debug) logDebug("initApplication()"); Application.targetFrameRate = 60; Application.runInBackground = true; Screen.autorotateToPortrait = false; Screen.autorotateToPortraitUpsideDown = false; Screen.autorotateToLandscapeLeft = Screen.autorotateToLandscapeRight = true; Screen.orientation = ScreenOrientation.AutoRotation; yield return true; } private IEnumerator initAPI() { if (debug) logDebug("initAPI's()"); bool useDummy = true; if (useDummy) { DataManager.getInstance().api = new DummyAPI(); } else { DataManager.getInstance().api = new API(); } yield return true; } private IEnumerator initGameModel() { if (debug) logDebug("initGameModel()"); DataManager.getInstance().gameModel = new GameModel(); DataManager.getInstance().gameModel.init(); yield return true; } private IEnumerator loadItems() { if (debug) logDebug("loadItems()"); string result; if ( Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.WP8Player ) { System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.Load(URLNames.ITEMS); result = xml.InnerXml; } else { string newUrl = URLNames.ITEMS; if ( Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.OSXWebPlayer ) { newUrl = "StreamingAssets/" + newUrl; } WWW www = new WWW(newUrl); yield return www; result = www.text; } if (debug) logDebug("loadItems() done"); DataManager.getInstance().gameModel.setItems(result); yield return true; } private IEnumerator initPlayerModel() { if (debug) logDebug("initPlayerModel()"); DataManager.getInstance().playerModel = new PlayerModel(); DataManager.getInstance().playerModel.addDummyItems(); DataManager.getInstance().playerModel.setRandomInventions(); yield return true; } private IEnumerator initLoadSaveFile() { if (debug) logDebug("initLoadSaveFile()"); int[] saveFile = PlayerPrefsX.GetIntArray(Project.NAME + "_" + Project.VERSION); int i = 0; if (saveFile.Length > 0) { for (i = 0; i < saveFile.Length; i++) { DataManager.getInstance().playerModel.addItem(DataManager.getInstance().gameModel.getItemFromId(saveFile[i])); } } else { logWarning("initLoadSaveFile() There is no save file, start with scratch!"); } yield return true; } private IEnumerator onStartupComplete(Action onStartupComplete) { if (debug) logDebug("onStartupComplete() onStartupComplete: " + onStartupComplete.Method.Name); onStartupComplete(); yield return true; } } }