views:

229

answers:

3

I'm building a game in as3 that has balls moving and bouncing off the walls. When the user clicks an explosion appears and any ball that hits that explosion explodes too. Any ball that then hits that explosion explodes and so on.

My question is what would be the best class structure for the balls. I have a level system to control levels and such and I've already come up with working ways to code the balls. Here's what I've done.

My first attempt was to create a class for Movement, Bounce, Explosion and finally Orb. These all extended each other in the order I just named them. I got it working but having Bounce extend Movement and Explosion extend Bounce, it just doesn't seem very object oriented because what if I wanted to add a box class that didn't move, but did explode? I would need a separate class for that explosion.

My second attempt was to create Movement, Bounce and Explosion without extending anything. Instead I passed in a reference to the Orb class to each. Then the class stores that reference and does what it needs to do based on events that are dispatched by the Orb such as update, which was broadcast from Orb every enter frame. This would drive the movement and bounce and also the explosion when the time came. This attempt worked as well but it just doesn't seem right.

I've also thought about using Interfaces but because they are more of an outline for classes, I feel like code reuse goes out the window as each class would need its own code for a specific task even if that task is exactly the same.

I feel as if I'm searching for some form of multiple inheritance for classes that as3 does not support.

Can someone explain to me a better way of doing what I'm attempting to do? Am I being to "Object Oriented" by having classed for Movement, Bounce, Explosion and Orb? Are Interfaces the way to go? Any feedback is appreciated!

A: 

Gidday,

This reminds me of an example I read not so long ago, and it used almost the same class names for exactly your example! If it helps you find answers, I do rememebr the book was a Friends of Ed publishing and was called "Object Oriented Actionscript" and though it was for AS2, the concept is the same in AS3.

That book aside, in my readings about Design Patterns (un/flame at will, people), it sounds like the Factory pattern could be used to generate different objects generated from a standard object, as you'd override particulars as you need to. The first para on Wikipedia explains that simply, and the diagram is helpful too.

-d

Danjah
Agreed. Check Chapter 2: Factory method pattern from O'Reilly http://www.adobe.com/devnet/actionscript/articles/ora_as3_design_patterns.html [Free Downloadable Chapter is in there with a Shooting game example]
phwd
I actually just got through reading that book. :) The Factory pattern seems like it might work. I will give it a go!
Jordan
A: 

I think you're looking for something like what PushButton Engine does: using Entities and Components. The concept is, you have an Entity class (Orb) which can contain the properties of the orb (maybe position and size) and maybe your rendering code. Then you have Component classes (Movement, Bounce, Explosion) that you attach to your Entity class.

alt text

The components should contain only the functionalities that they're supposed to do (i.e. Bounce component should only handle when/where to Bounce and not move the Orb on every frame). They should also have direct access to their owner (Entity) class. For example, they can modify the properties (moving, bouncing) or call explode on it.

You can then make use of the Factory pattern to have a separate Factory class that does the creation of entities and attaching of appropriate components to those entities.

Shiki
thanks for the response! I've set up a Factory class to create entities and another Factory class to create components. You talk about attaching the components to the entities but I'm not sure what the best way to do this is. I have the Orb class as a client of the component factory and it passes a reference to the entity Orb class which the Component Movement uses to move the Orb. Is there a better way to attach it?
Jordan
Yep, that sounds just about right. Entities would have access to the components attached to it (array property). And components would also have access to the entity (via property). You can also opt to have entities manage `onFrame` events and call `component.onFrame` on that event. You'd have a good control on which components should be able to modify the entity first.
Shiki
So your saying each entity should have an array of components that are attached to it so it can talk to them? Right now I have it so the entities do not hold a reference to their components, but instead it dispatches a stop event that any component can decide to listen for. The Bounce and Movement components listen for it but the Explosion component does not because it removes its enterFrame event listener once its through animating. Explain why holding a reference to components in the entity would be a better idea.
Jordan
That could work. Dispatching events instead of calling methods directly is a good idea. I think the main purpose why I like to have references to the attached components is so when I call `entity.delete()`, I can handle all deletion of component objects in there. I'll basically have good control over the components that belong to an entity.
Shiki
+1  A: 

An important rule for any engine or framework is that no matter what kind of inheritance or whatever you actually use, it should be fully optional for an entity to be handled by the engine. So while concrete entities that are handled by the engine may use complex inheritance, to the engine it is fully transparent, because they are abstracted to interfaces.

For example, you define an interface IMovable. The engine can handle this and move around such objects. Then you can provide a default implementation, let's say MovableBase, that can, but need not be reused by other entities through inheritance or composition.

Please also note, that composition is generally considered better and cleaner than inheritance. Overusing inheritance extremely often leads to violation of the Liskov substitution principle which is one of the 5 SOLID principles. You can find a lot of articles about inheritance vs. composition on Google.

Inheritance in common usage actually tends to break many OOP principles and to introduce a lot of dependency. It is often regarded as a fundamental concept in OOP, but you're better off if you conceive it just as one of many ways to achieve code reuse.

So rather than bothering so much with the question, how you will build specific components, first you need to abstract them to be able to apply the dependency inversion principle. This way, you will obtain an architecture with extremely lose coupling. If an entity proves to be unneccessarily complex, or if you find, that you can possibly extract some code for reuse, or that you need to rearrange your inheritance mechanism, no other code is affected.

You may discover, that actually trying to split your entities into multiple layers of inheritance makes only little sense because things become unneccessarily verbous and strangely interwoven. However abstracting different "aspects" or "roles" of your entities, such as IMovable, IBounceable, IExplodable and so on, will proove helpful. Any entity may choose to implement any interface at will and will thus be handled accordingly by the engine.

The most important step in software design is to devise an architecture, wherein different modules are radically split and interact through narrow interfaces. When a module grows big, then apply the same step to it, and refactor it to submodules (you can do so, without having to worry about the rest of the software, simply because the module is well encapsulated). Do not try to split a whole into parts, when it doesn't seem natural. You'll get more code, that is less flexible.

greetz
back2dos

back2dos
+1. You make some very good points. Sadly, the things you mention are not always observed, even in some widely used frameworks (Flex I'm looking at you!)
Juan Pablo Califano
@Juan Pablo Califano: Yeah, Flex is ... a different beast :) To me, Flex is more of a component set with an internal framework, heavily used by the componenents, but not so much actually usable by the developer directly (which is why Flex requires the use of inheritance to interoperate with). I personally think Adobe went wrong in several places. There's even an `IllegalOperationError`, which is used to handle violation of the Liskov substitution principle, which to me is the prequisite to polymorphism. The API does have this issue in various places but Flex is really full of that.
back2dos