add: on clear/fail level functionality

This commit is contained in:
KOSMOGOR
2025-04-27 17:15:31 +03:00
parent d68058fbee
commit 15ea51e465
3 changed files with 49 additions and 4 deletions
+1
View File
@@ -3,6 +3,7 @@ using UnityEngine;
public class Enemy : MonoBehaviour
{
public float hp = 100f;
public GameObject prefab;
public void DealDamage(float damage) {
hp -= damage;
+46 -3
View File
@@ -49,12 +49,51 @@ public class GameController : MonoBehaviour
return levels.Find(x => x.scene.name == sceneName);
}
public void OnLevelClear(string sceneName, float timeSpent) {
void RepairLevel(string sceneName) {
levels = levels.Select(li => {
if (li.scene.name == sceneName) {
li.hp = Math.Clamp(li.hp + 20, 0, li.maxHp);
}
return li;
}).ToList();
}
public void OnLevelFailed(string sceneName, float timeSpent) {
void DamageLevels(string sceneName, float timeSpent) {
levels = levels.Select(li => {
if (li.scene.name != sceneName && !IsLevelDestroyed(li.scene.name)) {
int damage = (int)(li.enemies.Values.Sum(x => x) * timeSpent / 10);
li.hp = Math.Clamp(li.hp - damage, 0, li.maxHp);
}
return li;
}).ToList();
}
void SpawnEnemies(string sceneName, float timeSpent) {
int enemiesToAdd = (int)(timeSpent / 10);
levels.ForEach(li => {
if (li.scene.name == sceneName || IsLevelDestroyed(li.scene.name)) return;
for (int i = 0; i < enemiesToAdd; ++i) AddEnemyInLevel(SelectEnemyWeighted().prefab, li.scene.name);
});
}
void AddBullets() {
levels.ForEach(li => {
if (!IsLevelDestroyed(li.scene.name) && li.factory != BulletType.None) inventory[li.factory] += 10;
});
}
public void OnLevelClear(string sceneName, float timeSpent) {
RepairLevel(sceneName);
DamageLevels(sceneName, timeSpent);
SpawnEnemies(sceneName, timeSpent);
AddBullets();
}
public void OnLevelFailed(string sceneName, float timeSpent, List<Enemy> aliveEnemies) {
aliveEnemies.ForEach(enemy => AddEnemyInLevel(enemy.prefab, sceneName));
DamageLevels(sceneName, timeSpent);
SpawnEnemies(sceneName, timeSpent * 1.5f);
AddBullets();
}
EnemyInfo SelectEnemyWeighted() {
@@ -98,6 +137,10 @@ public class GameController : MonoBehaviour
_ => "None",
};
}
public bool IsLevelDestroyed(string sceneName) {
return GetLevel(sceneName).hp <= 0;
}
}
[Serializable]
+2 -1
View File
@@ -24,6 +24,7 @@ public class LevelController : MonoBehaviour
Vector2 circle = Random.insideUnitCircle * spawnPointInfo.radius;
Vector3 spawnVector = spawnPointInfo.transform.position + new Vector3(circle.x, 0, circle.y);
Enemy spawnedEnemy = Instantiate(enemy, spawnVector, spawnPointInfo.transform.rotation).GetComponent<Enemy>();
spawnedEnemy.prefab = enemy;
aliveEnemies.Add(spawnedEnemy);
}
}
@@ -37,7 +38,7 @@ public class LevelController : MonoBehaviour
}
public void OnPlayerDie() {
GameController.i.OnLevelFailed(SceneManager.GetActiveScene().name, timeSpent);
GameController.i.OnLevelFailed(SceneManager.GetActiveScene().name, timeSpent, aliveEnemies);
GameController.i.LoadDeployScene();
}
}