tags:

views:

160

answers:

6

I have an "Enemy" object, that have many "gun" . Each "gun" can fire "bullet". Storing "gun" is using an array. when the "gun" is fired, the "bullet" will be created. And the enemy object will have an array to store the "bullet".

So, I am thinking about the fire method. I am think making a firebulletFromGun in the "enemy". It need have a parameter: "gun". while this method is called. The "enemy" 's bullet will be added in the Array.

Another design is writing the fire method in the "gun". The "enemy" use the "gun"'s fire method. And the "gun" will return a "bullet" object, and it will be added in the Array of "enemy".

Both method can work, but which way is better? or they are similar the same? plx drop ur ideas/suggestions. thz.

+3  A: 

Put the bullet in the gun. Fire the gun.

gun.fire(); // bang!

Finbarr
Bet you've never seen an answer like that on SO before!
Finbarr
+3  A: 

I know the song goes "Guns don't kill people, people kill people". But it is the Gun which fires the Bullet not the Enemy. The Enemy probably needs an attack() method which calls the appropriate method on their current Weapon - fire() for Gun, stab() for Dagger.

APC
+1. This is generally how I think about OO design when modeling real-world objects. Well said.
Matt Ball
+1  A: 

It depends on whether you want the bullet to remain "owned" by the enemy or the gun.

An alternative design would be for bullets to be separate entities (so there would be an independent list of "active bullets" in the system that any gun can add to when its owning enemy wishes to fire it). This would be more consistent with "real life" in that once you release a bullet from the gun, it is an independent entity that has no link (other than a historical one) with the gun that fired it. I wouldn't expect to have to enumerate the enemies in the system in order to find all the bullets that are currently flying. Especially if the player can also fire bullets.

Ultimately, use whichever of these designs works best for your system - if it is important to remember which enemy fired a bullet, and not important to know which gun fired it, then it may make life much easier to store the bullet in the enemy. Or if the gun that fires the bullet can subsequently affect its flight path (this happens with rockets in 1st person games, if not bullets per se) then perhaps it is important to retain that linkage by the gun "owning" the bullet.

The important thing is that you separate and encapsulate the data in different objects in an appropriate way (i.e. keep enemy, gun and bullet as separate objects that each handles its own behaviour).

In addition, you may keep unfired bullets in the gun, and fired bullets in the 'world'.

Jason Williams
A: 
public class Gun {
  private List<Bullet> ammunition;

  public void fire() {
    // Kablam!
  }

  public boolean hasAmmo() {
    return getAmmo().size() > 0;
  }

  private synchronized List<Bullet> getAmmo() {
    if( this.ammunition == null ) {
      this.ammunition = createAmmo();
    }

    return this.ammunition;
  }

  protected List<Bullet> createAmmo() {
    return new ArrayList<Bullet>();
  }

  public void load( Bullet bullet ) {
    getAmmo().add( bullet );
  }
}

public class MachineGun extends Gun {
  public void fire() {
    while( hasAmmo() && isFiring() ) {
      super.fire();
    }
  }

  public void load( List<Bullet> clip ) {
    for( Bullet bullet : clip ) {
      load( bullet );
    }
  }
}
Dave Jarvis
Each piece of AMMUNITION = BULLET + SHELL. After firing, BULLET travels fast towards target, SHELL falls to the floor in slow motion. Have you watched NO action movies, sir?
TomA
Sorry! I'm from Canada. We have no ammunition, guns, or violent action movies. We are a calm, kind, peace-loving nation, bent on saving the Earth through the purchase of carbon-offsets.
Dave Jarvis
@TomA: ...except when the gun fires the entire cartridge. Check out the intro for Chuck for example. :) http://www.nbc.com/chuck/
Guffa
A: 

Come on guys 'n' gals, you've got to take this question seriously. Unless OP is given serious help the program is never going to keep up with rates of fire from the Metal Storm prototype with a reported burst rate of fire of 1.62 million rounds per minute.

High Performance Mark
+1  A: 

You are making the common mistake of modeling the physical objects to be your classes instead of making classes out of what's really interresting.

A bullet is not really useful as a class at all.

A gun can have bullets, but there is no reason to store any specific data for each bullet, they are all the same. The gun can just have an integer property that keeps track of how many bullets there are.

A bullet can be fired, but it's still not the bullet in itself that is interresting, but the movement of the bullet.

So, in my implementation the Enemy object has a list of Gun objects. Calling the Fire method of a gun changes the gun's BulletCount, and creates a new instance of the BulletMovement class that is returned.

Guffa