add: now enemies properly spawn on deploy
This commit is contained in:
@@ -1,30 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class GameController : MonoBehaviour
|
||||
{
|
||||
public static GameController i;
|
||||
public List<LevelInfo> levels;
|
||||
public List<EnemyInfo> enemies;
|
||||
|
||||
public void Start() {
|
||||
if (i) { Destroy(gameObject); return; }
|
||||
i = gameObject.GetComponent<GameController>();
|
||||
i = GetComponent<GameController>();
|
||||
DontDestroyOnLoad(gameObject);
|
||||
levels = levels.Select(li => {
|
||||
li.hp = li.maxHp;
|
||||
li.enemies = new();
|
||||
return li;
|
||||
}).ToList();
|
||||
levels.ForEach(li => {
|
||||
AddEnemyInLevel(SelectEnemyWeighted().prefab, li.scene.name);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public Dictionary<GameObject, int> PopEnemies(string sceneName) {
|
||||
LevelInfo li = levels.Find(x => x.scene.name == sceneName);
|
||||
Dictionary<GameObject, int> enemies = li.enemies;
|
||||
Dictionary<GameObject, int> levelEnemies = new(li.enemies);
|
||||
li.enemies.Clear();
|
||||
return enemies;
|
||||
return levelEnemies;
|
||||
}
|
||||
|
||||
public void AddEnemyInLevel(GameObject enemy, string sceneName) {
|
||||
Dictionary<GameObject, int> enemies = levels.Find(x => x.scene.name == sceneName).enemies;
|
||||
if (enemies.ContainsKey(enemy)) enemies[enemy] += 1;
|
||||
else enemies[enemy] = 1;
|
||||
Dictionary<GameObject, int> levelEnemies = levels.Find(x => x.scene.name == sceneName).enemies;
|
||||
if (levelEnemies.ContainsKey(enemy)) levelEnemies[enemy] += 1;
|
||||
else levelEnemies[enemy] = 1;
|
||||
}
|
||||
|
||||
public LevelInfo GetLevel(string sceneName) {
|
||||
@@ -34,6 +46,17 @@ public class GameController : MonoBehaviour
|
||||
public void OnLevelClear(string sceneName, float timeSpent) {
|
||||
|
||||
}
|
||||
|
||||
EnemyInfo SelectEnemyWeighted() {
|
||||
float sumWeights = enemies.Sum(enemy => enemy.weight);
|
||||
float r = Random.Range(0, sumWeights);
|
||||
float curSum = 0;
|
||||
foreach (EnemyInfo en in enemies) {
|
||||
curSum += en.weight;
|
||||
if (curSum >= r) return en;
|
||||
}
|
||||
return enemies[^1];
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
@@ -52,3 +75,9 @@ public enum FactoryType {
|
||||
Minigun,
|
||||
Railgun
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct EnemyInfo {
|
||||
public GameObject prefab;
|
||||
public float weight;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user