Move Gives/Gains Experience and Bounty to Mods.Common

This commit is contained in:
penev92
2015-01-02 22:58:01 +02:00
parent 9dd607c846
commit 5e1af58bbe
7 changed files with 13 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
#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 OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.Common.Effects
{
class Rank : IEffect
{
readonly Actor self;
readonly Animation anim;
readonly string paletteName;
public Rank(Actor self, string paletteName)
{
this.self = self;
this.paletteName = paletteName;
var xp = self.Trait<GainsExperience>();
anim = new Animation(self.World, "rank");
anim.PlayRepeating("rank");
anim.PlayFetchIndex("rank", () => xp.Level == 0 ? 0 : xp.Level - 1);
}
public void Tick(World world)
{
if (self.IsDead)
world.AddFrameEndTask(w => w.Remove(this));
else
anim.Tick();
}
public IEnumerable<IRenderable> Render(WorldRenderer wr)
{
if (!self.IsInWorld)
yield break;
if (self.IsDead)
yield break;
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
yield break;
if (wr.world.FogObscures(self))
yield break;
var pos = wr.ScreenPxPosition(self.CenterPosition);
var bounds = self.Bounds;
bounds.Offset(pos.X, pos.Y);
var palette = wr.Palette(paletteName);
var offset = (int)(4 / wr.Viewport.Zoom);
var effectPos = wr.Position(new int2(bounds.Right - offset, bounds.Bottom - offset));
yield return new SpriteRenderable(anim.Image, effectPos, WVec.Zero, 0, palette, 1f / wr.Viewport.Zoom, true);
}
}
}

View File

@@ -75,6 +75,7 @@
<Compile Include="Effects\NukeLaunch.cs" />
<Compile Include="Effects\PowerdownIndicator.cs" />
<Compile Include="Effects\RallyPointIndicator.cs" />
<Compile Include="Effects\Rank.cs" />
<Compile Include="Effects\Smoke.cs" />
<Compile Include="Commands\ChatCommands.cs" />
<Compile Include="Commands\DevCommands.cs" />
@@ -134,6 +135,9 @@
<Compile Include="Traits\Buildings\RepairsUnits.cs" />
<Compile Include="Traits\Buildings\FootprintUtils.cs" />
<Compile Include="Traits\Burns.cs" />
<Compile Include="Traits\GainsExperience.cs" />
<Compile Include="Traits\GivesBounty.cs" />
<Compile Include="Traits\GivesExperience.cs" />
<Compile Include="Traits\Huntable.cs" />
<Compile Include="Traits\CustomBuildTimeValue.cs" />
<Compile Include="Traits\CustomSellValue.cs" />
@@ -284,4 +288,4 @@ cd "$(SolutionDir)"</PostBuildEvent>
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

View File

@@ -0,0 +1,131 @@
#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.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
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>
{
[FieldLoader.LoadUsing("LoadUpgrades")]
[Desc("Upgrades 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;
[Desc("Palette for the chevron glyph rendered in the selection box.")]
public readonly string ChevronPalette = "effect";
[Desc("Palette for the level up sprite.")]
public readonly string LevelUpPalette = "effect";
public object Create(ActorInitializer init) { return new GainsExperience(init, this); }
static object LoadUpgrades(MiniYaml y)
{
MiniYaml upgrades;
if (!y.ToDictionary().TryGetValue("Upgrades", out upgrades))
{
return new Dictionary<int, string[]>()
{
{ 200, new[] { "firepower", "damage", "speed", "reload", "inaccuracy" } },
{ 400, new[] { "firepower", "damage", "speed", "reload", "inaccuracy" } },
{ 800, new[] { "firepower", "damage", "speed", "reload", "inaccuracy" } },
{ 1600, new[] { "firepower", "damage", "speed", "reload", "inaccuracy", "eliteweapon", "selfheal" } }
};
}
return upgrades.Nodes.ToDictionary(
kv => FieldLoader.GetValue<int>("(key)", kv.Key),
kv => FieldLoader.GetValue<string[]>("(value)", kv.Value.Value));
}
}
public class GainsExperience : ISync
{
readonly Actor self;
readonly GainsExperienceInfo info;
readonly List<Pair<int, string[]>> nextLevel = new List<Pair<int, string[]>>();
// Stored as a percentage of our value
[Sync] int experience = 0;
[Sync] public int Level { get; private set; }
public readonly int MaxLevel;
public GainsExperience(ActorInitializer init, GainsExperienceInfo info)
{
self = init.self;
this.info = info;
MaxLevel = info.Upgrades.Count;
var cost = self.Info.Traits.Get<ValuedInfo>().Cost;
foreach (var kv in info.Upgrades)
nextLevel.Add(Pair.New(kv.Key * cost, kv.Value));
if (init.Contains<ExperienceInit>())
GiveExperience(init.Get<ExperienceInit, int>());
}
public bool CanGainLevel { get { return Level < MaxLevel; } }
public void GiveLevels(int numLevels)
{
var newLevel = Math.Min(Level + numLevels, MaxLevel);
GiveExperience(nextLevel[newLevel - 1].First - experience);
}
public void GiveExperience(int amount)
{
experience += amount;
while (Level < MaxLevel && experience >= nextLevel[Level].First)
{
var upgrades = nextLevel[Level].Second;
Level++;
var um = self.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in upgrades)
um.GrantUpgrade(self, u, this);
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", "LevelUp", self.Owner.Country.Race);
self.World.AddFrameEndTask(w => w.Add(new CrateEffect(self, "levelup", info.LevelUpPalette)));
if (Level == 1)
{
self.World.AddFrameEndTask(w =>
{
if (!self.IsDead)
w.Add(new Rank(self, info.ChevronPalette));
});
}
}
}
}
class ExperienceInit : IActorInit<int>
{
[FieldFromYamlKey] readonly int value;
public ExperienceInit() { }
public ExperienceInit(int init) { value = init; }
public int Value(World world) { return value; }
}
}

