Created
March 15, 2017 09:52
-
-
Save chheller/32c418e892c71e603196376b10821b27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class DiceHandler : MonoBehaviour { | |
public GameObject Die; | |
public float ThrowStrength; | |
public int Count; | |
public static DiceHandler Instance; | |
public int TotalDiceValue { | |
get | |
{ | |
int total = 0; | |
_dice.ForEach(die => total += die.GetComponent<Die>().FaceUp()); | |
return total; | |
} | |
} | |
public int[] Faces | |
{ | |
get | |
{ | |
for (int i = 0; i < _faces.Length; i++) | |
{ | |
_faces[i] = _dice[i].GetComponent<Die>().FaceUp(); | |
} | |
return _faces; | |
} | |
} | |
private List<GameObject> _dice; | |
private int[] _faces; | |
private void Awake() | |
{ | |
Instance = this; | |
_dice = new List<GameObject>(Count); | |
_faces = new int[Count]; | |
for (int i = 0; i < Count; i++) | |
{ | |
_dice.Add(Instantiate(Die, transform.position, Quaternion.identity, null)); | |
_dice[i].SetActive(false); | |
} | |
} | |
private void cleanDice() | |
{ | |
_dice.ForEach(die => | |
{ | |
die.GetComponent<Rigidbody>().velocity = Vector3.zero; | |
die.SetActive(false); | |
die.transform.position = transform.position; | |
}); | |
} | |
public void ThrowDice() | |
{ | |
cleanDice(); | |
_dice.ForEach(die => | |
{ | |
die.transform.rotation = Random.rotation; | |
die.transform.position = transform.position+ new Vector3( Random.Range(-.25f, .25f), 0, Random.Range(-.25f, .25f)); | |
die.SetActive(true); | |
die.GetComponent<Rigidbody>().AddForce(transform.forward * (ThrowStrength * Random.Range(1f, 1.25f)), ForceMode.Impulse); | |
}); | |
} | |
public void ThrowDice(Vector3 origin, Quaternion rotation, Vector3 direction, float originRandomRange = 0f, float rotationRandomRange = 0f, float forceRandomRange = 0f ) | |
{ | |
cleanDice(); | |
_dice.ForEach(die => | |
{ | |
die.transform.rotation = rotation; | |
die.transform.position = origin + new Vector3(Random.Range(-originRandomRange, originRandomRange), 0, Random.Range(-originRandomRange, originRandomRange)); | |
die.SetActive(true); | |
die.GetComponent<Rigidbody>().AddForce(direction * (ThrowStrength * Random.Range(-forceRandomRange, forceRandomRange)), ForceMode.Impulse); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Die : MonoBehaviour { | |
public AudioClip[] Impacts; | |
private Vector3[] cubeFaces; | |
private AudioSource audiosource; | |
private void Awake() | |
{ | |
audiosource = GetComponent<AudioSource>(); | |
} | |
public int FaceUp() | |
{ | |
cubeFaces = new [] { transform.up, -transform.up, transform.right, -transform.right, transform.forward, -transform.forward }; | |
float bestDot=10000; | |
float dot; | |
int faceUp = -1; | |
//int faceUp = -1; | |
for (int face = 0; face < 6; face++) | |
{ | |
dot = Vector3.Angle(cubeFaces[face], Vector3.up); | |
if (dot < bestDot) | |
{ | |
bestDot = dot; | |
faceUp = face; | |
} | |
} | |
return faceUp + 1; | |
} | |
private void OnCollisionEnter(Collision collision) | |
{ | |
CustomEvents.OnDieCollide(); | |
if (Impacts != null) | |
{ | |
if (audiosource) | |
{ | |
if (!audiosource.isPlaying) | |
{ | |
audiosource.clip = Impacts.GetRandomElement(); | |
audiosource.PlayOneShot(audiosource.clip, audiosource.volume); | |
} | |
} | |
else | |
AudioSource.PlayClipAtPoint(Impacts.GetRandomElement(), transform.position); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment