Game Design

Started by
5 comments, last by BeerNutts 11 years, 1 month ago

Hello, I just finished my first pong game and decided that my next project will be a space invaders clone. I am having a bit of trouble figuring out how to structure the project though. The way I see it is that I would have a Game class which basically runs the program. Then I have an entity class which is the base for all the game objects, Branching off that I have the player class, enemy and any other entity I might have. Then I would have an Image handler to handle the image loading and blitting. Now This is where I am stuck(assuming that the design I wrote down is not complete crap), would I need a separate class for audio and events? and what about the collision/physics system? on my pong game I had what little physics I needed in the entity classes them self and then a separate class that tested for collision on everything, every frame; however I heard that this is not the best way to do it. Thank you in advance for any help.

Advertisement

Entity systems are very complicated by themselves (no matter how much people state otherwise). There are plenty of discussions supporting this statement.

The main problem with having an Entity class is more or less than having a common base class for everything in a typical programming language. What does an Entity do? Historically, those systems are typically driven by inheritance and are said to be complicated, you might have heard about those "components" supposed to fix that issue.

Entity systems are also proven to work.

Personally I had issues with pure entity systems, mainly regarding code reuse. My issues were compounded by the fact I didn't had a design to follow and so I couldn't focus on what an Entity was supposed to do. Hopefully you won't have that issue. I think your choice is appropriate.

Yes, of course you need a system for audio. Audio is, for first, based on a different API and there's no reason to do a big code mess. At worse, you can always merge the two classes toghether in a derived class.

Events are... different. Again, use another class and eventually merge the various systems in a single class using inheritance.

Physics. What you're looking for is collision and rigid body dynamics. For your game, plenty of people is going to suggest you to "do your own physics" and maybe they are not completely off-track. Personally I think it is never a bad idea to consider a physics library. What they do however is exactly the same thing as you do, they check collisions every frame. Except they are better at this.

Previously "Krohm"

I think many of the resource handling and logic can be done with plain functions, but defining classes for entities is fine. Just keep things simple..

Use classes for things you need multiple instances of, such as entities, 2d vectors, rectangles, etc. Even the game class you described can be a few variables in the function scope, if you don't need more than one instance of the game running in one process.

In this situation, I always recommend to use the Model-View-Controller design pattern. You divide your source code into three sections:

1. The Model manages the physical behavior of objects in the game.

2. The View request state information and events from the Model, and presents it to the player. This also includes sound.

3. The Controller manages all input (keyboard, mouse, timers and other OS events), and tells the Model or View to act accordingly.

[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

I think you're design thus far is fine. Assuming your "Entity" class has all the similar properties of a physical object that can be displayed: Location, Velocity, Size, and Image/Animations.

This way you can use this Entity for all on screen objects: Player, Enemies, Bullets, Shields (in the classic space invaders), etc.

For Audio, I'm not sure you need a specific "system" for it. As long as you have an Audio Manager (similar to imageManager) that can handle loading and playing the Audio, that should be enough. I would think the Audio needed would be for:

#1, firing bullets (could be part of the specific bullet class, played on creation)

#2, taking a hit (would be called from player or enemy class, when collision happens)

#3, dying

#4, finishing a level

#5, Game Over

#6 Winning

For Events, I'm not sure which events you would need outside of Input events without things getting complicated. And, I would handle them inside the player class. In fact, I'd probably structure it like this:

 
TPlayer Player;
std::vector<TEnemy> Enemies;
std::vector<TBullet> Bullets;
 
// Setup Player and Enemies for the level
 
while(IsRunning) {
 
  // Check level progress: see if all enemies are dead or player dead
  CheckLevelProgress();
 
  // Check for input, set velocity based on input, and fire bullets (if refire rate has lapsed since last shot)
  Player->Update();
 
  // Loop through all the enemies and set velocities, and fire bullets if time
  UpdateEnemies();
 
  // Update (do they do anything special besides just move) all the Bullets
  UpdateBullets();
 
  // This performs all the physics, moves the player, bullets, and enemies, and checks for collisions
  // If you had a physics library, it could be done here
  PerformPhysics();
}
 
...
 
// parts of what physics should do
void PerformPhysics()
{
  Player->Move();
 
  // Loop through enemies moving them
  for (i = 0; i < Enemies.size(); i++) {
    Enemies[i].Move();
  }
 
  for (i = 0; i < Bullets.size(); i++) {
    if (Bullets[i].IsEnemy()) {
      if (IsCollision(Player, Bullets[i]) {
        Player->HandleCollision(Bullets[i]);
        // Mark Bullet for deletion
      }
      else {
        // Check Bullet hits any enemies
      }
    }
  }
}

That's a simplistic method, and it should give an idea of what should be done.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thank you for the responses. Making a separate audio manager class with SDL_mixer to play and load music I had thought to be redundant since I could do it all in a constructor and call it when needed (unlike the image manager which would make no sense to re-code the function that loads the images in every class that needs it). Also, when the player dies I was thinking I would call delete (assuming that I dynamically allocated it). Is this the best way? is there a better way?

Thank you for the responses. Making a separate audio manager class with SDL_mixer to play and load music I had thought to be redundant since I could do it all in a constructor and call it when needed (unlike the image manager which would make no sense to re-code the function that loads the images in every class that needs it). Also, when the player dies I was thinking I would call delete (assuming that I dynamically allocated it). Is this the best way? is there a better way?

Dpends how SDL_Mixer handles loading sounds. My assumption is you should only load a sound once, but be able to play it many times, similarly to how you only want to load an image once, but display it many times. That's what I mean by an Audio manager.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement