When you have an array of type Entity then you can only call methods on the entries which are declared in the class Entity.
If you want Human and Zomby to have different implementations for that method, then you can declare that function as virtual in the Entity class. You can then override that implementation in both sub-classes. That means when you call the method .ai on an Entity the program checks what kind of entity it is and calls the corresponding method of the corresponding class.
Another thing you might want to consider is to pass the argument by reference instead of by value. When you pass objects by value, then you are creating a copy of the object in memory, pass that copy to the function and then discard that copy when the function finishes. But when you pass objects by reference, you are passing that object. This has two advantages:
- it's faster because the program doesn't need to create a copy of the object.
- You can do changes to the object in the function which are applied to the original object.
Example:
class Entity // base class
{
public:
float x, y;
virtual void ai(Entity& other) { }
};
class Human : public Entity // sub-class
{
public:
virtual void ai(Entity& other) override {
// human-specific AI code
}
};
class Zomby : public Entity // sub-class
{
public:
virtual void ai(Entity& other) override {
// zomby-specific AI code
}
};