1
This commit is contained in:
206
Source/WulaFallenEmpire/BuildingComp/Building_ExtraGraphics.cs
Normal file
206
Source/WulaFallenEmpire/BuildingComp/Building_ExtraGraphics.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RimWorld;
|
||||
using UnityEngine;
|
||||
using Verse;
|
||||
using System.Linq;
|
||||
|
||||
namespace WulaFallenEmpire
|
||||
{
|
||||
public class Building_ExtraGraphics : Building
|
||||
{
|
||||
// 通过 ModExtension 配置的图形数据
|
||||
private ExtraGraphicsExtension modExtension;
|
||||
|
||||
// 图形缓存
|
||||
private Dictionary<string, Graphic> graphicsCache = new Dictionary<string, Graphic>();
|
||||
|
||||
// 动画状态 - 每个图层的独立浮动
|
||||
private Dictionary<int, float> layerHoverOffsets = new Dictionary<int, float>();
|
||||
private Dictionary<int, float> layerAnimationTimes = new Dictionary<int, float>();
|
||||
private int lastTick = -1;
|
||||
|
||||
public ExtraGraphicsExtension ModExtension
|
||||
{
|
||||
get
|
||||
{
|
||||
if (modExtension == null)
|
||||
{
|
||||
modExtension = def.GetModExtension<ExtraGraphicsExtension>();
|
||||
if (modExtension == null)
|
||||
{
|
||||
Log.Error($"Building_ExtraGraphics: No ExtraGraphicsExtension found for {def.defName}");
|
||||
// 创建默认配置避免空引用
|
||||
modExtension = new ExtraGraphicsExtension();
|
||||
}
|
||||
}
|
||||
return modExtension;
|
||||
}
|
||||
}
|
||||
|
||||
// 重写 Graphic 属性返回 null,完全自定义渲染
|
||||
public override Graphic Graphic => null;
|
||||
|
||||
// 获取缓存的图形
|
||||
private Graphic GetCachedGraphic(string texturePath, Vector2 scale, Color color)
|
||||
{
|
||||
string cacheKey = $"{texturePath}_{scale.x}_{scale.y}_{color}";
|
||||
|
||||
if (!graphicsCache.TryGetValue(cacheKey, out Graphic graphic))
|
||||
{
|
||||
graphic = GraphicDatabase.Get<Graphic_Single>(
|
||||
texturePath,
|
||||
ShaderDatabase.TransparentPostLight,
|
||||
scale,
|
||||
color);
|
||||
graphicsCache[cacheKey] = graphic;
|
||||
}
|
||||
|
||||
return graphic;
|
||||
}
|
||||
|
||||
// 完全重写 DrawAt 方法,实现自定义渲染系统
|
||||
protected override void DrawAt(Vector3 drawLoc, bool flip = false)
|
||||
{
|
||||
// 不调用基类的 DrawAt,完全自定义渲染
|
||||
|
||||
// 更新悬浮动画
|
||||
UpdateHoverAnimation();
|
||||
|
||||
// 绘制所有配置的图形层
|
||||
DrawGraphicLayers(drawLoc, flip);
|
||||
}
|
||||
|
||||
// 绘制所有图形层
|
||||
private void DrawGraphicLayers(Vector3 baseDrawPos, bool flip)
|
||||
{
|
||||
if (ModExtension.graphicLayers == null || ModExtension.graphicLayers.Count == 0)
|
||||
{
|
||||
Log.Warning($"Building_ExtraGraphics: No graphic layers configured for {def.defName}");
|
||||
return;
|
||||
}
|
||||
|
||||
// 按层级排序,确保正确的绘制顺序
|
||||
var sortedLayers = ModExtension.graphicLayers.OrderBy(layer => layer.drawOrder).ToList();
|
||||
|
||||
foreach (var layer in sortedLayers)
|
||||
{
|
||||
DrawGraphicLayer(baseDrawPos, flip, layer);
|
||||
}
|
||||
}
|
||||
|
||||
// 绘制单个图形层
|
||||
private void DrawGraphicLayer(Vector3 baseDrawPos, bool flip, GraphicLayerData layer)
|
||||
{
|
||||
if (string.IsNullOrEmpty(layer.texturePath))
|
||||
{
|
||||
Log.Warning($"Building_ExtraGraphics: Empty texture path in layer for {def.defName}");
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取图形
|
||||
Graphic graphic = GetCachedGraphic(layer.texturePath, layer.scale, layer.color);
|
||||
|
||||
// 计算图层浮动偏移
|
||||
float hoverOffset = 0f;
|
||||
if (layer.enableHover)
|
||||
{
|
||||
int layerIndex = ModExtension.graphicLayers.IndexOf(layer);
|
||||
if (layerHoverOffsets.ContainsKey(layerIndex))
|
||||
{
|
||||
hoverOffset = layerHoverOffsets[layerIndex];
|
||||
}
|
||||
}
|
||||
|
||||
// 最终绘制位置 = 基础位置 + 图层偏移 + 浮动偏移
|
||||
Vector3 drawPos = baseDrawPos + layer.offset;
|
||||
drawPos.z += hoverOffset;
|
||||
|
||||
// 绘制图形
|
||||
graphic.Draw(drawPos, flip ? base.Rotation.Opposite : base.Rotation, this, 0f);
|
||||
}
|
||||
|
||||
// 更新每个图层的独立悬浮动画
|
||||
private void UpdateHoverAnimation()
|
||||
{
|
||||
int currentTick = Find.TickManager.TicksGame;
|
||||
|
||||
if (currentTick != lastTick)
|
||||
{
|
||||
// 更新每个图层的动画
|
||||
for (int i = 0; i < ModExtension.graphicLayers.Count; i++)
|
||||
{
|
||||
var layer = ModExtension.graphicLayers[i];
|
||||
|
||||
if (layer.enableHover)
|
||||
{
|
||||
// 初始化动画时间
|
||||
if (!layerAnimationTimes.ContainsKey(i))
|
||||
{
|
||||
layerAnimationTimes[i] = 0f;
|
||||
}
|
||||
|
||||
// 更新动画时间
|
||||
layerAnimationTimes[i] += Time.deltaTime;
|
||||
|
||||
// 计算该图层的悬浮偏移
|
||||
float hoverSpeed = layer.hoverSpeed > 0 ? layer.hoverSpeed : ModExtension.globalHoverSpeed;
|
||||
float hoverIntensity = layer.hoverIntensity > 0 ? layer.hoverIntensity : ModExtension.globalHoverIntensity;
|
||||
|
||||
float hoverOffset = Mathf.Sin(layerAnimationTimes[i] * hoverSpeed + layer.hoverPhase) * hoverIntensity;
|
||||
layerHoverOffsets[i] = hoverOffset;
|
||||
}
|
||||
}
|
||||
|
||||
lastTick = currentTick;
|
||||
}
|
||||
}
|
||||
|
||||
// 保存和加载
|
||||
public override void ExposeData()
|
||||
{
|
||||
base.ExposeData();
|
||||
// 保存自定义状态(如果需要)
|
||||
}
|
||||
}
|
||||
|
||||
// 主要的 ModExtension 定义
|
||||
public class ExtraGraphicsExtension : DefModExtension
|
||||
{
|
||||
// 全局悬浮参数(作为默认值)
|
||||
public float globalHoverSpeed = 2f; // 全局悬浮速度
|
||||
public float globalHoverIntensity = 0.1f; // 全局悬浮强度
|
||||
|
||||
// 图形层配置
|
||||
public List<GraphicLayerData> graphicLayers = new List<GraphicLayerData>();
|
||||
|
||||
public ExtraGraphicsExtension()
|
||||
{
|
||||
// 默认配置,避免空列表
|
||||
if (graphicLayers == null)
|
||||
{
|
||||
graphicLayers = new List<GraphicLayerData>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 单个图形层的配置数据
|
||||
public class GraphicLayerData
|
||||
{
|
||||
// 基础配置
|
||||
public string texturePath; // 纹理路径(必需)
|
||||
public Vector2 scale = Vector2.one; // 缩放比例
|
||||
public Color color = Color.white; // 颜色
|
||||
public int drawOrder = 0; // 绘制顺序(数字小的先绘制)
|
||||
|
||||
// 位置配置 - 使用环世界坐标系
|
||||
// X: 左右偏移, Y: 图层深度, Z: 上下偏移
|
||||
public Vector3 offset = Vector3.zero;
|
||||
|
||||
// 独立悬浮配置
|
||||
public bool enableHover = true; // 是否启用悬浮
|
||||
public float hoverSpeed = 0f; // 悬浮速度(0表示使用全局速度)
|
||||
public float hoverIntensity = 0f; // 悬浮强度(0表示使用全局强度)
|
||||
public float hoverPhase = 0f; // 悬浮相位(用于错开浮动)
|
||||
}
|
||||
}
|
||||
@@ -312,71 +312,118 @@ namespace WulaFallenEmpire
|
||||
|
||||
return validSpots;
|
||||
}
|
||||
|
||||
// 新增:分配物品到空投舱,包含材质处理
|
||||
// 在 Building_GlobalWorkTable.cs 中修改 DistributeItemsToPods 方法
|
||||
private List<List<Thing>> DistributeItemsToPods(GlobalStorageWorldComponent storage, int podCount)
|
||||
{
|
||||
List<List<Thing>> podContents = new List<List<Thing>>();
|
||||
|
||||
// 初始化空投舱内容列表
|
||||
for (int i = 0; i < podCount; i++)
|
||||
{
|
||||
podContents.Add(new List<Thing>());
|
||||
}
|
||||
|
||||
// 获取所有输出物品并转换为Thing列表
|
||||
List<Thing> allItems = new List<Thing>();
|
||||
|
||||
// 首先处理机械体,因为需要特殊处理
|
||||
foreach (var kvp in storage.outputStorage.ToList())
|
||||
{
|
||||
if (kvp.Value <= 0) continue;
|
||||
|
||||
ThingDef thingDef = kvp.Key;
|
||||
int remainingCount = kvp.Value;
|
||||
|
||||
// 如果是Pawn,需要特殊处理
|
||||
if (thingDef.race != null)
|
||||
{
|
||||
Log.Message($"[Airdrop] Processing {remainingCount} pawns of type {thingDef.defName}");
|
||||
|
||||
// 对于Pawn,每个单独生成
|
||||
for (int i = 0; i < remainingCount; i++)
|
||||
{
|
||||
PawnKindDef randomPawnKind = GetRandomPawnKindForType(thingDef);
|
||||
if (randomPawnKind != null)
|
||||
{
|
||||
Pawn pawn = PawnGenerator.GeneratePawn(randomPawnKind, Faction.OfPlayer);
|
||||
allItems.Add(pawn);
|
||||
try
|
||||
{
|
||||
Pawn pawn = PawnGenerator.GeneratePawn(randomPawnKind, Faction.OfPlayer);
|
||||
// 确保Pawn处于活跃状态
|
||||
if (pawn != null)
|
||||
{
|
||||
// 设置Pawn为可用的状态
|
||||
pawn.health.Reset();
|
||||
pawn.drafter = new Pawn_DraftController(pawn);
|
||||
|
||||
allItems.Add(pawn);
|
||||
Log.Message($"[Airdrop] Successfully generated pawn: {pawn.Label}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error("[Airdrop] Generated pawn is null");
|
||||
}
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.Error($"[Airdrop] Error generating pawn: {ex}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"[Airdrop] Could not find suitable PawnKindDef for {thingDef.defName}");
|
||||
}
|
||||
}
|
||||
|
||||
// 立即从存储中移除已处理的机械体
|
||||
storage.RemoveFromOutputStorage(thingDef, remainingCount);
|
||||
}
|
||||
else
|
||||
}
|
||||
// 然后处理普通物品
|
||||
foreach (var kvp in storage.outputStorage.ToList())
|
||||
{
|
||||
if (kvp.Value <= 0) continue;
|
||||
ThingDef thingDef = kvp.Key;
|
||||
int remainingCount = kvp.Value;
|
||||
// 跳过已经处理的机械体
|
||||
if (thingDef.race != null) continue;
|
||||
Log.Message($"[Airdrop] Processing {remainingCount} items of type {thingDef.defName}");
|
||||
// 对于普通物品,按照堆叠限制分割
|
||||
while (remainingCount > 0)
|
||||
{
|
||||
// 对于普通物品,按照堆叠限制分割
|
||||
while (remainingCount > 0)
|
||||
int stackSize = Mathf.Min(remainingCount, thingDef.stackLimit);
|
||||
Thing thing = CreateThingWithMaterial(thingDef, stackSize);
|
||||
if (thing != null)
|
||||
{
|
||||
int stackSize = Mathf.Min(remainingCount, thingDef.stackLimit);
|
||||
Thing thing = CreateThingWithMaterial(thingDef, stackSize);
|
||||
allItems.Add(thing);
|
||||
remainingCount -= stackSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"[Airdrop] Failed to create thing: {thingDef.defName}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 从存储中移除已处理的物品
|
||||
storage.RemoveFromOutputStorage(thingDef, kvp.Value);
|
||||
}
|
||||
|
||||
if (allItems.Count == 0)
|
||||
{
|
||||
Log.Message("[Airdrop] No items to distribute");
|
||||
return podContents;
|
||||
|
||||
}
|
||||
Log.Message($"[Airdrop] Total items to distribute: {allItems.Count}");
|
||||
// 平均分配物品到空投舱
|
||||
int currentPod = 0;
|
||||
foreach (Thing item in allItems)
|
||||
{
|
||||
podContents[currentPod].Add(item);
|
||||
currentPod = (currentPod + 1) % podCount;
|
||||
if (item != null)
|
||||
{
|
||||
podContents[currentPod].Add(item);
|
||||
currentPod = (currentPod + 1) % podCount;
|
||||
}
|
||||
}
|
||||
|
||||
// 从存储中移除已分配的物品
|
||||
foreach (var kvp in storage.outputStorage.ToList())
|
||||
// 记录分配结果
|
||||
for (int i = 0; i < podContents.Count; i++)
|
||||
{
|
||||
storage.outputStorage[kvp.Key] = 0;
|
||||
Log.Message($"[Airdrop] Pod {i} contains {podContents[i].Count} items");
|
||||
}
|
||||
|
||||
return podContents;
|
||||
}
|
||||
|
||||
@@ -431,94 +478,127 @@ namespace WulaFallenEmpire
|
||||
return defaultThing;
|
||||
}
|
||||
|
||||
// 在 Building_GlobalWorkTable.cs 中修改 GetRandomPawnKindForType 方法
|
||||
// 改进 GetRandomPawnKindForType 方法
|
||||
private PawnKindDef GetRandomPawnKindForType(ThingDef pawnType)
|
||||
{
|
||||
if (pawnType.race == null) return null;
|
||||
|
||||
// 获取建筑拥有者派系
|
||||
Faction buildingFaction = this.Faction;
|
||||
if (buildingFaction == null)
|
||||
if (pawnType.race == null)
|
||||
{
|
||||
Log.Warning("Building has no faction, cannot select appropriate pawn kind");
|
||||
Log.Error($"[Airdrop] GetRandomPawnKindForType: {pawnType.defName} is not a pawn type");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取工作台的派系
|
||||
Faction workTableFaction = this.Faction;
|
||||
if (workTableFaction == null)
|
||||
{
|
||||
Log.Error($"[Airdrop] Work table has no faction");
|
||||
return null;
|
||||
}
|
||||
Log.Message($"[Airdrop] Work table faction: {workTableFaction.def.defName}");
|
||||
// 获取该种族的所有PawnKindDef
|
||||
var availableKinds = DefDatabase<PawnKindDef>.AllDefs
|
||||
.Where(kind => kind.race == pawnType)
|
||||
.ToList();
|
||||
|
||||
if (availableKinds.Count == 0) return null;
|
||||
|
||||
// 按优先级分组
|
||||
if (availableKinds.Count == 0)
|
||||
{
|
||||
Log.Error($"[Airdrop] No PawnKindDef found for race: {pawnType.defName}");
|
||||
return null;
|
||||
}
|
||||
Log.Message($"[Airdrop] Found {availableKinds.Count} PawnKindDefs for {pawnType.defName}");
|
||||
// 最高优先级:与工作台派系完全相同的PawnKind
|
||||
var matchingFactionKinds = availableKinds
|
||||
.Where(kind => kind.defaultFactionDef != null &&
|
||||
kind.defaultFactionDef == buildingFaction.def)
|
||||
kind.defaultFactionDef == workTableFaction.def)
|
||||
.ToList();
|
||||
|
||||
if (matchingFactionKinds.Count > 0)
|
||||
{
|
||||
var selected = matchingFactionKinds.RandomElement();
|
||||
Log.Message($"[Airdrop] Selected matching faction PawnKind: {selected.defName} (faction: {workTableFaction.def.defName})");
|
||||
return selected;
|
||||
}
|
||||
// 次高优先级:玩家派系的PawnKind(如果工作台是玩家派系)
|
||||
if (workTableFaction.IsPlayer)
|
||||
{
|
||||
var playerFactionKinds = availableKinds
|
||||
.Where(kind => kind.defaultFactionDef != null &&
|
||||
(kind.defaultFactionDef == FactionDefOf.PlayerColony ||
|
||||
kind.defaultFactionDef == FactionDefOf.PlayerTribe))
|
||||
.ToList();
|
||||
if (playerFactionKinds.Count > 0)
|
||||
{
|
||||
var selected = playerFactionKinds.RandomElement();
|
||||
Log.Message($"[Airdrop] Selected player faction PawnKind: {selected.defName}");
|
||||
return selected;
|
||||
}
|
||||
}
|
||||
// 备选:没有特定派系的PawnKind
|
||||
var noFactionKinds = availableKinds
|
||||
.Where(kind => kind.defaultFactionDef == null)
|
||||
.ToList();
|
||||
|
||||
// 排除与建筑派系不同的PawnKind
|
||||
var excludedKinds = availableKinds
|
||||
.Where(kind => kind.defaultFactionDef != null &&
|
||||
kind.defaultFactionDef != buildingFaction.def)
|
||||
.ToList();
|
||||
|
||||
// 优先级选择
|
||||
PawnKindDef selectedKind = null;
|
||||
|
||||
// 1. 最高优先级:与建筑派系相同的PawnKind
|
||||
if (matchingFactionKinds.Count > 0)
|
||||
if (noFactionKinds.Count > 0)
|
||||
{
|
||||
selectedKind = matchingFactionKinds.RandomElement();
|
||||
var selected = noFactionKinds.RandomElement();
|
||||
Log.Message($"[Airdrop] Selected no-faction PawnKind: {selected.defName}");
|
||||
return selected;
|
||||
}
|
||||
// 2. 备选:没有defaultFactionDef的PawnKind
|
||||
else if (noFactionKinds.Count > 0)
|
||||
{
|
||||
selectedKind = noFactionKinds.RandomElement();
|
||||
}
|
||||
// 3. 没有符合条件的PawnKind
|
||||
else
|
||||
{
|
||||
Log.Warning($"No suitable PawnKind found for {pawnType.defName} with building faction {buildingFaction.def.defName}");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 最后选择任何可用的PawnKind
|
||||
var selectedKind = availableKinds.RandomElement();
|
||||
Log.Message($"[Airdrop] Selected fallback PawnKind: {selectedKind.defName}");
|
||||
return selectedKind;
|
||||
}
|
||||
|
||||
// 新增:创建空投舱
|
||||
// 修改 CreateDropPod 方法
|
||||
private bool CreateDropPod(IntVec3 dropCell, List<Thing> contents)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (contents == null || contents.Count == 0)
|
||||
return false;
|
||||
|
||||
// 创建空投舱信息
|
||||
ActiveTransporterInfo dropPodInfo = new ActiveTransporterInfo();
|
||||
|
||||
// 添加所有物品到空投舱
|
||||
foreach (Thing thing in contents)
|
||||
{
|
||||
dropPodInfo.innerContainer.TryAdd(thing, true);
|
||||
Log.Warning("[Airdrop] CreateDropPod: contents is null or empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 设置空投舱参数
|
||||
Log.Message($"[Airdrop] Creating drop pod at {dropCell} with {contents.Count} items");
|
||||
// 检查目标单元格是否有效
|
||||
if (!dropCell.IsValid || !dropCell.InBounds(Map))
|
||||
{
|
||||
Log.Error($"[Airdrop] Invalid drop cell: {dropCell}");
|
||||
return false;
|
||||
}
|
||||
// 创建空投舱信息 - 使用 DropPodInfo 而不是 ActiveTransporterInfo
|
||||
ActiveTransporterInfo dropPodInfo = new ActiveTransporterInfo();
|
||||
dropPodInfo.openDelay = 180; // 3秒后打开
|
||||
dropPodInfo.leaveSlag = true;
|
||||
|
||||
// 创建容器并添加物品
|
||||
ThingOwner container = new ThingOwner<Thing>();
|
||||
foreach (Thing thing in contents)
|
||||
{
|
||||
if (thing != null)
|
||||
{
|
||||
if (!container.TryAdd(thing, true))
|
||||
{
|
||||
Log.Error($"[Airdrop] Failed to add {thing.Label} to drop pod");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Message($"[Airdrop] Added {thing.Label} to drop pod");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (container.Count == 0)
|
||||
{
|
||||
Log.Warning("[Airdrop] No items were successfully added to drop pod");
|
||||
return false;
|
||||
}
|
||||
dropPodInfo.innerContainer = container;
|
||||
// 生成空投舱
|
||||
DropPodUtility.MakeDropPodAt(dropCell, Map, dropPodInfo);
|
||||
|
||||
|
||||
Log.Message($"[Airdrop] Successfully created drop pod at {dropCell}");
|
||||
return true;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.Error($"Failed to create drop pod at {dropCell}: {ex}");
|
||||
Log.Error($"[Airdrop] Failed to create drop pod at {dropCell}: {ex}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@
|
||||
<Compile Include="Ability\WULA_AbilityEnergyLance\EnergyLance.cs" />
|
||||
<Compile Include="Ability\WULA_AbilityEnergyLance\EnergyLanceExtension.cs" />
|
||||
<Compile Include="Ability\WULA_AbilityCallSkyfaller\CompAbilityEffect_CallSkyfaller.cs" />
|
||||
<Compile Include="BuildingComp\Building_ExtraGraphics.cs" />
|
||||
<Compile Include="BuildingComp\Building_MapObserver.cs" />
|
||||
<Compile Include="BuildingComp\WULA_BuildingBombardment\CompBuildingBombardment.cs" />
|
||||
<Compile Include="Ability\WULA_AbilityCallSkyfaller\CompProperties_AbilityCallSkyfaller.cs" />
|
||||
|
||||
Reference in New Issue
Block a user