How do i make a command work for every character?

Started by gamerman, Sat 15/12/2007 17:36:58

Previous topic - Next topic

gamerman

i'm making a game wich the main character has some guns, when he shot the character [bullet] go from point A to B, and if it collides with character [guy] this one dies... How do i make it work for every character?

Scorpiorus

You simply need to test repeatedly whether the bullet character hit each character using a "while" loop. Every character has a corresponding number (ID ie. identifier) associated with it. Use that to go through all of them but the bullet itself to test for collision :)

Say you have 7 characters: 0, 1, 2, 3, 4, 5 and 6; where 0 is the player character and 1 is the bullet. Then you can do:


within repeatedly execute:

int bullet_id = 1;

int id = 2;
while (id <= 6) // run for characters with the following IDs: 2, 3, 4, 5, 6
{
    // test each for collision but make sure bullet cannot hit itself ;)
    if (AreThingsOverlapping( bullet_id ,  id ) && ( id != bullet_id ) )
    {
        // remove bullet from the current room
        character[ bullet_id ].ChangeRoom( -1 );

        // start playing a death animation on character being hit
        character[ id ].LockView( BEING_HIT_VIEW );
        character[ id ].Animate( 0, 0, eOnce, eNoBlock );
    }

    id ++; // proceed to testing the next character in a row
}


This should get you started figuring it out.

By the way, it is possible to set up a list of characters that are subject to be hit but I intentionally haven't shown how to do that yet as it relates to declaring an array of pointers to characters which may look somewhat confusing to understand if you are new to that sort of things.

gamerman

 :)i'll gonna try it, and report on this topic if there were some problem.

Thanks!! :)

gamerman

Hey! It worked, but...
I thought something different, there'll be lots of weapons (like shotguns, handguns, machine-guns...) and i would make a lot of bullets characters (and characters are  a real need on my game, wich will gonna have few rooms with lots of characters), so i thought its better work with the x, y and z coordinates:

int id = 1;   // player character id = 1
int shot; // weapon variable
function repeatedly_execute() {

// the normal characters id: 1, 2, 3, 4 , 5

while (id>=5) {

if (IsKeyPressed(404) == 1) {

if((shot==1) || (shot==2)) {
if ((character[id].y == cEgo.y) && (id!=0))   {
  character[id].ChangeView(6);
  }
}
}
}


but it doesn't seems to work. What i made wrong?


obs: i tried use the && in the "if" sentence above, but ags displayed : Parse error near && PE04.

Scorpiorus

The question is, how is the player supposed to aim?

Is there some kind of a crosshair or what?

Or does the player character only shoot in one specific direction?

Please elaborate on it more.

gamerman

the player just shoot at the direction.

The code above doesnt seems to work, the player shoot at the enemy character (who is at the same y coordinate) but nothing happens.

Scorpiorus

The thing is, you could still use the method with one bullet character. You can make the bullet invisible (transparent) and increase its moving (walking) speed and just use it for the sake of checking whether or not it hits some enemy at all.

Note how you can have your player character armed with any weapon you like (use different views) and give some amount of damage to enemy based on that.

SMF spam blocked by CleanTalk