Shift temporary owner-change logic from D2k to Common.

This commit is contained in:
Zimmermann Gyula
2018-05-20 17:25:22 +02:00
committed by abcdefg30
parent 38f415255b
commit c4b5ec5241
4 changed files with 6 additions and 7 deletions

View File

@@ -315,6 +315,7 @@
<Compile Include="Traits\Conditions\GrantExternalConditionToCrusher.cs" />
<Compile Include="Traits\Conditions\GrantRandomCondition.cs" />
<Compile Include="Traits\Contrail.cs" />
<Compile Include="Traits\TemporaryOwnerManager.cs" />
<Compile Include="Traits\TooltipDescription.cs" />
<Compile Include="Traits\Health.cs" />
<Compile Include="Traits\HitShape.cs" />
@@ -593,6 +594,7 @@
<Compile Include="UtilityCommands\ExtractZeroBraneStudioLuaAPI.cs" />
<Compile Include="UtilityCommands\ExtractTraitDocsCommand.cs" />
<Compile Include="UtilityCommands\ExtractWeaponDocsCommand.cs" />
<Compile Include="Warheads\ChangeOwnerWarhead.cs" />
<Compile Include="Widgets\Logic\MusicHotkeyLogic.cs" />
<Compile Include="WorldExtensions.cs" />
<Compile Include="UtilityCommands\GetMapHashCommand.cs" />

View File

@@ -0,0 +1,85 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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.Drawing;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Interacts with the ChangeOwner warhead.",
"Displays a bar how long this actor is affected and reverts back to the old owner on temporary changes.")]
public class TemporaryOwnerManagerInfo : ITraitInfo
{
public readonly Color BarColor = Color.Orange;
public object Create(ActorInitializer init) { return new TemporaryOwnerManager(init.Self, this); }
}
public class TemporaryOwnerManager : ISelectionBar, ITick, ISync, INotifyOwnerChanged
{
readonly TemporaryOwnerManagerInfo info;
Player originalOwner;
Player changingOwner;
[Sync] int remaining = -1;
int duration;
public TemporaryOwnerManager(Actor self, TemporaryOwnerManagerInfo info)
{
this.info = info;
originalOwner = self.Owner;
}
public void ChangeOwner(Actor self, Player newOwner, int duration)
{
remaining = this.duration = duration;
changingOwner = newOwner;
self.ChangeOwner(newOwner);
}
void ITick.Tick(Actor self)
{
if (!self.IsInWorld)
return;
if (--remaining == 0)
{
changingOwner = originalOwner;
self.ChangeOwner(originalOwner);
self.CancelActivity(); // Stop shooting, you have got new enemies
}
}
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
if (changingOwner == null || changingOwner != newOwner)
originalOwner = newOwner; // It wasn't a temporary change, so we need to update here
else
changingOwner = null; // It was triggered by this trait: reset
}
float ISelectionBar.GetValue()
{
if (remaining <= 0)
return 0;
return (float)remaining / duration;
}
Color ISelectionBar.GetColor()
{
return info.BarColor;
}
bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
}
}

View File

@@ -0,0 +1,53 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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 OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
[Desc("Interacts with the TemporaryOwnerManager trait.")]
public class ChangeOwnerWarhead : Warhead
{
[Desc("Duration of the owner change (in ticks). Set to 0 to make it permanent.")]
public readonly int Duration = 0;
public readonly WDist Range = WDist.FromCells(1);
public override void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers)
{
var actors = target.Type == TargetType.Actor ? new[] { target.Actor } :
firedBy.World.FindActorsInCircle(target.CenterPosition, Range);
foreach (var a in actors)
{
// Don't do anything on friendly fire
if (a.Owner == firedBy.Owner)
continue;
if (Duration == 0)
a.ChangeOwner(firedBy.Owner); // Permanent
else
{
var tempOwnerManager = a.TraitOrDefault<TemporaryOwnerManager>();
if (tempOwnerManager == null)
continue;
tempOwnerManager.ChangeOwner(a, firedBy.Owner, Duration);
}
// Stop shooting, you have new enemies
a.CancelActivity();
}
}
}
}