0

I have some things to clarify for myself, so please bear with me.

Let's say I want to have an object that will explode after a certain period of time. If the object explodes while the person is still holding it, you get killed.

Let's consider the following implementation:

public interface IKillable
{
    void Kill();
}

public interface IExplodable : IKillable
{
     float TimeLeft { get; set; }
     void Timer(float timeToExplode);
}

public abstract class Bomb: IExplodable
{
    public float TimeLeft { get; set; }

    public void Kill()
    {
        //Destroy the object
    }

    public void Timer(float timeLeft)
    {
        if (TimeLeft <= 0) {

            Kill();
        }
    }
}

What if I also want to add let's say a "Backpack" instead of "Bomb" that will have the exact same functionality or any other item that can explode (and kill)?

  1. Is inheriting a "Backpack from Bomb" reasonable in this case?
  2. Probably copying the code won't follow SOLID principles?
9
  • I can't understand clearly, the Backpack is exactly same with Bomb? Commented Jun 23, 2019 at 10:30
  • Because it can have the same functionality as Bomb (explode). I just gave the bad example.. Commented Jun 23, 2019 at 10:33
  • Could you explane it more clrearly? Commented Jun 23, 2019 at 10:34
  • I'm not sure what you don't understand? Commented Jun 23, 2019 at 10:35
  • 1
    @SeyedRaoufModarresi The question is that a Bomb has the same functionality (explodes and kills) as that of a BackPack, yet the two are different devices Commented Jun 23, 2019 at 10:41

2 Answers 2

1

Having a base class that provides common functionality for objects is both common and recommended in Unity.

Instead of calling the class Bomb, call it something more generic, like ExplodableDevice (as @zmbq answered). I would, however, make it more explicit that the device explods due to a timer, so perhaps TimerExplodableDevice. Notice that the base class should inherit from MonoBehaviour as otherwise you wouldn't be able to use those objects fully (as c# doesn't allow multiple inheritance).

An example of such implementation would be:

public interface IKillable
{
    void Kill();
}

public interface IExplodable : IKillable
{
     float TimeLeft { get; set; }
     void Timer(float timeToExplode);
}

public abstract class TimerExplodableDevice: MonoBehaviour, IExplodable
{
    public float TimeLeft { get; set; }

    public virtual void Kill()
    {
        //Destroy the object
    }

    public virtual void Timer(float timeLeft)
    {
        if (TimeLeft <= 0) 
        {
            Kill();
        }
    }
}

// this should be in a "Devices" folder, or otherwise be called appropriately
public class Backpack : TimerExplodableDevice
{
    void Start()
    {
        TimeLeft = 100;
    }
}

// this should be in a "Devices" folder, or otherwise be called appropriately
public class Bomb : TimerExplodableDevice
{
    void Start()
    {
        TimeLeft = 10;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this helped! :) Also, why do I need to extend from MonoB in this case?
@rootpanthera You don't need to, it's only if you want to that you should do it. It depends on the requirements
1

You can create an abstract ExplodableDevice class and have Bomb and Backpack inherit from it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.