using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum LuckyEggType
{
Score = 1,
Corn,
CarFly,
DeadCarFly,
}
public enum ScoreType
{
OnePointFive = 1,
Two,
}
public enum CornType
{
OnePointFive = 1,
Two,
Three,
}
public enum CarFlyType
{
Fly200 = 1,
Fly400,
Fly600,
Fly800,
Fly1000,
Fly1500,
Fly3000,
}
public enum DeadFlyType
{
Fly200 = 1,
Fly400,
Fly600,
Fly800,
Fly1000,
}
public class LuckyEggRandom {
private static LuckyEggRandom luckyEggRandom = null;
private LuckyEggRandom(){}
public static LuckyEggRandom GetInstance() {
if (luckyEggRandom == null)
{
luckyEggRandom = new LuckyEggRandom();
}
return luckyEggRandom;
}
private int[] randomWeight = new int[] { 50, 100, 1000, 800 };
private int[] randomScore = new int[] { 1000, 100 };
private int[] randomCorn = new int[] { 1000, 500,50 };
private int[] randomCarFly = new int[] { 800, 600, 400, 200, 100, 50, 20 };
private int[] randomDeadCarFly = new int[] { 800, 600, 400, 200, 100 };
private List<int> sumRandomWeight = new List<int>();
private int sumWeight = 0;
private List<int> scoreWeight = new List<int>();
private int scoreSumWeight = 0;
private List<int> cornWeight = new List<int>();
private int cornSumWeight = 0;
private List<int> carFlyWeight = new List<int>();
private int carFlySumWeight = 0;
private List<int> deadFlyWeight = new List<int>();
private int deadFlySumWeight = 0;
private int luckyEggType = 0;
private LuckyEgg luckyEgg;
public LuckyEgg GetRandomValue()
{
sumRandomWeight.Add(sumWeight += randomWeight[0]);
sumRandomWeight.Add(sumWeight += randomWeight[1]);
sumRandomWeight.Add(sumWeight += randomWeight[2]);
sumRandomWeight.Add(sumWeight += randomWeight[3]);
int randomNum = 0;
int count = 0;
randomNum = (int)(Random.value * sumWeight);//随机一个权重值
count = sumRandomWeight.Count;
for (int i = 0; i < count;i++ )
{
if (sumRandomWeight[i] > randomNum)
{
luckyEggType = i + 1;
WhichLuckyType(luckyEggType);
break;
}
}
return luckyEgg;
}
private void WhichLuckyType(int type) {
switch (type)
{
case (int)LuckyEggType.Score:
SetScoreRandomValue();
break;
case (int)LuckyEggType.Corn:
SetCornRandomValue();
break;
case (int)LuckyEggType.CarFly:
SetCarFlyRandomValue();
break;
case (int)LuckyEggType.DeadCarFly:
SetDeadFlyRandomValue();
break;
}
}
private void SetScoreRandomValue()
{
scoreWeight.Add(scoreSumWeight += randomScore[0]);
scoreWeight.Add(scoreSumWeight += randomScore[1]);
int scoreRandomNum = (int)(Random.value * scoreSumWeight);//随机一个权重值
int count = scoreWeight.Count;
int scoreType = 0;
for (int i = 0; i < count;i++ )
{
if (scoreWeight[i] > scoreRandomNum)
{
scoreType = i + 1;
GetScoreValue(scoreType);
break;
}
}
}
void GetScoreValue(int type) {
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)ScoreType.OnePointFive:
luckyEgg.type = type;
luckyEgg.name = "ScoreOnePointFive";
break;
case (int)ScoreType.Two:
luckyEgg.type = type;
luckyEgg.name = "ScoreTwo";
break;
}
}
private void SetCornRandomValue()
{
cornWeight.Add(cornSumWeight += randomCorn[0]);
cornWeight.Add(cornSumWeight += randomCorn[1]);
cornWeight.Add(cornSumWeight += randomCorn[2]);
int cornRandomNum = (int)(Random.value * cornSumWeight);//随机一个权重值
int count = cornWeight.Count;
int cornType = 0;
for (int i = 0; i < count; i++)
{
if (cornWeight[i] > cornRandomNum)
{
cornType = i + 1;
GetCornValue(cornType);
break;
}
}
}
void GetCornValue(int type)
{
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)CornType.OnePointFive:
luckyEgg.type = type;
luckyEgg.name = "CornOnePointFive";
break;
case (int)CornType.Two:
luckyEgg.type = type;
luckyEgg.name = "CornTwo";
break;
case (int)CornType.Three:
luckyEgg.type = type;
luckyEgg.name = "CornThree";
break;
}
}
private void SetCarFlyRandomValue()
{
carFlyWeight.Add(carFlySumWeight += randomCarFly[0]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[1]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[2]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[3]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[4]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[5]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[6]);
int carFlyRandomNum = (int)(Random.value * carFlySumWeight);//随机一个权重值
int count = carFlyWeight.Count;
int carFlyType = 0;
for (int i = 0; i < count; i++)
{
if (carFlyWeight[i] > carFlyRandomNum)
{
carFlyType = i + 1;
GetCarFlyValue(carFlyType);
break;
}
}
}
void GetCarFlyValue(int type)
{
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)CarFlyType.Fly200:
luckyEgg.type = type;
luckyEgg.name = "Fly200";
break;
case (int)CarFlyType.Fly400:
luckyEgg.type = type;
luckyEgg.name = "Fly400";
break;
case (int)CarFlyType.Fly600:
luckyEgg.type = type;
luckyEgg.name = "Fly600";
break;
case (int)CarFlyType.Fly800:
luckyEgg.type = type;
luckyEgg.name = "Fly800";
break;
case (int)CarFlyType.Fly1000:
luckyEgg.type = type;
luckyEgg.name = "Fly1000";
break;
case (int)CarFlyType.Fly1500:
luckyEgg.type = type;
luckyEgg.name = "Fly1500";
break;
case (int)CarFlyType.Fly3000:
luckyEgg.type = type;
luckyEgg.name = "Fly3000";
break;
}
}
private void SetDeadFlyRandomValue()
{
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[0]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[1]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[2]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[3]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[4]);
int deadFlyRandomNum = (int)(Random.value * deadFlySumWeight);//随机一个权重值
int count = deadFlyWeight.Count;
int deadFlyType = 0;
for (int i = 0; i < count; i++)
{
if (deadFlyWeight[i] > deadFlyRandomNum)
{
deadFlyType = i + 1;
GetDeadFlyValue(deadFlyType);
break;
}
}
}
void GetDeadFlyValue(int type)
{
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)DeadFlyType.Fly200:
luckyEgg.type = type;
luckyEgg.name = "DeadFly200";
break;
case (int)DeadFlyType.Fly400:
luckyEgg.type = type;
luckyEgg.name = "DeadFly400";
break;
case (int)DeadFlyType.Fly600:
luckyEgg.type = type;
luckyEgg.name = "DeadFly600";
break;
case (int)DeadFlyType.Fly800:
luckyEgg.type = type;
luckyEgg.name = "DeadFly800";
break;
case (int)DeadFlyType.Fly1000:
luckyEgg.type = type;
luckyEgg.name = "DeadFly1000";
break;
}
}
using System.Collections;
using System.Collections.Generic;
public enum LuckyEggType
{
Score = 1,
Corn,
CarFly,
DeadCarFly,
}
public enum ScoreType
{
OnePointFive = 1,
Two,
}
public enum CornType
{
OnePointFive = 1,
Two,
Three,
}
public enum CarFlyType
{
Fly200 = 1,
Fly400,
Fly600,
Fly800,
Fly1000,
Fly1500,
Fly3000,
}
public enum DeadFlyType
{
Fly200 = 1,
Fly400,
Fly600,
Fly800,
Fly1000,
}
public class LuckyEggRandom {
private static LuckyEggRandom luckyEggRandom = null;
private LuckyEggRandom(){}
public static LuckyEggRandom GetInstance() {
if (luckyEggRandom == null)
{
luckyEggRandom = new LuckyEggRandom();
}
return luckyEggRandom;
}
private int[] randomWeight = new int[] { 50, 100, 1000, 800 };
private int[] randomScore = new int[] { 1000, 100 };
private int[] randomCorn = new int[] { 1000, 500,50 };
private int[] randomCarFly = new int[] { 800, 600, 400, 200, 100, 50, 20 };
private int[] randomDeadCarFly = new int[] { 800, 600, 400, 200, 100 };
private List<int> sumRandomWeight = new List<int>();
private int sumWeight = 0;
private List<int> scoreWeight = new List<int>();
private int scoreSumWeight = 0;
private List<int> cornWeight = new List<int>();
private int cornSumWeight = 0;
private List<int> carFlyWeight = new List<int>();
private int carFlySumWeight = 0;
private List<int> deadFlyWeight = new List<int>();
private int deadFlySumWeight = 0;
private int luckyEggType = 0;
private LuckyEgg luckyEgg;
public LuckyEgg GetRandomValue()
{
sumRandomWeight.Add(sumWeight += randomWeight[0]);
sumRandomWeight.Add(sumWeight += randomWeight[1]);
sumRandomWeight.Add(sumWeight += randomWeight[2]);
sumRandomWeight.Add(sumWeight += randomWeight[3]);
int randomNum = 0;
int count = 0;
randomNum = (int)(Random.value * sumWeight);//随机一个权重值
count = sumRandomWeight.Count;
for (int i = 0; i < count;i++ )
{
if (sumRandomWeight[i] > randomNum)
{
luckyEggType = i + 1;
WhichLuckyType(luckyEggType);
break;
}
}
return luckyEgg;
}
private void WhichLuckyType(int type) {
switch (type)
{
case (int)LuckyEggType.Score:
SetScoreRandomValue();
break;
case (int)LuckyEggType.Corn:
SetCornRandomValue();
break;
case (int)LuckyEggType.CarFly:
SetCarFlyRandomValue();
break;
case (int)LuckyEggType.DeadCarFly:
SetDeadFlyRandomValue();
break;
}
}
private void SetScoreRandomValue()
{
scoreWeight.Add(scoreSumWeight += randomScore[0]);
scoreWeight.Add(scoreSumWeight += randomScore[1]);
int scoreRandomNum = (int)(Random.value * scoreSumWeight);//随机一个权重值
int count = scoreWeight.Count;
int scoreType = 0;
for (int i = 0; i < count;i++ )
{
if (scoreWeight[i] > scoreRandomNum)
{
scoreType = i + 1;
GetScoreValue(scoreType);
break;
}
}
}
void GetScoreValue(int type) {
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)ScoreType.OnePointFive:
luckyEgg.type = type;
luckyEgg.name = "ScoreOnePointFive";
break;
case (int)ScoreType.Two:
luckyEgg.type = type;
luckyEgg.name = "ScoreTwo";
break;
}
}
private void SetCornRandomValue()
{
cornWeight.Add(cornSumWeight += randomCorn[0]);
cornWeight.Add(cornSumWeight += randomCorn[1]);
cornWeight.Add(cornSumWeight += randomCorn[2]);
int cornRandomNum = (int)(Random.value * cornSumWeight);//随机一个权重值
int count = cornWeight.Count;
int cornType = 0;
for (int i = 0; i < count; i++)
{
if (cornWeight[i] > cornRandomNum)
{
cornType = i + 1;
GetCornValue(cornType);
break;
}
}
}
void GetCornValue(int type)
{
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)CornType.OnePointFive:
luckyEgg.type = type;
luckyEgg.name = "CornOnePointFive";
break;
case (int)CornType.Two:
luckyEgg.type = type;
luckyEgg.name = "CornTwo";
break;
case (int)CornType.Three:
luckyEgg.type = type;
luckyEgg.name = "CornThree";
break;
}
}
private void SetCarFlyRandomValue()
{
carFlyWeight.Add(carFlySumWeight += randomCarFly[0]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[1]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[2]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[3]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[4]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[5]);
carFlyWeight.Add(carFlySumWeight += randomCarFly[6]);
int carFlyRandomNum = (int)(Random.value * carFlySumWeight);//随机一个权重值
int count = carFlyWeight.Count;
int carFlyType = 0;
for (int i = 0; i < count; i++)
{
if (carFlyWeight[i] > carFlyRandomNum)
{
carFlyType = i + 1;
GetCarFlyValue(carFlyType);
break;
}
}
}
void GetCarFlyValue(int type)
{
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)CarFlyType.Fly200:
luckyEgg.type = type;
luckyEgg.name = "Fly200";
break;
case (int)CarFlyType.Fly400:
luckyEgg.type = type;
luckyEgg.name = "Fly400";
break;
case (int)CarFlyType.Fly600:
luckyEgg.type = type;
luckyEgg.name = "Fly600";
break;
case (int)CarFlyType.Fly800:
luckyEgg.type = type;
luckyEgg.name = "Fly800";
break;
case (int)CarFlyType.Fly1000:
luckyEgg.type = type;
luckyEgg.name = "Fly1000";
break;
case (int)CarFlyType.Fly1500:
luckyEgg.type = type;
luckyEgg.name = "Fly1500";
break;
case (int)CarFlyType.Fly3000:
luckyEgg.type = type;
luckyEgg.name = "Fly3000";
break;
}
}
private void SetDeadFlyRandomValue()
{
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[0]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[1]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[2]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[3]);
deadFlyWeight.Add(deadFlySumWeight += randomDeadCarFly[4]);
int deadFlyRandomNum = (int)(Random.value * deadFlySumWeight);//随机一个权重值
int count = deadFlyWeight.Count;
int deadFlyType = 0;
for (int i = 0; i < count; i++)
{
if (deadFlyWeight[i] > deadFlyRandomNum)
{
deadFlyType = i + 1;
GetDeadFlyValue(deadFlyType);
break;
}
}
}
void GetDeadFlyValue(int type)
{
luckyEgg = new LuckyEgg();
switch (type)
{
case (int)DeadFlyType.Fly200:
luckyEgg.type = type;
luckyEgg.name = "DeadFly200";
break;
case (int)DeadFlyType.Fly400:
luckyEgg.type = type;
luckyEgg.name = "DeadFly400";
break;
case (int)DeadFlyType.Fly600:
luckyEgg.type = type;
luckyEgg.name = "DeadFly600";
break;
case (int)DeadFlyType.Fly800:
luckyEgg.type = type;
luckyEgg.name = "DeadFly800";
break;
case (int)DeadFlyType.Fly1000:
luckyEgg.type = type;
luckyEgg.name = "DeadFly1000";
break;
}
}
}
using UnityEngine;
using System.Collections;
public class LuckyEgg {
public string name;
public int type;
}
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
void Start () {
LuckyEgg luckyEgg = LuckyEggRandom.GetInstance().GetRandomValue();
Debug.Log("======type: " + luckyEgg.type + "*********name: " + luckyEgg.name);
}
}