Skip to content

Commit 3ea531c

Browse files
committed
feat: Make dialogue system singleton
1 parent d095aec commit 3ea531c

File tree

5 files changed

+34
-45
lines changed

5 files changed

+34
-45
lines changed

Assets/JCSUnity/Scripts/Examples/JCS_ScriptTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private void Start()
4444
{
4545
// use default.
4646
if (mDialogueSystem == null)
47-
mDialogueSystem = JCS_UtilManager.instance.GetDialogueSystem();
47+
mDialogueSystem = JCS_DialogueSystem.instance;
4848
}
4949

5050
private void Update()

Assets/JCSUnity/Scripts/Interfaces/JCS_DialogueScript.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,14 @@ public abstract class JCS_DialogueScript : ScriptableObject
1818
{
1919
/* Variables */
2020

21-
protected JCS_DialogueSystem mDialogueSystem = null;
22-
2321
[Separator("Runtime Variables (JCS_DialogueScript)")]
2422

2523
[Tooltip("use to design the pages.")]
2624
public int Status = -1;
2725

2826
/* Setter & Getter */
2927

30-
public JCS_DialogueSystem DialogueSystem
31-
{
32-
get
33-
{
34-
if (mDialogueSystem == null)
35-
mDialogueSystem = JCS_UtilManager.instance.GetDialogueSystem();
36-
37-
return this.mDialogueSystem;
38-
}
39-
}
40-
protected JCS_DialogueSystem ds { get { return this.DialogueSystem; } }
28+
public JCS_DialogueSystem ds { get { return JCS_DialogueSystem.instance; } }
4129

4230
/* Functions */
4331

@@ -77,5 +65,21 @@ public virtual void ResetAction()
7765
{
7866
Status = -1;
7967
}
68+
69+
/// <summary>
70+
/// Return true if the selection is yes.
71+
/// </summary>
72+
public bool IsYes(int selection)
73+
{
74+
return selection == 1;
75+
}
76+
77+
/// <summary>
78+
/// Return true if the selection is no.
79+
/// </summary>
80+
public bool IsNo(int selection)
81+
{
82+
return selection == 0;
83+
}
8084
}
8185
}

Assets/JCSUnity/Scripts/Managers/JCS_UtilManager.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public class JCS_UtilManager : JCS_Manager<JCS_UtilManager>
3030
[ReadOnly]
3131
private JCS_IGLogSystem mIGLogSystem = null;
3232

33-
[Tooltip("In Game Dialogue System.")]
34-
[SerializeField]
35-
[ReadOnly]
36-
private JCS_DialogueSystem mDialogueSystem = null;
37-
3833
[Separator("Initialize Variables (JCS_UtilManager)")]
3934

4035
[Tooltip("Trasnparent sprite.")]
@@ -49,16 +44,13 @@ public class JCS_UtilManager : JCS_Manager<JCS_UtilManager>
4944
public void SetIGLogSystem(JCS_IGLogSystem sys) { this.mIGLogSystem = sys; }
5045
public JCS_IGLogSystem GetIGLogSystem() { return this.mIGLogSystem; }
5146

52-
public void SetDiaglogueSystem(JCS_DialogueSystem ds) { this.mDialogueSystem = ds; }
53-
public JCS_DialogueSystem GetDialogueSystem() { return this.mDialogueSystem; }
54-
5547
public Sprite SpriteTransparent { get { return this.mSpriteTransparent; } }
5648

5749
/* Functions */
5850

5951
private void Awake()
6052
{
61-
instance = this;
53+
instance = this;
6254
}
6355
}
6456
}

