View XNA Realtime Game Architecture Notes
Some working notes...I'm outlining some classes I wrote that are a little esoteric but are useful in filtering or generating "detected events" so I can write cleaner game logic.
Debouncer Class
When writing game logic, a detected condition is often TRUE or FALSE, and is polled at intervals (often on every frame). What is useful to know is when the condition first transitions to the TRUE state so you can handle it just once. The Debouncer class is a utility that helps with that. Additionally it imposes a maximum trigger rate, useful for implementing cooldowns on button inputs.
Key Methods & Properties
Debouncer () - Constructor takes no arguments.
bool Edge ( bool trigger ) - accepts a boolean value. Returns TRUE when trigger transitions from false to true. Call on every frame with trigger.
bool Edge ( int number ) - accepts a number. Returns TRUE when the value changes.
Support Methods & Properties
void SetDebounceTime ( float msInterval ) - to enable the cooldown, call this after construction with msInterval set to an interval in milliseconds.
BStator Class
The BStator is a kind of "detected state" variable that's used in my Piece AI classes. At the beginning of Piece AI, my code checks for a bunch of conditions and sets the state of an appropriately-named BStator. The named bstators are then used to drive decisions later in the decision process. This has the advantage of forcing me to clarify my thinking, makes the set of states easier to manipulate, and simplifies the code later.
For convenience, BStator also allows the storage of an Object. For example, a detected bstate might include the object that was detected (for example, the capture of one piece by another one). This feels clunky to me, but it works for now.
Key Methods & Properties
BStator () - Constructor takes no arguments.
void Trigger () - Call this to "set" the bstator to TRUE.
void Reset () - call this to reset the bstator to pristine state. This resets triggercount to 0, and also sets Tag to null. It does not, however, affect KeptTag.
bool IsSet - returns TRUE if Trigger() was called on this instance, or if KeptTag is not null;
bool NotSet - returns TRUE if Trigger() was not called on this instance AND there is no KeptTag; it's the opposite of IsSet().
Support Methods & Properties
int TriggerCount - returns the number of times that Trigger() was invoked. Usually 1, more if AllowMultiSet() was invoked.
Object Tag - returns an object that is stored with this bstator. It is erased every time Reset() is called. Provided so users of bstator can keep data that is relevant when the Trigger() detection occurred.
Object KeptTag - returns an object that is stored with this bstator. It is NOT erased when Reset() is called. If there is persistent state that needs to be kept with the bstator, store it here.
void AllowMultiSet () - Call this after construction to allow this bstator to be triggered more than once. Ordinarily this is considered an error condition, and the bstator will throw an Exception when this occurs.
Timer Class
This is a countdown timer class. It handles oneshot and repeating timer modes (though this is not set up exactly right yet). It can optionally call an EventHandler on expiration without explicit polling; otherwise, check the timer periodically to see if it's expired.
Key Methods & Properties
Timer () - The default constructor is set to trigger in 1 second and is inactive.
Timer ( long msec ) - Set the timer to expire in milliseconds.
void SetEventHandler (NotifyHandler returnCall, Piece evtObj, int evtCode) - Set the optional event handler for this timer instance to fire when time expires.
bool CheckTimer () - returns true if the timer has expired.
Support Methods & Properties
void SetTimerMS ( float msec ) - Set the timer countdown to msec milliseconds. The timer is also reset from the time of the call.
void SetOneshotMode (), SetRepeatMode (), SetRepeatNMode (int count) - Set the timer mode. Oneshot is the default mode.
void Activate (), Deactivate () - Timers that are active are actively counting down and firing events. Inactive timers they always return FALSE and never fire. When Activate() is called, the timer is started from the time it is invoked, using whatever interval was set at creation time.
void Pause (), Unpause () - Timers that are paused are inactive, but "saves" the remaining time. When the timer is unpaused, the new trigger time is calculated from that time + the saved remaining time. A paused timer is always inactive.
Counter Class
If I want to wait for 10 frames before doing something, I usually have to set up a few variables and clutter up my classes with increment/decrement logic. Bah, I say! The Counter class implements a countdown/countup, and provides methods to check when the count is complete.
Like the Timer class, Counter implements ONESHOT, REPEAT, and REPEATN modes. You can set the "bounce mode" to REFLECT or ROLLOVER.
After setting up the timer, call StepCheck() to increment or decrement it. The return value of StepCheck() is TRUE when the condition is met. It is true only once when the condition is met.
To check when a counter is done, use the Expired property or test the return value of StepCheck().
Key Methods & Properties
Counter () - creates a default counter that counts down from 1 in ONESHOT countdown mode. The default counter is inactive; call Activate() to enable the timer.
Counter ( int count ) - initializes an active timer in oneshot countdown mode.
bool StepCheck() - steps the counter and returns TRUE if the counter is triggered. This happens just once in ONESHOT mode, repeated in REPEAT mode, and up to N times in REPEATN mode. It will also fire an event if SetEventHandler() called beforehand.
bool Expired - returns TRUE if the counter had been tripped at some point.
bool NotExpired - returns TRUE is the counter is still running and hasn't been tripped.
int Value - returns the current value as it counts toward the end
int RemainingValue - returns the remaining value left in the counter
Support Methods & Properties
void Activate(), Deactivate() - turns on the counter.
void SetCount ( int count ) - sets the count magnitude
void SetEventHandler ( NotifyHandler handler, Piece evtObj, int evtCode ) - sets the event handler to call when a trigger callback is set.
void SetOneshotMode(), SetRepeatMode(), SetRepeatNMode( int count) - sets the timer trigger modes for one-time or repeating modes.
void SetToReflect(), SetToRollover() - sets the timer automatically to repeat mode, and whether to ping-pong between values or wrap around.
void SetSteppingDown(), SetSteppingUp() - whether to count up or down. DOES NOT reset the values, so use with care.
RandomGenerator Class
In a way similar to the Counter and Timer classes, RandomGenerator evaluates to TRUE randomly. It has ONESHOT, REPEAT, and REPEATN modes, and can inform an eventhandler when it fires. The frequency of the generated events is set by a SetFrequency() call that ranges from 0 to 100 percent.
Key Methods & Properties
RandomGenerator () - Constructor takes no arguments. It is inactive by default, and is set to a default frequency of 10%. The default mode is ONESHOT.
RandomGenerator ( float frequency ) - Constructor accepts a frequency range from 0 to 100. If the value is out of range then it's clamped to either 0 or 100. It is active, and the default mode is REPEAT.
bool StepCheck () - returns TRUE when the random events occurs. Depending on the mode, only a certain number of events is emitted.
bool Expired - returns TRUE if the generator is no longer active, having completed its service.
bool NotExpired - returns TRUE if the generator is still actively generating events.
Support Methods & Properties
void SetFrequency ( float frequency ) - Accepts a frequency range from 0 to 100. Clamps to 0 or 100 if value is out of range.
void SetOneshotMode (), SetRepeatMode (), SetRepeatNMode ( int repeatcount ) - sets the mode of the generator. In ONESHOT mode, only one random event is allowed and then the generator is deactivated. In REPEAT mode the generator continues on its way.
void SetEventHandler ( NotifyHandler handler, Piece evtObj, int evtCode ) - sets the event handler to call when a trigger callback is set.
void Activate(), Deactivate() - turns on/turns off the generator.
