Space-Smash-Out/Assets/Scripts/Managers/PlayerManager.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

96 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Xml.Serialization;
using log4net;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Managers
{
/// <summary>
/// Handles setting up and managing all local players.
/// </summary>
public class PlayerManager : MonoBehaviour
{
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Globally accessible member to use manager with.
/// </summary>
public static PlayerManager G { get; private set; }
/// <summary>
/// The player which has started/owns the game.
/// May be identified at the start of the game and not
/// to be changed.
/// </summary>
public Player playerOne { get; private set; }
/// <summary>
/// List of the players currently playing the game.
/// </summary>
public List<Player> localPlayers { get; private set; } = new List<Player>();
private PlayerInputManager pim;
[SerializeReference]
private CameraOperator cameraOperator;
void Awake()
{
G = this;
Log.Info("Awake");
}
void Start()
{
playerOne = CreateLocalPlayer(1);
}
/// <summary>
/// Create the minimum number of players for the match and
/// add them to the player list.
/// </summary>
/// <param name="minPlayers">Minimum number of players</param>
public void LocalMatchJoinPlayers(int minPlayers)
{
if (localPlayers.Count < minPlayers)
{
for (int i = localPlayers.Count; i < minPlayers; ++i)
{
CreateLocalPlayer(i + 1);
}
}
}
/// <summary>
/// Creates a new player for this local session.
/// </summary>
/// <param name="id">Number of the player</param>
/// <returns>The created player or null if the number was already choosen.</returns>
private Player CreateLocalPlayer(int id)
{
if (localPlayers.Any(p => p.playerNumber == id))
{
Log.Warn($"The local player with the number: {id}, already exists!");
return null;
}
Player player = ScriptableObject.CreateInstance<Player>();
player.playerNumber = id;
player.playerName = $"Player {id}";
localPlayers.Add(player);
Log.Debug($"Added player number {id}. GUID: {player.PlayerID}");
return player;
}
public void OnPlayerJoined(PlayerInput pi)
{
cameraOperator.AddPlayer(pi.gameObject);
}
}
}