Quantum-Engineering/ModMachine.cs

93 lines
3.8 KiB
C#
Raw Normal View History

2020-09-13 02:45:50 -04:00
using UnityEngine;
2021-05-16 14:12:13 -05:00
using UnityEngine.Networking;
using System;
using System.Collections;
using System.IO;
2020-09-13 02:45:50 -04:00
public class ModMachine : BasicMachine
{
private Material material;
2021-05-16 14:12:13 -05:00
private PlayerController playerController;
private GameManager gameManager;
private Coroutine modSoundCoroutine;
2020-09-13 02:45:50 -04:00
public string machineName;
2021-05-16 14:12:13 -05:00
public bool init;
2020-09-13 02:45:50 -04:00
2020-09-16 21:40:25 -05:00
//! Called by unity engine on start up to initialize variables.
2020-09-13 02:45:50 -04:00
public new void Start()
{
base.Start();
material = new Material(Shader.Find("Standard"));
2021-05-16 14:12:13 -05:00
playerController = GameObject.Find("Player").GetComponent<PlayerController>();
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
2020-09-13 02:45:50 -04:00
}
//! Called by MachineManager update coroutine.
2021-05-16 22:56:04 -04:00
public override void UpdateMachine()
2020-09-13 02:45:50 -04:00
{
2021-05-16 22:56:04 -04:00
base.UpdateMachine();
if (stateManager.initMachines == true && init == false)
{
2021-05-16 14:12:13 -05:00
BlockDictionary blockDictionary = playerController.GetComponent<BuildController>().blockDictionary;
if (blockDictionary != null)
2020-09-19 20:29:43 -05:00
{
2021-05-16 14:12:13 -05:00
if (blockDictionary.meshDictionary.ContainsKey(machineName) && GetComponent<MeshFilter>() != null)
{
GetComponent<MeshFilter>().mesh = blockDictionary.meshDictionary[machineName];
}
2020-09-19 20:29:43 -05:00
}
2021-05-16 14:12:13 -05:00
gameManager.meshManager.SetMaterial(gameObject, machineName);
modSoundCoroutine = StartCoroutine(GetAudioFile(this, GetComponent<AudioSource>(), machineName));
init = true;
}
if (recipes == null)
{
recipes = GameObject.Find("Player").GetComponent<BuildController>().blockDictionary.GetMachineRecipes(machineName);
}
}
//! Loads sound files.
public static IEnumerator GetAudioFile(ModMachine machine, AudioSource source, string machineName)
{
string modPath = Path.Combine(Application.persistentDataPath, "Mods");
Directory.CreateDirectory(modPath);
string[] modDirs = Directory.GetDirectories(modPath);
foreach (string path in modDirs)
{
string soundPath = path + "/Sounds/";
Directory.CreateDirectory(soundPath);
DirectoryInfo d = new DirectoryInfo(soundPath);
foreach (FileInfo file in d.GetFiles("*.wav"))
2020-09-19 20:29:43 -05:00
{
2021-05-16 14:12:13 -05:00
string filePath = soundPath + file.Name;
UriBuilder soundUriBuildier = new UriBuilder(filePath) { Scheme = "file" };
string url = soundUriBuildier.ToString();
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV))
{
yield return uwr.SendWebRequest();
if (!uwr.isNetworkError && !uwr.isHttpError)
{
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);
string soundName = file.Name.Remove(file.Name.Length - 4);
string modName = new DirectoryInfo(path).Name;
if (audioClip != null)
{
if (soundName == machineName)
{
audioClip.name = soundName;
source.clip = audioClip;
machine.hasCustomSound = true;
Debug.Log("Mod "+"["+modName+"]"+" loaded sound effect [" + soundName + ".wav] "+"for "+machineName);
}
}
else
{
Debug.Log("Mod "+"["+modName+"]"+" failed to load sound effect [" + soundName + ".wav] "+"for "+machineName);
}
}
}
2020-09-19 20:29:43 -05:00
}
2020-09-13 02:45:50 -04:00
}
}
}