Moves Upgrades and Invulnerable trait to Mods.Common

This commit is contained in:
reaperrr
2015-01-11 11:39:07 +01:00
parent e4ce46514c
commit f897a9ff01
8 changed files with 12 additions and 15 deletions

View File

@@ -233,6 +233,7 @@
<Compile Include="Traits\Immobile.cs" />
<Compile Include="Traits\Infantry\ScaredyCat.cs" />
<Compile Include="Traits\Infantry\TakeCover.cs" />
<Compile Include="Traits\Invulnerable.cs" />
<Compile Include="Traits\JamsMissiles.cs" />
<Compile Include="Traits\KillsSelf.cs" />
<Compile Include="Traits\LimitedAmmo.cs" />
@@ -330,7 +331,10 @@
<Compile Include="Traits\Tooltip.cs" />
<Compile Include="Traits\TransformOnCapture.cs" />
<Compile Include="Traits\Turreted.cs" />
<Compile Include="Traits\Upgrades\DisableUpgrade.cs" />
<Compile Include="Traits\Upgrades\GainsStatUpgrades.cs" />
<Compile Include="Traits\Upgrades\UpgradableTrait.cs" />
<Compile Include="Traits\Upgrades\UpgradeActorsNear.cs" />
<Compile Include="Traits\Upgrades\UpgradeManager.cs" />
<Compile Include="Traits\Valued.cs" />
<Compile Include="Traits\Wanders.cs" />
@@ -377,6 +381,7 @@
<Compile Include="Warheads\CreateEffectWarhead.cs" />
<Compile Include="Warheads\CreateResourceWarhead.cs" />
<Compile Include="Warheads\DestroyResourceWarhead.cs" />
<Compile Include="Warheads\GrantUpgradeWarhead.cs" />
<Compile Include="Warheads\HealthPercentageDamageWarhead.cs" />
<Compile Include="Warheads\LeaveSmudgeWarhead.cs" />
<Compile Include="Warheads\SpreadDamageWarhead.cs" />

View File

@@ -0,0 +1,23 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This unit cannot be damaged.")]
class InvulnerableInfo : TraitInfo<Invulnerable> { }
class Invulnerable : IDamageModifier
{
public int GetDamageModifier(Actor attacker, DamageWarhead warhead) { return 0; }
}
}

View File

@@ -0,0 +1,32 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class DisableUpgradeInfo : UpgradableTraitInfo, ITraitInfo
{
public object Create(ActorInitializer init) { return new DisableUpgrade(this); }
}
public class DisableUpgrade : UpgradableTrait<DisableUpgradeInfo>, IDisable, IDisableMove
{
public DisableUpgrade(DisableUpgradeInfo info)
: base(info) { }
// Disable the actor when this trait is enabled.
public bool Disabled { get { return !IsTraitDisabled; } }
public bool MoveDisabled(Actor self) { return !IsTraitDisabled; }
}
}

View File

@@ -0,0 +1,127 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor has properties that upgrade when a specific criteria is met.")]
public class GainsStatUpgradesInfo : ITraitInfo
{
public readonly string FirepowerUpgrade = "firepower";
public readonly int[] FirepowerModifier = { 110, 115, 120, 130 };
public readonly string DamageUpgrade = "damage";
public readonly int[] DamageModifier = { 91, 87, 83, 65 };
public readonly string SpeedUpgrade = "speed";
public readonly int[] SpeedModifier = { 110, 115, 120, 150 };
public readonly string ReloadUpgrade = "reload";
public readonly int[] ReloadModifier = { 95, 90, 85, 75 };
public readonly string InaccuracyUpgrade = "inaccuracy";
public readonly int[] InaccuracyModifier = { 90, 80, 70, 50 };
public object Create(ActorInitializer init) { return new GainsStatUpgrades(this); }
}
public class GainsStatUpgrades : IUpgradable, IFirepowerModifier, IDamageModifier, ISpeedModifier, IReloadModifier, IInaccuracyModifier, IDisabledTrait
{
readonly GainsStatUpgradesInfo info;
[Sync] int firepowerLevel = 0;
[Sync] int speedLevel = 0;
[Sync] int damageLevel = 0;
[Sync] int reloadLevel = 0;
[Sync] int inaccuracyLevel = 0;
public bool IsTraitDisabled { get { return firepowerLevel == 0 && speedLevel == 0 && damageLevel == 0 && reloadLevel == 0 && inaccuracyLevel == 0; } }
public IEnumerable<string> UpgradeTypes
{
get
{
yield return info.FirepowerUpgrade;
yield return info.DamageUpgrade;
yield return info.SpeedUpgrade;
yield return info.ReloadUpgrade;
yield return info.InaccuracyUpgrade;
}
}
public GainsStatUpgrades(GainsStatUpgradesInfo info)
{
this.info = info;
}
public bool AcceptsUpgradeLevel(Actor self, string type, int level)
{
if (level < 0)
return false;
if (type == info.FirepowerUpgrade)
return level <= info.FirepowerModifier.Length;
if (type == info.DamageUpgrade)
return level <= info.DamageModifier.Length;
if (type == info.SpeedUpgrade)
return level <= info.SpeedModifier.Length;
if (type == info.ReloadUpgrade)
return level <= info.ReloadModifier.Length;
if (type == info.InaccuracyUpgrade)
return level <= info.InaccuracyModifier.Length;
return false;
}
public void UpgradeLevelChanged(Actor self, string type, int oldLevel, int newLevel)
{
if (type == info.FirepowerUpgrade)
firepowerLevel = newLevel.Clamp(0, info.FirepowerModifier.Length);
else if (type == info.DamageUpgrade)
damageLevel = newLevel.Clamp(0, info.DamageModifier.Length);
else if (type == info.SpeedUpgrade)
speedLevel = newLevel.Clamp(0, info.SpeedModifier.Length);
else if (type == info.ReloadUpgrade)
reloadLevel = newLevel.Clamp(0, info.ReloadModifier.Length);
else if (type == info.InaccuracyUpgrade)
inaccuracyLevel = newLevel.Clamp(0, info.InaccuracyModifier.Length);
}
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
{
return damageLevel > 0 ? info.DamageModifier[damageLevel - 1] : 100;
}
public int GetFirepowerModifier()
{
return firepowerLevel > 0 ? info.FirepowerModifier[firepowerLevel - 1] : 100;
}
public int GetSpeedModifier()
{
return speedLevel > 0 ? info.SpeedModifier[speedLevel - 1] : 100;
}
public int GetReloadModifier()
{
return reloadLevel > 0 ? info.ReloadModifier[reloadLevel - 1] : 100;
}
public int GetInaccuracyModifier()
{
return inaccuracyLevel > 0 ? info.InaccuracyModifier[inaccuracyLevel - 1] : 100;
}
}
}

