示例#1
0
        public GameObject NextSpawn()
        {
            if (null == random)
            {
                random = new UnityRandom();
            }
            WeightedRandom wr = new WeightedRandom(random);

            float totalWeight = 0.0f;

            foreach (Item item in spawnItems)
            {
                totalWeight += item.weight;
                wr.Add(new Weight(item.weight, item));
            }

            var weight = wr.ChooseRandom();

            if (null == weight)
            {
                return(null);
            }
            if (null == weight.target)
            {
                return(null);
            }

            return(((Item)weight.target).spawnObject);
        }
示例#2
0
        public void UnitTests()
        {
            var fixedRandom = new FixedRandom();

            var test        = new WeightedRandom(fixedRandom);
            var w1          = new Weight(1.0f);
            var w2          = new Weight(0.5f);
            var w3          = new Weight(0.25f);
            var totalWeight = w1.Value + w2.Value + w3.Value;

            test.Add(w1);
            test.Add(w2);
            test.Add(w3);

            Assert.AreEqual(w1, test.ChooseFactor(.5f / totalWeight));
            Assert.AreEqual(w1, test.ChooseFactor(.9f / totalWeight));
            Assert.AreEqual(w2, test.ChooseFactor(1.1f / totalWeight));
            Assert.AreEqual(w3, test.ChooseFactor(1.6f / totalWeight));
        }