Android Open Source - DivisionByZero Spawn Manager






From Project

Back to project page DivisionByZero.

License

The source code is released under:

Apache License

If you think the Android project DivisionByZero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ggstudios.divisionbyzero;
/*  www  . j a va2s . c o m*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.LinkedList;

import com.ggstudios.divisionbyzero.LevelManager.SpawnEvent;
import com.ggstudios.utils.DebugLog;

/**
 * Handles the spawning of units.
 * @author iDunnololz
 *
 */
public class SpawnManager implements Updatable{
  private static final String TAG = "SpawnManager";
  
  private static class SpawnTimer {
    float spawnRate;
    int unitsLeft;
    float waitTime;
    
    SpawnEvent se;
  }
  
  // defines the max number of spawn events that can be run concurrently...
  private static final int MAX_CONCURRENT = 30;
  
  private LinkedList<SpawnEvent> spawnQueue = new LinkedList<SpawnEvent>();
  private SpawnTimer[] timers = new SpawnTimer[MAX_CONCURRENT];
  private int eventsRunning = 0;
  
  public SpawnManager() {
    for(int i = 0; i < timers.length; i++)
      timers[i] = new SpawnTimer();
  }
  
  public boolean canProcessAnotherEvent() {
    return eventsRunning == MAX_CONCURRENT;
  }
  
  public int eventsRunning() {
    return eventsRunning;
  }
  
  /**
   * Attempts to process the event concurrently. If the max concurrent
   * limit has been reached, then the event will simply be queued.
   * @param spawnEvent
   */
  public void queueSpawn(SpawnEvent spawnEvent) {
    if(eventsRunning == MAX_CONCURRENT) {
      DebugLog.d(TAG, "No more concurrent slots available! Event has been queued instead!");
      spawnQueue.add(spawnEvent);
    } else {
      SpawnTimer t = timers[eventsRunning];
      t.spawnRate = spawnEvent.spawnRate;
      t.unitsLeft = spawnEvent.numUnits;
      t.waitTime = 0f;
      t.se = spawnEvent;
      eventsRunning++;
    }
  }
  
  public void save(DataOutputStream stream) throws IOException {
    stream.writeInt(eventsRunning);
    for(int i = 0; i < eventsRunning; i++) {
      SpawnTimer timer = timers[i];
      stream.writeInt(timer.se.index);
      stream.writeInt(timer.unitsLeft);
      stream.writeFloat(timer.spawnRate);
      stream.writeFloat(timer.waitTime);
    }
  }
  
  public void load(DataInputStream stream) throws IOException {
    eventsRunning = stream.readInt();
    for(int i = 0; i < eventsRunning; i++) {
      SpawnTimer timer = timers[i];
      timer.se = ((SpawnEvent) Core.lm.getEventAt(stream.readInt()).args);
      timer.unitsLeft = stream.readInt();
      timer.spawnRate = stream.readFloat();
      timer.waitTime = stream.readFloat();
    }
  }
  
  private void switchToNext() {
    SpawnEvent e = spawnQueue.removeFirst();
    SpawnTimer t = timers[eventsRunning];
    t.spawnRate = e.spawnRate;
    t.unitsLeft = e.numUnits;
    t.se = e;
    t.waitTime = 0f;
    
    eventsRunning++;
  }
  
  @Override
  public boolean update(float dt) {
    if(eventsRunning != 0) {
      for(int i = 0; i < eventsRunning; i++) {
        SpawnTimer timer = timers[i];
        
        timer.waitTime -= dt;
        timer.se.group.groupLeader.elapsed += dt;
        
        if(timer.unitsLeft == 0) {
          // do a swap with the last elem.
          timers[i] = timers[eventsRunning - 1];
          timers[eventsRunning - 1] = timer;
          eventsRunning--;
          i--;
          
          if(spawnQueue.size() != 0) {
            // a spawn slot freed up... if there 
            // are more spawn events, run them!
            switchToNext();
          }
          
          continue;
        } else if(timer.waitTime <= 0) {
          // remember the Sprite here is a partially loaded instance...
          Sprite s = Core.game.spriteMgr.obtain();
          SpawnEvent se = timer.se;
          
          s.setPathIndex(se.pathIndex);
          s.preload(se.hp, se.gold, se.enemyType);
          
          // Game#addEnemy() will load the sprite for us...
          Core.game.addEnemy(s);
          
          timer.unitsLeft--;
          
          timer.waitTime += timer.spawnRate;
        }
      }
      
      if(eventsRunning == 0 && spawnQueue.size() == 0) {
        Core.lm.setCurrentEventDone();
      }
    }
    return true;
  }

