multiple shots

Member
Posts: 196
Joined: 2002.04
Post: #16
Ok here's the essential code(the timer and initwithframe are from GL Cubes) :
http://homepage.mac.com/jonwhite/project.sit

Also I took the for() loop out of initBullet so you can see what happens when it's not there.

Thanks,
Ice
Quote this message in a reply
Feanor
Unregistered
 
Post: #17
Quote:Originally posted by codemattic
lets look at the original asteroids code. There is a bullet struct ...
So when we start the game all of the bullet structs in our array have an inuse equal to 0 since they all start out inactive. Unfortunately the code doesnt explicitly set this at the beginning - it just assumes the compiler will put 0 in the variables to begin with...

I read recently (was it at idevgames or an Apple list I can't recall) that it is mandated by ISO C that structs are initialized automatically (unlike variables), so I think you're safe almost all the time. Are there any modern platforms that don't implement ISO C?

Brent
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #18
changes that I made to your code I marked with an //edited comment

Code:
- (void)drawRect
{  
    int    j;
    Angle2 += 3.0;

    glMatrixMode(GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef( 0.0, 0.0, -30.0);

    for (j=0; j<MAX_BULLETS; j++) {
        if (bullet[j].inuse) {
            bullet[j].z-=0.2; //edited
            glPushMatrix(); //edited
            glTranslatef( bullet[j].x, bullet[j].y, bullet[j].z);
            glRotatef(Angle2, 1.0, 1.0, 1.0);
            [self Asteroid];
            glPopMatrix(); //edited
        }
    }

    [[self openGLContext] flushBuffer];
}

- (void)shotBullet {
    int index_of_free_bullet;    //edited
    if ((index_of_free_bullet = [self allocBullet]) >= 0) { //edited
        [self initBullet:index_of_free_bullet]; //edited
    }
}

- (int)allocBullet {
    int    j;
    for (j=0; j<MAX_BULLETS; j++) {
        if (!bullet[j].inuse) {
            NSLog(@"found a free bullet at index %i",j); //edited
        return j;
        }
    }
    NSLog(@"there are no more free bullets - all are in use"); //edited
    return -1;
}

- (void)initBullet: (int)index {  //edited
    bullet[index].inuse = 1;
    bullet[index].x = 0.0;
    bullet[index].y = -15.0;     //edited
    bullet[index].z = 0.0;
}

remember to edit your .h file with the updated initBullet.
Code:
- (void)initBullet: (int)index;  //edited

You get five shots. But once a shot is in play - there is nothing that takes it out of play. Theres nothing for them to hit - and they dont time out on their own. So once you fire five times - thats it. This should get you started though.

happy coding,
Codemattic
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #19
Thanks codemattic it worked perfect. My bullets look awesome !Grin I'm working on deleting the bullets now and it's going great. Also thanks Lemming for the alloc idea I'm only making my first game so the game engine part really doesn't matter to me, but when I make my next game I'll make sure to see if I can understand how to use the alloc stuff properly.

Thanks so much,
Iceman
Quote this message in a reply
Post Reply