HeavenStudio/Assets/Scripts/Games/MrUpbeat/MrUpbeat.cs

180 lines
6 KiB
C#
Raw Normal View History

2022-03-04 20:47:40 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2022-03-04 20:47:40 +00:00
using UnityEngine;
using System;
using Starpelly;
2022-03-14 14:21:05 +00:00
using HeavenStudio.Util;
2022-03-04 20:47:40 +00:00
namespace HeavenStudio.Games.Loaders
{
using static Minigames;
public static class AgbUpbeatLoader
{
public static Minigame AddGame(EventCaller eventCaller) {
return new Minigame("mrUpbeat", "Mr. Upbeat", "FFFFFF", false, false, new List<GameAction>()
{
new GameAction("prepare", delegate { MrUpbeat.instance.SetInterval(eventCaller.currentEntity.beat); }, 0.5f, true, inactiveFunction: delegate { MrUpbeat.Beep(eventCaller.currentEntity.beat, eventCaller.currentEntity.length); }),
new GameAction("go", delegate { MrUpbeat.instance.Go(eventCaller.currentEntity.beat); }, 4f, true),
new GameAction("ding!", delegate { MrUpbeat.instance.Ding(eventCaller.currentEntity.toggle); }, 0.5f, parameters: new List<Param>()
{
new Param("toggle", false, "Applause")
}),
});
}
}
}
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Games
2022-03-04 20:47:40 +00:00
{
2022-03-12 04:10:13 +00:00
using Scripts_MrUpbeat;
2022-03-04 20:47:40 +00:00
public class MrUpbeat : Minigame
{
[Header("References")]
public GameObject metronome;
public UpbeatMan man;
public GameObject bt;
private static MultiSound beeps; //only used when this game isn't active.
2022-03-05 03:10:10 +00:00
public GameEvent beat = new GameEvent();
public bool canGo = false;
2022-03-06 17:34:54 +00:00
public int beatCount = 0;
2022-03-04 20:47:40 +00:00
public float beatOffset = 0f;
2022-03-04 20:47:40 +00:00
public static MrUpbeat instance;
private void Awake()
{
instance = this;
2022-03-06 17:34:54 +00:00
canGo = false;
man.stepTimes = 0;
SetInterval(0);
var pos = Conductor.instance.songPositionInBeats;
StartCoroutine(Upbeat(pos - Mathf.Round(pos)));
List<float> gos = GameManager.instance.Beatmap.entities.FindAll(c => c.datamodel == "mrUpbeat/go").Select(c => c.beat).ToList();
if (gos.Count > 0)
{
var nextInterval = gos.IndexOf(Mathp.GetClosestInList(gos, Conductor.instance.songPositionInBeats));
beatOffset = gos[nextInterval];
}
2022-03-06 17:34:54 +00:00
}
2022-03-05 03:10:10 +00:00
2022-03-06 17:34:54 +00:00
private void Update()
{
List<Beatmap.Entity> gos = GameManager.instance.Beatmap.entities.FindAll(c => c.datamodel == "mrUpbeat/go");
for (int i = 0; i < gos.Count; i++)
{
if ((gos[i].beat - 0.15f) <= Conductor.instance.songPositionInBeats && (gos[i].beat + gos[i].length) - 0.15f > Conductor.instance.songPositionInBeats)
{
canGo = true;
break;
} else
{
canGo = false;
}
}
2022-03-06 17:34:54 +00:00
if (canGo)
{
var songPos = Conductor.instance.songPositionInBeats - beatOffset;
2022-03-12 16:57:46 +00:00
metronome.transform.eulerAngles = new Vector3(0, 0, 270 - Mathf.Cos(Mathf.PI * songPos) * 60);
2022-03-06 17:34:54 +00:00
}
if (Conductor.instance.ReportBeat(ref beat.lastReportedBeat))
2022-03-05 03:10:10 +00:00
{
StartCoroutine(Upbeat());
if (canGo)
{
if (beatCount % 2 == 0)
Jukebox.PlayOneShotGame("mrUpbeat/metronomeRight");
else
Jukebox.PlayOneShotGame("mrUpbeat/metronomeLeft");
2022-03-05 03:10:10 +00:00
Beat(Mathf.Round(Conductor.instance.songPositionInBeats));
}
2022-03-05 03:10:10 +00:00
}
}
public override void OnGameSwitch(float beat)
{
foreach (Beatmap.Entity entity in GameManager.instance.Beatmap.entities)
{
if (entity.beat > beat) //the list is sorted based on the beat of the entity, so this should work fine.
{
break;
}
if (entity.datamodel != "mrUpbeat/prepare" || entity.beat + entity.length < beat) //check for prepares that happen before the switch
{
continue;
}
SetInterval(entity.beat);
break;
}
if(beeps != null)
{
beeps.Delete(); //the beeps are only for when the game isn't active
beeps = null;
}
}
public void SetInterval(float beat)
{
2022-03-05 03:10:10 +00:00
beatCount = 0;
man.targetBeat = beat + 320f;
man.Idle();
}
public void Go(float beat)
{
beatCount = 0;
}
2022-03-04 20:47:40 +00:00
2022-03-06 19:48:39 +00:00
public void Ding(bool applause)
{
Jukebox.PlayOneShotGame("mrUpbeat/ding");
if (applause)
Jukebox.PlayOneShot("applause");
2022-03-06 19:48:39 +00:00
}
public void Beat(float beat)
{
beatCount++;
GameObject _beat = Instantiate(bt);
_beat.transform.parent = bt.transform.parent;
_beat.SetActive(true);
UpbeatStep s = _beat.GetComponent<UpbeatStep>();
s.startBeat = beat;
s.beatOffset = beatOffset;
}
private IEnumerator Upbeat(float offset = 0)
{
yield return new WaitForSeconds(Conductor.instance.secPerBeat * 0.5f - offset);
man.Blip();
}
public static void Beep(float beat, float length)
{
if(GameManager.instance.currentGame == "mrUpbeat") //this function is only meant for making beeps while the game is inactive
{
return;
}
if (beeps != null)
{
beeps.Delete();
}
MultiSound.Sound[] beepSounds = new MultiSound.Sound[Mathf.CeilToInt(length)];
for(int i = 0; i < beepSounds.Length; i++)
{
beepSounds[i] = new MultiSound.Sound("mrUpbeat/blip", beat + 0.5f + i);
}
beeps = MultiSound.Play(beepSounds, forcePlay:true);
}
2022-03-04 20:47:40 +00:00
}
}