简体   繁体   中英

How to approach an Entity class with inheritance?

SO, my concern is not being completely aware if this is the best approach for my situation.

Lets, say I got a base class "Entity". Assuming the purpose is to construct a class hierarchy system.

public class Entity
{
    public int _hp;
    private int _max_hp;

    public Entity(int hp)
    {
        _max_hp        = hp;
        _hp            = _max_hp;
    }
}

And we got other derived classes

public class d_class1: Entity
{
    public d_class1(int hp):
        base(hp)
    {
    }
}

public class d_class1_Mono: MonoBehaviour 
{
    public d_class1 d1;
}




public class d_class2: Entity
{
    public d_class2(int hp):
        base(hp)
    {
    }
}

public class d_class2_Mono: MonoBehaviour 
{
    public d_class2 d2;
}

(Excuse the bad naming conventions, was done on purpose)

Assume the scripts that are inheriting from Mono are attached to different objects in the scene. I want to be able to distinguish which "class" I currently have when "click" on the unit.

Is there anyway I can avoid having to check between all possible classes I may have.

ex.

obj = gameObject containg the component d_class2_Mono

if(obj contains d_class1)
    do something with d1

else if(obj contains d_class2)
    do something with d2

else
    error("Unknown class derivation");

I feel myself having to do this sometimes to do something like get the hp, just because I need someway of accessing the component. Maybe this is not the best approach. Currently, this is all simple, but each of these class will be in their own way different from the other (ie different abilities, etc)

So, hoping someone could shed some knowledge in my understanding.

The GameObject that you attach the MonoBehaviour you can have a handler specifically for that object. For example you could implement "void OnUpdate()" on the d_class1_Mono and a different update function for d_class2_Mono.

If you assume that you don't know which object you have clicked on (because this is being routed through some other class/function) - You can do:

obj.GetComponent<d_class1_Mono>() and obj.GetComponent<d_class2_Mono>()

Whichever one returns a non-null reference has that script attached to it. Hope this makes sense.

I would probably define a virtual or abstract method in the Entity class which each inherited classes would implement. For example, if you have both mobile and immobile entities, and you have a void MoveToPosition(Position pos) method defined in Entity :

// Mobile entity
public class d_class1: Entity
{
    // ...stuff...
    public void MoveToPosition(Position pos)
    {
        TweenPosition(_currPosition, pos, movementSpeed);
    }
}

// Immobile entity
public class d_class2: Entity
{
    // ...stuff...
    public void MoveToPosition(Position pos)
    {
        // Do nothing
    }
}

Then within your logic/update loop, you can simply tell ALL entities to MoveToPosition(...) and those that shouldn't respond will simply do nothing.

I find that coding is cleaner when I've got a common interface or class with a common "do something" method, and let each derived class handle the "how" of that method call in its own ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM