Re: OO Game Architecture
Posted: Mon Jul 09, 2012 9:35 am
Your problem is not a "tools" or "libraries" problem. It's a problem of understanding. You seem a bit confused about what some key concepts work in Object Orientation - or at least that is what the line above makes me think.kexisse wrote:Basically my "Pushable" mixin should make some entity subclasses pushable, but not others.
It is mostly regarding inheritance.
Inheritance is a double-edged sword; it cuts both ways. 80% of the time it is exactly what you need. But other times it brings more troubles than it solves. You are on that situation.
Think about it for a moment. Inheritance allows you to write a "tree of classes" for a "taxomony" - a main characteristic. Usually the main characteristic can be deduce by looking at the root of the tree. For example, you have Vehicle, with Car and Airplane as its children. The main thing on this hierarchy is that you are organizing vehicles. Etc.
In your case, however, you don't have that. Your root node is "abstract". This is not bad per se, but it makes deciding what the main characteristic of your hierarchy is very difficult.
Plus, you have have several "ortogonal taxonomies" going around - That is what Pushable, CarryAble, etc are.
Inheritance is what you want most of the time, because very often you only are interested in "One main characteristic" to organize your classes around. You are trying to use it to organize several orthogonal taxonomies, and that is what your pain is coming from.
Once you know inheritance is not the right tool for the job, you must look into other tools. Middleclass' mixins are one possibility. If I where you, I would go easy on inheritance, leaving it for the "main taxonomy" (Entities), and use mixins for all the "secondary traits". Here are some examples:
Code: Select all
Entity = class("Entity") -- entity is the root. It has methods shared by all entities
-- blocks are Entities which are pushable
Block = class("Block", Entity):include(Pushable)
-- Trees are Entities which are pushable and also animated
Tree = class("Tree", Entity):include(Pushable, Animated)
-- EvilTrees are just like trees; they have a different sprite. They are Pushable and Animated, because Trees are, too
EvilTree = class("EvilTree", Tree)