AI and Interfaces + Inheritance

Started by
2 comments, last by TonyMetatawabin 11 years, 11 months ago
Upon creating the AI for the small console game that I am creating, I was a little bummed out by the thought of creating simple AI through only If statements and came across a thought. I know that if a Class inherits an Interface, the class must implement methods from that interface, sort of how abstract classes work (according to my knowledge). Seeing as classes can inherit multiple interfaces, I was wondering if I could use the interfaces to apply certain actions to all monsters from the parent Monster class. For example:

interface IAttack {}
interface IDefend {}

interface IBrain : IAttack, IDefend {} //The parent classes would generally define the characteristics of the monster in battle

class Soul {} //Parent to the Hero class as well.

class Monster : Soul, IBrain {}

class Slime : Monster {}
class Bat : Monster {}


I admit, I haven't done too much research on AI for games, but seeing as how I might be able to make something like this work, I thought I'd ask the question.
Advertisement
You're definitely on the right track, but you need to work on the implementation. Studying and practicing interfaced and inheritance is going to be the key to getting this down.
In case you were wondering about it though, here's kind of how I would put this together.

interface Creature //base class that provides methods for interaction and AI
{
Attack();
Defend();
Run();
}
class Monster : Creature
{
//this would be where you define the actual behavior
}
class Player : Creature
{
//likewise
}



More or less
You are on the right track. Polymorphism can be a great way to implement special cases without having complex if-else logic.
It's good to know I am doing at least something right.

This topic is closed to new replies.

Advertisement