Advertisement
Uhfgood

Command Queue

Jan 12th, 2016
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.88 KB | None | 0 0
  1. package;
  2.  
  3. /**
  4.  * ...
  5.  * @author Keith Weatherby II
  6.  */
  7.  
  8. enum CommandType
  9. {
  10.     UpdateEnemy( eid :Int, name : String, x : Int, y : Int );
  11.     UpdateScore( amount : Int );
  12. }
  13.  
  14. typedef TCommand =
  15. {
  16.     var timeStamp : Float;
  17.     var type : CommandType;
  18. }
  19.  
  20. class CommandQueue
  21. {
  22.     // each id needs it's own command list for everything to go well
  23.     public var commandList : Array<TCommand> = [];
  24.     public var commandsByTimestamp : Array<Array<TCommand>> = [];
  25.  
  26.     public function new()
  27.     {
  28.        
  29.     }
  30.    
  31.     public function Update() : Void
  32.     {
  33.         // we want to strip all the commands by timestamp and group them into the array of arrays
  34.         while ( this.GetNumCommands() > 0 )
  35.         {
  36.             var commands = this.GetListOfCommandsByFirstTimestamp();
  37.             this.commandsByTimestamp.push( commands );
  38.         }
  39.         //trace( commandsByTimestamp );
  40.     }
  41.    
  42.     public function GetNumCommands() : Int
  43.     {
  44.         return commandList.length;
  45.     }
  46.    
  47.     public function PullCommandSet() : Array<TCommand>
  48.     {
  49.         var returnSet : Array<TCommand> = [];
  50.        
  51.         // make sure we have some commands to process.
  52.         if ( this.commandsByTimestamp.length > 0 )
  53.         {
  54.  
  55.             // go ahead and pull off the first set of commands
  56.             // most of the time it will just be one command at a time
  57.             // but when you kill multiple enemies or drop them from above
  58.             // they run at the same time.      
  59.             returnSet = this.commandsByTimestamp.shift();
  60.         }
  61.  
  62.         return returnSet;
  63.     }
  64.    
  65.     public function AddCommand( stamp : Float, cType : CommandType )
  66.     {
  67.         commandList.push( { timeStamp : stamp, type : cType } );
  68.     }
  69.    
  70.     // a little verbose I realize, however, timestamps are simply numbers
  71.     // and you would pass in some integer indicating when a command was given
  72.     // for instance a loop number since the start of the level.  Now we don't
  73.     // know what timestamps are stored, so what we do is we get the time stamp
  74.     // of the first command in the list, then we retrieve any others we may find
  75.     // and then remove the commands afterward.  So let's say we kill a match of
  76.     // 3 in a row, then presumably the same time stamp is passed in at the time
  77.     // of the kills, and so then I can grab them here, and then we can process
  78.     // the commands with the same timestamp at the same time, in this case
  79.     // killing those that have the same stamp
  80.     public function GetListOfCommandsByFirstTimestamp() : Array<TCommand>
  81.     {
  82.         var commandsByFirstTimestamp : Array<TCommand> = [];
  83.         var i : Int = 0;
  84.        
  85.         if ( this.commandList.length > 0 )
  86.         {
  87.             var firstTimestamp = this.commandList[ 0 ].timeStamp;
  88.             while( i < this.commandList.length )
  89.             {
  90.                 var command = commandList[ i ];
  91.                 if ( command.timeStamp == firstTimestamp )
  92.                 {
  93.                     commandsByFirstTimestamp.push( command );
  94.                     this.commandList.remove( command );
  95.                    
  96.                     i--;  // loop has been reduced by one so subtract one from the counter
  97.                 }
  98.                
  99.                 i++;
  100.             }
  101.         }
  102.        
  103.         return commandsByFirstTimestamp;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement