Skip to content

Instantly share code, notes, and snippets.

@kwea123
Created October 25, 2021 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwea123/fc1926d6a91cbbd38dac583ba0980fd0 to your computer and use it in GitHub Desktop.
Save kwea123/fc1926d6a91cbbd38dac583ba0980fd0 to your computer and use it in GitHub Desktop.
Interactive pumpkin weight guessing game with youtube api
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PumpkinsGame : MonoBehaviour
{
public GameObject pumpkins;
public float glowingSpeed;
public Material pumpkinMat;
public TextMeshPro queryText, pumpkinLine;
public GameObject queryBubble, pumpkinSpeechBubble, fireworks;
private bool isFading;
private YoutubeAPI youtubeAPI;
private string apiStartTime;
private bool interactiveMode = false, isQuerying = false, isGuessing = false;
private string[] defaultLines;
private int currentFrame = 0, lastReset = 0;
private const int weight = 419;
private HashSet<string> priviledgedAuthors; // authors that have infinte guesses
private Dictionary<string, int> remainingGuesses;
void Start()
{
pumpkinMat.SetFloat("_EmissiveExposureWeight", 0.1f);
isFading = true;
defaultLines = new string[] {"大家猜我多重呢~~", "嘿嘿~可沒那麼好猜~",
"猜對的人會有驚喜!", "提示嗎?沒有提示!"};
pumpkinLine.text = defaultLines[0];
youtubeAPI = GetComponent<YoutubeAPI>();
remainingGuesses = new Dictionary<string, int>();
priviledgedAuthors = new HashSet<string>();
priviledgedAuthors.Add("AI葵");
priviledgedAuthors.Add("Merak Channel 天璇");
priviledgedAuthors.Add("えつ");
StartCoroutine(Interaction());
}
IEnumerator Interaction()
{
while(true)
{
if (interactiveMode)
{
currentFrame++;
if (youtubeAPI.history.Count>0)
{
var tuple = youtubeAPI.history.Dequeue();
string author = tuple.Item1, snip = tuple.Item2, t = tuple.Item3;
t = t.Substring(11, 8);
if (t.CompareTo(apiStartTime) > 0) // comments later than API start time
{
if (snip.StartsWith("#"))
{
isQuerying = true;
int n;
try
{
n = int.Parse(snip.Substring(1));
}
catch(System.Exception)
{
n = 1;
}
queryBubble.SetActive(true);
queryText.text = $"{author}:\n除以{n}的餘數是?";
yield return new WaitForSeconds(3); // wait for answer
if (n >= 1 && n <= 20)
{
int r = weight % n;
pumpkinLine.text = $"我的重量除以{n}的餘數是{r}";
}
else
pumpkinLine.text = "除數只能在1到20之間!";
yield return new WaitForSeconds(3); // wait to clear the query
queryBubble.SetActive(false);
ResetPumpkinLine();
}
else if (snip.StartsWith("!"))
{
isGuessing = true;
int n;
try
{
n = int.Parse(snip.Substring(1));
}
catch(System.Exception)
{
n = 1;
}
queryBubble.SetActive(true);
queryText.text = $"{author}:\n答案是{n}嗎?";
yield return new WaitForSeconds(3); // wait for answer
if (!remainingGuesses.ContainsKey(author))
{
if (priviledgedAuthors.Contains(author))
remainingGuesses[author] = 1000;
else
remainingGuesses[author] = 3;
}
remainingGuesses[author] -= 1;
if (n==weight && remainingGuesses[author]>=0)
{
pumpkinLine.text = $"{author}答對了!";
youtubeAPI.isActive = interactiveMode = false; // deactivate api
yield return new WaitForSeconds(3); // wait to clear the query
queryBubble.SetActive(false);
pumpkinSpeechBubble.SetActive(false);
ChangePumpkinColor(new Color(0.0f, 32.0f, 0.0f, 32.0f));
yield return new WaitForSeconds(2); // wait to change color
ToggleFireworks();
}
else
{
if (remainingGuesses[author] < 0)
pumpkinLine.text = "你猜的機會已經用完了!";
else
pumpkinLine.text = $"不是{n}~\n你還剩{remainingGuesses[author]}次機會!";
ChangePumpkinColor(new Color(32.0f, 0.0f, 0.0f, 32.0f));
yield return new WaitForSeconds(3); // wait to clear the query
ChangePumpkinColor(new Color(32.0f, 32.0f, 32.0f, 32.0f));
queryBubble.SetActive(false);
ResetPumpkinLine();
}
}
}
}
if (!isQuerying && !isGuessing)
{
if (currentFrame-lastReset > 600)
{
ResetPumpkinLine();
lastReset = currentFrame;
}
}
isQuerying = isGuessing = false;
}
yield return null;
}
}
void ResetPumpkinLine()
{
pumpkinLine.text = defaultLines[Random.Range(0, defaultLines.Length)];
}
void Glow()
{
float e = pumpkinMat.GetFloat("_EmissiveExposureWeight");
if (isFading)
{
e = Mathf.Lerp(e, 0.8f, Time.deltaTime*glowingSpeed);
if (e > 0.79f)
{
e = 0.8f;
isFading = false;
}
}
else
{
e = Mathf.Lerp(e, 0.1f, Time.deltaTime*glowingSpeed);
if (e < 0.11f)
{
e = 0.1f;
isFading = true;
}
}
pumpkinMat.SetFloat("_EmissiveExposureWeight", e);
}
void ChangePumpkinColor(Color c)
{
pumpkinMat.SetColor("_EmissiveColor", c);
}
void ToggleFireworks()
{
fireworks.SetActive(!fireworks.activeInHierarchy);
}
void Update()
{
Glow();
if (Input.GetKeyDown(KeyCode.R))
{
youtubeAPI.isActive = interactiveMode = !interactiveMode;
apiStartTime = System.DateTime.UtcNow.ToString("HH:mm:ss");
pumpkinSpeechBubble.SetActive(interactiveMode);
}
if(Input.GetKeyDown(KeyCode.P))
{
// in case api doesn't work, play it manually
ChangePumpkinColor(new Color(32.0f, 32.0f, 32.0f, 32.0f));
ToggleFireworks();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment