Move Guard, CashTrickler, Explodes, SelfHealing, Sellable and Crushable to Mods.Common.

Extract Guardable trait from Guard into own file.
This commit is contained in:
reaperrr
2015-01-04 16:17:09 +01:00
parent dbbc790dab
commit d07db9c6a9
12 changed files with 45 additions and 34 deletions

View File

@@ -0,0 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
class Sell : Activity
{
public override Activity Tick(Actor self)
{
var h = self.TraitOrDefault<Health>();
var si = self.Info.Traits.Get<SellableInfo>();
var pr = self.Owner.PlayerActor.Trait<PlayerResources>();
var cost = self.GetSellValue();
var refund = (cost * si.RefundPercent * (h == null ? 1 : h.HP)) / (100 * (h == null ? 1 : h.MaxHP));
pr.GiveCash(refund);
foreach (var ns in self.TraitsImplementing<INotifySold>())
ns.Sold(self);
if (refund > 0 && self.Owner.IsAlliedWith(self.World.RenderPlayer))
self.World.AddFrameEndTask(w => w.Add(new FloatingText(self.CenterPosition, self.Owner.Color.RGB, FloatingText.FormatCashTick(refund), 30)));
self.Destroy();
return this;
}
// Cannot be cancelled
public override void Cancel(Actor self) { }
}
}

View File

@@ -85,6 +85,7 @@
<Compile Include="Activities\RemoveSelf.cs" />
<Compile Include="Activities\Repair.cs" />
<Compile Include="Activities\RepairBuilding.cs" />
<Compile Include="Activities\Sell.cs" />
<Compile Include="Activities\SimpleTeleport.cs" />
<Compile Include="Activities\Turn.cs" />
<Compile Include="Activities\UnloadCargo.cs" />
@@ -188,10 +189,15 @@
<Compile Include="Traits\Buildings\FootprintUtils.cs" />
<Compile Include="Traits\Burns.cs" />
<Compile Include="Traits\Cargo.cs" />
<Compile Include="Traits\CashTrickler.cs" />
<Compile Include="Traits\Cloak.cs" />
<Compile Include="Traits\Crushable.cs" />
<Compile Include="Traits\IgnoresDisguise.cs" />
<Compile Include="Traits\DetectCloaked.cs" />
<Compile Include="Traits\EngineerRepair.cs" />
<Compile Include="Traits\Explodes.cs" />
<Compile Include="Traits\Guard.cs" />
<Compile Include="Traits\Guardable.cs" />
<Compile Include="Traits\GainsExperience.cs" />
<Compile Include="Traits\GivesBounty.cs" />
<Compile Include="Traits\GivesExperience.cs" />
@@ -259,6 +265,8 @@
<Compile Include="Traits\Render\WithShadow.cs" />
<Compile Include="Traits\Render\WithSmoke.cs" />
<Compile Include="Traits\Render\WithTurret.cs" />
<Compile Include="Traits\SelfHealing.cs" />
<Compile Include="Traits\Sellable.cs" />
<Compile Include="Traits\ShakeOnDeath.cs" />
<Compile Include="Traits\SmokeTrailWhenDamaged.cs" />
<Compile Include="Traits\Sound\ActorLostNotification.cs" />

View File

@@ -0,0 +1,65 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Mods.Common.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Lets the actor generate cash in a set periodic time.")]
class CashTricklerInfo : ITraitInfo
{
[Desc("Number of ticks to wait between giving money.")]
public readonly int Period = 50;
[Desc("Amount of money to give each time.")]
public readonly int Amount = 15;
[Desc("Whether to show the cash tick indicators (+$15 rising from actor).")]
public readonly bool ShowTicks = true;
[Desc("Amount of money awarded for capturing the actor.")]
public readonly int CaptureAmount = 0;
public object Create(ActorInitializer init) { return new CashTrickler(this); }
}
class CashTrickler : ITick, ISync, INotifyCapture
{
readonly CashTricklerInfo info;
[Sync] int ticks;
public CashTrickler(CashTricklerInfo info)
{
this.info = info;
}
public void Tick(Actor self)
{
if (--ticks < 0)
{
ticks = info.Period;
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(info.Amount);
MaybeAddCashTick(self, info.Amount);
}
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
if (info.CaptureAmount > 0)
{
newOwner.PlayerActor.Trait<PlayerResources>().GiveCash(info.CaptureAmount);
MaybeAddCashTick(self, info.CaptureAmount);
}
}
void MaybeAddCashTick(Actor self, int amount)
{
if (info.ShowTicks)
self.World.AddFrameEndTask(w => w.Add(new FloatingText(self.CenterPosition, self.Owner.Color.RGB, FloatingText.FormatCashTick(amount), 30)));
}
}
}

