单个单元:
publicclass RockerSingle : MonoBehaviour {
// 枚举。类别
RockerType rockerType;
//是否有效,最上面的为无效,即为false;
public bool valid=true;
//不同类别对应不同的效果
public Texture[] typyTexture;
public Material typeMaterial;
// rockerType ,属性可读可写
internal RockerType RockerType{
get { return rockerType;}
set{
gameObject.GetComponent<MeshRenderer>().material.mainTexture=typyTexture[(int)value];
rockerType=value;
}
}
// 0~maxRockerIndex-1,当前属于第几个单元。
int rockerIndex;
internal int RockerIndex{
get { return rockerIndex;}
set{ rockerIndex=value;}
}
// 1,2,3。最大单元数
int maxRockerIndex;
internal int MaxRockerIndex{
get { return maxRockerIndex;}
set{ maxRockerIndex=value;}
}
// 每个单元对应的Position,用于单元的移动;
Vector3[] rockerTrans;
internal Vector3[] RockerTrans{
get { return rockerTrans;}
set{ rockerTrans=value;}
}
// 控制单元移动
internal void getMovingV3(){
rockerIndex++;
if(rockerIndex>=maxRockerIndex){
rockerIndex=0;
myTransform.position=rockerTrans[0];
getMovingV3();
}
else {
toMoving(rockerTrans[rockerIndex]);
}
}
private bool moving=false;
internalbool Moving{
get { return moving;}
set{ moving=value ;}
}
internalvoid toMoving(Vector3 v3){
destination=v3;
moving=true;
}
Vector3 destination;
Transform myTransform;
Vector3 velocity;
void Awake(){
myTransform=transform;
}
public float speedF;
void Update(){
if(moving){
velocity = destination - myTransform.position;
//float damping = velocity.magnitude;
if (velocity.sqrMagnitude < 0.01f)
{
myTransform.position = destination;
moving=false;
if(! RockerController.RockerEnd)
getMovingV3();
}
else {
velocity.Normalize();
myTransform.position = (myTransform.position + (velocity*Time.deltaTime*speedF));
}
}
}
}
////////////////////// 一组摇杆单元 //////////////////////
publicclass PlayeRocker : MonoBehaviour {
// 一组单元Transform,按由上到下顺序
public Transform [] rockerTrans;
// 一组单元RockerSingle脚本,按由上到下顺序
public RockerSingle[] rockerSingle;
// 一组单元Position,按由上到下顺序
Vector3[] rockerV3;
// 开始快速移动的间隔时间
public float time=0.1f;
//最大奖励类别
int maxRockerType;
void Start(){
maxRockerType=(int)RockerType.Color4+1;
StartCoroutine("startRocker");
}
IEnumerator startRocker(){
yieldreturnnewWaitForSeconds(time);
Init();
}
void Init(){
int length=rockerTrans.Length;
rockerV3=new Vector3 [length];
for(int i=0;i<length;i++){
if(rockerTrans[i])
rockerV3[i]=rockerTrans[i].position;
else
rockerV3[i]=Vector3.zero;
}
for(int i=0;i<length;i++){
rockerSingle[i].RockerType=(RockerType)Random.Range(0,maxRockerType);
rockerSingle[i].RockerIndex=i;
rockerSingle[i].MaxRockerIndex=length;
rockerSingle[i].RockerTrans=rockerV3;
}
for(int i=0;i<length;i++){
rockerSingle[i].getMovingV3();
}
}
}
///////////// 摇杆奖励停止与奖励结果
//奖励类别,枚举表示
public enum RockerType{
Color0=0,
Color1=1,
Color2=2,
Color3=3,
Color4=4
}
publicclass RockerController : MonoBehaviour {
//bool 为true,则停止
internalstaticbool RockerEnd=false;
//所有奖励单元
RockerSingle[] rockerSingles;
// 奖励单元的标签,如是六个奖励单元,(包含空的奖励单元)。那 奖励单元的标签就为3。即停止时,中间三个都为一样,就获得奖励了
public int rockerIndex=3;
void Start(){
getRockerSingle();
StartCoroutine("stopRocker");
}
void getRockerSingle(){
GameObject [] gos=GameObject.FindGameObjectsWithTag("RockerSingle");
rockerSingles=new RockerSingle [gos.Length];
for(int i=0,j=gos.Length;i<j;i++){
rockerSingles[i]=gos[i].GetComponent<RockerSingle>();
}
}
IEnumerator stopRocker(){
yieldreturnnewWaitForSeconds(3f);
RockerStop();
}
//摇杆停止接口
void RockerStop(){
RockerEnd=true;
StartCoroutine("rockerRuselt");
}
//所有奖励单元都停止移动,获取奖励结果
IEnumerator rockerRuselt(){
bool isbool=true;
bool isStop=true;
while(isbool){
isStop=true;
foreach(RockerSingle rock in rockerSingles){
if(rock.Moving)
isStop=false;
}
if(isStop){
Debug.Log("Stop");
rockerRaward();
isbool=false;
}
yieldreturnnull;
}
}
// 判断是否奖励成功。
void rockerRaward(){
System.Collections.Generic.List<RockerType> tockerTypeList=new System.Collections.Generic.List<RockerType> ();
foreach(RockerSingle rock in rockerSingles){
if(rock.RockerIndex==rockerIndex && rock.valid){
Debug.Log(" Rocker Raward "+rock.transform.name+" "+rock.RockerType);
tockerTypeList.Add(rock.RockerType);
}
}
bool isSucced=true;
for(int i=0,j=tockerTypeList.Count-2;i<=j;i++){
if(tockerTypeList[i]!=tockerTypeList[i+1])
isSucced=false;
}
RawardRuselt(isSucced,tockerTypeList[0]);
}
// 奖励成功与失败的接口
void RawardRuselt(bool isSucced,RockerType rockerType){
}
}