• Advertisement
Sign in to follow this  

Timing Enemy Creation

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating a 2d side scroller game with blitzbasic. i got everything down but the timing. My current implementation creates a new enemy every frame. I tried to slow down the creation by using delay on the enemy creation, but all it did was delay the whole game. so what i am trying to ask is how do i slow down the enemy creation without slowing down the entire program?

Share this post


Link to post
Share on other sites
Advertisement
You'll need some kind of timer.

The most simple thing you could do is to store the number of the current/last frame in a variable. Also store when the last enemy was created in a variable. Decide on how often you want to add enemies and update the lastenemy variable when you add a new enemy.

Of course... you might want to use a real timer that actually measures time instead.

Share this post


Link to post
Share on other sites
what i was thinking of trying to do was create an actual timer. Then i would have a certain number. every time the number divided into the time with a remainder of 0 create an enemy.

Share this post


Link to post
Share on other sites
here's what i have come with:

timer= createtimer(100) ;100 is when to create a tick

...

create_enemy()
time= timerticks(timer) ;time equals total # of current ticks

if (timer modulus 3=0) then ;if = 0 crate an enemy.
{
enemy.enemy=new enemy
}

does anyone know of a better way of doing this?

Share this post


Link to post
Share on other sites
Leffe(or anyone else) could you elaborate on your idea of current and last frame. possibly post example code, thanks, in advance.

Share this post


Link to post
Share on other sites
Have two variables:

1) how often to create an enemy (in ticks)
2) how long since the previous enemy was created

Each physics step (tick), then do this:


curTick++;
if( curTick >= howOftenToCreateAnEnemy ) {
curTick -= howOftenToCreateAnEnemy;
CreateEnemy();
}


You can change the "++" to "+= elapsedTime" or whatever, depending on whether you use ticks or seconds for your measurements (if it's seconds, you probably want double rather than int for the counter, too :-)

Share this post


Link to post
Share on other sites
You could also just create a new enemy every 10th frame or so -- though that wouldn't be as accurate as a timer.

Share this post


Link to post
Share on other sites
Quote:
Original post by mumpo
You could also just create a new enemy every 10th frame or so -- though that wouldn't be as accurate as a timer.

yeah, i tried that. it worked fairly well, but the time would change constantly becase i use random enemies, and some of them are more detailed than others, so the time between creations would slow down and speed up constantly.

Share this post


Link to post
Share on other sites
I am remaking my game using C++ and openGL, and i am wondering, how to make a timer and use it with C++ and openGL?

Share this post


Link to post
Share on other sites
simple timer class. Also called time triggers.

Have a time trigger manager, where you had time triggers. Update the class, which in turn updates all the time triggers, which in turn, creates the enemy or whatever, and delete themselves, or just carry on until the time has expired.

usually, for things like this, it's good to have a sort of callback funtion mechanism.


struct CTimeTrigger
{
float m_fTimeLeft;
int m_iID;
void (*m_pfnCallback)(int iTimeTriggerID);

CTimeTrigger(float fDelay, int ID, void (*pfnCallback)(int iTimeTriggerID))
: m_fTimeLeft(fDelay)
, m_iID(ID)
, m_pfnCallback(pfnCallback)
{
s_xTriggers.AddToList(this);
}

~CTimeTrigger()
{
s_xTriggers.RemoveFromList(this);
}

bool Update(float fFrameTimeStep)
{
m_fTimeLeft -= fFrameTimeStep;

if (m_fTimeLeft < 0.0f)
{
m_pfnCallback(m_iID);
return false;
}
return true;
}
CList<CTimeTrigger> s_xTriggers;

static void UpdateAllTimeTriggers(float fFrameTimeStep)
{
for(CIterator<CTimeTrigger> xIterator(s_xTriggers); !xIterator.Done();)
{
CTimeTrigger* pxCurrent = xIterator.Current();

xIterator.Next();

if (pxCurrent->Update(fFrameTimeStep) == false)
{
delete pxCurrent;
}
}
}
};





void CreateEnemyCallback(int iEnemyID)
{
switch(iEnemyID)
{
case ENEMY_MOTHRA: new CMothra(); break;
case ENEMY_GODZILLA: new CGodzilla(); break;
case ENEMY_KINGKONG: new CKingKong(); break;
case ENEMY_MINGTHEMERCILESS: new CMingTheMerciless(); break;
default: Assert("Unknown enemy class."); break;
}
}

void CreateEnemies(void)
{
new CTimeTrigger(10.0f, ENEMY_MOTHRA, CreateEnemyCallback);
new CTimeTrigger(12.0f, ENEMY_KINGKONG, CreateEnemyCallback);
new CTimeTrigger(25.0f, ENEMY_GODZILLA, CreateEnemyCallback);

//.....
}

void Update(float fFrameTimeStep)
{
CTimeTrigger::UpdateAllTimeTriggers();

//.....
}




that's just an example. nothing prevents you to trigger an enemy when a specific enemy dies. FOr example, Mothra dies, it summons KingKong to beat up your ass 5 seconds later by creating a time trigger


CMothra::~CMothra()
{
new CTimeTrigger(5.0f, ENEMY_KINGKONG, CreateEnemyCallback);
}



ect.... endless possibilities....

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement