Saturday, 1 June 2013

Implementing a wait function in a game script

Implementing a wait function in a game script

I have been implementing behavior for enemies in my game and so far I have been doing it sort of like below:
public void update()
{
    if (frame > 60 && frame % 30 == 0)
    {
        shootAtPlayer();
    }
    frame++;
}
Which is meaning after frame 60 shoot the player every 30 frames. The frame-count is important as the game is fixed frame-rate and everything is relying on this at the moment. There are many small scripts for enemy behavior like above and I currently iterate through each one calling update(). I have realized it will quickly get unweildy dealing in absolute frames and I want to move towards a pattern where I can specify to do X for 60 frames then do Y e.t.c, something that is easily understandable and easy to write. I want to go for something like below:
wait(60)
while(true)
{
    shootAtPlayer();
    wait(30);
}
I was wondering if there is a design pattern that would easily support such a syntax. My initial thoughts were to create a thread for each script object and have the wait implemented similar to below:
wait(int frames)
{
    while(frames > 0)
    {
        frames--;
        suspend();
    }
}
with the method running the scripts calling resume in turn on each script. I could potentially have up to 300 scripts running at once which would mean 300 threads to handle. Am I taking the correct approach to solving this problem?
I am programming this in C# XNA.

No comments:

Post a Comment