forked from cgvr/DeltaVR
119 lines
3.2 KiB
C#
119 lines
3.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using FMOD.Studio;
|
|
using System.Threading.Tasks;
|
|
|
|
public class ArcheryRangeNPC : NPCController
|
|
{
|
|
[Header("Archery Range Config")]
|
|
public MicrophoneStand microphoneStand;
|
|
public PushableButton imageGenerationButton;
|
|
public PushableButton modelGenerationButton;
|
|
public Image imageDisplay;
|
|
public ModelDisplay modelDisplay;
|
|
|
|
private Texture2D GeneratedTexture;
|
|
private EventInstance printingSound;
|
|
|
|
// states:
|
|
// 0 - idle
|
|
// 1 - player approached
|
|
// 2 - player used microphone
|
|
// 3 - player pressed the image generation button
|
|
// 4 - player pressed the model generation button
|
|
// 5 - model spawned into the game
|
|
private int state;
|
|
|
|
private void Awake()
|
|
{
|
|
state = 0;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
microphoneStand.OnPlayerFinishedSpeaking += OnPlayerUsedMicrophone;
|
|
imageGenerationButton.OnButtonPressed += OnImageGenerationButtonPressed;
|
|
modelGenerationButton.OnButtonPressed += OnModelGenerationButtonPressed;
|
|
|
|
printingSound = AudioManager.Instance.CreateInstance(FMODEvents.Instance.Printing);
|
|
printingSound.setParameterByName("3DPrinterPrintingJob", 1);
|
|
}
|
|
|
|
protected async override void OnPlayerApproach()
|
|
{
|
|
if (state == 0)
|
|
{
|
|
SpeakVoiceLine(0);
|
|
await Task.Delay(2000);
|
|
SpeakVoiceLine(1);
|
|
await Task.Delay(6500);
|
|
SpeakVoiceLine(2);
|
|
await Task.Delay(3000);
|
|
|
|
state = 1;
|
|
}
|
|
}
|
|
|
|
protected override void OnPlayerLeave()
|
|
{
|
|
Debug.Log("Alien NPC: player left");
|
|
}
|
|
|
|
private void OnPlayerUsedMicrophone()
|
|
{
|
|
if (state == 1)
|
|
{
|
|
SpeakVoiceLine(3);
|
|
state = 2;
|
|
}
|
|
}
|
|
|
|
private async void OnImageGenerationButtonPressed()
|
|
{
|
|
if (state == 2)
|
|
{
|
|
state = 3;
|
|
}
|
|
|
|
string inputPrompt = microphoneStand.GetTextOutput();
|
|
byte[] imageBytes = await InvokeAiClient.Instance.GenerateImage(inputPrompt);
|
|
GeneratedTexture = ModelGenerationUtils.CreateTexture(imageBytes);
|
|
Sprite sprite = ModelGenerationUtils.CreateSprite(GeneratedTexture);
|
|
imageDisplay.sprite = sprite;
|
|
|
|
imageGenerationButton.Deactivate();
|
|
if (state == 3)
|
|
{
|
|
modelGenerationButton.Deactivate();
|
|
SpeakVoiceLine(4);
|
|
}
|
|
}
|
|
|
|
private async void OnModelGenerationButtonPressed()
|
|
{
|
|
if (state == 3)
|
|
{
|
|
state = 4;
|
|
}
|
|
|
|
printingSound.start();
|
|
printingSound.setParameterByName("3DPrinterPrintingJob", 0);
|
|
|
|
string encodedTexture = Convert.ToBase64String(GeneratedTexture.EncodeToJPG());
|
|
byte[] encodedModel = await TrellisClient.Instance.GenerateModel(encodedTexture);
|
|
|
|
GameObject spawnedObject = await ModelGenerationUtils.Instance.SpawnModel(encodedModel);
|
|
modelDisplay.DisplayModel(spawnedObject);
|
|
|
|
if (state == 4)
|
|
{
|
|
SpeakVoiceLine(5);
|
|
state = 5;
|
|
}
|
|
|
|
printingSound.setParameterByName("3DPrinterPrintingJob", 1);
|
|
modelGenerationButton.Deactivate();
|
|
}
|
|
}
|