Skip to content

Commit 86d14ce

Browse files
committed
1 parent f6510ca commit 86d14ce

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

Scripts/Goap/GoapSolver/GoapSolver_Pathfinding.cs

+22-19
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,8 @@ private GoapResult SolveAstar(GoapState stateCurrent, ConditionInterface goal, i
150150
// if arrived at goal...
151151
if (goal.IsSatisfied(current.state))
152152
{
153-
// ...return the Action path
154-
155-
// action list
156-
List<GoapAction> actions = new List<GoapAction>();
157-
while (current.action != null)
158-
{
159-
actions.Add(current.action ?? throw new InvalidOperationException());
160-
current = current.parent;
161-
}
162-
actions.Reverse();
163-
164-
// create GoapResult
165-
GoapResult result = new GoapResult(
166-
actions.ToArray(),
167-
current.currentCost,
168-
true
169-
);
170-
171-
return result;
153+
// ...early return result
154+
return CreateSuccessResult(current);
172155
}
173156

174157
// close the current node
@@ -201,6 +184,26 @@ private GoapResult SolveAstar(GoapState stateCurrent, ConditionInterface goal, i
201184
}
202185
}
203186

187+
return CreateFailureResult();
188+
}
189+
190+
private GoapResult CreateSuccessResult(AstarQueue latestQueue)
191+
{
192+
// action list
193+
List<GoapAction> actions = new List<GoapAction>();
194+
while (latestQueue.action != null)
195+
{
196+
actions.Add(latestQueue.action ?? throw new InvalidOperationException());
197+
latestQueue = latestQueue.parent;
198+
}
199+
actions.Reverse();
200+
201+
// create GoapResult
202+
return new GoapResult(actions.ToArray(), latestQueue.currentCost, true);
203+
}
204+
205+
private GoapResult CreateFailureResult()
206+
{
204207
return new GoapResult(null, -1, false);
205208
}
206209

0 commit comments

Comments
 (0)