Saturday 8 August 2009

Implementing Transient Events - an update

After some feedback, I've made a few tweaks to the TGameTimer class. Here is the new Tick method:
{ Decrease the duration of the timer by a turn }
function TGameTimer.Tick: Boolean;
begin
{ Trigger events if they are defined }
if (FTimerDurationLeft > 0) then
begin
{ Interval Event }
if (Assigned(FTimerTickEvent)) then
FTimerTickEvent(Self);
end
else if (FTimerDurationLeft = 0) then
begin
{ End Event }
if (Assigned(FTimerEndEvent)) then
FTimerEndEvent(Self);
end;

{ Decrement the Counter }
Dec(FTimerDurationLeft);

{ Return true if the Timer is still active }
Result := GetStatus;
end;
Moving the decrement of the tick timer to after the tick events means that it now correctly works with a duration of one turn - in these circumstances previously only the Start and End Events would be fired.

The interface to the Event Hooks themselves have changed

{ Procedure Pointer for Event Hook }
type TGameTimerEvent = procedure(const GameEvent: TObject);

This allows passing in of the triggering TGameTimer object (referenced by Self in the tick method, and subsequently cast to TGameTimer in the actual hook).

For those of you familiar with Delphi this is pretty much (with a few minor differences) what TNotifyEvent does.

I've also removed the Progress property entirely. Since the Tick event is designed to be called every game turn then its simply a matter of comparing FTimerDuration and FTimerDurationLeft as needed there instead.

No comments: