Space-Smash-Out/Assets/Scripts/Managers/CharacterManager.cs
Jakob Feldmann 64162cb4a1 feat: whole project restructuring
This can be seen as the initial state of the project after the released demo.

The changes include:
- New ship models
- Singleton manager structure to keep project scaleable in the future
     - Managing players, their settings, character choices, statistics, match setups, controls etc. in a separate decoupled scene
- Main menu with transitions to the arena scene
- Beginnings of a custom audio solution
- Logging with Log4Net

It is really a complete overhaul of the projects structure and management.
2024-04-01 23:06:39 +02:00

70 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using log4net;
using UnityEditor;
using UnityEngine;
namespace Managers
{
/// <summary>
/// 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.).
/// </summary>
public class CharacterManager : MonoBehaviour
{
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Globally accessible member to use manager with.
/// </summary>
public static CharacterManager G { get; private set; }
public List<ShipProperties> availableShips { get; private set; } = new List<ShipProperties>();
private static string characterAssetsPath = "Assets/ScriptedAssets/Characters";
void Awake()
{
G = this;
Log.Info("Awake");
}
void Start()
{
LoadAvailableCharacters();
}
/// <summary>
/// Loads the characters a player can choose from a fixed path
/// in the project.
/// </summary>
private void LoadAvailableCharacters()
{
string[] files = Directory.GetFiles(characterAssetsPath, "*.asset",
SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
availableShips.Add(AssetDatabase.LoadAssetAtPath<ShipProperties>(file));
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="p">Player</param>
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.");
}
}
}
}