//--------------------------------------------------------------------------------------------- // Project : Blitz Blox // File : .\gameScripts\schedule.cs // Copyright : Blade Edge Software © 2006 - 2007 // Author : Drew Sikora //--------------------------------------------------------------------------------------------- new ScriptObject(Schedule); //--------------------------------------------------------------------------------------------- // Name : addObject // 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 //--------------------------------------------------------------------------------------------- function Schedule::addObject(%this, %repeat, %object, %time, %funcName, %argumentList) { if (%this.appointments $= "") %this.appointments = new ScriptGroup(); %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 addObject //--------------------------------------------------------------------------------------------- // Name : addFunction // Class : Schedule // // Description : same as addObject, but for regular functions //--------------------------------------------------------------------------------------------- //function Schedule::addFunction(%this, %repeat, %time, %funcName, %argumentList) //{ // %this.addObject(%repeat, "", %time, %funcName, %argumentList); // //} // end addFunction //--------------------------------------------------------------------------------------------- // 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 Schedulerd 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 function call and execute eval(%this.appointments.getObject(%appt).object @ "." @ %this.appointments.getObject(%appt).funcName @ "(" @ %args @ ");"); // 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