# GameLogic Model as Property or Constants with lookup in other Models?

Basically imagine a game where the Player has a Weapon which has a BulletType/type of Bullet.

Now Player, Weapon and Bullet are Models having properties and they would represent Instances in the game.

I want to Instantiate a Bullet when the Player fires and I have to check what BulletType the Weapon currently has, but I don't understand what kind of Property Weapon should have to represent the current type of Bullet.

Should in the Weapon class the BulletType be an Instance of Bullet or should I use some kind of Constant as BulletType that would map to the Bullet Model?

class Weapon{
IBullet currentBulletType //<-- a Bullet Model with Position and everything
}


or

class Weapon{
BulletTypeConstant currentBulletType // a Constant that is mapped to a specific Model
}


The first approach is using a whole Model to represent the BulletType currently equipped. The Second uses some kind of Constant that must then be mapped to a specific Model of Bullet on firing.

I always find myself troubled as to which approach would be better. Or perhaps you have an even better Idea. Thanks

• I solved this issue by simply creating class Bullet and create static Bullet for each type. And then call Bullet.FromType(type, pos) that would create deep copy of my static bullet at defined position. – wondra Jul 30 '14 at 21:33
• Are you using C#? You might want to add that as a tag. – RandyGaul Jul 30 '14 at 22:09
• You should have a WeaponType similar to your BulletType, which holds the right BulletType, as I imagine you would have multiple weapons that fire the same bullets, but each would have its own state such as firing timers, number of bullets left in the clip and so on. – congusbongus Jul 31 '14 at 6:03

I think that any good OOP source you check will advise you to program against interfaces, which is one of the pillars of OOP, so your first option would be better.

However the problem here is conceptual. If you think about it, your weapons doesn't have a bullet, as your model suggest, but it produces bullets. In that sense, what your really need is to use a creational design pattern.

For my game, I use the Prototype pattern, which would make the code look similar to your first option (include an Interface variable, and make clones of it). However, depending on your needs, I think Factory Method can work as well.

• The link to the Design Patterns is really Helpful thank you. I think some kind of factory referenced in the Weapon will be the best solution for me – user1090755 Aug 6 '14 at 6:14

Whether or not a "Weapon", a "Character", or the "World" (or some other more global concept) should contain a collection of bullets fired depends largely on how you want the bullets (or, generally, any object/concept) to be considered in your game.