View File

@@ -0,0 +1,62 @@
#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.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("You get money for playing this actor.")]
class GivesBountyInfo : TraitInfo<GivesBounty>
{
[Desc("Calculated by Cost or CustomSellValue so they have to be set to avoid crashes.")]
public readonly int Percentage = 10;
[Desc("Higher ranked units give higher bounties.")]
public readonly int LevelMod = 125;
[Desc("Destroying creeps and enemies is rewarded.")]
public readonly Stance[] Stances = { Stance.Neutral, Stance.Enemy };
}
class GivesBounty : INotifyKilled
{
static int GetMultiplier(Actor self)
{
// returns 100's as 1, so as to keep accuracy for longer.
var info = self.Info.Traits.Get<GivesBountyInfo>();
var gainsExp = self.TraitOrDefault<GainsExperience>();
if (gainsExp == null)
return 100;
var slevel = gainsExp.Level;
return (slevel > 0) ? slevel * info.LevelMod : 100;
}
public void Killed(Actor self, AttackInfo e)
{
var info = self.Info.Traits.Get<GivesBountyInfo>();
if (e.Attacker == null || e.Attacker.Destroyed) return;
if (!info.Stances.Contains(e.Attacker.Owner.Stances[self.Owner])) return;
var cost = self.GetSellValue();
// 2 hundreds because of GetMultiplier and info.Percentage.
var bounty = cost * GetMultiplier(self) * info.Percentage / 10000;
if (bounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer))
e.Attacker.World.AddFrameEndTask(w => w.Add(new FloatingText(self.CenterPosition, e.Attacker.Owner.Color.RGB, FloatingText.FormatCashTick(bounty), 30)));
e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(bounty);
}
}
}

View File

@@ -0,0 +1,54 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor gives experience to a GainsExperience actor when they are killed.")]
class GivesExperienceInfo : ITraitInfo
{
[Desc("If -1, use the value of the unit cost.")]
public readonly int Experience = -1;
[Desc("Grant experience for team-kills.")]
public readonly bool FriendlyFire = false;
public object Create(ActorInitializer init) { return new GivesExperience(init.self, this); }
}
class GivesExperience : INotifyKilled
{
readonly GivesExperienceInfo info;
public GivesExperience(Actor self, GivesExperienceInfo info)
{
this.info = info;
}
public void Killed(Actor self, AttackInfo e)
{
// Prevent TK from giving exp
if (e.Attacker == null || e.Attacker.Destroyed || (!info.FriendlyFire && e.Attacker.Owner.Stances[self.Owner] == Stance.Ally))
return;
var valued = self.Info.Traits.GetOrDefault<ValuedInfo>();
// Default experience is 100 times our value
var exp = info.Experience >= 0
? info.Experience
: valued != null ? valued.Cost * 100 : 0;
var killer = e.Attacker.TraitOrDefault<GainsExperience>();
if (killer != null)
killer.GiveExperience(exp);
}
}
}