forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AttackMoveActivity.cs
102 lines (89 loc) · 3.34 KB
/
AttackMoveActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#region Copyright & License Information
/*
* Copyright 2007-2019 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class AttackMoveActivity : Activity
{
readonly Func<Activity> getInner;
readonly bool isAssaultMove;
AutoTarget autoTarget;
ConditionManager conditionManager;
AttackMove attackMove;
int token = ConditionManager.InvalidConditionToken;
public AttackMoveActivity(Actor self, Func<Activity> getInner, bool assaultMoving = false)
{
this.getInner = getInner;
autoTarget = self.TraitOrDefault<AutoTarget>();
conditionManager = self.TraitOrDefault<ConditionManager>();
attackMove = self.TraitOrDefault<AttackMove>();
isAssaultMove = assaultMoving;
}
protected override void OnFirstRun(Actor self)
{
// Start moving.
QueueChild(self, getInner());
if (conditionManager == null || attackMove == null)
return;
if (!isAssaultMove && !string.IsNullOrEmpty(attackMove.Info.AttackMoveCondition))
token = conditionManager.GrantCondition(self, attackMove.Info.AttackMoveCondition);
else if (isAssaultMove && !string.IsNullOrEmpty(attackMove.Info.AssaultMoveCondition))
token = conditionManager.GrantCondition(self, attackMove.Info.AssaultMoveCondition);
}
public override Activity Tick(Actor self)
{
// We are not currently attacking a target, so scan for new targets.
if (!IsCanceling && ChildActivity != null && ChildActivity.NextActivity == null && autoTarget != null)
{
// ScanForTarget already limits the scanning rate for performance so we don't need to do that here.
var target = autoTarget.ScanForTarget(self, true, true);
if (target.Type != TargetType.Invalid)
{
// We have found a target so cancel the current move activity and queue attack activities.
ChildActivity.Cancel(self);
var attackBases = autoTarget.ActiveAttackBases;
foreach (var ab in attackBases)
{
var activity = ab.GetAttackActivity(self, target, true, false);
QueueChild(self, activity);
ab.OnQueueAttackActivity(self, activity, target, true, false);
}
// Make sure to continue moving when the attack activities have finished.
QueueChild(self, getInner());
}
}
if (ChildActivity != null)
{
ChildActivity = ActivityUtils.RunActivity(self, ChildActivity);
if (ChildActivity != null)
return this;
}
// The last queued childactivity is guaranteed to be the inner move, so if we get here it means
// we have reached our destination and there are no more enemies on our path.
return NextActivity;
}
protected override void OnLastRun(Actor self)
{
if (conditionManager != null && token != ConditionManager.InvalidConditionToken)
token = conditionManager.RevokeCondition(self, token);
}
public override IEnumerable<Target> GetTargets(Actor self)
{
if (ChildActivity != null)
return ChildActivity.GetTargets(self);
return Target.None;
}
}
}