Skip to content

Commit

Permalink
Add GrantConditionAfterDelay
Browse files Browse the repository at this point in the history
Early work for proper GLA Hole logic. Closer to original than what we
currently have, but still different.
  • Loading branch information
MustaphaTR committed Jun 13, 2018
1 parent a44f93c commit 7862603
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions OpenRA.Mods.Gen/OpenRA.Mods.Gen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Traits\ChronoHarvester.cs" />
<Compile Include="Traits\Buildings\PeriodicProducer.cs" />
<Compile Include="Traits\Conditions\GrantStackableConditionOnFire.cs" />
<Compile Include="Traits\Conditions\GrantConditionAfterDelay.cs" />
<Compile Include="Traits\Conditions\GrantConditionOnAttack.cs" />
<Compile Include="Traits\Conditions\GrantConditionOnCapture.cs" />
<Compile Include="Traits\Conditions\GrantConditionOnHealth.cs" />
Expand Down
98 changes: 98 additions & 0 deletions OpenRA.Mods.Gen/Traits/Conditions/GrantConditionAfterDelay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#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("Gives a condition to the actor after a delay.")]
public class GrantConditionAfterDelayInfo : PausableConditionalTraitInfo
{
[GrantedConditionReference]
[Desc("The condition to grant")]
public readonly string Condition = null;

[Desc("Number of ticks to wait before applying the condition.")]
public readonly int Delay = 50;

public readonly bool ShowSelectionBar = true;
public readonly Color SelectionBarColor = Color.Magenta;

public override object Create(ActorInitializer init) { return new GrantConditionAfterDelay(this); }
}

public class GrantConditionAfterDelay : PausableConditionalTrait<GrantConditionAfterDelayInfo>, ITick, ISync, INotifyCreated, ISelectionBar
{
readonly GrantConditionAfterDelayInfo info;
ConditionManager manager;
int token = ConditionManager.InvalidConditionToken;
[Sync] public int Ticks { get; private set; }

public GrantConditionAfterDelay(GrantConditionAfterDelayInfo info)
: base(info)
{
this.info = info;
Ticks = info.Delay;
}

protected override void Created(Actor self)
{
manager = self.TraitOrDefault<ConditionManager>();

base.Created(self);
}

void GrantCondition(Actor self, string cond)
{
if (manager == null)
return;

if (string.IsNullOrEmpty(cond))
return;

token = manager.GrantCondition(self, cond);
}

void ITick.Tick(Actor self)
{
if (IsTraitDisabled)
Ticks = info.Delay;

if (IsTraitPaused || IsTraitDisabled)
return;

if (--Ticks < 0)
if (token == ConditionManager.InvalidConditionToken)
GrantCondition(self, info.Condition);
}

float ISelectionBar.GetValue()
{
if (IsTraitDisabled || !Info.ShowSelectionBar)
return 0f;

return 1f - (float)Ticks / Info.Delay;
}

bool ISelectionBar.DisplayWhenEmpty { get { return !IsTraitDisabled && Info.ShowSelectionBar; } }

Color ISelectionBar.GetColor() { return Info.SelectionBarColor; }

protected override void TraitDisabled(Actor self)
{
if (token == ConditionManager.InvalidConditionToken)
return;

token = manager.RevokeCondition(self, token);
}
}
}

0 comments on commit 7862603

Please sign in to comment.