Created
June 23, 2018 10:58
-
-
Save todorok1/0b9734da0c82c07d32026b25fd1203c8 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 RandomWithWeight : MonoBehaviour { | |
// アイテムのデータを保持する辞書 | |
Dictionary<int, string> itemInfo; | |
// 敵がドロップするアイテムの辞書 | |
Dictionary<int, float> itemDropDict; | |
void Start(){ | |
GetDropItem(); | |
} | |
void GetDropItem(){ | |
// 各種辞書の初期化 | |
InitializeDicts(); | |
// ドロップアイテムの抽選 | |
int itemId = Choose(); | |
// アイテムIDに応じたメッセージ出力 | |
if (itemId != 0){ | |
string itemName = itemInfo[itemId]; | |
Debug.Log(itemName + " を入手した!"); | |
} else { | |
Debug.Log("アイテムは入手できませんでした。"); | |
} | |
} | |
void InitializeDicts(){ | |
itemInfo = new Dictionary<int, string>(); | |
itemInfo.Add(0, "なし"); | |
itemInfo.Add(1, "竜のひげ"); | |
itemInfo.Add(2, "竜の爪"); | |
itemInfo.Add(3, "竜のうろこ"); | |
itemInfo.Add(4, "竜の翼"); | |
itemInfo.Add(5, "竜の逆鱗"); | |
itemInfo.Add(6, "竜の紅玉"); | |
itemDropDict = new Dictionary<int, float>(); | |
itemDropDict.Add(0, 60.0f); | |
itemDropDict.Add(2, 25.0f); | |
itemDropDict.Add(3, 12.0f); | |
itemDropDict.Add(5, 3.0f); | |
} | |
int Choose(){ | |
// 確率の合計値を格納 | |
float total = 0; | |
// 敵ドロップ用の辞書からドロップ率を合計する | |
foreach (KeyValuePair<int, float> elem in itemDropDict){ | |
total += elem.Value; | |
} | |
// Random.valueでは0から1までのfloat値を返すので | |
// そこにドロップ率の合計を掛ける | |
float randomPoint = Random.value * total; | |
// randomPointの位置に該当するキーを返す | |
foreach (KeyValuePair<int, float> elem in itemDropDict){ | |
if (randomPoint < elem.Value){ | |
return elem.Key; | |
} else { | |
randomPoint -= elem.Value; | |
} | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment