Skip to content

Commit 2dec0cb

Browse files
committed
must bind to AiContext, state is lightweight
1 parent cfe441d commit 2dec0cb

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

Components.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/Utils/AI/AIContext.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
// Copyright 2014 Tokarev Mikhail (also known as Deepscorn)
22
// http://www.apache.org/licenses/LICENSE-2.0
3+
4+
using System;
35
using UniRx;
46

57
namespace Utils.AI
68
{
7-
public class AiContext<TAiContext> where TAiContext : AiContext<TAiContext>
9+
public class AiContext<TAiContext> : IDisposable where TAiContext : AiContext<TAiContext>
810
{
911
// State must not be assignable from client code. It is controlled from
1012
// states themselves. Or maybe later it will be controlled from Context. But not client code
1113
private AiState state;
14+
private readonly BehaviorSubject<bool> stateEnterSubject = new BehaviorSubject<bool>(false);
1215

1316
public AiContext(AiState initialState)
1417
{
1518
state = initialState;
19+
stateEnterSubject.OnNext(true);
1620
state.EnterState(GetTemplateInstance());
1721
}
1822

23+
// call that function to calculate AI state
1924
public void Update()
2025
{
2126
state.Update(GetTemplateInstance());
2227
}
2328

29+
public IObservable<T> UntilStateLeave<T>(IObservable<T> observable)
30+
{
31+
return observable.TakeUntil(stateEnterSubject.Where(enter => !enter));
32+
}
33+
34+
// TODO bind to state update (?)
35+
2436
private void ChangeState(AiState newState)
2537
{
38+
stateEnterSubject.OnNext(false);
2639
state.LeaveState(GetTemplateInstance());
2740
state = newState;
41+
stateEnterSubject.OnNext(true);
2842
state.EnterState(GetTemplateInstance());
2943
}
3044

@@ -33,34 +47,31 @@ private TAiContext GetTemplateInstance()
3347
return (TAiContext) this;
3448
}
3549

50+
public void Dispose()
51+
{
52+
stateEnterSubject.OnNext(false);
53+
}
54+
3655
public class AiState
3756
{
38-
private readonly BehaviorSubject<bool> stateEnteredSubject = new BehaviorSubject<bool>(false);
3957
// for future, real state. Can be used to group different AiState logics per state
4058
//public virtual AIStateType Type { get; protected set; }
4159

4260
// Do something before we transition to this state
4361
public virtual void EnterState(TAiContext context)
4462
{
45-
stateEnteredSubject.OnNext(true);
4663
}
4764

4865
// Do something before we leave this state
4966
public virtual void LeaveState(TAiContext context)
5067
{
51-
stateEnteredSubject.OnNext(false);
5268
}
5369

5470
// Process the input - causes state actions, may cause state transition
5571
public virtual void Update(TAiContext context)
5672
{
5773
}
5874

59-
protected IObservable<T> UntilLeave<T>(IObservable<T> observable)
60-
{
61-
return observable.TakeUntil(stateEnteredSubject.Where(entered => !entered));
62-
}
63-
6475
protected virtual void ChangeState(TAiContext context, AiState newState)
6576
{
6677
context.ChangeState(newState);

LICENSE.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)