Remove duplication

This commit is contained in:
Paul Chote
2010-03-21 20:36:46 +13:00
parent 0510a7fb7f
commit 85f9b3627f
11 changed files with 113 additions and 122 deletions

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
@@ -19,11 +19,10 @@
#endregion
using System.Collections.Generic;
using OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Effects
namespace OpenRA.Effects
{
class CrateEffect : IEffect
{

View File

@@ -286,6 +286,8 @@
<Compile Include="Chrome\CheckboxWidget.cs" />
<Compile Include="Traits\HasUnitOnBuild.cs" />
<Compile Include="Traits\Activities\Wait.cs" />
<Compile Include="Traits\CrateAction.cs" />
<Compile Include="Effects\CrateEffect.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -62,7 +62,7 @@ namespace OpenRA.Traits
public void OnCrush(Actor crusher)
{
var shares = self.traits.WithInterface<ICrateAction>().Select(a => Pair.New(a, a.GetSelectionShares(crusher)));
var shares = self.traits.WithInterface<CrateAction>().Select(a => Pair.New(a, a.GetSelectionShares(crusher)));
var totalShares = shares.Sum(a => a.Second);
var n = self.World.SharedRandom.Next(totalShares);

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it 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.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using OpenRA.Effects;
namespace OpenRA.Traits
{
public class CrateActionInfo : ITraitInfo
{
public int SelectionShares = 10;
public string Effect = null;
public string Notification = null;
public virtual object Create(Actor self) { return new CrateAction(self, this); }
}
public class CrateAction
{
public Actor self;
public CrateActionInfo info;
public CrateAction(Actor self, CrateActionInfo info)
{
this.self = self;
this.info = info;
}
public virtual int GetSelectionShares(Actor collector)
{
return info.SelectionShares;
}
public virtual void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, info.Notification);
collector.World.AddFrameEndTask(w =>
{
if (info.Effect != null)
w.Add(new CrateEffect(collector, info.Effect));
});
}
}
}

View File

@@ -83,12 +83,6 @@ namespace OpenRA.Traits
bool IsPathableCrush(UnitMovementType umt, Player player);
}
public interface ICrateAction
{
int GetSelectionShares(Actor collector);
void Activate(Actor collector);
}
public struct Renderable
{
public readonly Sprite Sprite;

View File

@@ -18,43 +18,30 @@
*/
#endregion
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class ArmorUpgradeCrateActionInfo : ITraitInfo
class ArmorUpgradeCrateActionInfo : CrateActionInfo
{
public float Multiplier = 2.0f;
public int SelectionShares = 10;
public string Effect = null;
public string Notification = null;
public object Create(Actor self) { return new ArmorUpgradeCrateAction(self); }
public override object Create(Actor self) { return new ArmorUpgradeCrateAction(self, this); }
}
class ArmorUpgradeCrateAction : ICrateAction
class ArmorUpgradeCrateAction : CrateAction
{
Actor self;
public ArmorUpgradeCrateAction(Actor self)
{
this.self = self;
}
public ArmorUpgradeCrateAction(Actor self, ArmorUpgradeCrateActionInfo info)
: base(self, info) {}
public int GetSelectionShares(Actor collector)
public override void Activate(Actor collector)
{
return self.Info.Traits.Get<ArmorUpgradeCrateActionInfo>().SelectionShares;
}
public void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, self.Info.Traits.Get<ArmorUpgradeCrateActionInfo>().Notification);
collector.World.AddFrameEndTask(w =>
{
var multiplier = self.Info.Traits.Get<ArmorUpgradeCrateActionInfo>().Multiplier;
var multiplier = (info as ArmorUpgradeCrateActionInfo).Multiplier;
collector.traits.Add(new ArmorUpgrade(multiplier));
w.Add(new CrateEffect(collector, self.Info.Traits.Get<ArmorUpgradeCrateActionInfo>().Effect));
});
base.Activate(collector);
}
}

View File

@@ -7,39 +7,24 @@ using OpenRA.Effects;
namespace OpenRA.Mods.RA
{
class ExplodeCrateActionInfo : ITraitInfo
class ExplodeCrateActionInfo : CrateActionInfo
{
public string Weapon = null;
public int SelectionShares = 5;
public string Effect = null;
public string Notification = null;
public object Create(Actor self) { return new ExplodeCrateAction(self, this); }
public override object Create(Actor self) { return new ExplodeCrateAction(self, this); }
}
class ExplodeCrateAction : ICrateAction
class ExplodeCrateAction : CrateAction
{
Actor self;
ExplodeCrateActionInfo info;
public ExplodeCrateAction(Actor self, ExplodeCrateActionInfo info)
{
this.self = self;
this.info = info;
}
: base(self, info) {}
public int GetSelectionShares(Actor collector)
public override void Activate(Actor collector)
{
return info.SelectionShares;
}
public void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, self.Info.Traits.Get<ExplodeCrateActionInfo>().Notification);
self.World.AddFrameEndTask(
w => w.Add(new Bullet(info.Weapon, self.Owner,
w => w.Add(new Bullet((info as ExplodeCrateActionInfo).Weapon, self.Owner,
self, self.CenterLocation.ToInt2(), self.CenterLocation.ToInt2(),
0, 0)));
base.Activate(collector);
}
}
}

View File

