Page 1 of 1

Problem with enemy attacking player

Posted: Wed Aug 31, 2011 5:19 pm
by thoma91
Hi there! New day, new obstruction here.. :?
I want my enemy to attack the player, when they're near.
So I check their positions, and if they're near, the attack should happen.
I'm using a playerhit variable (until there isn't a better solution), because if I don't, the if condition will loop forever when I'm in the given area (the code is under the while(device->run()).. area).

This works for a single attack:

Code: Select all

 
if(enemy1health > 0){
playerpos = camera->getPosition();
enemy1pos = node->getPosition();
 
if(playerpos.X-enemy1pos.X < 0 && playerhit==0)
{
    if(playerpos.X-enemy1pos.X > -110){
        // the same as below
    }
}
 
else if(playerpos.X-enemy1pos.X > 0 && playerhit == 0){
    if(playerpos.X-enemy1pos.X < 110){
    ++playerhit;
    controller.playAnimationNow("attack");
    controller.playAnimation("walk");
    playerhealth -= 33;
    updateHealth();  // it displays the player's health depending on the playerhealth variable
    }
}
}
 
But as I said before, if I don't use the playerhit var., the attacks will never end,and the player's health drops below 0 immediately.
If I use similar conditions after these, replacing playerhit==0 with playerhit==1, the second attack happens, but the health drops immediately twice as it should after one attack, even if I run out of the enemy's area before the second attack.

What am I missing?

Re: Problem with enemy attacking player

Posted: Wed Aug 31, 2011 8:11 pm
by serengeor
thoma91 wrote: What am I missing?
Oh let me just guess.. Wait for it... wait for it... I'm just guessing programming/problem solving experience is the thing you're missing, correct me if I'm wrong.

You want people to write the whole game for you? If no than help yourself by sitting quietly and thinking what exactly could solve your problem and only then if you're not sure how to do that ask here.
Forum Etiquette - Plz read before posting! (taken from Baal) wrote: :arrow: Do your homework first. Investigate your problem by experimenting with variations, looking at logs, and using your debugger. Problem solving is a learned skill, you have to DO it to become self sufficient. We're happy to help but expect some effort from you too.

Re: Problem with enemy attacking player

Posted: Wed Aug 31, 2011 11:12 pm
by Lonesome Ducky
This is basic programming knowledge. Every single frame where the enemy is within the distance, it takes health from the player. If your fps is 200, when that enemy is within the distance, it attacks 200 times in one second because you have nothing to delay it. I'm curious as to what you thought would happen, honestly

Re: Problem with enemy attacking player

Posted: Thu Sep 01, 2011 12:48 am
by thoma91
serengeor wrote: I'm just guessing programming/problem solving experience is the thing you're missing, correct me if I'm wrong.
Yes, you're right. As I said, I'm a beginner, and I need (a lot of) help because this task is too big for me at the moment (and I have limited time too).
I wish I had years of experience like you and the others,but I haven't got. If I had, I'd not ask that much.
I don't do this to annoy you, or anybody else here.
I do this, because I can ask nowhere else.
This forum is for helping each other, if someone gets stuck.
Yes, I need more help than others.. and then what?
It's not pleasant for me, you could believe.
Sorry if it annoys you, but I have no chance, I have to ask. Please do not read my topics if you don't want to help, really.
You don't HAVE to help. No one HAS to help. It's optional. If someone would like to help, than he will.
Thank you for your understanding.

@Lonesome Ducky:
Yes,I know it's wrong,but this is how I started.
I tried to create a good condition for not to load it hundred times, but it's not so good so far.
This is why I turned to you, because at the moment I haven't got a better idea than this.

Re: Problem with enemy attacking player

Posted: Thu Sep 01, 2011 7:09 am
by Virion
search for frame independent movement (time-based movement) and you'll get to know how to limit the movement of the player by time. keyword: deltaTime
I don't know how to explain this but you should get the right solution after searching a bit with the keywords above.

Re: Problem with enemy attacking player

Posted: Thu Sep 01, 2011 8:28 am
by Andreas
As Virion said, add a new variable which counts the time passed since the last call, i.e. adds the deltaTime. You could use irrlicht's timer, e.g. like this:

Initialization (only once):

Code: Select all

 
  irr::ITimer *timer;
  irr::u32 lastTime, currentTime;
 
  timer = irrlichtDevice->getTimer();
  lastTime = timer->getRealTime();
 
Updates (every frame):

Code: Select all

 
  currentTime = timer->getRealTime();
  u32 deltaTime = currentTime - lastTime;
  lastTime = currentTime;
 
You can then add the value of deltaTime to a counter, and if this counter is bigger than, e.g. 2000 (two seconds), you could attack the enemy again.

Re: Problem with enemy attacking player

Posted: Thu Sep 01, 2011 12:07 pm
by thoma91
Virion, Andreas: Thank you so much guys for your help :)

Re: Problem with enemy attacking player

Posted: Wed Sep 07, 2011 1:34 am
by christianclavet
As everybody says, you need to add a timer between each attack. But you should also need to check that your animation (seen that you use an animation controller) as completed playing.

If this can help, here something that could be done (some sort of quick logic pseudocode):

Code: Select all

-> Check for an attack range
       |-> No: -> walk to target or seek a new target
       |-> Yes
               |-> Prepare a attack timer (pre-attack) delay before doing the attack
                            |-> Do the attack (animation + damage)
                                             |> Wait for recuperation (animation ended, counter attack, block, etc) (post-attack)
If you look at this, you'll need at least 2 timers to make it "look" ok. During the pre-attack, you could check if the player is blocking, or even attack back, then determine if the NPC will have it's attack blocked and it being hurt (so instead of calling the "attack" anim, you call the "hurt" animation). Using a single timer, could work, but you will have to increase the timer duration to have the duration of the animation.

For the waiting for the end of the animation, the best way to do it is to check for the animation callback. (check the forum/API)