From 149673a0ce7bb0f98d9cc69af173ad69ae323913 Mon Sep 17 00:00:00 2001 From: Guilherme Hernandez Date: Sun, 29 Nov 2020 00:30:24 -0300 Subject: [PATCH] Improve switching between ROCamera and FreeflyCam (2) --- Assets/Scripts/Core.cs | 54 ++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/Assets/Scripts/Core.cs b/Assets/Scripts/Core.cs index f2ac18fc..ef482565 100644 --- a/Assets/Scripts/Core.cs +++ b/Assets/Scripts/Core.cs @@ -38,8 +38,7 @@ public static PathFindingManager PathFinding { private Hashtable configs = new Hashtable(); private static string CFG_NAME = "config.txt"; - private bool isMapSelectorEnabled = false; - private bool isRoCamEnabled = true; + private bool roCamEnabled; private void Awake() { if (Instance == null) { @@ -57,6 +56,7 @@ private void Awake() { void Start() { MapRenderer.SoundsMixerGroup = soundsMixerGroup; MapRenderer.WorldLight = worldLight; + roCamEnabled = MainCamera.GetComponent().enabled; LoadConfigs(); LoadGrf(); @@ -69,9 +69,16 @@ private void BuildMapSelector() { selector.buildDropdown(mapDropdown); Debug.Log($"Map list has {selector.GetMapList().Count} entries."); - if (!string.IsNullOrEmpty(mapname)) { + // do we have a map to load on startup + var preLoadMap = !string.IsNullOrEmpty(mapname); + + // there is a map to load on startup: load it + if (preLoadMap) { selector.ChangeMap(mapname); } + + // if a map is pre loaded, do not display map selector on startup + mapDropdown.gameObject.SetActive(!preLoadMap); } private void LoadGrf() { @@ -114,39 +121,46 @@ void FixedUpdate() { } void Update() { + if (mapRenderer.Ready) { + mapRenderer.Render(); + } + + // is map selector enabled + var mapSelectorEnabled = mapDropdown.gameObject.activeSelf; + // ESC pressed: toggle map selector visiblity if (Input.GetKeyDown(KeyCode.Escape)) { - isMapSelectorEnabled = !isMapSelectorEnabled; - mapDropdown.gameObject.SetActive(isMapSelectorEnabled); + + // toggle map selector + mapSelectorEnabled = !mapSelectorEnabled; + mapDropdown.gameObject.SetActive(mapSelectorEnabled); // disable cameras when map selector is visible - MainCamera.GetComponent().enabled = !isMapSelectorEnabled && isRoCamEnabled; - MainCamera.GetComponent().enabled = !isMapSelectorEnabled && !isRoCamEnabled; + MainCamera.GetComponent().enabled = !mapSelectorEnabled && roCamEnabled; + MainCamera.GetComponent().enabled = !mapSelectorEnabled && !roCamEnabled; // disable entity when map selector is visible - MainCamera.GetComponentInParent().enabled = !isMapSelectorEnabled; + MainCamera.GetComponentInParent().enabled = !mapSelectorEnabled; } // F1 pressed and not on map selector: switch between ROCamera and FreeflyCam - if (Input.GetKeyDown(KeyCode.F1) && !isMapSelectorEnabled) { - isRoCamEnabled = !isRoCamEnabled; - MainCamera.GetComponent().enabled = isRoCamEnabled; - MainCamera.GetComponent().enabled = !isRoCamEnabled; + else if (Input.GetKeyDown(KeyCode.F1) && !mapSelectorEnabled) { + + // switch cameras + roCamEnabled = !roCamEnabled; + MainCamera.GetComponent().enabled = roCamEnabled; + MainCamera.GetComponent().enabled = !roCamEnabled; - Cursor.lockState = isRoCamEnabled ? CursorLockMode.None : Cursor.lockState; - Cursor.visible = isRoCamEnabled; + // handle cursor + Cursor.lockState = roCamEnabled ? CursorLockMode.None : Cursor.lockState; + Cursor.visible = roCamEnabled; // switched to ROCamera: updated it so we go from wherever // freefly is to where ROCam should be - if (isRoCamEnabled) { + if (roCamEnabled) { MainCamera.GetComponent().Start(); } } - - - if (mapRenderer.Ready) { - mapRenderer.Render(); - } } public void OnPostRender() {