I'm assuming that you're already looping over all the game objects in order to move them. If so, you can just update the tiles during each object's movement, rather than in a separate loop.renegadeandy wrote: each enemy on each game loop :
call a function which will iterate over all enemies, find and update their tile positions
If you're using an animator to move your objects, I'd recommend switching to doing movement yourself, since it gives you a much finer degree of control.
Each time round your game loop, work out how long the previous frame took by diffing device->getTimer()->getTime() to get a difference in milliseconds. Either leave it as milliseconds, or divide it by 1000 to get a (float) time in seconds. I like to work in seconds, so I div by 1000.renegadeandy wrote: each tower or defencive unit scan for the tiles around it and shoot if something is near.
I say everygame loop, i suppose as you say it could be every 100ms or every 250ms.
**unsure how to make it do it less often but thats a problem which im sure can be easily sorted later**
Code: Select all
u32 then = device->getTimer()->getTime();
while(device->run())
// if (device->isWindowActive())
{
SContext::Time = device->getTimer()->getTime();
SContext::FrameDelta = (SContext::Time - then) / 1000.f;
if(SContext::FrameDelta > 0.1f) // Limit the delta, in case I've been debugging
SContext::FrameDelta = 0.1f;
then = SContext::Time;
For each tower, use a counter. Each frame, decrement it by the frame time. When it's <= 0, do your targeting, and set the counter to the delay that you want (e.g. 0.1f).
Code: Select all
void doPointDefence(void)
{
TimeToNextPointDefenceTargetting -= SContext::FrameDelta;
if(TimeToNextPointDefenceTargetting <= 0.f)
{
TimeToNextPointDefenceTargetting = SConstants::PointDefenceTargettingDelay;
// ... do the targetting
}