capitalex revised this gist 6 months ago. Go to revision
1 file changed, 87 insertions
StoryEngine.cs(file created)
| @@ -0,0 +1,87 @@ | |||
| 1 | + | using System.Collections; | |
| 2 | + | using System.Collections.Generic; | |
| 3 | + | using UnityEngine; | |
| 4 | + | ||
| 5 | + | [RequireComponent(typeof(AudioSource))] | |
| 6 | + | public class StoryEngine : MonoBehaviour | |
| 7 | + | { | |
| 8 | + | public delegate void HandlePassage(StoryEngine engine, params Object[] args); | |
| 9 | + | Dictionary<string, HandlePassage> callbacks = new Dictionary<string, HandlePassage>(); | |
| 10 | + | Dictionary<string, AudioClip> audioClips = new Dictionary<string, AudioClip>(); | |
| 11 | + | Dictionary<string, Animator> actors = new Dictionary<string, Animator>(); | |
| 12 | + | AudioSource source; | |
| 13 | + | // Start is called before the first frame update | |
| 14 | + | ||
| 15 | + | void Start() | |
| 16 | + | { | |
| 17 | + | source = GetComponent<AudioSource>(); | |
| 18 | + | } | |
| 19 | + | ||
| 20 | + | ||
| 21 | + | public void LoudAudioClips(string path) | |
| 22 | + | { | |
| 23 | + | audioClips = new Dictionary<string, AudioClip>(); | |
| 24 | + | var clips = Resources.LoadAll<AudioClip>(path); | |
| 25 | + | foreach (var clip in clips) | |
| 26 | + | { | |
| 27 | + | audioClips.Add(clip.name, clip); | |
| 28 | + | } | |
| 29 | + | Debug.Log(audioClips); | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | public void AddActor(string path) | |
| 33 | + | { | |
| 34 | + | GameObject gameObject = GameObject.Find(path); | |
| 35 | + | Animator animator = gameObject.GetComponent<Animator>(); | |
| 36 | + | var result = path.Split('/'); | |
| 37 | + | string name = result[result.Length - 1]; | |
| 38 | + | if (animator) | |
| 39 | + | { | |
| 40 | + | actors.Add(name, animator); | |
| 41 | + | } | |
| 42 | + | } | |
| 43 | + | ||
| 44 | + | public void AddActor(string name, Animator animator) | |
| 45 | + | { | |
| 46 | + | actors.Add(name, animator); | |
| 47 | + | } | |
| 48 | + | ||
| 49 | + | public void PlayClip(string name) | |
| 50 | + | { | |
| 51 | + | source.clip = audioClips[name]; | |
| 52 | + | source.Play(); | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | public void SetActorTrigger(string actorName, string name) | |
| 56 | + | { | |
| 57 | + | actors[actorName].SetTrigger(name); | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | public float GetClipLength(string name) | |
| 61 | + | { | |
| 62 | + | return audioClips[name].length; | |
| 63 | + | } | |
| 64 | + | public void Register(string name, HandlePassage callback) | |
| 65 | + | { | |
| 66 | + | callbacks.Add(name, callback); | |
| 67 | + | } | |
| 68 | + | ||
| 69 | + | public void Run(string name, params Object[] args) | |
| 70 | + | { | |
| 71 | + | if (callbacks.ContainsKey(name)) { | |
| 72 | + | callbacks[name](this, args); | |
| 73 | + | } | |
| 74 | + | } | |
| 75 | + | ||
| 76 | + | public void Run(float delay, string name, params Object[] args) | |
| 77 | + | { | |
| 78 | + | StartCoroutine(WaitAndRun(delay, name, args)); | |
| 79 | + | } | |
| 80 | + | ||
| 81 | + | private IEnumerator WaitAndRun(float delay, string name, params Object[] args) | |
| 82 | + | { | |
| 83 | + | yield return new WaitForSeconds(delay); | |
| 84 | + | Run(name, args); | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | } | |
Newer
Older