  public void reset() {
    eventsRunning = 0;
    spawnQueue.clear();
  }

}




Java Source Code List

com.ggstudios.divisionbyzero.ActivityAbout.java
com.ggstudios.divisionbyzero.ActivityLevelSelection.java
com.ggstudios.divisionbyzero.ActivityMainMenu.java
com.ggstudios.divisionbyzero.ActivitySettings.java
com.ggstudios.divisionbyzero.ActivitySplashScreen.java
com.ggstudios.divisionbyzero.ApplicationMain.java
com.ggstudios.divisionbyzero.BaseActivity.java
com.ggstudios.divisionbyzero.BaseDialog.java
com.ggstudios.divisionbyzero.BulletManager.java
com.ggstudios.divisionbyzero.Bullet.java
com.ggstudios.divisionbyzero.Button.java
com.ggstudios.divisionbyzero.Circle.java
com.ggstudios.divisionbyzero.ClickableCollection.java
com.ggstudios.divisionbyzero.Clickable.java
com.ggstudios.divisionbyzero.ConfirmDialog.java
com.ggstudios.divisionbyzero.Core.java
com.ggstudios.divisionbyzero.CustomGLSurfaceView.java
com.ggstudios.divisionbyzero.DialogManager.java
com.ggstudios.divisionbyzero.DrawableCollection.java
com.ggstudios.divisionbyzero.DrawableString.java
com.ggstudios.divisionbyzero.Drawable.java
com.ggstudios.divisionbyzero.EndDialog.java
com.ggstudios.divisionbyzero.Event.java
com.ggstudios.divisionbyzero.ExplosionGenerator.java
com.ggstudios.divisionbyzero.ExtrasManager.java
com.ggstudios.divisionbyzero.FontManager.java
com.ggstudios.divisionbyzero.GameRenderer.java
com.ggstudios.divisionbyzero.GameUpdater.java
com.ggstudios.divisionbyzero.Game.java
com.ggstudios.divisionbyzero.Grid.java
com.ggstudios.divisionbyzero.Hud.java
com.ggstudios.divisionbyzero.InGameMenu.java
com.ggstudios.divisionbyzero.InfoDialog.java
com.ggstudios.divisionbyzero.Label.java
com.ggstudios.divisionbyzero.LevelManager.java
com.ggstudios.divisionbyzero.LevelMap.java
com.ggstudios.divisionbyzero.LineGuide.java
com.ggstudios.divisionbyzero.MainActivity.java
com.ggstudios.divisionbyzero.Map.java
com.ggstudios.divisionbyzero.MessageDialog.java
com.ggstudios.divisionbyzero.ParticleEngine.java
com.ggstudios.divisionbyzero.PathFinder.java
com.ggstudios.divisionbyzero.PauseMenu.java
com.ggstudios.divisionbyzero.PictureBox.java
com.ggstudios.divisionbyzero.Player.java
com.ggstudios.divisionbyzero.PopupMenu.java
com.ggstudios.divisionbyzero.Rectangle.java
com.ggstudios.divisionbyzero.Shader.java
com.ggstudios.divisionbyzero.SpawnManager.java
com.ggstudios.divisionbyzero.SpriteAnimation.java
com.ggstudios.divisionbyzero.SpriteManager.java
com.ggstudios.divisionbyzero.Sprite.java
com.ggstudios.divisionbyzero.Statbar.java
com.ggstudios.divisionbyzero.StateManager.java
com.ggstudios.divisionbyzero.TargetRectangle.java
com.ggstudios.divisionbyzero.TextureManager.java
com.ggstudios.divisionbyzero.TowerInfoDialog.java
com.ggstudios.divisionbyzero.TowerLibrary.java
com.ggstudios.divisionbyzero.TowerManager.java
com.ggstudios.divisionbyzero.TowerMenu.java
com.ggstudios.divisionbyzero.Tower.java
com.ggstudios.divisionbyzero.UpdatableCollection.java
com.ggstudios.divisionbyzero.Updatable.java
com.ggstudios.divisionbyzero.UpgradeDialog.java
com.ggstudios.divisionbyzero.Utils.java
com.ggstudios.divisionbyzero.VBO.java
com.ggstudios.divisionbyzero.WaveControlDialog.java
com.ggstudios.divisionbyzero.WaveControlHud.java
com.ggstudios.divisionbyzero.ZoomControl.java
com.ggstudios.utils.BitmapUtils.java
com.ggstudios.utils.BufferUtils.java
com.ggstudios.utils.DebugLog.java
com.ggstudios.utils.ShaderUtils.java
com.ggstudios.widget.LevelMapBackground.java
fix.android.opengl.GLES20.java