Skip to content

Instantly share code, notes, and snippets.

@pepoipod
Created January 20, 2017 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepoipod/f0f1fc0a3e20d7bd196cdd4acd4d72f0 to your computer and use it in GitHub Desktop.
Save pepoipod/f0f1fc0a3e20d7bd196cdd4acd4d72f0 to your computer and use it in GitHub Desktop.
Unityエディタ上で出現率、発生率を設定出来るプログラム
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SetIncidenceSample : MonoBehaviour {
[SerializeField]
int monster1Incidence;
[SerializeField]
int monster2Incidence;
[SerializeField]
int monster3Incidence;
string [] monsterNames = { "スライム", "ゴブリン", "ドラゴン" };
// Use this for initialization
void Start () {
print (GetRandomMonsterName ());
}
// Update is called once per frame
void Update () {
}
string GetRandomMonsterName () {
var incidenceDistoribution = GetIncidenceDistributionList (monster1Incidence, monster2Incidence, monster3Incidence);
int rdm = Random.Range (0, incidenceDistoribution.Count);
return monsterNames [incidenceDistoribution [rdm]];
}
List<int> GetIncidenceDistributionList (params int [] incidences) {
var incidenceList = new List<int> ();
int gcd = GCD (incidences);
for (int i = 0, len = incidences.Length; i < len; i++) {
int incidence = incidences [i] / gcd;
for (int j = 0; j <= incidence; j++) {
incidenceList.Add (i);
}
}
return incidenceList;
}
int GCD (int [] numbers) {
return numbers.Aggregate (GCD);
}
int GCD (int a, int b) {
return b == 0 ? a : GCD (b, a % b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment