Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI 입력, GameData 가져오는부분 최적화 작업 #128

Merged
merged 4 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions Project_Blind/Assets/Scripts/Core/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public void LoadGameData()
Debug.Log("New Data Created!");
_gameData = new GameData();
}

_gameData.MakeClueDict();
}
public void SaveGameData()
{
Expand All @@ -74,21 +76,31 @@ public void SaveGameData()
Debug.Log("Save Completed!");
}
#endregion
public bool GetClueItem(int itemId)
public bool AddClueItem(int itemId)
{
foreach(ClueInfo info in _gameData.clueSlotInfos)
{
if (info.itemId == itemId)
return false;
}
ClueInfo newInfo = new ClueInfo() { itemId = itemId, slot = UI_Clue.Size++ };
_gameData.clueSlotInfos.Add(newInfo);
ClueInfo clue = null;
_gameData.ClueInfoById.TryGetValue(itemId, out clue);
if (clue != null)
return false;

clue = new ClueInfo() { itemId = itemId, slot = UI_Clue.Size++ };
_gameData.AddClueItem(clue);
SaveGameData();
return true;
}
public void DeleteClueItem(int itemId)
{
ClueInfo clue = null;
_gameData.ClueInfoById.TryGetValue(itemId, out clue);
if (clue == null)
return;

_gameData.DeleteClueItem(clue);
SaveGameData();
}
public void ClearClueData()
{
_gameData.clueSlotInfos.Clear();
_gameData.ClearClueData();
SaveGameData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ IEnumerator GetDotDamage()
while (isOnLava)
{
// hp를 깎음
_damage.GetDamage(1f);
HpCenter.GetDamage(1f);
yield return new WaitForSeconds(0.5f);
}
DebuffOff();
Expand Down
18 changes: 18 additions & 0 deletions Project_Blind/Assets/Scripts/Core/UIManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blind;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand Down Expand Up @@ -29,6 +30,22 @@ public class UIManager : Manager<UIManager>

int _deviceWidth = Screen.width;
int _deviceHeight = Screen.height;

public Action KeyInputEvents = null;
private void Update()
{
BroadCastKeyInputEvents();
}
private void BroadCastKeyInputEvents()
{
if (KeyInputEvents == null)
return;

if (Input.anyKey == false)
return;

KeyInputEvents.Invoke();
}
public Define.Resolution Resolution
{
get
Expand Down Expand Up @@ -258,6 +275,7 @@ public void Clear()
CloseAllPopupUI();
CloseAllWorldSpaceUI();
CloseAllNormalUI();
KeyInputEvents = null;
SceneUI = null;
}
}
Expand Down
37 changes: 35 additions & 2 deletions Project_Blind/Assets/Scripts/Data/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Blind
{
[Serializable]
public class GameData
public partial class GameData
{
#region Settings
// Sound
Expand All @@ -26,7 +26,7 @@ public class GameData
#endregion

#region Clue
public List<ClueInfo> clueSlotInfos = new List<ClueInfo>();
public List<ClueInfo> clueInfos = new List<ClueInfo>();
#endregion
}
[Serializable]
Expand All @@ -35,5 +35,38 @@ public class ClueInfo
public int slot;
public int itemId;
}
public partial class GameData
{
#region ClueDict
public Dictionary<int, ClueInfo> ClueInfoBySlot { get; private set; } = new Dictionary<int, ClueInfo>();
public Dictionary<int, ClueInfo> ClueInfoById { get; private set; } = new Dictionary<int, ClueInfo>();
public void MakeClueDict()
{
foreach(ClueInfo clueInfo in clueInfos)
{
ClueInfoBySlot.Add(clueInfo.slot, clueInfo);
ClueInfoById.Add(clueInfo.itemId, clueInfo);
}
}
public void AddClueItem(ClueInfo clue)
{
clueInfos.Add(clue);
ClueInfoBySlot.Add(clue.slot, clue);
ClueInfoById.Add(clue.itemId, clue);
}
public void DeleteClueItem(ClueInfo clue)
{
clueInfos.Remove(clue);
ClueInfoBySlot.Remove(clue.slot);
ClueInfoById.Remove(clue.itemId);
}
public void ClearClueData()
{
clueInfos.Clear();
ClueInfoBySlot.Clear();
ClueInfoById.Clear();
}
#endregion
}
}

37 changes: 22 additions & 15 deletions Project_Blind/Assets/Scripts/MapObjects/GlowStone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ private void Start()
{

}

private void FixedUpdate()
{
if (InputController.Instance.Interaction.Down)
{
if (_isReady)
{
if (_ui != null)
_ui.CloseWorldSpaceUI();
DoInteraction();
}
}
}

/// <summary>
/// 이 함수 호출하면 켜집니다.
/// </summary>
Expand Down Expand Up @@ -76,6 +62,9 @@ protected override void OnTriggerEnter2D(Collider2D collision)
if (collision.gameObject.GetComponent<PlayerCharacter>() == null)
return;

UIManager.Instance.KeyInputEvents -= HandleKeyInput;
UIManager.Instance.KeyInputEvents += HandleKeyInput;

_ui = UIManager.Instance.ShowWorldSpaceUI<UI_TestInteraction>();
_ui.SetPosition(transform.position, Vector3.down * 3);
_isReady = true;
Expand All @@ -86,7 +75,10 @@ protected override void OnTriggerExit2D(Collider2D collision)
if (collision.gameObject.GetComponent<PlayerCharacter>() == null)
return;

_ui.CloseWorldSpaceUI();
UIManager.Instance.KeyInputEvents -= HandleKeyInput;

if (_ui != null)
_ui.CloseWorldSpaceUI();
_isReady = false;
}