View File

@@ -0,0 +1,73 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor is crushable.")]
class CrushableInfo : ITraitInfo
{
[Desc("Sound to play when being crushed.")]
public readonly string CrushSound = null;
[Desc("Which crush classes does this actor belong to.")]
public readonly string[] CrushClasses = { "infantry" };
[Desc("Probability of mobile actors noticing and evading a crush attempt.")]
public readonly int WarnProbability = 75;
[Desc("Will friendly units just crush me instead of pathing around.")]
public readonly bool CrushedByFriendlies = false;
public object Create(ActorInitializer init) { return new Crushable(init.Self, this); }
}
class Crushable : ICrushable
{
readonly Actor self;
readonly CrushableInfo info;
public Crushable(Actor self, CrushableInfo info)
{
this.self = self;
this.info = info;
}
public void WarnCrush(Actor crusher)
{
var mobile = self.TraitOrDefault<Mobile>();
if (mobile != null && self.World.SharedRandom.Next(100) <= info.WarnProbability)
mobile.Nudge(self, crusher, true);
}
public void OnCrush(Actor crusher)
{
Sound.Play(info.CrushSound, crusher.CenterPosition);
var wda = self.TraitOrDefault<WithDeathAnimation>();
if (wda != null)
{
var palette = wda.Info.CrushedSequencePalette;
if (wda.Info.CrushedPaletteIsPlayerPalette)
palette += self.Owner.InternalName;
wda.SpawnDeathAnimation(self, wda.Info.CrushedSequence, palette);
}
self.Kill(crusher);
}
public bool CrushableBy(string[] crushClasses, Player crushOwner)
{
if (!info.CrushedByFriendlies && crushOwner.IsAlliedWith(self.Owner))
return false;
return info.CrushClasses.Intersect(crushClasses).Any();
}
}
}

View File

@@ -0,0 +1,65 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class ExplodesInfo : ITraitInfo
{
[WeaponReference]
public readonly string Weapon = "UnitExplode";
[WeaponReference]
public readonly string EmptyWeapon = "UnitExplode";
public readonly int Chance = 100;
public readonly string[] DeathType = null;
public object Create(ActorInitializer init) { return new Explodes(this); }
}
public class Explodes : INotifyKilled
{
readonly ExplodesInfo explodesInfo;
public Explodes(ExplodesInfo info) { explodesInfo = info; }
public void Killed(Actor self, AttackInfo e)
{
if (!self.IsInWorld)
return;
if (self.World.SharedRandom.Next(100) > explodesInfo.Chance)
return;
if (explodesInfo.DeathType != null && e.Warhead != null && !explodesInfo.DeathType.Contains(e.Warhead.DeathType))
return;
var weaponName = ChooseWeaponForExplosion(self);
if (weaponName != null)
{
var weapon = e.Attacker.World.Map.Rules.Weapons[weaponName.ToLowerInvariant()];
if (weapon.Report != null && weapon.Report.Any())
Sound.Play(weapon.Report.Random(e.Attacker.World.SharedRandom), self.CenterPosition);
// Use .FromPos since this actor is killed. Cannot use Target.FromActor
weapon.Impact(Target.FromPos(self.CenterPosition), e.Attacker, Enumerable.Empty<int>());
}
}
string ChooseWeaponForExplosion(Actor self)
{
var shouldExplode = self.TraitsImplementing<IExplodeModifier>().All(a => a.ShouldExplode(self));
return shouldExplode ? explodesInfo.Weapon : explodesInfo.EmptyWeapon;
}
}
}

View File

