131 lines
4.2 KiB
C#
131 lines
4.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using Platinio;
|
|
using UnityEngine.Playables;
|
|
using UnityEngine.SceneManagement;
|
|
using VRBeats.ScriptableEvents;
|
|
using VRSDK;
|
|
|
|
namespace VRBeats
|
|
{
|
|
public class VR_BeatManager : Singleton<VR_BeatManager>
|
|
{
|
|
[SerializeField] private BoxCollider playZone = null;
|
|
[SerializeField] private Transform player = null;
|
|
[SerializeField] private VR_BeatSettings settings = null;
|
|
private AudioManager audioManager = null;
|
|
private EnviromentController enviromentController = null;
|
|
private PlayableDirector playableDirector = null;
|
|
private bool isGameRunning = true;
|
|
|
|
public Color RightColor { get { return settings.RightColor * settings.GlowIntensity; } }
|
|
public Color LeftColor { get { return settings.LeftColor * settings.GlowIntensity; } }
|
|
public VR_BeatSettings GameSettings { get { return settings; } }
|
|
|
|
public Transform Player { get { return player; } }
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
audioManager = FindFirstObjectByType<AudioManager>();
|
|
enviromentController = FindFirstObjectByType<EnviromentController>();
|
|
playableDirector = FindFirstObjectByType<PlayableDirector>();
|
|
}
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
}
|
|
|
|
|
|
public Color GetColorFromColorSide(ColorSide side)
|
|
{
|
|
return side == ColorSide.Right ? RightColor : LeftColor;
|
|
}
|
|
|
|
public Color GetColorFromControllerType(VR_ControllerType controller)
|
|
{
|
|
return controller == VR_ControllerType.Right ? RightColor : LeftColor;
|
|
}
|
|
|
|
private int frame = 0;
|
|
private void Update()
|
|
{
|
|
frame++;
|
|
}
|
|
|
|
public void Spawn(Spawneable spawneable , SpawnEventInfo info)
|
|
{
|
|
if (!isGameRunning)
|
|
return;
|
|
|
|
Vector3 finalPosition = CalculateSpawnPosition( info.position);
|
|
Vector3 travelOffset = Vector3.forward * -settings.TargetTravelDistance;
|
|
Vector3 spawnPosition = finalPosition - travelOffset;
|
|
float travelTime = info.travelTimeOverride > 0f ? info.travelTimeOverride : settings.TargetTravelTime;
|
|
|
|
info.speed = settings.TargetTravelDistance / Mathf.Max(0.05f, travelTime);
|
|
SetSpeedRelativeToPlayZone(info);
|
|
|
|
Spawneable clone = Instantiate( spawneable , spawnPosition , Quaternion.Euler( info.rotation ) );
|
|
clone.Construct(info);
|
|
StartCoroutine(BeginContinuousSpawnNextFrame(clone));
|
|
}
|
|
|
|
private IEnumerator BeginContinuousSpawnNextFrame(Spawneable clone)
|
|
{
|
|
yield return null;
|
|
|
|
if (clone != null)
|
|
clone.OnSpawn();
|
|
}
|
|
|
|
private void SetSpeedRelativeToPlayZone(SpawnEventInfo info)
|
|
{
|
|
info.speedMultiplier = (int) Mathf.Sign(playZone.transform.forward.z * -1.0f);
|
|
}
|
|
|
|
private Vector3 CalculateSpawnPosition(Vector3 relativePosition)
|
|
{
|
|
Vector3 pos = CalculatePlayZoneCenter();
|
|
|
|
pos += Vector3.right * relativePosition.x * playZone.size.x;
|
|
pos += Vector3.up * relativePosition.y * playZone.size.y;
|
|
pos += Vector3.forward * relativePosition.z * playZone.size.z;
|
|
|
|
return pos;
|
|
}
|
|
|
|
private Vector3 CalculatePlayZoneCenter()
|
|
{
|
|
return playZone.transform.position + playZone.center;
|
|
}
|
|
|
|
public void GameOver()
|
|
{
|
|
//the game is already stopped
|
|
if (!isGameRunning)
|
|
{
|
|
return;
|
|
}
|
|
|
|
isGameRunning = false;
|
|
//slowdown the music to 0 and stop the playabledirector
|
|
audioManager.BlendAudioMixerPitch(1.0f , 0.0f).SetOnComplete( delegate {
|
|
if (playableDirector != null)
|
|
playableDirector.Stop();
|
|
}
|
|
).SetOwner(gameObject);
|
|
enviromentController.TurnLightsOff();
|
|
|
|
}
|
|
|
|
public void RestartLevel()
|
|
{
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
|
|
}
|
|
|
|
}
|