Exemple #1
0
 public void Initialize(MobSO mobSO)
 {
     spriteRenderer.sprite = mobSO.sprite;
     mobInfo.Initialize(
         mobSO.name,
         mobSO.health,
         mobSO.attackDamage,
         mobSO.attackFrequency
         );
 }
        void Update()
        {
            timer += Time.deltaTime;
            if (timer > spawnerInfo.SpawnTimer)
            {
                timer -= spawnerInfo.SpawnTimer;

                if (spawnerInfo.SpawnCount >= spawnerInfo.SpawnLimit)
                {
                    return;
                }

                GameObject go = ObjectPooler.Instance.GetPooledObject(mobPrefab);
                go.transform.SetParent(spawnerInfo.SpawnPoints[UnityEngine.Random.Range(0, spawnerInfo.SpawnPoints.Length)]);
                go.transform.localPosition = Vector3.zero;

                //Use object pooler to instantiate mobPrefab
                MobInfo mobInfo = go.GetComponent <MobInfo>();

                //Sum up the total weight of mobs
                float totalWeight = 0f;
                foreach (WeightedMob wm in waveInfo.CurrentWave.mobs)
                {
                    totalWeight += wm.weight;
                }

                //Pick a random number up to the maximum weight
                float randMob = UnityEngine.Random.Range(0f, totalWeight);

                //Iterate through the mobs until that weight is reached.
                MobSO mob = null;
                foreach (WeightedMob wm in waveInfo.CurrentWave.mobs)
                {
                    randMob -= wm.weight;
                    if (randMob <= 0f)
                    {
                        mob = wm.mob;
                        break;
                    }
                }

                mobInfo.Initialize(mob, scalerInfo.DifficultyScale);
                MobMovement mobMovement = mobInfo.GetComponent <MobMovement>();
                mobMovement.Initialize();
                //Update the spawnerInfo with this specific mob
                spawnerInfo.MobSpawned(mobInfo);
                //Generic event stating a mob has spawned
                EventManager.Instance.MobSpawned(mobInfo);
            }
        }