/
PortableChrono.cs
229 lines (185 loc) · 6.66 KB
/
PortableChrono.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#region Copyright & License Information
/*
* Copyright 2007-2017 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.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Mods.Cnc.Activities;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Orders;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
class PortableChronoInfo : ITraitInfo
{
[Desc("Cooldown in ticks until the unit can teleport.")]
public readonly int ChargeDelay = 500;
[Desc("Can the unit teleport only a certain distance?")]
public readonly bool HasDistanceLimit = true;
[Desc("The maximum distance in cells this unit can teleport (only used if HasDistanceLimit = true).")]
public readonly int MaxDistance = 12;
[Desc("Sound to play when teleporting.")]
public readonly string ChronoshiftSound = "chrotnk1.aud";
[Desc("Cursor to display when able to deploy the actor.")]
public readonly string DeployCursor = "deploy";
[Desc("Cursor to display when unable to deploy the actor.")]
public readonly string DeployBlockedCursor = "deploy-blocked";
[Desc("Cursor to display when targeting a teleport location.")]
public readonly string TargetCursor = "chrono-target";
[Desc("Cursor to display when the targeted location is blocked.")]
public readonly string TargetBlockedCursor = "move-blocked";
[Desc("Kill cargo on teleporting.")]
public readonly bool KillCargo = true;
[Desc("Flash the screen on teleporting.")]
public readonly bool FlashScreen = false;
[VoiceReference] public readonly string Voice = "Action";
public object Create(ActorInitializer init) { return new PortableChrono(this); }
}
class PortableChrono : IIssueOrder, IResolveOrder, ITick, ISelectionBar, IOrderVoice, ISync
{
[Sync] int chargeTick = 0;
public readonly PortableChronoInfo Info;
public PortableChrono(PortableChronoInfo info)
{
Info = info;
}
public void Tick(Actor self)
{
if (chargeTick > 0)
chargeTick--;
}
public IEnumerable<IOrderTargeter> Orders
{
get
{
yield return new PortableChronoOrderTargeter(Info.TargetCursor);
yield return new DeployOrderTargeter("PortableChronoDeploy", 5,
() => CanTeleport ? Info.DeployCursor : Info.DeployBlockedCursor);
}
}
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
{
if (order.OrderID == "PortableChronoDeploy" && CanTeleport)
self.World.OrderGenerator = new PortableChronoOrderGenerator(self, Info);
if (order.OrderID == "PortableChronoTeleport")
return new Order(order.OrderID, self, queued) { TargetLocation = self.World.Map.CellContaining(target.CenterPosition) };
return new Order(order.OrderID, self, queued) { TargetActor = target.Actor };
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "PortableChronoTeleport" && CanTeleport)
{
var maxDistance = Info.HasDistanceLimit ? Info.MaxDistance : (int?)null;
self.CancelActivity();
self.QueueActivity(new Teleport(self, order.TargetLocation, maxDistance, Info.KillCargo, Info.FlashScreen, Info.ChronoshiftSound));
}
}
public string VoicePhraseForOrder(Actor self, Order order)
{
return order.OrderString == "PortableChronoTeleport" && CanTeleport ? Info.Voice : null;
}
public void ResetChargeTime()
{
chargeTick = Info.ChargeDelay;
}
public bool CanTeleport
{
get { return chargeTick <= 0; }
}
float ISelectionBar.GetValue()
{
return (float)(Info.ChargeDelay - chargeTick) / Info.ChargeDelay;
}
Color ISelectionBar.GetColor() { return Color.Magenta; }
bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
}
class PortableChronoOrderTargeter : IOrderTargeter
{
readonly string targetCursor;
public PortableChronoOrderTargeter(string targetCursor)
{
this.targetCursor = targetCursor;
}
public string OrderID { get { return "PortableChronoTeleport"; } }
public int OrderPriority { get { return 5; } }
public bool IsQueued { get; protected set; }
public bool TargetOverridesSelection(TargetModifiers modifiers) { return true; }
public bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, ref TargetModifiers modifiers, ref string cursor)
{
// TODO: When target modifiers are configurable this needs to be revisited
if (modifiers.HasModifier(TargetModifiers.ForceMove) || modifiers.HasModifier(TargetModifiers.ForceQueue))
{
var xy = self.World.Map.CellContaining(target.CenterPosition);
IsQueued = modifiers.HasModifier(TargetModifiers.ForceQueue);
if (self.IsInWorld && self.Owner.Shroud.IsExplored(xy))
{
cursor = targetCursor;
return true;
}
return false;
}
return false;
}
}
class PortableChronoOrderGenerator : IOrderGenerator
{
readonly Actor self;
readonly PortableChronoInfo info;
public PortableChronoOrderGenerator(Actor self, PortableChronoInfo info)
{
this.self = self;
this.info = info;
}
public IEnumerable<Order> Order(World world, CPos cell, int2 worldPixel, MouseInput mi)
{
if (mi.Button == Game.Settings.Game.MouseButtonPreference.Cancel)
{
world.CancelInputMode();
yield break;
}
if (self.IsInWorld && self.Location != cell
&& self.Trait<PortableChrono>().CanTeleport && self.Owner.Shroud.IsExplored(cell))
{
world.CancelInputMode();
yield return new Order("PortableChronoTeleport", self, mi.Modifiers.HasModifier(Modifiers.Shift)) { TargetLocation = cell };
}
}
public void Tick(World world)
{
if (!self.IsInWorld || self.IsDead)
world.CancelInputMode();
}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World world)
{
yield break;
}
public IEnumerable<IRenderable> RenderAboveShroud(WorldRenderer wr, World world)
{
if (!self.IsInWorld || self.Owner != self.World.LocalPlayer)
yield break;
if (!self.Trait<PortableChrono>().Info.HasDistanceLimit)
yield break;
yield return new RangeCircleRenderable(
self.CenterPosition,
WDist.FromCells(self.Trait<PortableChrono>().Info.MaxDistance),
0,
Color.FromArgb(128, Color.LawnGreen),
Color.FromArgb(96, Color.Black));
}
public string GetCursor(World world, CPos cell, int2 worldPixel, MouseInput mi)
{
if (self.IsInWorld && self.Location != cell
&& self.Trait<PortableChrono>().CanTeleport && self.Owner.Shroud.IsExplored(cell))
return info.TargetCursor;
else
return info.TargetBlockedCursor;
}
}
}