Files
BeatSaber/Assets/VRBeatsKit/Scripts/Core/VR_BeatCube.cs
T

124 lines
3.8 KiB
C#
Raw Normal View History

using DamageSystem;
using UnityEngine;
using VRBeats.ScriptableEvents;
namespace VRBeats
{
public class VR_BeatCube : MonoBehaviour
{
[SerializeField] private float minCutSpeed = 0.5f;
[SerializeField] private float maxCutAngle = 40f;
[SerializeField] private GameEvent onCorrectSlice = null;
[SerializeField] private GameEvent onIncorrectSlice = null;
[SerializeField] private GameEvent onPlayerMiss = null;
private MaterialBindings materialBindings = null;
private ColorSide thisColorSide = ColorSide.Right;
private Transform player = null;
private VR_BeatCubeSpawneable thisSpawneable = null;
private bool canBeKilled = true;
private bool spawnComplete = false;
private bool destroyed = false;
public float MinCutSpeed { get { return minCutSpeed; } }
public Direction HitDirection { get { return thisSpawneable.HitDirection; } }
public ColorSide ThisColorSide { get { return thisColorSide; } }
private void Awake()
{
player = VR_BeatManager.instance.Player.transform;
}
public void Start()
{
thisSpawneable = GetComponent<VR_BeatCubeSpawneable>();
thisSpawneable.onSpawnComplete += delegate { spawnComplete = true; };
materialBindings = GetComponent<MaterialBindings>();
thisColorSide = thisSpawneable.ColorSide;
Color color = VR_BeatManager.instance.GetColorFromColorSide(thisColorSide);
materialBindings.SetEmmisiveColor( color );
}
private void OnDestroy()
{
destroyed = true;
}
public void OnCut(DamageInfo info)
{
canBeKilled = false;
//notify to whoever is listening that the player did a correct/incorrect slice
if ( IsCutIntentValid(info as BeatDamageInfo) )
{
2026-05-28 19:01:20 +09:00
ScoreManager.ReportSliceTiming(GetTimingErrorSeconds());
onCorrectSlice.Invoke();
}
else
{
2026-05-28 19:01:20 +09:00
ScoreManager.ReportMiss();
onIncorrectSlice.Invoke();
}
}
public bool IsCutIntentValid(BeatDamageInfo info)
{
if (info == null) return false;
if (info.velocity < minCutSpeed) return false;
//no matter the hit direction as soon as we have the right velocity for a cube that has a dot
if (HitDirection == Direction.Center)
return true;
float cutAngle = Vector2.Angle(transform.up, info.hitDir);
return info.colorSide == ThisColorSide && cutAngle < maxCutAngle;
}
private void Update()
{
if(spawnComplete)
transform.position += Vector3.forward * thisSpawneable.Speed * Time.deltaTime;
if ( ShouldKillCube() )
{
Kill();
}
}
private bool ShouldKillCube()
{
return canBeKilled && transform.position.z < player.position.z - 2.0f;
}
public void Kill()
{
2026-05-28 19:01:20 +09:00
ScoreManager.ReportMiss();
onPlayerMiss.Invoke();
canBeKilled = false;
transform.ScaleTween(Vector3.zero, 2.0f).SetEase(Ease.EaseOutExpo).SetOnComplete( delegate
{
if(!destroyed)
Destroy(gameObject);
} );
}
2026-05-28 19:01:20 +09:00
private float GetTimingErrorSeconds()
{
float speed = Mathf.Max(Mathf.Abs(thisSpawneable.Speed), 0.001f);
float distanceFromPlayer = Mathf.Abs(transform.position.z - player.position.z);
return distanceFromPlayer / speed;
}
}
}