Expand All @@ -96,6 +88,21 @@ public override void DoInteraction()
ActivateInvisibleFloor();
}

private void HandleKeyInput()
{
if (InputController.Instance.Interaction.Down)
{
if (_isReady)
{
if (_ui != null)
_ui.CloseWorldSpaceUI();
DoInteraction();

UIManager.Instance.KeyInputEvents -= HandleKeyInput;
}
}
}

private void ActivateInvisibleFloor()
{
GameObject[] floors = GameObject.FindGameObjectsWithTag("InvisibleFloor");
Expand Down
26 changes: 0 additions & 26 deletions Project_Blind/Assets/Scripts/Object/TestClue.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Project_Blind/Assets/Scripts/Object/TestClue.cs.meta

This file was deleted.

29 changes: 19 additions & 10 deletions Project_Blind/Assets/Scripts/TestKjh/InteractionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ public class InteractionTest : InteractionAble
int _x = 7;
int _y = 7;

private void FixedUpdate()
{
if (InputController.Instance.Interaction.Down)
{
if (_ui != null)
_ui.CloseWorldSpaceUI();
}
}

protected override void Init(int x = 5, int y = 5)
{
base.Init(_x, _y);
Expand All @@ -27,13 +18,31 @@ protected override void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.GetComponent<PlayerCharacter>() == null) return;
// UI_TestInteraction를 WorldSpace로 띄운다.

UIManager.Instance.KeyInputEvents -= HandleKeyInput;
UIManager.Instance.KeyInputEvents += HandleKeyInput;

_ui = UIManager.Instance.ShowWorldSpaceUI<UI_TestInteraction>();
_ui.SetPosition(gameObject.transform.position, Vector3.up * 3);
}
protected override void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.GetComponent<PlayerCharacter>() == null) return;
_ui.CloseWorldSpaceUI();

UIManager.Instance.KeyInputEvents -= HandleKeyInput;

if (_ui != null)
_ui.CloseWorldSpaceUI();
}
private void HandleKeyInput()
{
if (InputController.Instance.Interaction.Down)
{
if (_ui != null)
_ui.CloseWorldSpaceUI();

UIManager.Instance.KeyInputEvents -= HandleKeyInput;
}
}
public override void DoInteraction()
{
Expand Down
31 changes: 12 additions & 19 deletions Project_Blind/Assets/Scripts/UI/Normal/UI_Clue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,25 @@ enum Texts
Dictionary<int, Data.Clue> _cludData;
public static int Size { get; set; }
public List<UI_Clue_Item> Items { get; } = new List<UI_Clue_Item>();
List<ClueInfo> infos;
GameObject grid;
public override void Init()
{
Bind<Image>(typeof(Images));
Bind<Text>(typeof(Texts));
UIManager.Instance.KeyInputEvents -= HandleUIKeyInput;
UIManager.Instance.KeyInputEvents += HandleUIKeyInput;

infos = DataManager.Instance.GameData.clueSlotInfos;
_cludData = DataManager.Instance.ClueDict;

Items.Clear();

// 일단 아이템 슬롯들을 전부 날려준 뒤
//GameObject grid = transform.Find("Background").transform.Find("ItemGrid").gameObject;
grid = GetComponentInChildren<GridLayoutGroup>().gameObject;
foreach (Transform child in grid.transform)
Destroy(child.gameObject);

// SIZE만큼 슬롯들을 만들어서 Items에서 관리하도록 한다.
Size = DataManager.Instance.GameData.clueSlotInfos.Count;
Size = DataManager.Instance.GameData.clueInfos.Count;
for (int i = 0; i < Size; i++)
{
GameObject go = ResourceManager.Instance.Instantiate("UI/Normal/UI_Clue_Item", grid.transform);
Expand All @@ -71,10 +70,6 @@ void TestInit()

Get<Image>((int)Images.Image_TestClearClue).gameObject.BindEvent(PushTestClear, Define.UIEvent.Click);
}
private void Update()
{
HandleUIKeyInput();
}
private void HandleUIKeyInput()
{
if (!Input.anyKey)
Expand All @@ -92,20 +87,17 @@ private void HandleUIKeyInput()
// 현재 가지고 있는 단서들의 정보를 이용해서 UI를 갱신해준다.
public void RefreshUI()
{
Size = DataManager.Instance.GameData.clueSlotInfos.Count;
Size = DataManager.Instance.GameData.clueInfos.Count;
grid.GetComponent<RectTransform>().sizeDelta = new Vector2(750f, 220f * (float)Size);
for(int i = 0; i < Size; i++)
{
int itemId = -1;
foreach(ClueInfo info in infos)
{
// 아이템의 슬롯 정보가 현재 슬롯과 같을 때
if (i == info.slot)
{
itemId = info.itemId;
break;
}
}

ClueInfo info = null;
DataManager.Instance.GameData.ClueInfoBySlot.TryGetValue(i, out info);
if (info != null)
itemId = info.itemId;

// 각각의 슬롯에 아이템 ID를 넘겨줘서 갱신한다.
// 위에서 찾지 못하면 ID = -1이고 ID가 -1인 아이템은 없기 때문에
// 자동으로 null로 할당된다.
Expand All @@ -114,12 +106,13 @@ public void RefreshUI()
}
private void PushCloseButton()
{
UIManager.Instance.KeyInputEvents -= HandleUIKeyInput;
UIManager.Instance.CloseNormalUI(this);
}
private void PushTestImage(int id)
{
// 게임데이터에 아이템을 추가를 시도한다.
bool result = DataManager.Instance.GetClueItem(id);
bool result = DataManager.Instance.AddClueItem(id);
// 만약 성공한다면 슬롯을 하나 더 만들어주고 UI를 새로고침해준다.
if (result == true)
{
Expand Down
Loading