Map.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //================================================================================
  2. //
  3. //================================================================================
  4. using UnityEngine;
  5. using System.Collections;
  6. using KishiTechUnity.ScreenFade;
  7. using KishiTechUnity.KishiTechDebug;
  8. //================================================================================
  9. //
  10. //================================================================================
  11. namespace ReaderRabbit
  12. {
  13. //================================================================================
  14. //
  15. //================================================================================
  16. public class Map : MonoBehaviour
  17. {
  18. //================================================================================
  19. // Singleton
  20. //================================================================================
  21. private static Map s_Instance = null;
  22. public static Map Instance()
  23. {
  24. if (s_Instance == null)
  25. {
  26. s_Instance = GameObject.FindObjectOfType<Map>();
  27. if (s_Instance == null)
  28. {
  29. GameObject gameObject = new GameObject("Map");
  30. DontDestroyOnLoad(gameObject);
  31. s_Instance = gameObject.AddComponent<Map>();
  32. }
  33. }
  34. return s_Instance;
  35. }
  36. //================================================================================
  37. // This is the same as enum Location but as string.
  38. // Used in TypeWriterStartButton to load the last location that the player was in.
  39. //================================================================================
  40. public static string[] LocationAsString =
  41. {
  42. "EdgeOfCloudNine",
  43. "CloudNineGateway",
  44. "CloudSpellingChallenge",
  45. "DowntownCloudNine",
  46. "CloudNineArtGallery",
  47. "AmazingArtGallery",
  48. "CloudSheepCrossing",
  49. "NewspaperCarrierScene",
  50. "RaingearIsland",
  51. "IrvinsHouse",
  52. "GumballScience",
  53. "RaingearThrowingMachine",
  54. "MusicalMarblePatterns",
  55. "RaingearForest",
  56. "RaingearForestMath",
  57. "SillySandwichShop",
  58. "RockHead",
  59. "FlowerpotPhonics",
  60. "RaingearRescue",
  61. };
  62. //================================================================================
  63. //
  64. //================================================================================
  65. SceneCommon m_ParentScene;
  66. [SerializeField] RRInteractiveRectangle m_BackgroundRectangle;
  67. [SerializeField] SpriteRenderer m_MapBackground;
  68. [SerializeField] GameObject m_CurrentLocation;
  69. // ATTENTION: Navigation order is important! Must match enum Location.
  70. [SerializeField] HUDMapNavigation[] m_Navigation;
  71. [SerializeField] HUDCommon[] m_HUD;
  72. System.Action m_PreviousOnFadeOutFinishedCallback;
  73. // HACK: Force visual update (position and size) according to current resolution.
  74. static bool m_CalledUpdateVisual;
  75. string m_LevelSelected;
  76. public enum MapOpenedType
  77. {
  78. None = 0,
  79. Opened,
  80. OpenedSpeechMarble,
  81. };
  82. private bool m_ChangedLocationFromMap;
  83. //================================================================================
  84. //
  85. //================================================================================
  86. void Awake()
  87. {
  88. if (s_Instance == null)
  89. {
  90. s_Instance = this;
  91. DontDestroyOnLoad(this);
  92. }
  93. else
  94. {
  95. if (s_Instance != this)
  96. Destroy(this.gameObject);
  97. }
  98. }
  99. //================================================================================
  100. //
  101. //================================================================================
  102. void Start()
  103. {
  104. float deltaX = (Screen.width - m_BackgroundRectangle.GetAABB().width) * 0.5f;
  105. float deltaY = -(Screen.height - m_BackgroundRectangle.GetAABB().height) * 0.5f;
  106. Vector3 mapPosition = s_Instance.gameObject.transform.position;
  107. mapPosition.x = deltaX;
  108. mapPosition.y = deltaY;
  109. s_Instance.gameObject.transform.position = mapPosition;
  110. if (m_BackgroundRectangle != null)
  111. m_BackgroundRectangle.UpdateAABB();
  112. Close();
  113. m_CalledUpdateVisual = false;
  114. m_ChangedLocationFromMap = false;
  115. PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.None;
  116. }
  117. //================================================================================
  118. // HACK: Force visual update (position and size) according to current resolution.
  119. //================================================================================
  120. void UpdateVisual()
  121. {
  122. KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateGameObject(s_Instance.gameObject);
  123. // HACK: Oh boy. Here comes some ugly code.
  124. // After updating game object (position and size), we update the background rectangle.
  125. if (m_BackgroundRectangle != null)
  126. {
  127. KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ForceUpdateInteractiveRectangle(m_BackgroundRectangle);
  128. m_BackgroundRectangle.UpdateAABB();
  129. }
  130. // We use the updated background rectangle here - center the Map parent but using a top-left anchor point.
  131. float deltaX = (Screen.width - m_BackgroundRectangle.GetAABB().width) * 0.5f;
  132. float deltaY = -(Screen.height - m_BackgroundRectangle.GetAABB().height) * 0.5f;
  133. // Then we center the Map parent onscreen.
  134. Vector3 mapPosition = s_Instance.gameObject.transform.position;
  135. mapPosition.x = deltaX;
  136. mapPosition.y = deltaY;
  137. s_Instance.gameObject.transform.position = mapPosition;
  138. // And we update the background rectangle (once again). Maybe we should simply use a center anchor point.
  139. if (m_BackgroundRectangle != null)
  140. m_BackgroundRectangle.UpdateAABB();
  141. m_CalledUpdateVisual = true;
  142. }
  143. //================================================================================
  144. //
  145. //================================================================================
  146. private void SetFadeOutCallback()
  147. {
  148. m_PreviousOnFadeOutFinishedCallback = ScreenFade.Instance().GetOnFadeOutFinishedCallback();
  149. ScreenFade.Instance().SetOnFadeOutFinishedCallback(FinishedFadeOut);
  150. }
  151. //================================================================================
  152. //
  153. //================================================================================
  154. private void RestorePreviousFadeOutCallback()
  155. {
  156. ScreenFade.Instance().SetOnFadeOutFinishedCallback(m_PreviousOnFadeOutFinishedCallback);
  157. m_PreviousOnFadeOutFinishedCallback = null;
  158. }
  159. //================================================================================
  160. //
  161. //================================================================================
  162. private void UpdateMap()
  163. {
  164. if (!m_CalledUpdateVisual)
  165. UpdateVisual();
  166. if (PlayerData.Instance().IsPracticeMode())
  167. {
  168. if (m_Navigation != null)
  169. {
  170. for (int i = 0; i < m_Navigation.Length; ++i)
  171. {
  172. m_Navigation[i].Hide();
  173. }
  174. m_Navigation[(int)Location.CloudSpellingChallenge].Show();
  175. m_Navigation[(int)Location.AmazingArtGallery].Show();
  176. m_Navigation[(int)Location.GumballScience].Show();
  177. m_Navigation[(int)Location.MusicalMarblePatterns].Show();
  178. m_Navigation[(int)Location.RaingearForestMath].Show();
  179. m_Navigation[(int)Location.SillySandwichShop].Show();
  180. m_Navigation[(int)Location.FlowerpotPhonics].Show();
  181. m_Navigation[(int)Location.RaingearRescue].Show();
  182. }
  183. }
  184. else
  185. {
  186. bool debugEnabled = KTDebug.Instance().IsEnabled();
  187. if (m_Navigation != null)
  188. {
  189. for (int i = 0; i < m_Navigation.Length; ++i)
  190. {
  191. m_Navigation[i].SetVisible(PlayerData.Instance().DidVisit(i) || debugEnabled);
  192. }
  193. }
  194. }
  195. Location currentLocation = PlayerData.Instance().GetCurrentLocation();
  196. if (currentLocation == Location.Quantity)
  197. {
  198. m_CurrentLocation.SetActive(false);
  199. }
  200. else
  201. {
  202. if (!m_CurrentLocation.activeSelf)
  203. m_CurrentLocation.SetActive(true);
  204. float offsetX = 5.0f * KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ConversionRatio;
  205. float offsetY = -5.0f;
  206. // 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.
  207. switch (currentLocation)
  208. {
  209. case Location.CloudSpellingChallenge:
  210. offsetY = -2.0f;
  211. break;
  212. case Location.GumballScience:
  213. offsetY = 11.0f;
  214. break;
  215. case Location.RaingearForestMath:
  216. offsetY = 8.0f;
  217. break;
  218. case Location.SillySandwichShop:
  219. offsetY = 8.0f;
  220. break;
  221. case Location.FlowerpotPhonics:
  222. offsetY = 11.0f;
  223. break;
  224. }
  225. Vector3 currentScreenPosition = m_Navigation[(int)currentLocation].transform.position;
  226. currentScreenPosition.x -= offsetX;
  227. currentScreenPosition.y -= (offsetY * KishiTechUnity.ScreenResolution.ScreenResolutionManager.Instance().ConversionRatio);
  228. m_CurrentLocation.transform.position = currentScreenPosition;
  229. }
  230. if (m_BackgroundRectangle != null)
  231. m_BackgroundRectangle.UpdateDebugEnabled();
  232. }
  233. //================================================================================
  234. //
  235. //================================================================================
  236. public SceneCommon GetParentScene()
  237. {
  238. return m_ParentScene;
  239. }
  240. //================================================================================
  241. //
  242. //================================================================================
  243. public void Open(SceneCommon parentScene)
  244. {
  245. m_ChangedLocationFromMap = false;
  246. m_ParentScene = parentScene;
  247. if (m_ParentScene != null)
  248. m_ParentScene.DisableColliders();
  249. s_Instance.gameObject.SetActive(true);
  250. AudioManager.Instance().PlaySFX("Map_Options");
  251. SetFadeOutCallback();
  252. UpdateMap();
  253. m_LevelSelected = "";
  254. if (PlayerData.Instance().CurrentMapOpenedType == MapOpenedType.None)
  255. {
  256. PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.Opened;
  257. if (PlayerData.Instance().IsPracticeMode())
  258. AudioManager.Instance().PlaySpeech("4601");
  259. else
  260. AudioManager.Instance().PlaySpeech("4503");
  261. }
  262. else
  263. {
  264. if (PlayerData.Instance().IsPracticeMode())
  265. {
  266. AudioManager.Instance().PlaySpeech("4602");
  267. }
  268. else if (PlayerData.Instance().CurrentMapOpenedType == MapOpenedType.Opened)
  269. {
  270. if (PlayerData.Instance().DidVisitMusicalMarblePatterns())
  271. {
  272. AudioManager.Instance().PlaySpeech("4301");
  273. PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.OpenedSpeechMarble;
  274. }
  275. }
  276. }
  277. }
  278. //================================================================================
  279. //
  280. //================================================================================
  281. public void Close()
  282. {
  283. if (AudioManager.Instance().IsPlayingSpeech("4601")
  284. || AudioManager.Instance().IsPlayingSpeech("4602")
  285. || AudioManager.Instance().IsPlayingSpeech("4503")
  286. || AudioManager.Instance().IsPlayingSpeech("4301")
  287. )
  288. {
  289. AudioManager.Instance().StopSpeech();
  290. }
  291. RestorePreviousFadeOutCallback();
  292. s_Instance.gameObject.SetActive(false);
  293. if (m_ParentScene != null)
  294. {
  295. m_ParentScene.RestoreColliders();
  296. m_ParentScene.OnMapClose();
  297. m_ParentScene = null;
  298. }
  299. }
  300. //================================================================================
  301. //
  302. //================================================================================
  303. public void ResetMapOpenedType()
  304. {
  305. PlayerData.Instance().CurrentMapOpenedType = MapOpenedType.None;
  306. }
  307. //================================================================================
  308. // Called when Options is closed.
  309. //================================================================================
  310. public void BackFromOptions()
  311. {
  312. s_Instance.gameObject.SetActive(true);
  313. }
  314. //================================================================================
  315. // Called when Options is opened.
  316. //================================================================================
  317. public void HideForOptions()
  318. {
  319. s_Instance.gameObject.SetActive(false);
  320. }
  321. //================================================================================
  322. //
  323. //================================================================================
  324. public bool IsOpened()
  325. {
  326. return s_Instance.gameObject.activeInHierarchy;
  327. }
  328. //================================================================================
  329. //
  330. //================================================================================
  331. public void EnableAll()
  332. {
  333. if (m_Navigation != null)
  334. {
  335. foreach (HUDMapNavigation navigation in m_Navigation)
  336. {
  337. navigation.Enable();
  338. }
  339. }
  340. if (m_HUD != null)
  341. {
  342. foreach (HUDCommon hud in m_HUD)
  343. {
  344. hud.Enable();
  345. }
  346. }
  347. }
  348. //================================================================================
  349. //
  350. //================================================================================
  351. public void DisableAll()
  352. {
  353. if (m_Navigation != null)
  354. {
  355. foreach (HUDMapNavigation navigation in m_Navigation)
  356. {
  357. navigation.Disable();
  358. }
  359. }
  360. if (m_HUD != null)
  361. {
  362. foreach (HUDCommon hud in m_HUD)
  363. {
  364. hud.Disable();
  365. }
  366. }
  367. }
  368. //================================================================================
  369. //
  370. //================================================================================
  371. void FinishedFadeOut()
  372. {
  373. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Calling Map.FinishedFadeOut()...");
  374. Close();
  375. Chester.Instance().CloseInstantly();
  376. KishiTechUnity.Loading.Loading.Instance().Show();
  377. // HUDMapNavigation started fade-out. When it is over, we load the level defined there (which is copied to m_LevelSelected).
  378. if (!string.IsNullOrEmpty(m_LevelSelected))
  379. {
  380. m_ChangedLocationFromMap = true;
  381. Application.LoadLevel(m_LevelSelected);
  382. }
  383. }
  384. //================================================================================
  385. //
  386. //================================================================================
  387. public void SetLevelSelected(string levelName)
  388. {
  389. m_LevelSelected = levelName;
  390. }
  391. //================================================================================
  392. //
  393. //================================================================================
  394. bool CheckHUD()
  395. {
  396. if (m_HUD != null)
  397. {
  398. foreach (HUDCommon hud in m_HUD)
  399. {
  400. if (hud.WasUpAsButton() || hud.DidExitWhileDown())
  401. {
  402. hud.ClearWasUpAsButton();
  403. hud.ClearDidExitWhileDown();
  404. return true;
  405. }
  406. }
  407. }
  408. return false;
  409. }
  410. //================================================================================
  411. //
  412. //================================================================================
  413. bool CheckNavigation(float x, float y)
  414. {
  415. if (m_Navigation != null)
  416. {
  417. foreach (HUDCommon navigation in m_Navigation)
  418. {
  419. if (navigation.WasUpAsButton() || navigation.DidExitWhileDown())
  420. {
  421. navigation.ClearWasUpAsButton();
  422. navigation.ClearDidExitWhileDown();
  423. return true;
  424. }
  425. }
  426. }
  427. return false;
  428. }
  429. //================================================================================
  430. //
  431. //================================================================================
  432. public void CheckAll(float x, float y)
  433. {
  434. // HUD priority is #1.
  435. bool clickedOnHUD = CheckHUD();
  436. if (!clickedOnHUD)
  437. {
  438. // Navigation priority is #2.
  439. bool clickedOnNavigation = CheckNavigation(x, y);
  440. if (!clickedOnNavigation)
  441. {
  442. if (m_BackgroundRectangle != null)
  443. {
  444. if (m_BackgroundRectangle.IsPointInside(x, y))
  445. {
  446. int textureX = (int)(x - gameObject.transform.position.x);
  447. int textureY = (int)y;//((Screen.height - y) - (-gameObject.transform.position.y));
  448. float alpha = m_MapBackground.sprite.texture.GetPixel(textureX, textureY).a;
  449. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("xy(" + x + ", " + y + "), tex(" + textureX + ", " + textureY + "), alpha: " + alpha);
  450. if (alpha < 0.0001f)
  451. {
  452. Close();
  453. }
  454. }
  455. else
  456. {
  457. Close();
  458. }
  459. }
  460. }
  461. }
  462. }
  463. //================================================================================
  464. //
  465. //================================================================================
  466. public bool ChangedLocationFromMap
  467. {
  468. get { return m_ChangedLocationFromMap; }
  469. set { m_ChangedLocationFromMap = value; }
  470. }
  471. } // public class Map : MonoBehaviour
  472. } // namespace ReaderRabbit