@@ -18,45 +18,38 @@
*/
#endregion
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class FirepowerUpgradeCrateActionInfo : ITraitInfo
class FirepowerUpgradeCrateActionInfo : CrateActionInfo
{
public float Multiplier = 2.0f;
public int SelectionShares = 10;
public string Effect = null;
public string Notification = null;
public object Create(Actor self) { return new FirepowerUpgradeCrateAction(self); }
public override object Create(Actor self) { return new FirepowerUpgradeCrateAction(self, this); }
}
class FirepowerUpgradeCrateAction : ICrateAction
class FirepowerUpgradeCrateAction : CrateAction
{
Actor self;
public FirepowerUpgradeCrateAction(Actor self)
{
this.self = self;
}
public FirepowerUpgradeCrateAction(Actor self, FirepowerUpgradeCrateActionInfo info)
: base(self, info) {}
public int GetSelectionShares(Actor collector)
public override int GetSelectionShares(Actor collector)
{
if (collector.GetPrimaryWeapon() == null && collector.GetSecondaryWeapon() == null)
return 0;
return self.Info.Traits.Get<FirepowerUpgradeCrateActionInfo>().SelectionShares;
return base.GetSelectionShares(collector);
}
public void Activate(Actor collector)
public override void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, self.Info.Traits.Get<FirepowerUpgradeCrateActionInfo>().Notification);
collector.World.AddFrameEndTask(w =>
{
var multiplier = self.Info.Traits.Get<FirepowerUpgradeCrateActionInfo>().Multiplier;
var multiplier = (info as FirepowerUpgradeCrateActionInfo).Multiplier;
collector.traits.Add(new FirepowerUpgrade(multiplier));
w.Add(new CrateEffect(collector, self.Info.Traits.Get<FirepowerUpgradeCrateActionInfo>().Effect));
});
base.Activate(collector);
}
}

View File

@@ -18,43 +18,29 @@
*/
#endregion
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class GiveCashCrateActionInfo : ITraitInfo
class GiveCashCrateActionInfo : CrateActionInfo
{
public int Amount = 2000;
public int SelectionShares = 10;
public string Effect = null;
public string Notification = null;
public object Create(Actor self) { return new GiveCashCrateAction(self); }
public override object Create(Actor self) { return new GiveCashCrateAction(self, this); }
}
class GiveCashCrateAction : ICrateAction
class GiveCashCrateAction : CrateAction
{
Actor self;
public GiveCashCrateAction(Actor self)
{
this.self = self;
}
public GiveCashCrateAction(Actor self, GiveCashCrateActionInfo info)
: base(self, info) {}
public int GetSelectionShares(Actor collector)
public override void Activate(Actor collector)
{
return self.Info.Traits.Get<GiveCashCrateActionInfo>().SelectionShares;
}
public void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, self.Info.Traits.Get<GiveCashCrateActionInfo>().Notification);
collector.World.AddFrameEndTask(w =>
{
var amount = self.Info.Traits.Get<GiveCashCrateActionInfo>().Amount;
var amount = (info as GiveCashCrateActionInfo).Amount;
collector.Owner.GiveCash(amount);
w.Add(new CrateEffect(collector, self.Info.Traits.Get<GiveCashCrateActionInfo>().Effect));
});
base.Activate(collector);
}
}
}

View File

@@ -18,43 +18,29 @@
*/
#endregion
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SpeedUpgradeCrateActionInfo : ITraitInfo
class SpeedUpgradeCrateActionInfo : CrateActionInfo
{
public float Multiplier = 1.7f;
public int SelectionShares = 10;
public string Effect = null;
public string Notification = null;
public object Create(Actor self) { return new SpeedUpgradeCrateAction(self); }
public override object Create(Actor self) { return new SpeedUpgradeCrateAction(self, this); }
}
class SpeedUpgradeCrateAction : ICrateAction
class SpeedUpgradeCrateAction : CrateAction
{
Actor self;
public SpeedUpgradeCrateAction(Actor self)
{
this.self = self;
}
public SpeedUpgradeCrateAction(Actor self, SpeedUpgradeCrateActionInfo info)
: base(self, info) {}
public int GetSelectionShares(Actor collector)
public override void Activate(Actor collector)
{
return self.Info.Traits.Get<SpeedUpgradeCrateActionInfo>().SelectionShares;
}
public void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, self.Info.Traits.Get<SpeedUpgradeCrateActionInfo>().Notification);
collector.World.AddFrameEndTask(w =>
{
var multiplier = self.Info.Traits.Get<SpeedUpgradeCrateActionInfo>().Multiplier;
var multiplier = (info as SpeedUpgradeCrateActionInfo).Multiplier;
collector.traits.Add(new SpeedUpgrade(multiplier));
w.Add(new CrateEffect(collector, self.Info.Traits.Get<SpeedUpgradeCrateActionInfo>().Effect));
});
base.Activate(collector);
}
}

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -56,7 +56,6 @@
<Compile Include="Crates\ExplodeCrateAction.cs" />
<Compile Include="Crates\FirepowerUpgradeCrateAction.cs" />
<Compile Include="Crates\GiveCashCrateAction.cs" />
<Compile Include="Effects\CrateEffect.cs" />
<Compile Include="Effects\GpsSatellite.cs" />
<Compile Include="Effects\InvulnEffect.cs" />
<Compile Include="Effects\Parachute.cs" />
@@ -90,7 +89,7 @@
<Name>OpenRA.Game</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">