View File

@@ -0,0 +1,137 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Applies an upgrade to actors within a specified range.")]
public class UpgradeActorsNearInfo : ITraitInfo
{
[Desc("The upgrades to grant.")]
public readonly string[] Upgrades = { };
[Desc("The range to search for actors to upgrade.")]
public readonly WRange Range = WRange.FromCells(3);
[Desc("What diplomatic stances are affected.")]
public readonly Stance ValidStances = Stance.Ally;
[Desc("Grant the upgrades apply to this actor.")]
public readonly bool AffectsParent = false;
public readonly string EnableSound = null;
public readonly string DisableSound = null;
public object Create(ActorInitializer init) { return new UpgradeActorsNear(init.Self, this); }
}
public class UpgradeActorsNear : ITick, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyOtherProduction
{
readonly UpgradeActorsNearInfo info;
readonly Actor self;
int proximityTrigger;
WPos cachedPosition;
WRange cachedRange;
WRange desiredRange;
bool cachedDisabled = true;
public UpgradeActorsNear(Actor self, UpgradeActorsNearInfo info)
{
this.info = info;
this.self = self;
cachedRange = info.Range;
}
public void AddedToWorld(Actor self)
{
cachedPosition = self.CenterPosition;
proximityTrigger = self.World.ActorMap.AddProximityTrigger(cachedPosition, cachedRange, ActorEntered, ActorExited);
}
public void RemovedFromWorld(Actor self)
{
self.World.ActorMap.RemoveProximityTrigger(proximityTrigger);
}
public void Tick(Actor self)
{
var disabled = self.IsDisabled();
if (cachedDisabled != disabled)
{
Sound.Play(disabled ? info.DisableSound : info.EnableSound, self.CenterPosition);
desiredRange = disabled ? WRange.Zero : info.Range;
cachedDisabled = disabled;
}
if (self.CenterPosition != cachedPosition || desiredRange != cachedRange)
{
cachedPosition = self.CenterPosition;
cachedRange = desiredRange;
self.World.ActorMap.UpdateProximityTrigger(proximityTrigger, cachedPosition, cachedRange);
}
}
void ActorEntered(Actor a)
{
if (a.Destroyed)
return;
if (a == self && !info.AffectsParent)
return;
var stance = self.Owner.Stances[a.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = a.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.GrantUpgrade(a, u, this);
}
public void UnitProducedByOther(Actor self, Actor producer, Actor produced)
{
// Work around for actors produced within the region not triggering until the second tick
if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= info.Range.Range * info.Range.Range)
{
var stance = self.Owner.Stances[produced.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = produced.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.GrantTimedUpgrade(produced, u, 1);
}
}
void ActorExited(Actor a)
{
if (a == self || a.Destroyed)
return;
var stance = self.Owner.Stances[a.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = a.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.RevokeUpgrade(a, u, this);
}
}
}

View File

@@ -0,0 +1,55 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
public class GrantUpgradeWarhead : Warhead
{
[Desc("The upgrades to apply.")]
public readonly string[] Upgrades = { };
[Desc("Duration of the upgrade (in ticks). Set to 0 for a permanent upgrade.")]
public readonly int Duration = 0;
public readonly WRange Range = WRange.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)
{
var um = a.TraitOrDefault<UpgradeManager>();
if (um == null)
continue;
foreach (var u in Upgrades)
{
if (!um.AcceptsUpgrade(a, u))
continue;
if (Duration > 0)
um.GrantTimedUpgrade(a, u, Duration);
else
um.GrantUpgrade(a, u, this);
}
}
}
}
}