using System; using System.Collections.Generic; using System.IO; using System.Reflection; using log4net; using UnityEditor; using UnityEngine; namespace Managers { /// /// Handles setting up the available character/ship options, which the players can choose from, /// connects them to the players when choosen and manages them (spawning, despawning etc.). /// public class CharacterManager : MonoBehaviour { private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// /// Globally accessible member to use manager with. /// public static CharacterManager G { get; private set; } public List AvailableShips { get; private set; } = new List(); private static readonly string characterAssetsBundleName = "characters"; private static readonly string characterAssetsPath = "Assets/ScriptedAssets/Characters"; void Awake() { G = this; Log.Info("Awake"); } void Start() { LoadAvailableCharacters(); } /// /// Loads the characters a player can choose from a fixed path /// in the project. /// private void LoadAvailableCharacters() { #if UNITY_EDITOR string[] files = Directory.GetFiles(characterAssetsPath, "*.asset", SearchOption.TopDirectoryOnly); foreach (var file in files) { AvailableShips.Add(AssetDatabase.LoadAssetAtPath(file)); } #else var characterAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, characterAssetsBundleName)); if (characterAssetBundle == null) { Log.Error("Failed to load rules asset bundle!"); return; } AvailableShips.AddRange(characterAssetBundle.LoadAllAssets()); #endif } /// /// Assigns a ship to the player. /// Depends on the number of the player. /// Player number 2 always gets ship number 2 in the characters folder. /// /// Player public void AssignShipFixed(Player p) { if (availableShips.Count >= p.playerNumber) { p.character = availableShips[p.playerNumber - 1]; } else { Log.Error($"There was no ship assigned to player number: {p.playerNumber}" + " There are not enough ships to choose from."); } } } }