CEntityManager.java :  » Game » gcgame » gcgame » game » Android Open Source

Android Open Source » Game » gcgame 
gcgame » gcgame » game » CEntityManager.java
package gcgame.game;

import android.os.SystemClock;
import android.util.Log;
import gcgame.game.CBaseEntity;
import gcgame.game.CEntNode;


public class CEntityManager
{
  //constants
  final long tickrate = 1; //time in ms between cycles
  
  //variables
  public int entCount;  //number of entities the manager is using
  
  long nextcycle;       //time in ms till next cycle
  
  //linked list variables
  
  CEntNode head;   //head of the linked list
  CEntNode cur;    // LL node for traversal operations
  CEntNode prev;   //for deletion
  
  
  //constructor
  CEntityManager()
  {
    entCount = 0;
    nextcycle = 0;
    head = null;
    cur = null;
    prev = null;
  }
  
  /********************** methods *********************************/
  
  /*addEnt - will add an entity to the list
   * 
   */
  public void addEnt(CBaseEntity ent)
  {
    if (head == null)
    {
      head = new CEntNode(ent);
    }
    else
    {
      cur = head;
      while (cur.next != null)
      {
        cur = cur.next;
      }
      cur.next = new CEntNode(ent);
    }
    entCount++;
  }
  
  public void cycle()
  {
    cur = head;
    while (cur != null)
    {
      if (cur.entity.isAlive)
      {  
        cur.checkThink();
        prev = cur;
        cur = cur.next;
      }
      else
      {
        if (cur == head)
        {
          head = cur.next;
          cur = cur.next;
        }
        else
        {
          if (prev != null)
          {
            prev.next = cur.next;
            cur = cur.next;
          }
        }
      }
      
    }
    nextcycle = SystemClock.elapsedRealtime() + tickrate;
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.