Convert GainsExperience to conditions.
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Effects;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
@@ -18,13 +19,13 @@ using OpenRA.Traits;
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("This actor's experience increases when it has killed a GivesExperience actor.")]
|
||||
public class GainsExperienceInfo : ITraitInfo, Requires<ValuedInfo>, Requires<UpgradeManagerInfo>
|
||||
public class GainsExperienceInfo : ITraitInfo, Requires<ValuedInfo>
|
||||
{
|
||||
[FieldLoader.Require]
|
||||
[Desc("Upgrades to grant at each level.",
|
||||
[Desc("Condition to grant at each level.",
|
||||
"Key is the XP requirements for each level as a percentage of our own value.",
|
||||
"Value is a list of the upgrade types to grant")]
|
||||
public readonly Dictionary<int, string[]> Upgrades = null;
|
||||
public readonly Dictionary<int, string> Conditions = null;
|
||||
|
||||
[Desc("Palette for the level up sprite.")]
|
||||
[PaletteReference] public readonly string LevelUpPalette = "effect";
|
||||
@@ -35,13 +36,14 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public object Create(ActorInitializer init) { return new GainsExperience(init, this); }
|
||||
}
|
||||
|
||||
public class GainsExperience : ISync, IResolveOrder
|
||||
public class GainsExperience : INotifyCreated, ISync, IResolveOrder
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly GainsExperienceInfo info;
|
||||
readonly UpgradeManager um;
|
||||
readonly int initialExperience;
|
||||
|
||||
readonly List<Pair<int, string[]>> nextLevel = new List<Pair<int, string[]>>();
|
||||
readonly List<Pair<int, string>> nextLevel = new List<Pair<int, string>>();
|
||||
UpgradeManager um;
|
||||
|
||||
// Stored as a percentage of our value
|
||||
[Sync] int experience = 0;
|
||||
@@ -54,16 +56,20 @@ namespace OpenRA.Mods.Common.Traits
|
||||
self = init.Self;
|
||||
this.info = info;
|
||||
|
||||
MaxLevel = info.Upgrades.Count;
|
||||
|
||||
MaxLevel = info.Conditions.Count;
|
||||
var cost = self.Info.TraitInfo<ValuedInfo>().Cost;
|
||||
foreach (var kv in info.Upgrades)
|
||||
foreach (var kv in info.Conditions)
|
||||
nextLevel.Add(Pair.New(kv.Key * cost, kv.Value));
|
||||
|
||||
if (init.Contains<ExperienceInit>())
|
||||
GiveExperience(init.Get<ExperienceInit, int>(), info.SuppressLevelupAnimation);
|
||||
initialExperience = init.Get<ExperienceInit, int>();
|
||||
}
|
||||
|
||||
um = self.Trait<UpgradeManager>();
|
||||
void INotifyCreated.Created(Actor self)
|
||||
{
|
||||
um = self.TraitOrDefault<UpgradeManager>();
|
||||
if (initialExperience > 0)
|
||||
GiveExperience(initialExperience, info.SuppressLevelupAnimation);
|
||||
}
|
||||
|
||||
public bool CanGainLevel { get { return Level < MaxLevel; } }
|
||||
@@ -76,17 +82,18 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public void GiveExperience(int amount, bool silent = false)
|
||||
{
|
||||
if (amount < 0)
|
||||
throw new ArgumentException("Revoking experience is not implemented.", "amount");
|
||||
|
||||
experience += amount;
|
||||
|
||||
while (Level < MaxLevel && experience >= nextLevel[Level].First)
|
||||
{
|
||||
var upgrades = nextLevel[Level].Second;
|
||||
if (um != null)
|
||||
um.GrantCondition(self, nextLevel[Level].Second);
|
||||
|
||||
Level++;
|
||||
|
||||
foreach (var u in upgrades)
|
||||
um.GrantUpgrade(self, u, this);
|
||||
|
||||
if (!silent)
|
||||
{
|
||||
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", "LevelUp", self.Owner.Faction.InternalName);
|
||||
|
||||
Reference in New Issue
Block a user