@@ -0,0 +1,106 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("The player can give this unit the order to follow and protect friendly units with the Guardable trait.")]
public class GuardInfo : TraitInfo<Guard> { }
public class Guard : IResolveOrder, IOrderVoice
{
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "Guard")
{
var target = Target.FromActor(order.TargetActor);
GuardTarget(self, target);
}
}
public void GuardTarget(Actor self, Target target)
{
self.SetTargetLine(target, Color.Yellow);
var range = WRange.FromCells(target.Actor.Info.Traits.Get<GuardableInfo>().Range);
self.QueueActivity(false, new AttackMoveActivity(self, self.Trait<IMove>().MoveFollow(self, target, WRange.Zero, range)));
}
public string VoicePhraseForOrder(Actor self, Order order)
{
return order.OrderString == "Guard" ? "Move" : null;
}
}
public class GuardOrderGenerator : IOrderGenerator
{
readonly IEnumerable<Actor> subjects;
public GuardOrderGenerator(IEnumerable<Actor> subjects)
{
this.subjects = subjects;
}
public IEnumerable<Order> Order(World world, CPos xy, MouseInput mi)
{
if (mi.Button == MouseButton.Left)
{
world.CancelInputMode();
yield break;
}
var target = FriendlyGuardableUnits(world, mi).FirstOrDefault();
if (target == null || subjects.All(s => s.IsDead))
yield break;
foreach (var subject in subjects)
if (subject != target)
yield return new Order("Guard", subject, false) { TargetActor = target };
}
public void Tick(World world)
{
if (subjects.All(s => s.IsDead || !s.HasTrait<Guard>()))
world.CancelInputMode();
}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World world) { yield break; }
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world) { yield break; }
public string GetCursor(World world, CPos xy, MouseInput mi)
{
if (!subjects.Any())
return null;
var multiple = subjects.Count() > 1;
var canGuard = FriendlyGuardableUnits(world, mi)
.Any(a => multiple || a != subjects.First());
return canGuard ? "guard" : "move-blocked";
}
static IEnumerable<Actor> FriendlyGuardableUnits(World world, MouseInput mi)
{
return world.ScreenMap.ActorsAt(mi)
.Where(a => !world.FogObscures(a) && !a.IsDead &&
a.AppearsFriendlyTo(world.LocalPlayer.PlayerActor) &&
a.HasTrait<Guardable>());
}
}
}

View File

@@ -0,0 +1,22 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This unit can be guarded (followed and protected) by a Guard unit.")]
public class GuardableInfo : TraitInfo<Guardable>
{
public readonly int Range = 2;
}
public class Guardable { }
}

View File

@@ -0,0 +1,68 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Attach this to actors which should be able to regenerate their health points.")]
class SelfHealingInfo : UpgradableTraitInfo, ITraitInfo, Requires<HealthInfo>
{
public readonly int Step = 5;
public readonly int Ticks = 5;
public readonly float HealIfBelow = .5f;
public readonly int DamageCooldown = 0;
public virtual object Create(ActorInitializer init) { return new SelfHealing(init.Self, this); }
}
class SelfHealing : UpgradableTrait<SelfHealingInfo>, ITick, INotifyDamage
{
readonly Health health;
[Sync] int ticks;
[Sync] int damageTicks;
public SelfHealing(Actor self, SelfHealingInfo info)
: base(info)
{
health = self.Trait<Health>();
}
public void Tick(Actor self)
{
if (self.IsDead || IsTraitDisabled)
return;
if (health.HP >= Info.HealIfBelow * health.MaxHP)
return;
if (damageTicks > 0)
{
--damageTicks;
return;
}
if (--ticks <= 0)
{
ticks = Info.Ticks;
self.InflictDamage(self, -Info.Step, null);
}
}
public void Damaged(Actor self, AttackInfo e)
{
if (e.Damage > 0)
damageTicks = Info.DamageCooldown;
}
}
}

View File

@@ -0,0 +1,61 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Actor can be sold")]
public class SellableInfo : UpgradableTraitInfo, ITraitInfo
{
public readonly int RefundPercent = 50;
public readonly string[] SellSounds = { };
public object Create(ActorInitializer init) { return new Sellable(this); }
}
public class Sellable : UpgradableTrait<SellableInfo>, IResolveOrder
{
public Sellable(SellableInfo info)
: base(info) { }
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "Sell")
Sell(self);
}
public void Sell(Actor self)
{
if (IsTraitDisabled)
return;
var building = self.TraitOrDefault<Building>();
if (building != null && !building.Lock())
return;
self.CancelActivity();
foreach (var s in Info.SellSounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterPosition);
foreach (var ns in self.TraitsImplementing<INotifySold>())
ns.Selling(self);
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, new Sell());
else
self.QueueActivity(new Sell());
}
}
}