From f50e2180133027f1150000e4fe4d89ed59e691e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=98=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD?= Date: Sat, 24 Aug 2024 12:13:40 +0200 Subject: [PATCH] feat: add ai type and state info --- .../ViewModels/HeroSummaryInfoViewModel.cs | 42 ++++++++++++++----- .../Application/ViewModels/MainViewModel.cs | 2 +- Client/Application/Views/MainWindow.xaml | 11 ++++- Client/Domain/AI/AI.cs | 29 ++++++------- Client/Domain/AI/AIInterface.cs | 7 +++- Client/Domain/Common/ObservableObject.cs | 2 +- .../Common/ObservableObjectInterface.cs | 15 +++++++ 7 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 Client/Domain/Common/ObservableObjectInterface.cs diff --git a/Client/Application/ViewModels/HeroSummaryInfoViewModel.cs b/Client/Application/ViewModels/HeroSummaryInfoViewModel.cs index f3182a4..ac22587 100644 --- a/Client/Application/ViewModels/HeroSummaryInfoViewModel.cs +++ b/Client/Application/ViewModels/HeroSummaryInfoViewModel.cs @@ -1,4 +1,5 @@ -using Client.Domain.Common; +using Client.Domain.AI; +using Client.Domain.Common; using Client.Domain.Entities; using Client.Domain.ValueObjects; using System; @@ -59,13 +60,6 @@ namespace Client.Application.ViewModels return hero.InventoryInfo; } } - public ulong Money - { - get - { - return 0; - } - } public TargetSummaryInfoViewModel? Target { @@ -81,10 +75,24 @@ namespace Client.Application.ViewModels return hero.AttackerIds.ToList(); } } - public HeroSummaryInfoViewModel(Hero hero) + public string AIType + { + get + { + return ai.Type.ToString(); + } + } + public string AIState + { + get + { + return ai.IsEnabled ? ai.CurrentState.ToString() : "Disabled"; + } + } + public HeroSummaryInfoViewModel(Hero hero, AIInterface ai) { this.hero = hero; - + this.ai = ai; hero.FullName.PropertyChanged += FullName_PropertyChanged; hero.Phenotype.PropertyChanged += Phenotype_PropertyChanged; hero.ExperienceInfo.PropertyChanged += ExperienceInfo_PropertyChanged; @@ -92,6 +100,19 @@ namespace Client.Application.ViewModels hero.VitalStats.PropertyChanged += VitalStats_PropertyChanged; hero.InventoryInfo.PropertyChanged += InventoryInfo_PropertyChanged; hero.PropertyChanged += Hero_PropertyChanged; + ai.PropertyChanged += Ai_PropertyChanged; + } + + private void Ai_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) + { + if (e.PropertyName == "Type") + { + OnPropertyChanged("AIType"); + } + if (e.PropertyName == "CurrentState" || e.PropertyName == "IsEnabled") + { + OnPropertyChanged("AIState"); + } } private void Hero_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) @@ -147,6 +168,7 @@ namespace Client.Application.ViewModels } private readonly Hero hero; + private readonly AIInterface ai; private TargetSummaryInfoViewModel? target; } } diff --git a/Client/Application/ViewModels/MainViewModel.cs b/Client/Application/ViewModels/MainViewModel.cs index 36be4c5..8bd0c73 100644 --- a/Client/Application/ViewModels/MainViewModel.cs +++ b/Client/Application/ViewModels/MainViewModel.cs @@ -39,7 +39,7 @@ namespace Client.Application.ViewModels public void Handle(HeroCreatedEvent @event) { - Hero = new HeroSummaryInfoViewModel(@event.Hero); + Hero = new HeroSummaryInfoViewModel(@event.Hero, ai); hero = @event.Hero; Map.Hero = hero; Map.CombatZone = new AICombatZoneMapViewModel(aiConfig.Combat.Zone, hero); diff --git a/Client/Application/Views/MainWindow.xaml b/Client/Application/Views/MainWindow.xaml index fd4fccb..19b4401 100644 --- a/Client/Application/Views/MainWindow.xaml +++ b/Client/Application/Views/MainWindow.xaml @@ -225,8 +225,8 @@ Position: Exp: Weight: - Adena: Inv. slots: + AI: @@ -254,7 +254,6 @@ - @@ -263,6 +262,14 @@ + + + + + + + + diff --git a/Client/Domain/AI/AI.cs b/Client/Domain/AI/AI.cs index ec64b1e..d6446db 100644 --- a/Client/Domain/AI/AI.cs +++ b/Client/Domain/AI/AI.cs @@ -1,4 +1,5 @@ using Client.Domain.AI.State; +using Client.Domain.Common; using Client.Domain.Entities; using Client.Domain.Events; using Client.Domain.Service; @@ -13,7 +14,7 @@ using System.Windows.Input; namespace Client.Domain.AI { - public class AI : AIInterface + public class AI : ObservableObject, AIInterface { public AI(WorldHandler worldHandler, Config config, AsyncPathMoverInterface asyncPathMover, TransitionBuilderLocator locator) { @@ -27,16 +28,17 @@ namespace Client.Domain.AI public void Toggle() { - isEnabled = !isEnabled; - if (isEnabled) + IsEnabled = !IsEnabled; + if (IsEnabled) { ResetState(); } } - public bool IsEnabled => isEnabled; + public bool IsEnabled { get { return isEnabled; } private set { if (isEnabled != value) { isEnabled = value; OnPropertyChanged(); } } } - public TypeEnum Type { get { return type; } set { if (type != value) { type = value; ResetState(); } } } + public TypeEnum Type { get { return type; } set { if (type != value) { type = value; ResetState(); OnPropertyChanged(); } } } + public BaseState.Type CurrentState { get { return currentState; } private set { if (currentState != value) { currentState = value; OnPropertyChanged(); } } } public async Task Update() { @@ -44,19 +46,18 @@ namespace Client.Domain.AI await Task.Run(() => { - if (isEnabled && worldHandler.Hero != null) + if (IsEnabled && worldHandler.Hero != null) { - states[currentState].Execute(); + states[CurrentState].Execute(); foreach (var transition in locator.Get(Type).Build(worldHandler, config, asyncPathMover)) { - if (transition.fromStates.ContainsKey(BaseState.Type.Any) && transition.toState != currentState || transition.fromStates.ContainsKey(currentState)) + if (transition.fromStates.ContainsKey(BaseState.Type.Any) && transition.toState != CurrentState || transition.fromStates.ContainsKey(CurrentState)) { - if (transition.predicate(states[currentState])) + if (transition.predicate(states[CurrentState])) { - states[currentState].OnLeave(); - currentState = transition.toState; - Debug.WriteLine(currentState.ToString()); - states[currentState].OnEnter(); + states[CurrentState].OnLeave(); + CurrentState = transition.toState; + states[CurrentState].OnEnter(); break; } } @@ -87,7 +88,7 @@ namespace Client.Domain.AI private void ResetState() { - currentState = BaseState.Type.Idle; + CurrentState = BaseState.Type.Idle; } private readonly WorldHandler worldHandler; diff --git a/Client/Domain/AI/AIInterface.cs b/Client/Domain/AI/AIInterface.cs index 54df675..b0a7e0b 100644 --- a/Client/Domain/AI/AIInterface.cs +++ b/Client/Domain/AI/AIInterface.cs @@ -1,4 +1,6 @@ -using Client.Domain.Events; +using Client.Domain.AI.State; +using Client.Domain.Common; +using Client.Domain.Events; using System; using System.Collections.Generic; using System.Data; @@ -8,7 +10,7 @@ using System.Threading.Tasks; namespace Client.Domain.AI { - public interface AIInterface + public interface AIInterface : ObservableObjectInterface { Task Update(); @@ -17,5 +19,6 @@ namespace Client.Domain.AI bool IsEnabled { get; } TypeEnum Type { get; set; } + BaseState.Type CurrentState { get; } } } diff --git a/Client/Domain/Common/ObservableObject.cs b/Client/Domain/Common/ObservableObject.cs index 7375292..106b3fd 100644 --- a/Client/Domain/Common/ObservableObject.cs +++ b/Client/Domain/Common/ObservableObject.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Client.Domain.Common { - public class ObservableObject : INotifyPropertyChanged + public class ObservableObject : ObservableObjectInterface { public event PropertyChangedEventHandler? PropertyChanged; public void OnPropertyChanged([CallerMemberName] string prop = "") diff --git a/Client/Domain/Common/ObservableObjectInterface.cs b/Client/Domain/Common/ObservableObjectInterface.cs new file mode 100644 index 0000000..480a4d2 --- /dev/null +++ b/Client/Domain/Common/ObservableObjectInterface.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Domain.Common +{ + public interface ObservableObjectInterface : INotifyPropertyChanged + { + void OnPropertyChanged([CallerMemberName] string prop = ""); + } +}