123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- //================================================================================
- //
- //================================================================================
- using UnityEngine;
- using System.Collections;
- using KishiTechUnity.ScreenFade;
- using KishiTechUnity.KishiTechDebug;
- //================================================================================
- //
- //================================================================================
- namespace ReaderRabbit
- {
- //================================================================================
- //
- //================================================================================
- public class Map : MonoBehaviour
- {
- //================================================================================
- // Singleton
- //================================================================================
- private static Map s_Instance = null;
- public static Map Instance()
- {
- if (s_Instance == null)
- {
- s_Instance = GameObject.FindObjectOfType<Map>();
-
- if (s_Instance == null)
- {
- GameObject gameObject = new GameObject("Map");
- DontDestroyOnLoad(gameObject);
- s_Instance = gameObject.AddComponent<Map>();
- }
- }
- return s_Instance;
- }
- //================================================================================
- // This is the same as enum Location but as string.
- // Used in TypeWriterStartButton to load the last location that the player was in.
- //================================================================================
- public static string[] LocationAsString =
- {
- "EdgeOfCloudNine",
- "CloudNineGateway",
- "CloudSpellingChallenge",
- "DowntownCloudNine",
- "CloudNineArtGallery",
- "AmazingArtGallery",
- "CloudSheepCrossing",
- "NewspaperCarrierScene",
- "RaingearIsland",
- "IrvinsHouse",
- "GumballScience",
- "RaingearThrowingMachine",
- "MusicalMarblePatterns",
- "RaingearForest",
- "RaingearForestMath",
- "SillySandwichShop",
- "RockHead",
- "FlowerpotPhonics",
- "RaingearRescue",
- };
- //================================================================================
- //
- //================================================================================
- SceneCommon m_ParentScene;
- [SerializeField] RRInteractiveRectangle m_BackgroundRectangle;
- [SerializeField] SpriteRenderer m_MapBackground;
- [SerializeField] GameObject m_CurrentLocation;
-
- // ATTENTION: Navigation order is important! Must match enum Location.
- [SerializeField] HUDMapNavigation[] m_Navigation;
- [SerializeField] HUDCommon[] m_HUD;
- System.Action m_PreviousOnFadeOutFinishedCallback;
- // HACK: Force visual update (position and size) according to current resolution.
- static bool m_CalledUpdateVisual;
- string m_LevelSelected;
- public enum MapOpenedType
- {
- None = 0,
- Opened,
- OpenedSpeechMarble,
- };
- private bool m_ChangedLocationFromMap;
- //================================================================================
- //
- //================================================================================
- void Awake()
- {
- if (s_Instance == null)
- {
- s_Instance = this;
- DontDestroyOnLoad(this);
- }
- else
- {
- if (s_Instance != this)
- Destroy(this.gameObject);
- }
- }
- //================================================================================
- //
- //================================================================================
- void Start()
- {
- float deltaX = (Screen.width - m_BackgroundRectangle.GetAABB().width) * 0.5f;
- float deltaY = -(Screen.height - m_BackgroundRectangle.GetAABB().height) * 0.5f;
-
- Vector3 mapPosition = s_Instance.gameObject.transform.position;
- mapPosition.x = deltaX;
- mapPosition.y = deltaY;
- s_Instance.gameObject.transform.position = mapPosition;
- if (m_BackgroundRectangle != null)
- m_BackgroundRectangle.UpdateAABB();
- Close();
- m_CalledUpdateVisual = false;
- m_ChangedLocationFromMap = false;
- PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.None;
- }
- //================================================================================
- // HACK: Force visual update (position and size) according to current resolution.
- //================================================================================
- void UpdateVisual()
- {
- KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateGameObject(s_Instance.gameObject);
- // HACK: Oh boy. Here comes some ugly code.
- // After updating game object (position and size), we update the background rectangle.
- if (m_BackgroundRectangle != null)
- {
- KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateInteractiveRectangle(m_BackgroundRectangle);
- m_BackgroundRectangle.UpdateAABB();
- }
- // We use the updated background rectangle here - center the Map parent but using a top-left anchor point.
- float deltaX = (Screen.width - m_BackgroundRectangle.GetAABB().width) * 0.5f;
- float deltaY = -(Screen.height - m_BackgroundRectangle.GetAABB().height) * 0.5f;
-
- // Then we center the Map parent onscreen.
- Vector3 mapPosition = s_Instance.gameObject.transform.position;
- mapPosition.x = deltaX;
- mapPosition.y = deltaY;
- s_Instance.gameObject.transform.position = mapPosition;
- // And we update the background rectangle (once again). Maybe we should simply use a center anchor point.
- if (m_BackgroundRectangle != null)
- m_BackgroundRectangle.UpdateAABB();
- m_CalledUpdateVisual = true;
- }
- //================================================================================
- //
- //================================================================================
- private void SetFadeOutCallback()
- {
- m_PreviousOnFadeOutFinishedCallback = ScreenFade.Instance().GetOnFadeOutFinishedCallback();
- ScreenFade.Instance().SetOnFadeOutFinishedCallback(FinishedFadeOut);
- }
- //================================================================================
- //
- //================================================================================
- private void RestorePreviousFadeOutCallback()
- {
- ScreenFade.Instance().SetOnFadeOutFinishedCallback(m_PreviousOnFadeOutFinishedCallback);
- m_PreviousOnFadeOutFinishedCallback = null;
- }
- //================================================================================
- //
- //================================================================================
- private void UpdateMap()
- {
- if (!m_CalledUpdateVisual)
- UpdateVisual();
- if (PlayerData.Instance().IsPracticeMode())
- {
- if (m_Navigation != null)
- {
- for (int i = 0; i < m_Navigation.Length; ++i)
- {
- m_Navigation[i].Hide();
- }
- m_Navigation[(int)Location.CloudSpellingChallenge].Show();
- m_Navigation[(int)Location.AmazingArtGallery].Show();
- m_Navigation[(int)Location.GumballScience].Show();
- m_Navigation[(int)Location.MusicalMarblePatterns].Show();
- m_Navigation[(int)Location.RaingearForestMath].Show();
- m_Navigation[(int)Location.SillySandwichShop].Show();
- m_Navigation[(int)Location.FlowerpotPhonics].Show();
- m_Navigation[(int)Location.RaingearRescue].Show();
- }
- }
- else
- {
- bool debugEnabled = KTDebug.Instance().IsEnabled();
- if (m_Navigation != null)
- {
- for (int i = 0; i < m_Navigation.Length; ++i)
- {
- m_Navigation[i].SetVisible(PlayerData.Instance().DidVisit(i) || debugEnabled);
- }
- }
- }
- Location currentLocation = PlayerData.Instance().GetCurrentLocation();
- if (currentLocation == Location.Quantity)
- {
- m_CurrentLocation.SetActive(false);
- }
- else
- {
- if (!m_CurrentLocation.activeSelf)
- m_CurrentLocation.SetActive(true);
- float offsetX = 5.0f * KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ConversionRatio;
- float offsetY = -5.0f;
- // HACK: There are five activity icons that has the marble at their top-right corner so we need to adjust the yellow circle y-position accordingly.
- switch (currentLocation)
- {
- case Location.CloudSpellingChallenge:
- offsetY = -2.0f;
- break;
- case Location.GumballScience:
- offsetY = 11.0f;
- break;
- case Location.RaingearForestMath:
- offsetY = 8.0f;
- break;
- case Location.SillySandwichShop:
- offsetY = 8.0f;
- break;
- case Location.FlowerpotPhonics:
- offsetY = 11.0f;
- break;
- }
- Vector3 currentScreenPosition = m_Navigation[(int)currentLocation].transform.position;
- currentScreenPosition.x -= offsetX;
- currentScreenPosition.y -= (offsetY * KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ConversionRatio);
- m_CurrentLocation.transform.position = currentScreenPosition;
- }
- if (m_BackgroundRectangle != null)
- m_BackgroundRectangle.UpdateDebugEnabled();
- }
- //================================================================================
- //
- //================================================================================
- public SceneCommon GetParentScene()
- {
- return m_ParentScene;
- }
- //================================================================================
- //
- //================================================================================
- public void Open(SceneCommon parentScene)
- {
- m_ChangedLocationFromMap = false;
- m_ParentScene = parentScene;
- if (m_ParentScene != null)
- m_ParentScene.DisableColliders();
- s_Instance.gameObject.SetActive(true);
- AudioManager.Instance().PlaySFX("Map_Options");
- SetFadeOutCallback();
- UpdateMap();
- m_LevelSelected = "";
- if (PlayerData.Instance().CurrentMapOpenedType == MapOpenedType.None)
- {
- PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.Opened;
- if (PlayerData.Instance().IsPracticeMode())
- AudioManager.Instance().PlaySpeech("4601");
- else
- AudioManager.Instance().PlaySpeech("4503");
- }
- else
- {
- if (PlayerData.Instance().IsPracticeMode())
- {
- AudioManager.Instance().PlaySpeech("4602");
- }
- else if (PlayerData.Instance().CurrentMapOpenedType == MapOpenedType.Opened)
- {
- if (PlayerData.Instance().DidVisitMusicalMarblePatterns())
- {
- AudioManager.Instance().PlaySpeech("4301");
- PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.OpenedSpeechMarble;
- }
- }
- }
- }
- //================================================================================
- //
- //================================================================================
- public void Close()
- {
- if (AudioManager.Instance().IsPlayingSpeech("4601")
- || AudioManager.Instance().IsPlayingSpeech("4602")
- || AudioManager.Instance().IsPlayingSpeech("4503")
- || AudioManager.Instance().IsPlayingSpeech("4301")
- )
- {
- AudioManager.Instance().StopSpeech();
- }
- RestorePreviousFadeOutCallback();
- s_Instance.gameObject.SetActive(false);
- if (m_ParentScene != null)
- {
- m_ParentScene.RestoreColliders();
- m_ParentScene.OnMapClose();
- m_ParentScene = null;
- }
- }
- //================================================================================
- //
- //================================================================================
- public void ResetMapOpenedType()
- {
- PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.None;
- }
- //================================================================================
- // Called when Options is closed.
- //================================================================================
- public void BackFromOptions()
- {
- s_Instance.gameObject.SetActive(true);
- }
-
- //================================================================================
- // Called when Options is opened.
- //================================================================================
- public void HideForOptions()
- {
- s_Instance.gameObject.SetActive(false);
- }
-
- //================================================================================
- //
- //================================================================================
- public bool IsOpened()
- {
- return s_Instance.gameObject.activeInHierarchy;
- }
- //================================================================================
- //
- //================================================================================
- public void EnableAll()
- {
- if (m_Navigation != null)
- {
- foreach (HUDMapNavigation navigation in m_Navigation)
- {
- navigation.Enable();
- }
- }
- if (m_HUD != null)
- {
- foreach (HUDCommon hud in m_HUD)
- {
- hud.Enable();
- }
- }
- }
- //================================================================================
- //
- //================================================================================
- public void DisableAll()
- {
- if (m_Navigation != null)
- {
- foreach (HUDMapNavigation navigation in m_Navigation)
- {
- navigation.Disable();
- }
- }
- if (m_HUD != null)
- {
- foreach (HUDCommon hud in m_HUD)
- {
- hud.Disable();
- }
- }
- }
- //================================================================================
- //
- //================================================================================
- void FinishedFadeOut()
- {
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Calling Map.FinishedFadeOut()...");
-
- Close();
- Chester.Instance().CloseInstantly();
- KishiTechUnity.Loading.Loading.Instance().Show();
- // HUDMapNavigation started fade-out. When it is over, we load the level defined there (which is copied to m_LevelSelected).
- if (!string.IsNullOrEmpty(m_LevelSelected))
- {
- m_ChangedLocationFromMap = true;
- Application.LoadLevel(m_LevelSelected);
- }
- }
-
- //================================================================================
- //
- //================================================================================
- public void SetLevelSelected(string levelName)
- {
- m_LevelSelected = levelName;
- }
- //================================================================================
- //
- //================================================================================
- bool CheckHUD()
- {
- if (m_HUD != null)
- {
- foreach (HUDCommon hud in m_HUD)
- {
- if (hud.WasUpAsButton() || hud.DidExitWhileDown())
- {
- hud.ClearWasUpAsButton();
- hud.ClearDidExitWhileDown();
- return true;
- }
- }
- }
-
- return false;
- }
- //================================================================================
- //
- //================================================================================
- bool CheckNavigation(float x, float y)
- {
- if (m_Navigation != null)
- {
- foreach (HUDCommon navigation in m_Navigation)
- {
- if (navigation.WasUpAsButton() || navigation.DidExitWhileDown())
- {
- navigation.ClearWasUpAsButton();
- navigation.ClearDidExitWhileDown();
- return true;
- }
- }
- }
- return false;
- }
-
- //================================================================================
- //
- //================================================================================
- public void CheckAll(float x, float y)
- {
- // HUD priority is #1.
- bool clickedOnHUD = CheckHUD();
- if (!clickedOnHUD)
- {
- // Navigation priority is #2.
- bool clickedOnNavigation = CheckNavigation(x, y);
- if (!clickedOnNavigation)
- {
- if (m_BackgroundRectangle != null)
- {
- if (m_BackgroundRectangle.IsPointInside(x, y))
- {
- int textureX = (int)(x - gameObject.transform.position.x);
- int textureY = (int)y;//((Screen.height - y) - (-gameObject.transform.position.y));
- float alpha = m_MapBackground.sprite.texture.GetPixel(textureX, textureY).a;
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("xy(" + x + ", " + y + "), tex(" + textureX + ", " + textureY + "), alpha: " + alpha);
- if (alpha < 0.0001f)
- {
- Close();
- }
- }
- else
- {
- Close();
- }
- }
- }
- }
- }
- //================================================================================
- //
- //================================================================================
- public bool ChangedLocationFromMap
- {
- get { return m_ChangedLocationFromMap; }
- set { m_ChangedLocationFromMap = value; }
- }
- } // public class Map : MonoBehaviour
- } // namespace ReaderRabbit
|