C# class design question - Noob

Hi All,

I have started a small new project to learn some c#. So far so good, but what I have noticed is that class design is really important and inheritence is a nice feature. However, I have having some trouble how to do it all correctly. I can get everything to work, but I am afraid I will get stuck later on.

What I want is to create a small game in which 2 players fight it out in a 2D top down space shooter (like StarControl). Each player has a ship that they control. Each ship can be of a different style which influences the type of ammo they fire, the abilities they have and their speed, weigth, size, turnrate etc.

I was thinking that I should make a seperate controller class for movement, a base class of ships from which all types of ships inherit and a seperate class for firing projectiles, but it all seems very complicated when I do it like this.

Can anyone give me some ideas on what to do here?

Personally I try stay away from heavy inheritance on my projects, unless over time it becomes obviously needed.

For what you are suggesting, here are some classes that come to mind that you might want to write:

PlayerMovementController:

  • takes arguments for weight, rate of turn, engine power, max speed, etc.
  • tracks and manages speed and rotation

PlayerShieldController:

  • accepts damage from collision events with bullets and/or other players/planets
  • mitigates damage according to type and shield strength
  • passes unmitigated damage to the PlayerDamageController
  • maybe ramming damage goes right through, but shields can mitigate bullets?

PlayerDamageController:

  • tracks damage taken, details about it, etc.
  • receives damage that passes through the shield

PlayerWeaponController:

  • what kinds of weapon does this player fire?
  • is his weapon operative/damaged (see damage controller)

BulletMovementController:

  • takes params as above
  • also keeps track of its nature: photon beam, bullet, etc.
  • tracks and manages speed and rotation

BulletDamageController:

  • damage dealt when contacting a player
  • keep track of your owner - can you damage the guy who fired you?
  • you may need to turn self-damage off for a small time to allow the bullet to “get away” from the person who fired it.

All of the above are simple “components” that plug in with an .AddComponent<>() call to the objects, or by adding them to the prefab. A player ship would have the three player scripts attached to it, a bullet object would have the two bullet objects attached, as well as colliders, rigidbodies, etc. for both.

Keep in mind, the above are just some basic ideas for class design. Start out with something like that, keep it real simple. If your ship/weapon variations are similar enough, the parameter modifications may be all you need. If you need to make something fundamentally and completely different, then refactor it into something that shares a base class etc, but that shouldn’t be necessary immediately.

Best,
Kurt