using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.Tracing; using UnityEngine; using UnityEngine.UI; public class NPCManager : MonoBehaviour { public static NPCManager Instance { get; private set; } private List npcs; private void Awake() { Instance = this; } // Start is called before the first frame update void Start() { npcs = new List(); } public void processEntityUpdate(int entityId, int flags, int npcType, float x, float z, int deltaMultiplier, float face, float normalizedHealth) { bool entityFound = false; foreach (NPC npc in npcs) { if (npc.GetID() == entityId) { entityFound = true; //Debug.Log("EntityID: " + entityId); npc.processUpdateMessage(entityId, x, z, deltaMultiplier, face, normalizedHealth); //process packet here return; } } if (!entityFound) { //create new entity here CreateEntity(entityId, npcType, flags, x, z); } } public void DestroyAllLocalPlayersNPCs() { for (int i = npcs.Count - 1; i >= 0; i--) { Destroy(npcs[i]); } npcs.Clear(); } public void InitializeNpc(int entityId, int weaponId, float moveSpeed, float scale, float h, float s, float v) { foreach (NPC npc in npcs) { if (npc.GetID() == entityId) { npc.getNA().InitializeVisuals(weaponId, scale, h, s, v); //try and get localNPC component if (npc.l != null) { npc.l.setMoveSpeed(moveSpeed); } return; } } } public void ProcessProjectileUpdate(int entityId, int targetPlayerId, string projectile) { //if (entityId == Player.Instance.GetID()) { return; } foreach (NPC npc in npcs) { if (npc.GetID() == entityId) { npc.getNA().FireProjectile(targetPlayerId, projectile); } } } public void AnimationUpdate(int npcId, string anim) { foreach (NPC npc in npcs) { if (npc.GetID() == npcId) { npc.PlayAnimation(anim); return; } } } private void CreateEntity(int id, int type, int flags, float x, float z) { // get the npc prefab (brings the gameObject along with it) NPC npcPrefab = OnDemandLoader.Load("Prefabs/NPCs/" + type); NPC npcClone = Instantiate(npcPrefab, new Vector3(x, 100, z), Quaternion.identity); npcClone.getNA().BindEntityToTerrain(); // set ID of the entity npcClone.SetID(id); npcClone.type = type; npcClone.name = "Entity_" + id; npcClone.flags = flags; npcs.Add(npcClone); } public void RemoveNPC(NPC npc) { npcs.Remove(npc); Destroy(npc.gameObject); } public void RemoveNPCWithID(int uuid) { NPC npc = GetNPC(uuid); if (npc != null) { RemoveNPC(npc); } } public NPC GetNPC(int uuid) { foreach (NPC npc in npcs) { if (npc.GetID() == uuid) { return npc; } } return null; } }