GOAP
Ask or search…
K

Upgrading

Upgrading from 2.0 to 2.1

IAgentMover is removed

IAgentMover is removed in favor of having movement based events on the agent.
AgentMoveBehaviour.cs
1
using CrashKonijn.Goap.Behaviours;
2
using CrashKonijn.Goap.Interfaces;
3
using UnityEngine;
4
5
public class AgentMoveBehaviour : MonoBehaviour
6
{
7
private AgentBehaviour agent;
8
private ITarget currentTarget;
9
private bool shouldMove;
10
11
private void Awake()
12
{
13
this.agent = this.GetComponent<AgentBehaviour>();
14
}
15
16
private void OnEnable()
17
{
18
this.agent.Events.OnTargetInRange += this.OnTargetInRange;
19
this.agent.Events.OnTargetChanged += this.OnTargetChanged;
20
this.agent.Events.OnTargetOutOfRange += this.OnTargetOutOfRange;
21
}
22
23
private void OnDisable()
24
{
25
this.agent.Events.OnTargetInRange -= this.OnTargetInRange;
26
this.agent.Events.OnTargetChanged -= this.OnTargetChanged;
27
this.agent.Events.OnTargetOutOfRange -= this.OnTargetOutOfRange;
28
}
29
30
private void OnTargetInRange(ITarget target)
31
{
32
this.shouldMove = false;
33
}
34
35
private void OnTargetChanged(ITarget target, bool inRange)
36
{
37
this.currentTarget = target;
38
this.shouldMove = !inRange;
39
}
40
41
private void OnTargetOutOfRange(ITarget target)
42
{
43
this.shouldMove = true;
44
}
45
46
public void Update()
47
{
48
if (!this.shouldMove)
49
return;
50
51
if (this.currentTarget == null)
52
return;
53
54
this.transform.position = Vector3.MoveTowards(this.transform.position, new Vector3(this.currentTarget.Position.x, this.transform.position.y, this.currentTarget.Position.z), Time.deltaTime);
55
}
56
}

Setup through code now requires actual classes as the WorldKey and TargetKey.

1
public class WanderTarget : TargetKeyBase
2
{
3
}
4
5
public class IsWandering : WorldKeyBase
6
{
7
}
8
9
public class GoapSetConfigFactory : GoapSetFactoryBase
10
{
11
public override IGoapSetConfig Create()
12
{
13
var builder = new GoapSetBuilder("GettingStartedSet");
14
15
// Goals
16
builder.AddGoal<WanderGoal>()
17
.AddCondition<IsWandering>(Comparison.GreaterThanOrEqual, 1);
18
19
// Actions
20
builder.AddAction<WanderAction>()
21
.SetTarget<WanderTarget>()
22
.AddEffect<IsWandering>(true)
23
.SetBaseCost(1)
24
.SetInRange(0.3f);
25
26
// Target Sensors
27
builder.AddTargetSensor<WanderTargetSensor>()
28
.SetTarget<WanderTarget>();
29
30
// World Sensors
31
// This example doesn't have any world sensors. Look in the examples for more information on how to use them.
32
33
return builder.Build();
34
}
35
}
Last modified 8mo ago