Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- /**
- * ...
- * @author Keith Weatherby II
- */
- enum CommandType
- {
- UpdateEnemy( eid :Int, name : String, x : Int, y : Int );
- UpdateScore( amount : Int );
- }
- typedef TCommand =
- {
- var timeStamp : Float;
- var type : CommandType;
- }
- class CommandQueue
- {
- // each id needs it's own command list for everything to go well
- public var commandList : Array<TCommand> = [];
- public var commandsByTimestamp : Array<Array<TCommand>> = [];
- public function new()
- {
- }
- public function Update() : Void
- {
- // we want to strip all the commands by timestamp and group them into the array of arrays
- while ( this.GetNumCommands() > 0 )
- {
- var commands = this.GetListOfCommandsByFirstTimestamp();
- this.commandsByTimestamp.push( commands );
- }
- //trace( commandsByTimestamp );
- }
- public function GetNumCommands() : Int
- {
- return commandList.length;
- }
- public function PullCommandSet() : Array<TCommand>
- {
- var returnSet : Array<TCommand> = [];
- // make sure we have some commands to process.
- if ( this.commandsByTimestamp.length > 0 )
- {
- // go ahead and pull off the first set of commands
- // most of the time it will just be one command at a time
- // but when you kill multiple enemies or drop them from above
- // they run at the same time.
- returnSet = this.commandsByTimestamp.shift();
- }
- return returnSet;
- }
- public function AddCommand( stamp : Float, cType : CommandType )
- {
- commandList.push( { timeStamp : stamp, type : cType } );
- }
- // a little verbose I realize, however, timestamps are simply numbers
- // and you would pass in some integer indicating when a command was given
- // for instance a loop number since the start of the level. Now we don't
- // know what timestamps are stored, so what we do is we get the time stamp
- // of the first command in the list, then we retrieve any others we may find
- // and then remove the commands afterward. So let's say we kill a match of
- // 3 in a row, then presumably the same time stamp is passed in at the time
- // of the kills, and so then I can grab them here, and then we can process
- // the commands with the same timestamp at the same time, in this case
- // killing those that have the same stamp
- public function GetListOfCommandsByFirstTimestamp() : Array<TCommand>
- {
- var commandsByFirstTimestamp : Array<TCommand> = [];
- var i : Int = 0;
- if ( this.commandList.length > 0 )
- {
- var firstTimestamp = this.commandList[ 0 ].timeStamp;
- while( i < this.commandList.length )
- {
- var command = commandList[ i ];
- if ( command.timeStamp == firstTimestamp )
- {
- commandsByFirstTimestamp.push( command );
- this.commandList.remove( command );
- i--; // loop has been reduced by one so subtract one from the counter
- }
- i++;
- }
- }
- return commandsByFirstTimestamp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement