StrangeLoopGames / EcoModKit

Eco Modkit
https://docs.play.eco
71 stars 24 forks source link

Autogen Component Class #70

Open RainbowIris323 opened 12 months ago

RainbowIris323 commented 12 months ago

Describe the mod example you'd like to see A lengthy description of all functions methods etc for making autogen UI's for world object components using AutogenClass and Autogen/SyncToView attributes and tooltips with updates based on property value changes General things to cover:

https://github.com/StrangeLoopGames/EcoModKit/assets/108814359/6aca9be5-3f12-43e6-8075-c8e0a7c52890

Additional context Currently making any kind of UI for components is a pain and the fact that I have spent countless hours trying to figure this out and have no results is kind of annoying so documentation would help. For example i have code that just wont work shown here:

using Eco.Core.Controller;
using Eco.Core.Items;
using Eco.Gameplay.Items;
using Eco.RM.Items;
using Eco.Shared.Localization;
using Eco.Shared.Serialization;
using Eco.Shared.Utils;
using Eco.Gameplay.Objects;
using Eco.Gameplay.Components.Storage;
using System.ComponentModel;

namespace Eco.RM.Components
{
    [Serialized]
    [AutogenClass]
    [RequireComponent(typeof(PublicStorageComponent))]
    [LocDisplayName("Battery Storage")]
    [LocDescription("Battery storage for a object.")]
    [HasIcon("BatteryStorageComponentIcon")]
    [CreateComponentTabLoc("Battery Storage")]
    [Ecopedia("[RM] Stored Power", "Batteries", true, true, "Battery Storage")]
    public partial class BatteryStorageComponent : WorldObjectComponent
    {
        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float MaxCharge { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float MaxDischargeRate { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float MaxChargeRate { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float CurrentCharge { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float EnergyTransferRate { get; set; }

        public Inventory Inventory => Parent.GetOrCreateComponent<PublicStorageComponent>().Inventory;
        float energyChangeLastTick;

        public void UpdateStats()
        {
            MaxCharge = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).MaxCharge);
            MaxDischargeRate = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).MaxDischargeRate);
            MaxChargeRate = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).MaxChargeRate);
            UpdateCharge();
        }
        public void UpdateCharge()
        {
            CurrentCharge = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).CurrentCharge);
        }

        public void Initialize(int slots)
        {
            Parent.GetOrCreateComponent<PublicStorageComponent>().Initialize(slots);
            Inventory.AddInvRestriction(new TagRestriction(new Tag[] {TagManager.GetOrMake("Battery")}));
            Inventory.SetOwner(Parent);
            UpdateStats();
        }
        public float Charge(float watts)
        {
            var og_watts = watts;
            Inventory.NonEmptyStacks.ForEach((itemStack) => {
                watts -= ((BatteryItem)itemStack.Item).Charge(watts);
            });
            energyChangeLastTick += og_watts - watts;
            return og_watts - watts;
        }
        public float Discharge(float watts)
        {
            var og_watts = watts;
            Inventory.NonEmptyStacks.ForEach((itemStack) => {
                watts -= ((BatteryItem)itemStack.Item).Discharge(watts);
            });
            energyChangeLastTick -= og_watts - watts;
            return og_watts - watts;
        }
        public override void LateTick()
        {
            if (energyChangeLastTick > 0) UpdateCharge();
            if (energyChangeLastTick != EnergyTransferRate) EnergyTransferRate = energyChangeLastTick;
            energyChangeLastTick = 0;
        }
    }
}