From d519cabae31f285118191af1c3752fe74b026ba6 Mon Sep 17 00:00:00 2001 From: Curtis Shmyr Date: Thu, 23 Jul 2020 21:53:00 -0600 Subject: [PATCH] Add actor experience to the Lua API --- .../Properties/GainsExperienceProperties.cs | 53 +++++++++++++++++++ OpenRA.Mods.Common/Traits/GainsExperience.cs | 11 ++-- 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 OpenRA.Mods.Common/Scripting/Properties/GainsExperienceProperties.cs diff --git a/OpenRA.Mods.Common/Scripting/Properties/GainsExperienceProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/GainsExperienceProperties.cs new file mode 100644 index 0000000000..d61750ace0 --- /dev/null +++ b/OpenRA.Mods.Common/Scripting/Properties/GainsExperienceProperties.cs @@ -0,0 +1,53 @@ +#region Copyright & License Information +/* + * Copyright 2007-2020 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, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using OpenRA.Mods.Common.Traits; +using OpenRA.Scripting; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Scripting +{ + [ScriptPropertyGroup("Experience")] + public class GainsExperienceProperties : ScriptActorProperties, Requires + { + readonly GainsExperience exp; + + public GainsExperienceProperties(ScriptContext context, Actor self) + : base(context, self) + { + exp = self.Trait(); + } + + [Desc("The actor's amount of experience.")] + public int Experience { get { return exp.Experience; } } + + [Desc("The actor's level.")] + public int Level { get { return exp.Level; } } + + [Desc("The actor's maximum possible level.")] + public int MaxLevel { get { return exp.MaxLevel; } } + + [Desc("Returns true if the actor can gain a level.")] + public bool CanGainLevel { get { return exp.CanGainLevel; } } + + [Desc("Gives the actor experience. If 'silent' is true, no animation or sound will be played if the actor levels up.")] + public void GiveExperience(int amount, bool silent = false) + { + exp.GiveExperience(amount, silent); + } + + [Desc("Gives the actor level(s). If 'silent' is true, no animation or sound will be played.")] + public void GiveLevels(int numLevels, bool silent = false) + { + exp.GiveLevels(numLevels, silent); + } + } +} diff --git a/OpenRA.Mods.Common/Traits/GainsExperience.cs b/OpenRA.Mods.Common/Traits/GainsExperience.cs index eb548ae902..6d1882dbef 100644 --- a/OpenRA.Mods.Common/Traits/GainsExperience.cs +++ b/OpenRA.Mods.Common/Traits/GainsExperience.cs @@ -62,7 +62,7 @@ namespace OpenRA.Mods.Common.Traits // Stored as a percentage of our value [Sync] - int experience = 0; + public int Experience { get; private set; } [Sync] public int Level { get; private set; } @@ -73,6 +73,7 @@ namespace OpenRA.Mods.Common.Traits self = init.Self; this.info = info; + Experience = 0; MaxLevel = info.Conditions.Count; initialExperience = init.GetValue(info, 0); } @@ -96,7 +97,7 @@ namespace OpenRA.Mods.Common.Traits return; var newLevel = Math.Min(Level + numLevels, MaxLevel); - GiveExperience(nextLevel[newLevel - 1].First - experience, silent); + GiveExperience(nextLevel[newLevel - 1].First - Experience, silent); } public void GiveExperience(int amount, bool silent = false) @@ -107,9 +108,9 @@ namespace OpenRA.Mods.Common.Traits if (MaxLevel == 0) return; - experience = (experience + amount).Clamp(0, nextLevel[MaxLevel - 1].First); + Experience = (Experience + amount).Clamp(0, nextLevel[MaxLevel - 1].First); - while (Level < MaxLevel && experience >= nextLevel[Level].First) + while (Level < MaxLevel && Experience >= nextLevel[Level].First) { self.GrantCondition(nextLevel[Level].Second); @@ -141,7 +142,7 @@ namespace OpenRA.Mods.Common.Traits void ITransformActorInitModifier.ModifyTransformActorInit(Actor self, TypeDictionary init) { - init.Add(new ExperienceInit(info, experience)); + init.Add(new ExperienceInit(info, Experience)); } }