177 lines
5.4 KiB
C#
177 lines
5.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Util;
|
|
|
|
public class PlayerManager : MonoBehaviour
|
|
{
|
|
public static PlayerManager Instance { get; private set; }
|
|
|
|
private List<PlayerEntity> players;
|
|
private Dictionary<string, PlayerEntity> VoiceIDDict = new Dictionary<string, PlayerEntity>();
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
players = new List<PlayerEntity>();
|
|
}
|
|
|
|
public Dictionary<string, PlayerEntity> GetVoiceIDDictionary() => VoiceIDDict;
|
|
|
|
public void ProcessUpdate(int entityId, float x, float y, float z, float rotationY, float lookAngle, int hitPoints, string name, float animX, float animZ, float magnitude, int flags, string forceAnim, string voiceID)
|
|
{
|
|
//Debug.Log("Entity update id " + entityId + ", x " + x + " y " + y + " z " + z + ", hitpoints: " + hitPoints.ToString());
|
|
if (entityId == Player.Instance.GetID())
|
|
{
|
|
name = Misc.uppercaseFirst(name);
|
|
Player.Instance.ProcessEntityUpdate(entityId, x, y, z, hitPoints, name, rotationY);
|
|
return;
|
|
}
|
|
|
|
bool entityFound = false;
|
|
foreach (PlayerEntity player in players)
|
|
{
|
|
if (player.GetID() == entityId)
|
|
{
|
|
entityFound = true;
|
|
player.ProcessUpdateMessage(entityId, x, y, z, rotationY, lookAngle, name, animX, animZ, magnitude, flags, forceAnim);
|
|
if(voiceID != "null" && !VoiceIDDict.ContainsKey(voiceID))
|
|
{
|
|
player.SetVoiceID(voiceID);
|
|
VoiceIDDict.Add(voiceID, player);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
if (!entityFound)
|
|
{
|
|
//create new entity here
|
|
CreatePlayer(entityId, name, voiceID);
|
|
}
|
|
}
|
|
|
|
public void ProcessProjectileUpdate(int entityId, Vector3 direction, string projectile)
|
|
{
|
|
if (entityId == Player.Instance.GetID())
|
|
{
|
|
return;
|
|
}
|
|
foreach (PlayerEntity player in players)
|
|
{
|
|
if (player.GetID() == entityId)
|
|
{
|
|
StartCoroutine(player.processWeaponFire(direction, projectile));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ProcessAppearenceUpdate(int entityId, string skinName, int weapon)
|
|
{
|
|
//Debug.Log("skin change " + entityId + " " + skin);
|
|
//mainPlayer
|
|
|
|
Debugger.Logger($"skinName from server: {skinName}");
|
|
skinName = CharacterPool.ServerToClientNameMap[skinName];
|
|
Debugger.Logger($"skinName re-worked: {skinName}");
|
|
if (entityId == Player.Instance.GetID())
|
|
{
|
|
PlayerEntity playerEntity = Player.Instance.PlayerEntity;
|
|
var skins = playerEntity.transform.Find("Skins");
|
|
PlayableCharacterSkin newSkin = null;
|
|
try
|
|
{
|
|
newSkin = skins.Find(skinName).GetComponent<PlayableCharacterSkin>();
|
|
if (playerEntity.CurrentlyEquippedSkin != newSkin && newSkin != null)
|
|
{
|
|
playerEntity.SetSkin(skinName);
|
|
}
|
|
Player.Instance.SetWeapon(weapon);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debugger.Logger(e.Message, Process.TrashHold);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
foreach (PlayerEntity player in players)
|
|
{
|
|
if (player.GetID() == entityId)
|
|
{
|
|
var skins = player.transform.Find("Skins");
|
|
PlayableCharacterSkin newSkin = skins.Find(skinName).GetComponent<PlayableCharacterSkin>();
|
|
if (player.CurrentlyEquippedSkin != newSkin)
|
|
{
|
|
player.SetSkin(skinName);
|
|
}
|
|
|
|
player.setWeapon(weapon);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool TryGetPlayerFromVoiceID(string voiceID, out PlayerEntity playerEntity)
|
|
{
|
|
return VoiceIDDict.TryGetValue(voiceID, out playerEntity);
|
|
}
|
|
|
|
private void CreatePlayer(int id, string name, string voiceID)
|
|
{
|
|
GameObject playerPrefab = OnDemandLoader.Load<GameObject>("Prefabs/Player/Player");
|
|
GameObject playerObject = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
|
|
PlayerEntity player = playerObject.GetComponent<PlayerEntity>();
|
|
|
|
// set ID of the entity
|
|
player.SetID(id);
|
|
player.name = $"Player_{name}_{id}";
|
|
players.Add(player);
|
|
}
|
|
|
|
public List<PlayerEntity> GetPlayerList()
|
|
{
|
|
return players;
|
|
}
|
|
|
|
public void RemovePlayer(PlayerEntity player)
|
|
{
|
|
VoiceIDDict.Remove(player.GetVoiceID());
|
|
players.Remove(player);
|
|
Destroy(player.gameObject);
|
|
}
|
|
|
|
public void RemovePlayerWithID(int uuid)
|
|
{
|
|
PlayerEntity player = GetPlayer(uuid);
|
|
if (player != null)
|
|
{
|
|
RemovePlayer(player);
|
|
}
|
|
}
|
|
|
|
public PlayerEntity GetPlayer(int uuid)
|
|
{
|
|
if (uuid == Client.Instance.playerId)
|
|
{
|
|
return Player.Instance.PlayerEntity;
|
|
}
|
|
else
|
|
{
|
|
foreach (PlayerEntity player in players)
|
|
{
|
|
if (player.GetID() == uuid)
|
|
{
|
|
return player;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|