99 lines
2.2 KiB
C#
Executable File
99 lines
2.2 KiB
C#
Executable File
#region Copyright & License Information
|
|
/*
|
|
* Copyright 2007-2011 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 OpenRA.Mods.RA.Effects;
|
|
using OpenRA.Traits;
|
|
|
|
namespace OpenRA.Mods.RA.Buildings
|
|
{
|
|
public class RepairableBuildingInfo : ITraitInfo, Requires<HealthInfo>
|
|
{
|
|
public readonly int RepairPercent = 20;
|
|
public readonly int RepairInterval = 24;
|
|
public readonly int RepairStep = 7;
|
|
|
|
public object Create(ActorInitializer init) { return new RepairableBuilding(init.self, this); }
|
|
}
|
|
|
|
public class RepairableBuilding : ITick, ISync
|
|
{
|
|
[Sync] public Player Repairer = null;
|
|
|
|
Health Health;
|
|
RepairableBuildingInfo Info;
|
|
|
|
public RepairableBuilding(Actor self, RepairableBuildingInfo info)
|
|
{
|
|
Health = self.Trait<Health>();
|
|
Info = info;
|
|
}
|
|
|
|
public void RepairBuilding(Actor self, Player p)
|
|
{
|
|
if (self.HasTrait<RepairableBuilding>())
|
|
{
|
|
if (self.AppearsFriendlyTo(p.PlayerActor))
|
|
{
|
|
if (Repairer == p)
|
|
Repairer = null;
|
|
|
|
else
|
|
{
|
|
Repairer = p;
|
|
Sound.PlayToPlayer(Repairer, p.World.WorldActor.Info.Traits.Get<EvaAlertsInfo>().Repairing);
|
|
|
|
self.World.AddFrameEndTask(
|
|
w => w.Add(new RepairIndicator(self, p)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int remainingTicks;
|
|
|
|
public void Tick(Actor self)
|
|
{
|
|
if (Repairer == null) return;
|
|
|
|
if (remainingTicks == 0)
|
|
{
|
|
if (Repairer.WinState != WinState.Undefined)
|
|
{
|
|
Repairer = null;
|
|
return;
|
|
}
|
|
|
|
var buildingValue = self.GetSellValue();
|
|
|
|
var hpToRepair = Math.Min(Info.RepairStep, Health.MaxHP - Health.HP);
|
|
var cost = (hpToRepair * Info.RepairPercent * buildingValue) / (Health.MaxHP * 100);
|
|
if (!Repairer.PlayerActor.Trait<PlayerResources>().TakeCash(cost))
|
|
{
|
|
remainingTicks = 1;
|
|
return;
|
|
}
|
|
|
|
self.InflictDamage(self, -hpToRepair, null);
|
|
|
|
if (Health.DamageState == DamageState.Undamaged)
|
|
{
|
|
Repairer = null;
|
|
return;
|
|
}
|
|
|
|
remainingTicks = Info.RepairInterval;
|
|
}
|
|
else
|
|
--remainingTicks;
|
|
}
|
|
}
|
|
}
|