Saturday, December 10, 2022
HomeGame Developmentc# - The best way to accurately use summary and digital strategies...

c# – The best way to accurately use summary and digital strategies in Unity parts?


I’m making an attempt to make use of digital and summary strategies to make my sport structure higher.

I exploit a ShipComponent as a base Class as a result of I need all of the baby courses to do the identical factor.
However generally I need a sure ShipComponent to do one thing else.

The code will make it lots clearer:

ShipComponent.cs:

public summary class ShipComponent : MonoBehaviour
{
    [HideInInspector] public ShipControl shipControl;
    public digital void Init(ShipControl management)
    {
        this.shipControl = management;
    }
    public digital void IsPlayer()
    {
        SetListeners();
    }


    public summary void IsNotPlayer();

    public summary void ReEnable();

    public summary void SetListeners();
}

One of many many baby courses that inherits from ShipComponent:

    public class Rudder : ShipComponent
{
    [Header("Settings")]
    public Remodel rudder;

    [Header("Debug Info")]
    [SerializeField] float rudderSpeed;
    [SerializeField][Range(-45, 45)] int setRudderAngle = 0;
    [SerializeField][Range(-45f, 45f)] float realRudderAngle = 0f;

    public override void Init(ShipControl shipControl)
    {
        base.Init(shipControl);

        rudder = remodel.GetChild(0).GetChild(4);
        StartCoroutine(SmoothRudderChange());

        SetListeners();
    }
    public override void IsPlayer()
    {
        base.IsPlayer();
    }

    public override void IsNotPlayer()
    {
        PlayerShipControl.OnRudderChange -= SetRudder;
    }

    public override void ReEnable()
    {
        StartCoroutine(SmoothRudderChange());

        SetListeners();
    }

    public override void SetListeners()
    {
        PlayerShipControl.OnRudderChange -= SetRudder;
        if (!shipControl.shipWrapper.ship.IsPlayer) return;
        PlayerShipControl.OnRudderChange += SetRudder;
    }

    void OnDisable()
    {
        PlayerShipControl.OnRudderChange -= SetRudder;
        StopAllCoroutines();
    }

The principle draw again I expertise with this, is that I’ve to repeat paste all 5 or 6 strategies everytime I create a brand new ShipComponent class, and I’ve lots, the identical goes everytime I need to change a technique, ailing have to vary it in all 20 ShipComponent subclass variations…
So this appears to be unhealthy structure and I have to be lacking one thing.

It appears messy and theres numerous repeating code, more often than not the one distinction in every ShipComponent is the SetListeners half, and StartCoroutines if any.
Is there a technique to dynamically set delegate listeners up? So I may set them within the base class ShipComponent? As a substitute of setting every element individually?

One other script that inherits from ShipComponent for completeness:

public class Weapons : ShipComponent
{

    IEnumerator mouseAimCycle;

    public override void Init(ShipControl shipControl)
    {
        base.Init(shipControl);

        InitCannons();

        SetListeners();
    }

    public override void ReEnable()
    {
        SetListeners();
    }
    public override void IsPlayer()
    {
        base.IsPlayer();

        mouseAimCycle = AimCycle();
        StartCoroutine(mouseAimCycle);

        SetListeners();
    }

    public override void SetListeners()
    {
        PlayerShipControl.OnFireGuns -= TryFire;
        if (!shipControl.shipWrapper.ship.IsPlayer) return;
        PlayerShipControl.OnFireGuns += TryFire;
    }
    public override void IsNotPlayer()
    {

        StopCoroutine(mouseAimCycle);
        PlayerShipControl.OnFireGuns -= TryFire;
    }

    void OnDisable()
    {
        PlayerShipControl.OnFireGuns -= TryFire;
        StopAllCoroutines();
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments