Files
OpenRA/OpenRA.Mods.Cnc/Traits/World/TSTiberiumRenderer.cs
let5sne.win10 9cf6ebb986
Some checks failed
Continuous Integration / Linux (.NET 8.0) (push) Has been cancelled
Continuous Integration / Windows (.NET 8.0) (push) Has been cancelled
Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:46:54 +08:00

97 lines
3.8 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* 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 System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Renders the Tiberian Sun Tiberium resources.", "Attach this to the world actor")]
public class TSTiberiumRendererInfo : ResourceRendererInfo
{
[Desc("Sequences to use for ramp type 1.", "Dictionary of [resource type]: [list of sequences].")]
public readonly FrozenDictionary<string, ImmutableArray<string>> Ramp1Sequences = FrozenDictionary<string, ImmutableArray<string>>.Empty;
[Desc("Sequences to use for ramp type 2.", "Dictionary of [resource type]: [list of sequences].")]
public readonly FrozenDictionary<string, ImmutableArray<string>> Ramp2Sequences = FrozenDictionary<string, ImmutableArray<string>>.Empty;
[Desc("Sequences to use for ramp type 3.", "Dictionary of [resource type]: [list of sequences].")]
public readonly FrozenDictionary<string, ImmutableArray<string>> Ramp3Sequences = FrozenDictionary<string, ImmutableArray<string>>.Empty;
[Desc("Sequences to use for ramp type 4.", "Dictionary of [resource type]: [list of sequences].")]
public readonly FrozenDictionary<string, ImmutableArray<string>> Ramp4Sequences = FrozenDictionary<string, ImmutableArray<string>>.Empty;
public override object Create(ActorInitializer init) { return new TSTiberiumRenderer(init.Self, this); }
}
public class TSTiberiumRenderer : ResourceRenderer
{
readonly TSTiberiumRendererInfo info;
readonly World world;
readonly Dictionary<string, Dictionary<string, ISpriteSequence>> ramp1Variants = [];
readonly Dictionary<string, Dictionary<string, ISpriteSequence>> ramp2Variants = [];
readonly Dictionary<string, Dictionary<string, ISpriteSequence>> ramp3Variants = [];
readonly Dictionary<string, Dictionary<string, ISpriteSequence>> ramp4Variants = [];
public TSTiberiumRenderer(Actor self, TSTiberiumRendererInfo info)
: base(self, info)
{
this.info = info;
world = self.World;
}
void LoadVariants(FrozenDictionary<string, ImmutableArray<string>> rampSequences, Dictionary<string, Dictionary<string, ISpriteSequence>> rampVariants)
{
var sequences = world.Map.Sequences;
foreach (var kv in rampSequences)
{
if (!Info.ResourceTypes.TryGetValue(kv.Key, out var resourceInfo))
continue;
var resourceVariants = kv.Value
.ToDictionary(v => v, v => sequences.GetSequence(resourceInfo.Image, v));
rampVariants.Add(kv.Key, resourceVariants);
}
}
protected override void WorldLoaded(World w, WorldRenderer wr)
{
LoadVariants(info.Ramp1Sequences, ramp1Variants);
LoadVariants(info.Ramp2Sequences, ramp2Variants);
LoadVariants(info.Ramp3Sequences, ramp3Variants);
LoadVariants(info.Ramp4Sequences, ramp4Variants);
base.WorldLoaded(w, wr);
}
protected override ISpriteSequence ChooseVariant(string resourceType, CPos cell)
{
Dictionary<string, Dictionary<string, ISpriteSequence>> variants;
switch (world.Map.Ramp[cell])
{
case 1: variants = ramp1Variants; break;
case 2: variants = ramp2Variants; break;
case 3: variants = ramp3Variants; break;
case 4: variants = ramp4Variants; break;
default: variants = Variants; break;
}
return variants[resourceType].Values.Random(world.LocalRandom);
}
}
}