Skip to content

Commit

Permalink
changed to new dialogue system (breaks the old dialogue!)
Browse files Browse the repository at this point in the history
  • Loading branch information
pxntilde committed May 6, 2019
1 parent 2b56da5 commit 8bdc676
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 90 deletions.
129 changes: 76 additions & 53 deletions Assets/Scripts/DialogueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,102 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DialogueSystem;

[System.Serializable]
public class PortraitPair {
public string Key;
public Sprite Portrait;
}

public class DialogueManager : MonoBehaviour
//[System.Serializable]
//public class Dialogue
//{
// public List<MessagePair> Messages;
// public int Priority = 0;
// public bool IsInteruptable;
//}

namespace DialogueSystem
{
// Start is called before the first frame update
public GameObject DialogueCanvas;
public List<PortraitPair> Portraits;

bool AwaitingConfirmation = true;
public int WaitFrames = 0;
bool TimeUp = false;
bool isOpen = false;

public bool IsOpen {
get {
return isOpen;
}
}
void Start()
[System.Serializable]
public class DialogueManager : MonoBehaviour
{
// Start is called before the first frame update
public GameObject DialogueCanvas;
protected List<Dialogue> DialogueQueue = new List<Dialogue>();
protected Coroutine DialogueCoroutine;
public float WaitForInputTime = 7f;
public bool IsOpen { get; protected set; }

}
protected bool AdvanceScript = false;

// Update is called once per frame
void Update()
{
if (AwaitingConfirmation && Input.GetButton("PayRespects")) {
AwaitingConfirmation = false;
WaitFrames = 0;
};
if (WaitFrames < 800)


// Update is called once per frame
void Update()
{
WaitFrames++;
AdvanceScript = Input.GetButton("PayRespects");
}
else

/// <summary>
/// Adds a dialogue to the queue
/// </summary>
/// <param name="Dialogue">Dialogue to add</param>
public void AddToQueue(Dialogue Dialogue)
{
AwaitingConfirmation = false;
WaitFrames = 0;
DialogueQueue.Add(Dialogue.Clone());

if (DialogueCoroutine is null)
{
DialogueCoroutine = StartCoroutine(RunThroughDialogue());
}
}
}

public IEnumerator Write(string Message, string Character) {
var dialogue = Instantiate(DialogueCanvas);
var characterSprite = CharacterSpriteFor(Character);
protected IEnumerator RunThroughDialogue()
{
IsOpen = true;
if (DialogueQueue.Count == 0)
{
DialogueCoroutine = null;

var textBox = dialogue.transform.Find("Speech").GetComponent<Text>();
textBox.text = Message;
} else
{
MessagePair Message;

var portrait = dialogue.transform.Find("Portrait").GetComponent<Image>();
portrait.sprite = characterSprite;
var DialogueCanvas = Instantiate(this.DialogueCanvas);
var MessageBox = DialogueCanvas.transform.Find("Speech").GetComponent<Text>();
var Portrait = DialogueCanvas.transform.Find("Portrait").GetComponent<Image>();

// Immediately set the dialogue to the open state
isOpen = true;
yield return new WaitForSeconds(0.5f);
float EndTime;

// After a grace period, allow for confirmation
// (to avoid accientally confirming during the same button press)
AwaitingConfirmation = true;
yield return new WaitUntil(() => !AwaitingConfirmation);
while (DialogueQueue.Count > 0)
{
if (DialogueQueue[0].Messages.Count == 0)
{
DialogueQueue.Remove(DialogueQueue[0]);
continue;
}

// Destroy the instantiated dialogue and reset the manager state
Destroy(dialogue);
isOpen = false;
}

Sprite CharacterSpriteFor(string Character) {
// Debug.Log(Character);
// Debug.Log(Portraits);
return Portraits.Find(p => p.Key == Character)?.Portrait;
Message = DialogueQueue[0].Messages[0];
Portrait.sprite = Message.CharacterSprite;
MessageBox.text = Message.Message;

yield return new WaitForSeconds(.5f);

EndTime = Time.time + WaitForInputTime;

yield return new WaitUntil(() => Time.time >= EndTime || AdvanceScript);

DialogueQueue[0].Messages.Remove(Message);
}

Destroy(DialogueCanvas);
IsOpen = false;
DialogueCoroutine = null;

}
}
}
}
}
18 changes: 7 additions & 11 deletions Assets/Scripts/DialogueTester.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DialogueSystem;

public class DialogueTester : MonoBehaviour
{
Expand All @@ -12,16 +13,11 @@ void Start()
// Update is called once per frame
void Update()
{
var DialogueManager = gameObject.GetComponent<DialogueManager>();
Coroutine dialogue;
if (!DialogueManager.IsOpen && Input.GetButtonDown("PayRespects")) {
dialogue = StartCoroutine(SpawnDialogue());
}
}

IEnumerator SpawnDialogue() {
var DialogueManager = gameObject.GetComponent<DialogueManager>();
yield return DialogueManager.Write("This is a test for", "olympus");
yield return DialogueManager.Write("Speakonia", "olympus");
Debug.Log("Is this even being used?");
//var DialogueManager = gameObject.GetComponent<DialogueManager>();
//Coroutine dialogue;
//if (!DialogueManager.IsOpen && Input.GetButtonDown("PayRespects")) {
// dialogue = StartCoroutine(SpawnDialogue());
//}
}
}
34 changes: 8 additions & 26 deletions Assets/Scripts/DialogueTrigger.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DialogueSystem;


[System.Serializable]
public class MessagePair {
public string Character;
[TextArea]
public string Message;
}

[System.Serializable]
public class DialogueTrigger : MonoBehaviour
{
// Start is called before the first frame update
public List<MessagePair> Messages;
public Dialogue Dialogue;

public int MaxOccurrences = 1;
private int Occurrences = 0;
void Start()
{
gameObject.GetComponent<MeshRenderer>().enabled = false;
}

// Update is called once per frame
void Update()
{

}

/// <summary>
/// If the player collides with this trigger, start a dialogue sequence.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && Occurrences < MaxOccurrences) {
var dialogueManager = other.GetComponent<DialogueManager>();
if (!dialogueManager.IsOpen) {
Occurrences++;
StartCoroutine(RunThroughDialogue(dialogueManager));
}
}
}

IEnumerator RunThroughDialogue(DialogueManager dialogueManager) {
foreach (var message in Messages) {
yield return dialogueManager.Write(message.Message, message.Character);
if (other.tag == "Player" && Occurrences < MaxOccurrences)
{
other.GetComponent<DialogueManager>()?.AddToQueue(Dialogue.Clone());
}
}
}

0 comments on commit 8bdc676

Please sign in to comment.