NSTimer: initial implementation of an object timer. Handles temporary (self garbgage collecting) timers as well.
This commit is contained in:
parent
c67e4b5276
commit
acaa918403
4 changed files with 135 additions and 0 deletions
31
src/shared/NSTimer.h
Normal file
31
src/shared/NSTimer.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2022 Vera Visions LLC.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
||||
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
class
|
||||
NSTimer:NSEntity
|
||||
{
|
||||
entity m_eReceiver;
|
||||
virtual void(void) m_vFunc = 0;
|
||||
float m_flTime;
|
||||
bool m_bRepeats;
|
||||
|
||||
void(void) NSTimer;
|
||||
|
||||
virtual void(void) _TimerThink;
|
||||
virtual void(void) _TempTimerThink;
|
||||
static NSTimer(entity, void(), float, bool) ScheduleTimer;
|
||||
static NSTimer(entity, void(), float, bool) TemporaryTimer;
|
||||
};
|
102
src/shared/NSTimer.qc
Normal file
102
src/shared/NSTimer.qc
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2022 Vera Visions LLC.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
||||
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
void
|
||||
NSTimer::NSTimer(void)
|
||||
{
|
||||
m_eReceiver = __NULL__;
|
||||
m_vFunc = __NULL__;
|
||||
m_flTime = 0.0f;
|
||||
m_bRepeats = false;
|
||||
}
|
||||
|
||||
/* wrapper to execute our trigger */
|
||||
static void
|
||||
_NSTimerWrapper(entity receiver, void() func)
|
||||
{
|
||||
entity oldself = self;
|
||||
self = receiver;
|
||||
func();
|
||||
self = oldself;
|
||||
}
|
||||
|
||||
void
|
||||
NSTimer::_TimerThink(void)
|
||||
{
|
||||
_NSTimerWrapper(m_eReceiver, m_vFunc);
|
||||
|
||||
if (m_bRepeats == true)
|
||||
nextthink = time + m_flTime;
|
||||
}
|
||||
|
||||
void
|
||||
NSTimer::_TempTimerThink(void)
|
||||
{
|
||||
_NSTimerWrapper(m_eReceiver, m_vFunc);
|
||||
|
||||
if (m_bRepeats == true)
|
||||
nextthink = time + m_flTime;
|
||||
else {
|
||||
think = Destroy;
|
||||
nextthink = time;
|
||||
}
|
||||
}
|
||||
|
||||
NSTimer
|
||||
NSTimer::ScheduleTimer(entity receiver, void() call, float interval, bool repeats)
|
||||
{
|
||||
NSTimer ret;
|
||||
|
||||
if (this)
|
||||
ret = this;
|
||||
else {
|
||||
ret = spawn(NSTimer);
|
||||
this = ret;
|
||||
}
|
||||
|
||||
ret.m_eReceiver = receiver;
|
||||
ret.m_flTime = interval;
|
||||
ret.m_vFunc = call;
|
||||
ret.m_bRepeats = repeats;
|
||||
|
||||
/* time to start running */
|
||||
ret.think = NSTimer::_TimerThink;
|
||||
ret.nextthink = time + interval;
|
||||
return ret;
|
||||
}
|
||||
|
||||
NSTimer
|
||||
NSTimer::TemporaryTimer(entity receiver, void() call, float interval, bool repeats)
|
||||
{
|
||||
NSTimer ret;
|
||||
|
||||
if (this)
|
||||
ret = this;
|
||||
else {
|
||||
ret = spawn(NSTimer);
|
||||
this = ret;
|
||||
}
|
||||
|
||||
ret.m_eReceiver = receiver;
|
||||
ret.m_flTime = interval;
|
||||
ret.m_vFunc = call;
|
||||
ret.m_bRepeats = repeats;
|
||||
|
||||
/* time to start running */
|
||||
ret.think = NSTimer::_TempTimerThink;
|
||||
ret.nextthink = time + interval;
|
||||
return ret;
|
||||
}
|
|
@ -49,6 +49,7 @@ string __fullspawndata;
|
|||
#include "NSIO.h"
|
||||
#include "NSTrigger.h"
|
||||
#include "NSEntity.h"
|
||||
#include "NSTimer.h"
|
||||
#include "NSRenderableEntity.h"
|
||||
#include "NSSurfacePropEntity.h"
|
||||
#include "NSPhysicsEntity.h"
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
NSIO.qc
|
||||
NSTrigger.qc
|
||||
NSEntity.qc
|
||||
NSTimer.qc
|
||||
NSRenderableEntity.qc
|
||||
NSSurfacePropEntity.qc
|
||||
NSPhysicsEntity.qc
|
||||
|
|
Loading…
Reference in a new issue