Assets/JCSUnity/Scripts/UI/Dialogue/JCS_DialogueSystem.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace JCSUnity
1717
/// <summary>
1818
/// Dialogue system core implementation.
1919
/// </summary>
20-
public class JCS_DialogueSystem : MonoBehaviour
20+
public class JCS_DialogueSystem : JCS_Instance<JCS_DialogueSystem>
2121
{
2222
/* Variables */
2323

@@ -28,12 +28,15 @@ public class JCS_DialogueSystem : MonoBehaviour
2828
// disposed when executing the function `NextOrDispose`.
2929
public Func<bool> onNextOrDisposeCondition = null;
3030

31-
private bool mInitialized = false;
32-
3331
private JCS_DialogueScript mPreselectingScript = null;
3432

3533
[Separator("Check Variables (JCS_DialogueSystem)")]
3634

35+
[Tooltip("True if initialized.")]
36+
[SerializeField]
37+
[ReadOnly]
38+
private bool mInitialized = false;
39+
3740
[Tooltip("Script to run the current text box.")]
3841
[SerializeField]
3942
[ReadOnly]
@@ -252,12 +255,11 @@ public class JCS_DialogueSystem : MonoBehaviour
252255

253256
private void Awake()
254257
{
258+
instance = this;
259+
255260
// try to get transfrom by it own current transfrom.
256261
if (mPanelTrans == null)
257262
mPanelTrans = this.GetComponent<RectTransform>();
258-
259-
// set to manager to get manage.
260-
JCS_UtilManager.instance.SetDiaglogueSystem(this);
261263
}
262264

263265
private void Start()
@@ -387,7 +389,7 @@ public void ActiveDialogue(JCS_DialogueScript script)
387389

388390
if (mActive)
389391
{
390-
Debug.LogError("Dialogue System is already active!");
392+
Debug.LogWarning("Dialogue System is already active!");
391393
return;
392394
}
393395

@@ -449,7 +451,6 @@ public void SendChoice(int index, string msg)
449451
/// <summary>
450452
/// Next button is the only option for current status.
451453
/// </summary>
452-
/// <param name="msg"></param>
453454
public void SendNext(string msg)
454455
{
455456
NextBtnActive(true);
@@ -473,7 +474,6 @@ public void SendNext(string msg)
473474
/// <summary>
474475
/// Current status will be next and prev control/options.
475476
/// </summary>
476-
/// <param name="msg"></param>
477477
public void SendNextPrev(string msg)
478478
{
479479
NextBtnActive(true);
@@ -495,9 +495,8 @@ public void SendNextPrev(string msg)
495495
}
496496

497497
/// <summary>
498-
/// Okay button for only option.
498+
/// Sned okay button for only option.
499499
/// </summary>
500-
/// <param name="msg"></param>
501500
public void SendOk(string msg)
502501
{
503502
NextBtnActive(false);
@@ -519,9 +518,8 @@ public void SendOk(string msg)
519518
}
520519

521520
/// <summary>
522-
/// Yes/No options for current status.
521+
/// Send yes/no options for current status.
523522
/// </summary>
524-
/// <param name="msg"></param>
525523
public void SendYesNo(string msg)
526524
{
527525
NextBtnActive(false);
@@ -545,7 +543,6 @@ public void SendYesNo(string msg)
545543
/// <summary>
546544
/// Only exit button will be the only option.
547545
/// </summary>
548-
/// <param name="msg"></param>
549546
public void SendSimple(string msg)
550547
{
551548
NextBtnActive(false);
@@ -569,7 +566,6 @@ public void SendSimple(string msg)
569566
/// <summary>
570567
/// Accept/Decline options.
571568
/// </summary>
572-
/// <param name="msg"></param>
573569
public void SendAcceptDecline(string msg)
574570
{
575571
NextBtnActive(false);
@@ -592,10 +588,7 @@ public void SendAcceptDecline(string msg)
592588

593589
/// <summary>
594590
/// Send Empty option, expect selections take over it.
595-
///
596-
/// NOTE(jenchieh): Will better if use Gamepad/Controller/Joystick.
597591
/// </summary>
598-
/// <param name="msg"></param>
599592
public void SendEmpty(string msg)
600593
{
601594
NextBtnActive(false);
@@ -1229,11 +1222,11 @@ private void RightImageActive(bool act)
12291222
/// </summary>
12301223
private void ClearSelectBtnMessage()
12311224
{
1232-
for (int index = 0; index < mSelectMessage.Length; ++index)
1225+
JCS_Loop.Times(mSelectMessage.Length, (index) =>
12331226
{
12341227
mSelectMessage[index] = "";
12351228
mSelectBtn[index].ItText.text = "";
1236-
}
1229+
});
12371230
}
12381231

12391232
/// <summary>

Assets/JCSUnity/Scripts/UI/Dialogue/JCS_TalkObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void OnMouseOver()
3232
{
3333
if (JCS_Input.OnMouseDoubleClick(JCS_MouseButton.LEFT))
3434
{
35-
JCS_DialogueSystem ds = JCS_UtilManager.instance.GetDialogueSystem();
35+
var ds = JCS_DialogueSystem.instance;
3636

3737
if (ds != null)
3838
{
@@ -51,7 +51,7 @@ private void CreateCharacterImage(Sprite sp)
5151
if (sp == null)
5252
return;
5353

54-
54+
// ..
5555
}
5656
}
5757
}

0 commit comments

Comments
 (0)