//--------------------------------------------------------------------------------------------- // File : schedule.cs // Copyright : Blade Edge Software © 2006 - 2007 // Author : Drew Sikora //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- // Name : onAdd // Class : Schedule // // Description : used to create the ScriptGroup that will hold all our appointments //--------------------------------------------------------------------------------------------- function Schedule::onAdd(%this) { %this.appointments = new ScriptGroup(); } // end onAdd //--------------------------------------------------------------------------------------------- // Name : onRemove // Class : Schedule // // Description : in case you forget to call remove() with no arguments to delete all scheduled // : events at the end of the game, this does it for you //--------------------------------------------------------------------------------------------- function Schedule::onRemove(%this) { %this.appointments.remove(); } // end onRemove new ScriptObject(Schedule); //--------------------------------------------------------------------------------------------- // Name : addFunction // Class : Schedule // // Arguments : repeat - whether the scheduled call repeats // : [object] - the object this function belongs to // : time - the time until the appointment is executed // : funcName - the name of the function we will be calling // : [argumentList] - the list of arguments to apply to ths function call // // Description : adds a new appointment to the schedule manager and stores the handle and appointment // : data for later reference. Functions without objects should pass "" for %object //--------------------------------------------------------------------------------------------- function Schedule::addFunction(%this, %repeat, %object, %time, %funcName, %argumentList) { %appointment = new ScriptObject() { id = %this.id++; object = %object; repeat = %repeat; time = %time; funcName = %funcName; arguments = %argumentList; handle = %this.schedule(%time, "call", %this.id); }; // store appointment and return handle to caller %this.appointments.add(%appointment); return %appointment.handle; } // end addFunction //--------------------------------------------------------------------------------------------- // Name : addCommand // Class : Schedule // // Arguments : repeat - whether the scheduled call repeats // : time - the time until the appointment is executed // : command - the command to execute // // Description : adds a new appointment to the schedule manager and stores the handle and appointment // : data for later reference. Make sure all commands are terminated with a semi-colon //--------------------------------------------------------------------------------------------- function Schedule::addCommand(%this, %repeat, %time, %command) { %appointment = new ScriptObject() { id = %this.id++; repeat = %repeat; time = %time; command = %command; handle = %this.schedule(%time, "call", %this.id); }; // store appointment and return handle to caller %this.appointments.add(%appointment); return %appointment.handle; } // end addCommand //--------------------------------------------------------------------------------------------- // Name : remove // Class : Schedule // // Arguments : [apptHandle] - the handle to the appointment we want to cancel/remove // // Description : removes an appointment from the schedule manager and cancels the scheduled event. // : if apptHandle is unused, all appointments are deleted //--------------------------------------------------------------------------------------------- function Schedule::remove(%this, %apptHandle) { // find the handle, cancel and remove it for (%appt = 0; %appt < %this.appointments.getCount(); %appt++) { if ((%apptHandle !$= "") && (%this.appointments.getObject(%appt).handle == %apptHandle)) { cancel(%this.appointments.getObject(%appt).handle); %this.appointments.remove(%this.appointments.getObject(%appt)); break; } else if (%apptHandle $= "") cancel(%this.appointments.getObject(%appt).handle); } if (%apptHandle $= "") %this.appointments.delete(); } // end remove //--------------------------------------------------------------------------------------------- // Name : pause // Class : Schedule // // Arguments : isPaused - whether to pause (true) or unpause (false) all scheduled events // // Description : Cancels all scheduled events and stores their remaining time for reactivation // : when game is unpaused //--------------------------------------------------------------------------------------------- function Schedule::pause(%this, %isPaused) { if (%isPaused) { for (%appt = 0; %appt < %this.appointments.getCount(); %appt++) { %this.appointments.getObject(%appt).timeLeft = getEventTimeLeft(%this.appointments.getObject(%appt).handle); cancel(%this.appointments.getObject(%appt).handle); } } else { for (%appt = 0; %appt < %this.appointments.getCount(); %appt++) { %this.appointments.getObject(%appt).handle = %this.schedule(%this.appointments.getObject(%appt).timeLeft, "call", %this.appointments.getObject(%appt).id); } } } // end pause //--------------------------------------------------------------------------------------------- // Name : call // Class : Schedule // // Arguments : id - the unique id assigned to this appointment // // Description : is called to carry out scheduled function call but also to loop the event or // : remove it from our appointment group //--------------------------------------------------------------------------------------------- function Schedule::call(%this, %id) { // find the appointment for (%appt = 0; %appt < %this.appointments.getCount(); %appt++) { if (%this.appointments.getObject(%appt).id == %id) { // check for any arguments if (%this.appointments.getObject(%appt).arguments !$= "") { // first argument has no comma or space %args = getWord(%this.appointments.getObject(%appt).arguments, 0); for(%arg = 1; %arg < getWordCount(%this.appointments.getObject(%appt).arguments); %arg++) { %args = %args @ "," SPC getWord(%this.appointments.getObject(%appt).arguments, %arg); } } // write out the command or function call and execute if (%this.appointments.getObject(%appt).funcName !$= "") { if (%this.appointments.getObject(%appt).object !$= "") %call = %this.appointments.getObject(%appt).object @ "."; %call = %call @ %this.appointments.getObject(%appt).funcName @ "(" @ %args @ ");"; } else if (%this.appointments.getObject(%appt).command !$= "") { %call = %this.appointments.getObject(%appt).command; } eval(%call); // renew appointment or delete? if (%this.appointments.getObject(%appt).repeat) %this.appointments.getObject(%appt).handle = %this.schedule(%this.appointments.getObject(%appt).time, "call", %this.appointments.getObject(%appt).id); else %this.appointments.remove(%this.appointments.getObject(%appt)); break; } } } // end call