ParticleManager.clone()

Hi all,

I needed a quick way to add many explosions to my system. So ive added a few methods in ParticleSystem to clone it quickly:



// how many times the manager has warmed up
private int iterations;




/**
   *
   * Return the number this manager has warmed
   * up
   * @return int
   */
  public int getIterations() {
     return iterations;
  }




/**
   * Runs the update method of this particle manager for iteration seconds
   * with an update every .1 seconds (IE <code>iterations</code> * 10
   * update(.1f) calls).  This is used to "warm up" and get the particle
   * manager going.
   *
   * @param iterations The number of iterations to warm up.
   */
  public void warmUp(int iterations) {
     this.iterations = iterations;
    for (int i = iterations; --i>= 0; )
      update(.1f);
  }




  /**
   * Sets the iterations for the warmup and calls
   * warmUp with the number of iterations as the argument
   * @param iterations
   */
  public void setIterations(int iterations) {
     warmUp(iterations);
  }



And for the most important part:


   /**
    * Clones every aspect of this manager into a new manager
    */
   public Object clone() {
      ParticleManager manager = new ParticleManager(getParticlesNumber(),
            getCamera());
      manager.setControlFlow(getControlFlow());
      manager.setEmissionDirection((Vector3f) getEmissionDirection().clone());
      manager.setEmissionMaximumAngle(getEmissionMaximumAngle());
      manager.setEndColor((ColorRGBA) getEndColor().clone());
      manager.setEndSize(getEndSize());
      manager.setGeometry(getGeoMesh());
      manager.setGeometry(getGeometry());
      manager.setGravityForce((Vector3f) getGravityForce().clone());
      manager.setInitialVelocity(getInitialVelocity());
      manager.setParticlesMinimumLifeTime(getParticlesMinimumLifeTime());
      manager.setParticlesOrigin((Vector3f) getParticlesOrigin().clone());
      manager.setParticleSpinSpeed(getParticleSpinSpeed());
      manager.setPrecision(getPrecision());
      manager.setRandomMod(getRandomMod());
      manager.setReleaseRate(getReleaseRate());
      manager.setReleaseVariance(getReleaseVariance());
      manager.setSpeed(getSpeed());
      manager.setStartColor((ColorRGBA) getStartColor().clone());
      manager.setStartSize(getStartSize());
      manager.setIterations(getIterations());
      manager.setRepeatType(this.getRepeatType());

      manager.getParticles().addController(manager);

      for (int i = 0; i < getParticles().getRenderStateList().length; i++) {
         if (getParticles().getRenderStateList()[i] != null) {
            manager.getParticles().setRenderState(getParticles()
                  .getRenderStateList()[i]);
         }
      }

      manager.getParticles().setModelBound(getParticles().getModelBound());
      manager.getParticles().updateModelBound();

      return manager;
   }



What do you think?

Edit: The clone method definetly works now. And the brilliant thing is that you dont have to reload the textures and recreate the alpha states and zBufferStates :D

DP

You will need a setIterations(int iterations).

that method (setIterations) comes in the form of warmUp(int iterations)



I suppose we could have a set iterations method and make warmUp a private method. So you set the iterations, and that will warm up the manager.



DP

warmup should be public, it may be needed for the same reasons you have it in clone.

ive added and taken away a few lines in the clone() method to apply the render states of the current to the clone. Ive also added a setIterations(…) which automatically calls warmUp(…);



DP

It looks pretty good to me although I would say that setting Iterations should only do that and not call warmup automatically. Warmup is already public and can be called in clone and also by the user as needed.



But other than that, looks like a simple add. This and the other add are fine by me but will of course need a sign off by Mojo.