87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class Spinner : MonoBehaviour
|
|
{
|
|
|
|
public static int LandedNumber{get; private set;}
|
|
public static UnityEvent<int> OnSpinStopped = new UnityEvent<int>();
|
|
public Transform wheel;
|
|
public Vector3 rotationAxis;
|
|
public float CurrentRotation { get{ return (wheel.rotation * rotationAxis).magnitude;}}
|
|
[Tooltip("Initial Spin force")]
|
|
public float SpinForce = 10;
|
|
[Tooltip("How smooth the speed goes up")]
|
|
public float SpinSmoothness = 1f;
|
|
[Tooltip("How smooth the speed goes down")]
|
|
public float Friction = 0.005f;
|
|
[Tooltip("When should the wheel stop")]
|
|
public float StopSpeed =0.01f;
|
|
public int offset = -4;
|
|
public int CurNumber;
|
|
public float CurrentSpeed {get; private set;}
|
|
|
|
void Start()
|
|
{
|
|
Debug.Log(RouletteNumbers.Length);
|
|
}
|
|
bool pushing = false;
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
HandleFriction();
|
|
wheel.Rotate(rotationAxis * CurrentSpeed);
|
|
CurNumber = GetLandedNumber();
|
|
}
|
|
|
|
void HandleFriction(){
|
|
if(pushing){return;}
|
|
if(CurrentSpeed > 0){
|
|
if(CurrentSpeed < StopSpeed){
|
|
CurrentSpeed=0;
|
|
m_onSpinStopped();
|
|
}else{
|
|
CurrentSpeed -= Friction;
|
|
}
|
|
}
|
|
}
|
|
public void Spin(){
|
|
StartCoroutine(spin());
|
|
}
|
|
|
|
IEnumerator spin(){
|
|
pushing=true;
|
|
while(CurrentSpeed < SpinForce){
|
|
CurrentSpeed += SpinSmoothness;
|
|
yield return new WaitForFixedUpdate();
|
|
}
|
|
pushing = false;
|
|
}
|
|
|
|
public int[] RouletteNumbers = {0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26};
|
|
|
|
private void m_onSpinStopped(){
|
|
|
|
LandedNumber = GetLandedNumber();
|
|
Debug.Log($"Landed on {LandedNumber}");
|
|
|
|
|
|
OnSpinStopped.Invoke(LandedNumber);
|
|
}
|
|
|
|
int GetLandedNumber(){
|
|
float fraction = ((wheel.eulerAngles.z) / 360f) * (float)RouletteNumbers.Length;
|
|
int index= Mathf.RoundToInt(fraction) + offset;
|
|
if(index < 0){
|
|
index = RouletteNumbers.Length + index - 1;
|
|
}else if(index >= RouletteNumbers.Length){
|
|
index = index - RouletteNumbers.Length;
|
|
}
|
|
int m_LandedNumber = RouletteNumbers[index];
|
|
|
|
return m_LandedNumber;
|
|
}
|
|
}
|