2021-12-24 03:36:16 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2023-01-05 04:04:31 +00:00
|
|
|
using HeavenStudio.Util;
|
|
|
|
|
2022-03-14 14:21:05 +00:00
|
|
|
namespace HeavenStudio.Games
|
2021-12-24 03:36:16 +00:00
|
|
|
{
|
|
|
|
public class Minigame : MonoBehaviour
|
|
|
|
{
|
2023-01-14 04:53:25 +00:00
|
|
|
public static float earlyTime = 0.07f, perfectTime = 0.04f, aceEarlyTime = 0.01f, aceLateTime = 0.01f, lateTime = 0.04f, endTime = 0.07f;
|
2023-01-05 04:04:31 +00:00
|
|
|
[SerializeField] public SoundSequence.SequenceKeyValue[] SoundSequences;
|
|
|
|
|
2021-12-29 06:52:48 +00:00
|
|
|
public List<Minigame.Eligible> EligibleHits = new List<Minigame.Eligible>();
|
2021-12-25 12:16:40 +00:00
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
public class Eligible
|
|
|
|
{
|
|
|
|
public GameObject gameObject;
|
|
|
|
public bool early;
|
|
|
|
public bool perfect;
|
|
|
|
public bool late;
|
2022-01-23 03:40:53 +00:00
|
|
|
public bool notPerfect() { return early || late; }
|
|
|
|
public bool eligible() { return early || perfect || late; }
|
2022-01-17 05:00:26 +00:00
|
|
|
public float createBeat;
|
2021-12-25 12:16:40 +00:00
|
|
|
}
|
2021-12-25 02:37:03 +00:00
|
|
|
|
2022-05-04 18:05:51 +00:00
|
|
|
public List<PlayerActionEvent> scheduledInputs = new List<PlayerActionEvent>();
|
|
|
|
|
2022-06-24 00:05:27 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Schedule an Input for a later time in the minigame. Executes the methods put in parameters
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="startBeat">When the scheduling started (in beats)</param>
|
|
|
|
/// <param name="timer">How many beats later should the input be expected</param>
|
|
|
|
/// <param name="inputType">The type of the input that's expected (Press, Release, A, B, Directions>)</param>
|
|
|
|
/// <param name="OnHit">Method to run if the Input has been Hit</param>
|
|
|
|
/// <param name="OnMiss">Method to run if the Input has been Missed</param>
|
|
|
|
/// <param name="OnBlank">Method to run whenever there's an Input while this is Scheduled (Shouldn't be used too much)</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public PlayerActionEvent ScheduleInput(
|
|
|
|
float startBeat,
|
2022-05-03 20:36:55 +00:00
|
|
|
float timer,
|
|
|
|
InputType inputType,
|
|
|
|
PlayerActionEvent.ActionEventCallbackState OnHit,
|
|
|
|
PlayerActionEvent.ActionEventCallback OnMiss,
|
|
|
|
PlayerActionEvent.ActionEventCallback OnBlank)
|
|
|
|
{
|
|
|
|
|
|
|
|
GameObject evtObj = new GameObject("ActionEvent" + (startBeat+timer));
|
|
|
|
evtObj.AddComponent<PlayerActionEvent>();
|
|
|
|
|
|
|
|
PlayerActionEvent evt = evtObj.GetComponent<PlayerActionEvent>();
|
|
|
|
|
|
|
|
evt.startBeat = startBeat;
|
|
|
|
evt.timer = timer;
|
|
|
|
evt.inputType = inputType;
|
|
|
|
evt.OnHit = OnHit;
|
|
|
|
evt.OnMiss = OnMiss;
|
|
|
|
evt.OnBlank = OnBlank;
|
|
|
|
|
2022-05-04 18:05:51 +00:00
|
|
|
evt.OnDestroy = RemoveScheduledInput;
|
|
|
|
|
2022-05-03 20:36:55 +00:00
|
|
|
evt.canHit = true;
|
|
|
|
evt.enabled = true;
|
|
|
|
|
|
|
|
evt.transform.parent = this.transform.parent;
|
|
|
|
|
|
|
|
evtObj.SetActive(true);
|
|
|
|
|
2022-05-04 18:05:51 +00:00
|
|
|
scheduledInputs.Add(evt);
|
|
|
|
|
2022-05-03 20:36:55 +00:00
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
2022-05-06 20:05:19 +00:00
|
|
|
public PlayerActionEvent ScheduleAutoplayInput(float startBeat,
|
|
|
|
float timer,
|
|
|
|
InputType inputType,
|
|
|
|
PlayerActionEvent.ActionEventCallbackState OnHit,
|
|
|
|
PlayerActionEvent.ActionEventCallback OnMiss,
|
|
|
|
PlayerActionEvent.ActionEventCallback OnBlank)
|
|
|
|
{
|
|
|
|
PlayerActionEvent evt = ScheduleInput(startBeat, timer, inputType, OnHit, OnMiss, OnBlank);
|
|
|
|
evt.autoplayOnly = true;
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerActionEvent ScheduleUserInput(float startBeat,
|
|
|
|
float timer,
|
|
|
|
InputType inputType,
|
|
|
|
PlayerActionEvent.ActionEventCallbackState OnHit,
|
|
|
|
PlayerActionEvent.ActionEventCallback OnMiss,
|
|
|
|
PlayerActionEvent.ActionEventCallback OnBlank)
|
|
|
|
{
|
|
|
|
PlayerActionEvent evt = ScheduleInput(startBeat, timer, inputType, OnHit, OnMiss, OnBlank);
|
|
|
|
evt.noAutoplay = true;
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-04 18:37:52 +00:00
|
|
|
//Clean up method used whenever a PlayerActionEvent has finished
|
2022-05-04 18:05:51 +00:00
|
|
|
public void RemoveScheduledInput(PlayerActionEvent evt)
|
|
|
|
{
|
|
|
|
scheduledInputs.Remove(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the scheduled input that should happen the **Soonest**
|
|
|
|
//Can return null if there's no scheduled inputs
|
2023-01-12 21:28:23 +00:00
|
|
|
// remark: need a check for specific button(s)
|
2023-01-14 04:53:25 +00:00
|
|
|
public PlayerActionEvent GetClosestScheduledInput(InputType input = InputType.ANY)
|
2022-05-04 18:05:51 +00:00
|
|
|
{
|
|
|
|
PlayerActionEvent closest = null;
|
|
|
|
|
|
|
|
foreach(PlayerActionEvent toCompare in scheduledInputs)
|
|
|
|
{
|
2023-01-15 04:33:37 +00:00
|
|
|
// ignore inputs that are for sequencing in autoplay
|
|
|
|
if (toCompare.autoplayOnly) continue;
|
|
|
|
|
2022-05-04 18:05:51 +00:00
|
|
|
if(closest == null)
|
|
|
|
{
|
2023-01-14 04:53:25 +00:00
|
|
|
if (input == InputType.ANY || toCompare.inputType.HasFlag(input))
|
|
|
|
closest = toCompare;
|
2022-05-04 18:05:51 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
float t1 = closest.startBeat + closest.timer;
|
|
|
|
float t2 = toCompare.startBeat + toCompare.timer;
|
|
|
|
|
2022-06-09 03:35:15 +00:00
|
|
|
// Debug.Log("t1=" + t1 + " -- t2=" + t2);
|
2022-05-04 18:05:51 +00:00
|
|
|
|
2023-01-14 04:53:25 +00:00
|
|
|
if (t2 < t1)
|
|
|
|
{
|
|
|
|
if (input == InputType.ANY || toCompare.inputType.HasFlag(input))
|
|
|
|
closest = toCompare;
|
|
|
|
}
|
2022-05-04 18:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
2022-05-04 18:37:52 +00:00
|
|
|
//Hasn't been tested yet. *Should* work.
|
|
|
|
//Can be used to detect if the user is expected to input something now or not
|
|
|
|
//Useful for strict call and responses games like Tambourine
|
2023-01-14 04:53:25 +00:00
|
|
|
public bool IsExpectingInputNow(InputType wantInput = InputType.ANY)
|
2022-05-04 18:37:52 +00:00
|
|
|
{
|
2023-01-14 04:53:25 +00:00
|
|
|
PlayerActionEvent input = GetClosestScheduledInput(wantInput);
|
2022-05-04 18:37:52 +00:00
|
|
|
if (input == null) return false;
|
|
|
|
return input.IsExpectingInputNow();
|
|
|
|
}
|
|
|
|
|
2022-06-04 03:15:56 +00:00
|
|
|
// now should fix the fast bpm problem
|
2021-12-25 02:37:03 +00:00
|
|
|
public static float EarlyTime()
|
|
|
|
{
|
2022-06-04 03:15:56 +00:00
|
|
|
return 1f - ScaleTimingMargin(earlyTime);
|
2021-12-25 02:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static float PerfectTime()
|
|
|
|
{
|
2022-06-04 03:15:56 +00:00
|
|
|
return 1f - ScaleTimingMargin(perfectTime);
|
2021-12-25 02:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static float LateTime()
|
|
|
|
{
|
2022-06-04 03:15:56 +00:00
|
|
|
return 1f + ScaleTimingMargin(lateTime);
|
2021-12-25 02:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static float EndTime()
|
|
|
|
{
|
2022-06-04 03:15:56 +00:00
|
|
|
return 1f + ScaleTimingMargin(endTime);
|
|
|
|
}
|
|
|
|
|
2023-01-14 04:53:25 +00:00
|
|
|
public static float AceStartTime()
|
|
|
|
{
|
|
|
|
return 1f + ScaleTimingMargin(aceEarlyTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float AceEndTime()
|
|
|
|
{
|
|
|
|
return 1f + ScaleTimingMargin(aceLateTime);
|
|
|
|
}
|
|
|
|
|
2022-06-04 03:15:56 +00:00
|
|
|
//scales timing windows to the BPM in an ""intelligent"" manner
|
|
|
|
static float ScaleTimingMargin(float f)
|
|
|
|
{
|
|
|
|
float bpm = Conductor.instance.songBpm * Conductor.instance.musicSource.pitch;
|
|
|
|
float a = bpm / 120f;
|
|
|
|
float b = (Mathf.Log(a) + 2f) * 0.5f;
|
|
|
|
float r = Mathf.Lerp(a, b, 0.25f);
|
|
|
|
return r * f;
|
2021-12-25 02:37:03 +00:00
|
|
|
}
|
2021-12-24 23:41:35 +00:00
|
|
|
|
2021-12-24 03:36:16 +00:00
|
|
|
public int firstEnable = 0;
|
|
|
|
|
2022-03-08 04:46:49 +00:00
|
|
|
public virtual void OnGameSwitch(float beat)
|
2021-12-24 03:36:16 +00:00
|
|
|
{
|
2022-03-07 02:37:27 +00:00
|
|
|
//Below is a template that can be used for handling previous entities.
|
2022-03-02 21:59:35 +00:00
|
|
|
//section below is if you only want to look at entities that overlap the game switch
|
2022-03-07 02:37:27 +00:00
|
|
|
/*
|
|
|
|
List<Beatmap.Entity> prevEntities = GameManager.instance.Beatmap.entities.FindAll(c => c.beat <= beat && c.datamodel.Split(0) == [insert game name]);
|
2022-03-02 21:59:35 +00:00
|
|
|
foreach(Beatmap.Entity entity in prevEntities)
|
|
|
|
{
|
|
|
|
if(entity.beat + entity.length >= beat)
|
|
|
|
{
|
2022-03-07 02:37:27 +00:00
|
|
|
EventCaller.instance.CallEvent(entity, true);
|
2022-03-02 21:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-07 02:37:27 +00:00
|
|
|
*/
|
2021-12-24 03:36:16 +00:00
|
|
|
}
|
2021-12-26 05:11:54 +00:00
|
|
|
|
|
|
|
public virtual void OnTimeChange()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2022-01-17 05:00:26 +00:00
|
|
|
|
2022-09-18 20:48:14 +00:00
|
|
|
public virtual void OnPlay(float beat)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-17 05:00:26 +00:00
|
|
|
public int MultipleEventsAtOnce()
|
|
|
|
{
|
|
|
|
int sameTime = 0;
|
|
|
|
for (int i = 0; i < EligibleHits.Count; i++)
|
|
|
|
{
|
|
|
|
float createBeat = EligibleHits[i].createBeat;
|
|
|
|
if (EligibleHits.FindAll(c => c.createBeat == createBeat).Count > 0)
|
|
|
|
{
|
|
|
|
sameTime += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sameTime == 0 && EligibleHits.Count > 0)
|
|
|
|
sameTime = 1;
|
|
|
|
|
|
|
|
return sameTime;
|
|
|
|
}
|
2023-01-05 04:04:31 +00:00
|
|
|
|
2023-01-12 01:42:12 +00:00
|
|
|
public static MultiSound PlaySoundSequence(string game, string name, float startBeat, params SoundSequence.SequenceParams[] args)
|
2023-01-05 04:04:31 +00:00
|
|
|
{
|
2023-01-12 01:42:12 +00:00
|
|
|
Minigames.Minigame gameInfo = GameManager.instance.GetGameInfo(game);
|
|
|
|
foreach (SoundSequence.SequenceKeyValue pair in gameInfo.LoadedSoundSequences)
|
2023-01-05 04:04:31 +00:00
|
|
|
{
|
|
|
|
if (pair.name == name)
|
|
|
|
{
|
2023-01-12 01:42:12 +00:00
|
|
|
// Debug.Log($"Playing sound sequence {name} at beat {startBeat}");
|
2023-01-05 04:04:31 +00:00
|
|
|
return pair.sequence.Play(startBeat);
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 01:42:12 +00:00
|
|
|
Debug.LogWarning($"Sound sequence {name} not found in game {game} (did you build AssetBundles?)");
|
2023-01-05 04:04:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-12-24 03:36:16 +00:00
|
|
|
}
|
2022-06-24 00:05:27 +00:00
|
|
|
}
|