170 lines
5.3 KiB
C#
170 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using log4net;
|
|
using PrimeTween;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using FishNet;
|
|
|
|
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 readonly 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 readonly string characterAssetsPath = "Assets/ScriptedAssets/Characters";
|
|
private static readonly string characterAssetsBundleName = "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()
|
|
{
|
|
#if UNITY_EDITOR
|
|
GameManager.LoadAssetsFromFolder(characterAssetsPath, AvailableShips);
|
|
#else
|
|
GameManager.LoadAssetsFromBundle(characterAssetsBundleName, AvailableShips);
|
|
#endif
|
|
}
|
|
|
|
/// <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, int id = -1)
|
|
{
|
|
if (id == -1)
|
|
id = p.playerNumber - 1;
|
|
|
|
if (AvailableShips.Count > id)
|
|
{
|
|
p.character = AvailableShips[id];
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"There was no ship assigned to player number: {p.playerNumber}" +
|
|
" There are not enough ships to choose from.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spawns the characters which were chosen by the players
|
|
/// and sets them up to be playable.
|
|
/// </summary>
|
|
/// <param name="players">List of players for this match</param>
|
|
public void SpawnCharacters(List<Player> players)
|
|
{
|
|
if (MatchManager.G.MatchArena == null)
|
|
{
|
|
Log.Error("ArenaPlacement not found in scenes. Cannot spawn players.");
|
|
return;
|
|
}
|
|
if (MatchManager.G.MatchCamera == null)
|
|
{
|
|
Log.Error("Match camera not found in scenes. Cannot initialize spawned Characters.");
|
|
return;
|
|
}
|
|
var MatchCamera = MatchManager.G.MatchCamera;
|
|
var MatchArena = MatchManager.G.MatchArena;
|
|
List<Player> spawned = new List<Player>();
|
|
|
|
foreach (Player p in players)
|
|
{
|
|
SpawnCharacter(p, MatchCamera, MatchArena);
|
|
spawned.Add(p);
|
|
}
|
|
return;
|
|
}
|
|
|
|
public void SpawnOnlineCharacter(Player player)
|
|
{
|
|
if (MatchManager.G.MatchArena == null)
|
|
{
|
|
Log.Error("ArenaPlacement not found in scenes. Cannot spawn players.");
|
|
return;
|
|
}
|
|
if (MatchManager.G.MatchCamera == null)
|
|
{
|
|
Log.Error("Match camera not found in scenes. Cannot initialize spawned Characters.");
|
|
return;
|
|
}
|
|
var MatchCamera = MatchManager.G.MatchCamera;
|
|
var MatchArena = MatchManager.G.MatchArena;
|
|
List<Player> spawned = new List<Player>();
|
|
|
|
GameObject shipObject = Instantiate(player.character.shipPrefab);
|
|
player.spawnedCharacter = shipObject;
|
|
shipObject.TryGetComponent(out PredictedShip ship);
|
|
ship.props = player.character;
|
|
ship.state = new ShipHandling.ShipState();
|
|
ship.input = new ShipHandling.ShipInput();
|
|
ApplyShipColor(player.character.shipHullColor, ship.bodyMeshRenderer);
|
|
ship.cameraOperator = MatchCamera.GetComponent<CameraOperator>();
|
|
shipObject.transform.SetParent(MatchArena.transform, false);
|
|
shipObject.transform.localPosition =
|
|
MatchManager.G.ArenaProperties.spawnPositions[player.playerNumber - 1];
|
|
|
|
|
|
shipObject.transform.localScale = new Vector3();
|
|
Tween.Scale(shipObject.transform, new Vector3(0.7f, 0.7f, 0.7f), 1f);
|
|
InstanceFinder.ServerManager.Spawn(shipObject);
|
|
spawned.Add(player);
|
|
return;
|
|
}
|
|
|
|
public void SpawnCharacter(Player p, GameObject camera, GameObject arena)
|
|
{
|
|
Log.Debug($"Spawning ship:{p.character.shipName} for player: {p.playerName}");
|
|
GameObject shipObject = Instantiate(p.character.shipPrefab);
|
|
Log.Debug($"Instantiated prefab");
|
|
p.spawnedCharacter = shipObject;
|
|
shipObject.TryGetComponent(out Ship ship);
|
|
ship.Props = p.character;
|
|
ship.State = new ShipHandling.ShipState();
|
|
ship.Input = new ShipHandling.ShipInput();
|
|
ship.CameraOperator = camera.GetComponent<CameraOperator>();
|
|
ApplyShipColor(p.character.shipHullColor, ship.BodyMeshRenderer);
|
|
Log.Debug($"Applied ships color");
|
|
shipObject.transform.SetParent(arena.transform, false);
|
|
shipObject.transform.localPosition =
|
|
MatchManager.G.ArenaProperties.spawnPositions[p.playerNumber - 1];
|
|
|
|
shipObject.transform.localScale = new Vector3();
|
|
Log.Debug($"Set ships location");
|
|
Tween.Scale(shipObject.transform, new Vector3(0.7f, 0.7f, 0.7f), 1f);
|
|
Log.Debug($"Started ship spawn tween");
|
|
}
|
|
|
|
private void ApplyShipColor(Color color, MeshRenderer renderer)
|
|
{
|
|
MaterialPropertyBlock materialPropertyBlock = new();
|
|
materialPropertyBlock.SetColor("_BaseColor", color);
|
|
renderer.SetPropertyBlock(materialPropertyBlock);
|
|
}
|
|
}
|
|
} |