Indeed! Agree with Monkey. I actually hadn't ever thought about putting the Character in the struct (silly me), but especially on line 4 above, it shouldn't be Character *enemy, just any other name, like Character *tempDude, or whatever.
I also realize that this may seem like a lot of setup work, but it allows you to run checks against every character you want with a loop instead of calling up character names individually.
But regarding the other aspects of your script, you could have multiple bad guys around, too.
Code: AGS
I hope this addresses some of the other things that you were trying to accomplish, as well.
I also realize that this may seem like a lot of setup work, but it allows you to run checks against every character you want with a loop instead of calling up character names individually.
But regarding the other aspects of your script, you could have multiple bad guys around, too.
int i = 0;
int closestEnemy = -1;
int maxRange = 20500;
int xDiff; int yDiff; int enemyDistance; // If not defined elsewhere.
while(i < 100){
Character *cTempDude = character[enemy[i].characterLink]; // Or Character *cTempDude = enemy[i].agsCharacter;
if(cTempDude.Room == cEgo.Room){ // Check to see if the character is in the room.
xDiff = cRoy.x - cTempDude.x;
yDiff = cRoy.y - cTempDude.y;
enemyDistance = xDiff*xDiff + yDiff*yDiff; // Calculates the distance between EACH enemy and Roy.
if(enemyDistance < maxRange){ // If an enemy is close enough to hit you, they are marked as the closest
closestEnemy = i; // enemy and their distance is now the "range" to check the next enemy by.
maxRange = enemyDistance;
}
}
i++;
}
if(closestEnemy == -1){
Display("Not close enough!"); // If no one was in range...
}
else{
int i = closestEnemy; // If there was an enemy in range, we pick him.
Character *cTempDude = character[enemy[i].characterLink]; // Or Character *cTempDude = enemy[i].agsCharacter;
damageDone = Random(10);
enemy[i].health -= damageDone;
if(enemy[i].health < 0){
cTempDude.ChangeView(8); // The magic of this method is that you can edit the variables (like health) and the actual character functions at the same time. You could also use cTempDude.Walk, cTempDude.Animate, cTempDude.ChangeRoom, etc. And the one script will work with any enemy character that you throw against the user!
}
}
I hope this addresses some of the other things that you were trying to accomplish, as well.