public void OnDestroy() { if (AIThread != null) { AI_Params.PushEvent(EEventType.EventTypeGameEnd); AI_Params.Send(Time.time); AIThread.Join(); // Waiting for the AI thread to finish saving AIThread = null; } InstanceCount--; }
public void Update() { AI_Params.PushCharacter(AI_Params.PlayerID, ECharacterType.CharacterTypePlayer, Vector3.zero, Vector3.zero, Vector3.zero, true); //AI_Params.PushEvent(EEventType.EventTypeActionStarted, AI_Params.PlayerID, AI_Params.UnknownID, EActionCode.ActionCodeAttackLight); //if (Time.time > 2.0) AI_Params.PushEvent(EEventType.EventTypeGameEnd); AI_Params.Send(Time.time); AI_Order order = AI_Orders.GetOrderForCharacter(AI_Params.PlayerID); if (order != null) { Debug.Log("[Game] Order received (id: " + order.CharacterID + ", code: " + order.ActionCode + ")."); } }
/// <summary> /// Update all characters. /// </summary> /// <param name="p">Update parameters</param> public void UpdateCharacters(AI_Params p) { foreach (AI_Params.AI_ParamCharacter c in p.Characters) { // Create a new character if ID doesn't already exist if (!this.Characters.ContainsKey(c.CharacterID)) { Debug.Log("Adding new character (Character ID: " + c.CharacterID + ")."); this.Characters[c.CharacterID] = new AI_Character(c.CharacterID, c.CharacterType); } // Update character information AI_Character character = this.Characters[c.CharacterID]; character.OnSeenByAI(this.LastUpdateTime, c.Position, c.Direction, c.Velocity); } }
/// <summary> /// Starts the AI process, waiting for updates and saving network state upon exiting /// </summary> public void Run() { this.IsRunning = true; while (this.IsRunning) { Debug.Log("[AI] Waiting for update..."); AI_Params p = AI_Params.Get(); Debug.Log("[AI] Update received (at time " + p.CurrentGameTime + "s)."); this.LastUpdateTime = p.CurrentGameTime; this.UpdateCharacters(p); this.UpdateEvents(p); this.ThinkAnticipation(); this.ThinkAction(); } Debug.Log("[AI] Terminating..."); this.SaveNetworkState(); }
/// <summary> /// Update all events. /// </summary> /// <param name="p">Update parameters</param> public void UpdateEvents(AI_Params p) { for (EEventType t = 0; t < EEventType.Count; t++) { foreach (AI_Params.AI_ParamEvent e in p.Events) { if (e.EventType == t) { switch (e.EventType) { // A character started an action case EEventType.EventTypeActionStarted: Debug.Assert(this.Characters.ContainsKey(e.CasterID), "[AI] Invalid caster ID (" + e.CasterID + ")."); this.OnCharacterActionStarted(this.Characters[e.CasterID], e.ActionCode); break; // A character finished an action case EEventType.EventTypeActionFinished: Debug.Assert(this.Characters.ContainsKey(e.CasterID), "[AI] Invalid caster ID (" + e.CasterID + ")."); this.OnCharacterActionFinished(this.Characters[e.CasterID]); break; // A character took damage case EEventType.EventTypeDamageDealt: Debug.Assert(this.Characters.ContainsKey(e.CasterID), "[AI] Invalid caster ID (" + e.CasterID + ")."); Debug.Assert(this.Characters.ContainsKey(e.TargetID), "[AI] Invalid target ID (" + e.TargetID + ")."); this.OnCharacterDamageDealt(this.Characters[e.CasterID], this.Characters[e.TargetID], e.Damage); break; // A character died case EEventType.EventTypeCharacterDeath: Debug.Assert(this.Characters.ContainsKey(e.CasterID), "[AI] Invalid caster ID (" + e.CasterID + ")."); Debug.Assert(this.Characters.ContainsKey(e.TargetID), "[AI] Invalid target ID (" + e.TargetID + ")."); this.OnCharacterDeath(this.Characters[e.TargetID]); break; // The game session ended case EEventType.EventTypeGameEnd: this.IsRunning = false; break; } } } } }
// OnStateEnter is called before OnStateEnter is called on any state inside this state machine override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { AI_Params.PushEvent(EEventType.EventTypeActionStarted, animator.transform.root.GetComponent <NNAgent>().ID, -2, EActionCode.ActionCodeRoll); }
// OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // //} //OnStateExit is called before OnStateExit is called on any state inside this state machine override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { AI_Params.PushEvent(EEventType.EventTypeActionFinished, animator.transform.root.GetComponent <NNAgent>().ID); }
/// <summary> /// [Game thread] Clears the current parameters. Called automatically by Send(). /// </summary> public static void Clear() { Instance = new AI_Params(); }