Created
March 17, 2018 15:42
-
-
Save honno/43c6b9b79e78257bc3dd256e614a956b to your computer and use it in GitHub Desktop.
Slot machine-like dice animation
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; | |
using UnityEngine.UI; | |
public class DiceManager: MonoBehaviour { | |
/* component reference */ | |
private Image image; | |
private TravelLightsDisplay hud; | |
/* path variables */ | |
private string path = "dice_faces_hd"; | |
private string prefix = ""; | |
private string suffix = "_dot"; | |
/* animation variables */ | |
[SerializeField] private int dCons = 15; | |
[SerializeField] private int dExp = 5; | |
private int dTotal; | |
[SerializeField] private float tBase = 0.05 f; | |
private float tNext; | |
private int count = 1; | |
[SerializeField] private float mod = 5 f; | |
private float tick; | |
private bool anim = false; | |
private int diceNum = 0; | |
private int currDiceNum; | |
void Start() { | |
image = this.GetComponent < Image > (); | |
dTotal = dCons + dExp; | |
tick = 0 f; | |
tNext = tBase; | |
currDiceNum = diceNum; | |
} | |
void Update() { | |
if (anim) { | |
tick += Time.deltaTime; | |
if (tick > tNext) { | |
if (!(count > dTotal)) { | |
if (count > dCons) { | |
tNext = tBase * Mathf.Pow(mod, Mathf.Log(count - dCons)); | |
} | |
int i = genNum(); | |
if (count == dTotal) { | |
while (i == currDiceNum || i == diceNum) { | |
i = genNum(); | |
} | |
changeFace(i); | |
} else { | |
while (i == currDiceNum) { | |
i = genNum(); | |
} | |
changeFace(i); | |
} | |
resetTick(); | |
count++; | |
} else { | |
changeFace(diceNum); | |
hud.changeLightHUD(diceNum); | |
tNext = tBase; | |
resetTick(); | |
count = 0; | |
anim = false; | |
} | |
} | |
} | |
} | |
string getPath(int diceNum) { | |
return (path != "" ? path + "/" : "") + prefix + diceNum.ToString() + suffix; | |
} | |
public void rolled(int diceNum) { | |
this.diceNum = diceNum; | |
anim = true; | |
} | |
void resetTick() { | |
tick = 0 f; | |
} | |
int genNum() { | |
return Random.Range(1, 7); | |
} | |
void changeFace(int diceNum) { | |
image.overrideSprite = Resources.Load < Sprite > (getPath(diceNum)); | |
currDiceNum = diceNum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment