Introduce IUpgradable and purchasable unit upgrades.
This commit is contained in:
62
OpenRA.Mods.RA/GlobalUpgradable.cs
Normal file
62
OpenRA.Mods.RA/GlobalUpgradable.cs
Normal 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using OpenRA.Mods.RA;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA
|
||||
{
|
||||
public class GlobalUpgradableInfo : ITraitInfo
|
||||
{
|
||||
public readonly string[] Upgrades = { };
|
||||
public readonly string[] Prerequisites = { };
|
||||
|
||||
public object Create(ActorInitializer init) { return new GlobalUpgradable(init.self, this); }
|
||||
}
|
||||
|
||||
public class GlobalUpgradable : INotifyAddedToWorld
|
||||
{
|
||||
readonly GlobalUpgradableInfo info;
|
||||
readonly GlobalUpgradeManager manager;
|
||||
|
||||
public GlobalUpgradable(Actor actor, GlobalUpgradableInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
manager = actor.Owner.PlayerActor.Trait<GlobalUpgradeManager>();
|
||||
}
|
||||
|
||||
public void AddedToWorld(Actor self)
|
||||
{
|
||||
if (info.Prerequisites.Any())
|
||||
manager.Register(self, this, info.Prerequisites);
|
||||
}
|
||||
|
||||
public void RemovedFromWorld(Actor self)
|
||||
{
|
||||
if (info.Prerequisites.Any())
|
||||
manager.Unregister(self, this, info.Prerequisites);
|
||||
}
|
||||
|
||||
public void PrerequisitesUpdated(Actor self, bool available)
|
||||
{
|
||||
var upgrades = self.TraitsImplementing<IUpgradable>();
|
||||
foreach (var u in upgrades)
|
||||
{
|
||||
foreach (var t in info.Upgrades)
|
||||
if (u.AcceptsUpgrade(t))
|
||||
u.UpgradeAvailable(self, t, available);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -550,6 +550,8 @@
|
||||
<Compile Include="Graphics\ActorPreview.cs" />
|
||||
<Compile Include="Graphics\SpriteActorPreview.cs" />
|
||||
<Compile Include="Graphics\VoxelActorPreview.cs" />
|
||||
<Compile Include="GlobalUpgradable.cs" />
|
||||
<Compile Include="Player\GlobalUpgradeManager.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
|
||||
|
||||
94
OpenRA.Mods.RA/Player/GlobalUpgradeManager.cs
Normal file
94
OpenRA.Mods.RA/Player/GlobalUpgradeManager.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
#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.Graphics;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA
|
||||
{
|
||||
[Desc("Attach this to the player actor.")]
|
||||
public class GlobalUpgradeManagerInfo : ITraitInfo, Requires<TechTreeInfo>
|
||||
{
|
||||
public object Create(ActorInitializer init) { return new GlobalUpgradeManager(init); }
|
||||
}
|
||||
|
||||
public class GlobalUpgradeManager : ITechTreeElement
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly Dictionary<string, List<Pair<Actor, GlobalUpgradable>>> upgradables = new Dictionary<string, List<Pair<Actor, GlobalUpgradable>>>();
|
||||
readonly TechTree techTree;
|
||||
|
||||
public GlobalUpgradeManager(ActorInitializer init)
|
||||
{
|
||||
self = init.self;
|
||||
techTree = self.Trait<TechTree>();
|
||||
}
|
||||
|
||||
static string MakeKey(string[] prerequisites)
|
||||
{
|
||||
return "upgrade_" + string.Join("_", prerequisites.OrderBy(a => a));
|
||||
}
|
||||
|
||||
public void Register(Actor actor, GlobalUpgradable u, string[] prerequisites)
|
||||
{
|
||||
var key = MakeKey(prerequisites);
|
||||
if (!upgradables.ContainsKey(key))
|
||||
{
|
||||
upgradables.Add(key, new List<Pair<Actor, GlobalUpgradable>>());
|
||||
techTree.Add(key, prerequisites, 0, this);
|
||||
}
|
||||
|
||||
upgradables[key].Add(Pair.New(actor, u));
|
||||
|
||||
// Notify the current state
|
||||
u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites));
|
||||
}
|
||||
|
||||
public void Unregister(Actor actor, GlobalUpgradable u, string[] prerequisites)
|
||||
{
|
||||
var key = MakeKey(prerequisites);
|
||||
var list = upgradables[key];
|
||||
|
||||
list.RemoveAll(x => x.First == actor && x.Second == u);
|
||||
if (!list.Any())
|
||||
{
|
||||
upgradables.Remove(key);
|
||||
techTree.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void PrerequisitesAvailable(string key)
|
||||
{
|
||||
List<Pair<Actor, GlobalUpgradable>> list;
|
||||
if (!upgradables.TryGetValue(key, out list))
|
||||
return;
|
||||
|
||||
foreach (var u in list)
|
||||
u.Second.PrerequisitesUpdated(u.First, true);
|
||||
}
|
||||
|
||||
public void PrerequisitesUnavailable(string key)
|
||||
{
|
||||
List<Pair<Actor, GlobalUpgradable>> list;
|
||||
if (!upgradables.TryGetValue(key, out list))
|
||||
return;
|
||||
|
||||
foreach (var u in list)
|
||||
u.Second.PrerequisitesUpdated(u.First, false);
|
||||
}
|
||||
|
||||
public void PrerequisitesItemHidden(string key) { }
|
||||
public void PrerequisitesItemVisible(string key) { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user