"ProtoThreads" with a twist.

For a long time I’v been interested in running game-specific/entity-specific code in coroutines. Something like the following.

void some_game_object_behavior( entity ent, ... )
{
    pnt3 points[] = {
        {1,1,1},
        {2,2,2},
        {3,3,3},
        {4,4,4}
    };

    int pos = 0;

    while(entity_alive(ent))
    {
        // move the entity to a position and yield coroutine while movement is ongoing.
        move_to(ent, points[pos % ARRAY_LENGTH(points)]);
        pos++;

        for(int i = 0; i < 4; ++i)
        {
            shoot_bullet(ent);
            wait_sec(ent, 2); // do nothing for 2 seconds and yield the coroutine for that duration.
        }
    }
}

      Read More