Reputation: 447
First, i would want to say that my question is more about designing a good object-oriented system than game deveploment. I am making a spaceshooter game, with objects like bullets,enemies, big rocks and the spaceship(the player). I have a collision manager(static class) that check for collision every frame. In order to pass the "effect" from the bullet to the spaceship or an enemy i did this:
Interface IHit
{
Action<BaseGameSprite> GetEffect();
}
*for example in the bullet class i did:
return new Action<ICollisionInterest>(gameobject =>
{
gameobject.LifePoints -= Damage;
});
and in the collision manager, if he finds a collision:
TheHitObject.GetEffect()(HittenObject);
The solution works but i am not sure if it is a good design because the GetEffect() totally breaks encapsulation, and it seems the bullet can do whatever he wants to the HittenObject(spaceship, or enemy).So my question is: Is this a good design? or i should use something different?
Upvotes: 1
Views: 550
Reputation: 56
IMHO, as far as OO design is concerned, I think the design is fine. If it is your game logic that decides that your bullet object can do whatever it wants to the objects it hits, then why not? I didn't see any close coupling between different classes in the sample code you list, and the GetEffect()
method didn't make assumptions about any concrete classes except requiring that the gameobject
has LifePoints
property, you may have to add an if
statement to check if the gamobject
does have this property though.
Upvotes: 2
Reputation: 56
Did you check out the entity system? http://entity-systems.wikidot.com/
It promotes a better architecture than the traditional object oriented design for game development. In an entity system, every game object is represented as an entity which is just an id. Behaviors such as rendering, collision detection, calculating health are not encapsulated in the entity, instead they are represented as components which can be added to an entity. The actual rendering, physic, health point calculation, etc are handled by independent sub systems. Each sub system process all the same kinds of components disregard which entity they belong to. In this way, rendering, logic, physics, ai are clearly separated.
For a start on learning the entity system, check out the classic 3 part article written by T=Machine:
http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/
(you can google the part2 and part3)
Upvotes: 1