/// <summary> /// This is the other thread that handles the AI /// Main thread is the RaiseEvent /// </summary> public void ThreadOfAI() { // ---- Website did this, so i do this ( smth abt ensuring that update only this ) lock (obj) { // Get the currently running thread var thread = Thread.CurrentThread; // ensure main thread is started before updating this thread while (RaiseEventTestPlugin.GetInstance() == null) { Thread.Sleep(100); } // ---- Start updating this thread // Initialise list m_AIList = new List <EnemyAI>(); // Spawn enemies in list SpawnEnemy("EnemyName " + m_ID.ToString(), new Vector3(10.0f, 0.0f, 10.0f)); SpawnEnemy("EnemyName " + m_ID.ToString(), new Vector3(20.0f, 0.0f, 20.0f)); SpawnEnemy("EnemyName " + m_ID.ToString(), new Vector3(30.0f, 0.0f, 30.0f)); // Update the enemies while (true) { //RaiseEventTestPlugin.GetInstance().PluginHost.BroadcastEvent(target: ReciverGroup.All, // senderActor: 0, // targetGroup: 0, // evCode: (byte)99, // data: new Dictionary<byte, object>() { { (byte)245, "entered while loop" } }, // cacheOp: 0); // Update each enemy foreach (EnemyAI enemy in Instance.m_AIList) { enemy.Update(); // set target packet to send to be player //foreach (Player player in RaiseEventTestPlugin.GetInstance().m_playerList) //{ //enemy.SetTargetPacket(player.GetName()); // send over to client to update the Enemy obj position byte[] store = Packet_Serialisation.GetInstance().SerializeCustomAI(enemy); //RaiseEventTestPlugin.GetInstance().PluginHost.BroadcastEvent(target: ReciverGroup.All, // senderActor: 0, // targetGroup: 0, // evCode: (byte)99, // data: new Dictionary<byte, object>() { { (byte)245, "entered while loop" } }, // cacheOp: 0); //RaiseEventTestPlugin.GetInstance().PluginHost.BroadcastEvent(target: ReciverGroup.All, // senderActor: 0, // targetGroup: 0, // evCode: (byte)13, // data: new Dictionary<byte, object>() { { (byte)245, store } }, // cacheOp: 0); //} } Thread.Sleep(100); // 0.1s } } }
/// <summary> /// Handles the Changing of Enemy States /// </summary> private void ChangingOfStates() { // ---- Check for all Players in the PlayerList foreach (Player player in RaiseEventTestPlugin.GetInstance().m_playerList) { // ---- If the Distance from Enemy AI to Player is lesser than (_blank_), change to CHASE if (DistanceFromPlayer(player) > 4 * 4 && DistanceFromPlayer(player) < 10 * 10) { // ---- Choose the nearest Player if (player.GetPosition().x < m_nearestPlayerPos.x && player.GetPosition().z < m_nearestPlayerPos.z) { // update nearest player pos m_nearestPlayerPos = player.GetPosition(); // set new target m_targetPos = player.GetPosition(); m_playerTarget = player; } // save previous position of AI if (!m_bSavePrevPos) { m_prevPos = this.GetPosition(); m_bSavePrevPos = true; } m_state = "CHASE"; } // ---- If the Distance from Enemy AI to Player is lesser than (_blank_), change to ATTACK else if (DistanceFromPlayer(player) < 4 * 4) { // If a Player suddenly intercepts, change target if (player.GetPosition().x < m_nearestPlayerPos.x && player.GetPosition().z < m_nearestPlayerPos.z) { // update nearest player pos m_nearestPlayerPos = player.GetPosition(); // set new target m_targetPos = player.GetPosition(); m_playerTarget = player; } m_state = "ATTACK"; } // ---- If the Distance from Enemy AI to Player is more than (_blank_), change to RETURN else if (DistanceFromPlayer(player) >= 10 * 10 && !(this.GetPosition().x == m_prevPos.x && this.GetPosition().z == m_prevPos.z)) { m_state = "RETURN"; } else { m_state = "ROAM"; } } RaiseEventTestPlugin.GetInstance().PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, evCode: (byte)99, data: new Dictionary <byte, object>() { { (byte)245, "State : " + m_state } }, cacheOp: 0); }