|
| 1 | +using RimWorld; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using UnityEngine; |
| 8 | +using Verse; |
| 9 | + |
| 10 | +namespace TabulaRasa |
| 11 | +{ |
| 12 | + public class Comp_SlotLoadable : ThingComp |
| 13 | + { |
| 14 | + public bool GizmosOnEquip = true; |
| 15 | + |
| 16 | + public bool isGathering; |
| 17 | + |
| 18 | + public bool isInitialized; |
| 19 | + public bool IsInitialized => isInitialized; |
| 20 | + |
| 21 | + public List<SlotLoadable> slots = new List<SlotLoadable>(); |
| 22 | + public List<SlotLoadable> Slots => slots; |
| 23 | + |
| 24 | + public List<SlotLoadableDef> SlotDefs |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + var result = new List<SlotLoadableDef>(); |
| 29 | + if (slots != null) |
| 30 | + { |
| 31 | + foreach (var slot in slots) |
| 32 | + { |
| 33 | + result.Add(slot.def as SlotLoadableDef); |
| 34 | + } |
| 35 | + } |
| 36 | + return result; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public Map GetMap |
| 41 | + { |
| 42 | + get |
| 43 | + { |
| 44 | + var map = parent.Map; |
| 45 | + if (map == null) |
| 46 | + { |
| 47 | + if (GetPawn != null) |
| 48 | + { |
| 49 | + map = GetPawn.Map; |
| 50 | + } |
| 51 | + } |
| 52 | + return map; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public CompEquippable GetEquippable => parent.GetComp<CompEquippable>(); |
| 57 | + |
| 58 | + private Pawn GetPawn => GetEquippable.verbTracker.PrimaryVerb.CasterPawn; |
| 59 | + |
| 60 | + |
| 61 | + public CompProperties_SlotLoadable Props => (CompProperties_SlotLoadable)props; |
| 62 | + |
| 63 | + public void Initialize() |
| 64 | + { |
| 65 | + if (!isInitialized) |
| 66 | + { |
| 67 | + isInitialized = true; |
| 68 | + if (Props?.slots != null) |
| 69 | + { |
| 70 | + foreach (var slot in Props.slots) |
| 71 | + { |
| 72 | + var newSlot = new SlotLoadable(slot, parent); |
| 73 | + LogUtil.LogMessage("Added Slot"); |
| 74 | + slots.Add(newSlot); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public override void PostPostMake() |
| 81 | + { |
| 82 | + base.PostPostMake(); |
| 83 | + |
| 84 | + if (slots.NullOrEmpty() && Props?.slots != null) |
| 85 | + { |
| 86 | + Initialize(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public override void CompTick() |
| 91 | + { |
| 92 | + base.CompTick(); |
| 93 | + } |
| 94 | + |
| 95 | + private void TryCancel(string reason = "") |
| 96 | + { |
| 97 | + var pawn = GetPawn; |
| 98 | + if (pawn != null) |
| 99 | + { |
| 100 | + if (pawn.CurJob.def == TabulaRasaDefOf.TabulaRasa_GatherSlotItem) |
| 101 | + { |
| 102 | + pawn.jobs.StopAll(); |
| 103 | + } |
| 104 | + isGathering = false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void TryGiveLoadSlotJob(Thing itemToLoad) |
| 109 | + { |
| 110 | + if (GetPawn != null) |
| 111 | + { |
| 112 | + if (!GetPawn.Drafted) |
| 113 | + { |
| 114 | + isGathering = true; |
| 115 | + |
| 116 | + var job = JobMaker.MakeJob(TabulaRasaDefOf.TabulaRasa_GatherSlotItem, itemToLoad); |
| 117 | + job.count = 1; |
| 118 | + GetPawn.jobs.TryTakeOrderedJob(job); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + Messages.Message(string.Format("{ 0} is drafted.", new object[] |
| 123 | + { |
| 124 | + GetPawn.Label |
| 125 | + }), MessageTypeDefOf.RejectInput); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public virtual bool TryLoadSlot(Thing thing) |
| 131 | + { |
| 132 | + isGathering = false; |
| 133 | + if (slots != null) |
| 134 | + { |
| 135 | + var loadSlot = slots.FirstOrDefault(x => x.IsEmpty() && x.CanLoad(thing.def)); |
| 136 | + if (loadSlot == null) |
| 137 | + { |
| 138 | + loadSlot = slots.FirstOrDefault(y => y.CanLoad(thing.def)); |
| 139 | + } |
| 140 | + if (loadSlot != null) |
| 141 | + { |
| 142 | + if (loadSlot.TryLoadSlot(thing, true)) |
| 143 | + { |
| 144 | + return true; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + public void ProcessInput(SlotLoadable slot) |
| 152 | + { |
| 153 | + var floatList = new List<FloatMenuOption>(); |
| 154 | + if (!isGathering) |
| 155 | + { |
| 156 | + var map = GetMap; |
| 157 | + if (slot.SlotOccupant == null && slot.SlottableTypes is List<ThingDef> loadTypes) |
| 158 | + { |
| 159 | + if (loadTypes.Count > 0) |
| 160 | + { |
| 161 | + foreach (var current in loadTypes) |
| 162 | + { |
| 163 | + var thingToLoad = map.listerThings.ThingsOfDef(current).FirstOrDefault(x => map.reservationManager.CanReserve(GetPawn, x)); |
| 164 | + if (thingToLoad != null) |
| 165 | + { |
| 166 | + var text = "Load".Translate() + " " + thingToLoad.def.label; |
| 167 | + floatList.Add(new FloatMenuOption(text, delegate { TryGiveLoadSlotJob(thingToLoad); }, MenuOptionPriority.Default, null, null, 29f, null, null)); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + FloatMenuOption option = new FloatMenuOption(string.Format("{0} unavailable", new object[] { current.label }), delegate { }, MenuOptionPriority.Default); |
| 172 | + option.Disabled = true; |
| 173 | + floatList.Add(option); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + FloatMenuOption option = new FloatMenuOption("No load options available.", delegate { }, MenuOptionPriority.Default); |
| 180 | + option.Disabled = true; |
| 181 | + floatList.Add(option); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + if (!slot.IsEmpty()) |
| 186 | + { |
| 187 | + var text = string.Format("Unload {0}", new object[] { slot.SlotOccupant.Label }); |
| 188 | + floatList.Add(new FloatMenuOption(text, delegate { TryEmptySlot(slot); }, MenuOptionPriority.Default, null, null, 29f, null, null)); |
| 189 | + } |
| 190 | + Find.WindowStack.Add(new FloatMenu(floatList)); |
| 191 | + } |
| 192 | + |
| 193 | + public virtual void TryEmptySlot(SlotLoadable slot) |
| 194 | + { |
| 195 | + slot.TryEmptySlot(); |
| 196 | + } |
| 197 | + |
| 198 | + public virtual IEnumerable<Gizmo> EquippedGizmos() |
| 199 | + { |
| 200 | + if (!slots.NullOrEmpty() && GetPawn.Faction.IsPlayer) |
| 201 | + { |
| 202 | + if (isGathering) |
| 203 | + { |
| 204 | + yield return new Command_Action |
| 205 | + { |
| 206 | + defaultLabel = "Designator_Cancel".Translate(), |
| 207 | + defaultDesc = "Designator_CancelDesc".Translate(), |
| 208 | + icon = ContentFinder<Texture2D>.Get("UI/Designators/Cancel", true), |
| 209 | + action = delegate { TryCancel(); } |
| 210 | + }; |
| 211 | + } |
| 212 | + foreach (var slot in slots) |
| 213 | + { |
| 214 | + if (slot.IsEmpty()) |
| 215 | + { |
| 216 | + yield return new Command_Action |
| 217 | + { |
| 218 | + defaultLabel = slot.LabelNoCount, |
| 219 | + icon = Command.BGTex, |
| 220 | + defaultDesc = SlotDesc(slot), |
| 221 | + action = delegate { ProcessInput(slot); } |
| 222 | + }; |
| 223 | + } |
| 224 | + else |
| 225 | + { |
| 226 | + yield return new Command_Action |
| 227 | + { |
| 228 | + defaultLabel = slot.LabelNoCount, |
| 229 | + icon = slot.SlotIcon(), |
| 230 | + defaultDesc = SlotDesc(slot), |
| 231 | + defaultIconColor = slot.SlotColor(), |
| 232 | + action = delegate { ProcessInput(slot); } |
| 233 | + }; |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + public virtual string SlotDesc(SlotLoadable slot) |
| 240 | + { |
| 241 | + var s = new StringBuilder(); |
| 242 | + s.AppendLine(slot.def.description); |
| 243 | + if (!slot.IsEmpty()) |
| 244 | + { |
| 245 | + s.AppendLine(); |
| 246 | + s.AppendLine(string.Format("Loaded {0}", new object[] { slot.SlotOccupant.LabelCap })); |
| 247 | + } |
| 248 | + return s.ToString(); |
| 249 | + } |
| 250 | + |
| 251 | + public override void PostExposeData() |
| 252 | + { |
| 253 | + Scribe_Values.Look(ref isInitialized, "isInitialized", false); |
| 254 | + Scribe_Values.Look(ref isGathering, "isGathering", false); |
| 255 | + Scribe_Collections.Look(ref slots, "slots", LookMode.Deep); |
| 256 | + base.PostExposeData(); |
| 257 | + if (slots == null) |
| 258 | + { |
| 259 | + slots = new List<SlotLoadable>(); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments