コード例 #1
0
ファイル: ArmActuator.cs プロジェクト: PainGod85/MechEngineer
        public void OnItemGrabbed(IMechLabDraggableItem item, MechLabPanel mechLab, MechLabLocationWidget widget)
        {
            if (widget.loadout.Location != ChassisLocations.LeftArm &&
                widget.loadout.Location != ChassisLocations.RightArm)
            {
                return;
            }

            var loc_helper = new LocationHelper(widget);
            var total_slot = ArmActuatorSlot.None;
            var mount_loc  = widget.loadout.Location;
            var ml_helper  = new MechLabHelper(mechLab);

            CustomComponents.Control.LogDebug(DType.ComponentInstall, $"- ArmActuator: {Def.Description.Id} {Type}");


            void add_default(ArmActuatorSlot slot)
            {
                bool add_item(string id)
                {
                    if (string.IsNullOrEmpty(id))
                    {
                        return(false);
                    }

                    var r = DefaultHelper.CreateRef(id, ComponentType.Upgrade, mechLab.dataManager, mechLab.Sim);

                    if (r.Is <ArmActuator>(out var actuator) && (actuator.Type & total_slot) == 0)
                    {
                        DefaultHelper.AddMechLab(id, ComponentType.Upgrade, ml_helper, mount_loc);
                        total_slot = total_slot | actuator.Type;
                        return(true);
                    }

                    return(false);
                }

                CustomComponents.Control.LogDebug(DType.ComponentInstall, $"--- adding {slot} to {total_slot}");

                if (total_slot.HasFlag(slot))
                {
                    CustomComponents.Control.LogDebug(DType.ComponentInstall, $"---- already present");
                    return;
                }

                if (add_item(ArmActuatorCC.GetDefaultActuator(mechLab.activeMechDef, mount_loc, slot)))
                {
                    return;
                }

                add_item(ArmActuatorCC.GetDefaultActuator(null, mount_loc, slot));
            }

            for (int i = loc_helper.LocalInventory.Count - 1; i >= 0; i--)
            {
                var slotitem = loc_helper.LocalInventory[i];

                if (!slotitem.ComponentRef.Is <ArmActuator>())
                {
                    continue;
                }

                var actuator = slotitem.ComponentRef.GetComponent <ArmActuator>();
                if (slotitem.ComponentRef.IsDefault() &&
                    !slotitem.ComponentRef.IsModuleFixed(ml_helper.MechLab.activeMechDef))
                {
                    CustomComponents.Control.LogDebug(DType.ComponentInstall,
                                                      $"-- removing {slotitem.ComponentRef.ComponentDefID} {actuator.Type}");

                    DefaultHelper.RemoveMechLab(slotitem.ComponentRef.ComponentDefID,
                                                slotitem.ComponentRef.ComponentDefType, ml_helper, mount_loc);
                }
                else
                {
                    CustomComponents.Control.LogDebug(DType.ComponentInstall,
                                                      $"-- checking {slotitem.ComponentRef.ComponentDefID} {actuator.Type}");
                    total_slot = total_slot | actuator.Type;
                }
            }

            CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- actuators {total_slot}");

            add_default(ArmActuatorSlot.PartShoulder);
            add_default(ArmActuatorSlot.PartUpper);

            if (ArmActuatorCC.IsIgnoreFullActuators(mechLab.activeMechDef))
            {
                if (total_slot.HasFlag(ArmActuatorSlot.PartHand) && !total_slot.HasFlag(ArmActuatorSlot.PartLower))
                {
                    CustomComponents.Control.LogDebug(DType.ComponentInstall, $"-- Removing hand from {total_slot}");

                    var hand = loc_helper.LocalInventory.FirstOrDefault(i =>
                                                                        i.ComponentRef.Is <ArmActuator>(out var actuator) &&
                                                                        actuator.Type.HasFlag(ArmActuatorSlot.PartHand));
                    if (hand == null || hand.ComponentRef.IsFixed)
                    {
                        return;
                    }
                    var dragitem = mechLab.DragItem;
                    widget.OnRemoveItem(hand, false);
                    mechLab.ForceItemDrop(hand);
                    ml_helper.SetDragItem(dragitem as MechLabItemSlotElement);
                }
                else
                {
                    CustomComponents.Control.LogDebug(DType.ComponentInstall,
                                                      $"-- hand already removed from {total_slot}");
                }
            }
            else
            {
                var limit = mechLab.activeMechDef.Chassis.Is <ArmActuatorSupport>(out var s)
                    ? s.GetLimit(mount_loc)
                    : ArmActuatorSlot.Hand;
                if (limit.HasFlag(ArmActuatorSlot.PartLower))
                {
                    add_default(ArmActuatorSlot.PartLower);
                }
                if (limit.HasFlag(ArmActuatorSlot.PartHand))
                {
                    add_default(ArmActuatorSlot.PartHand);
                }
            }
        }