O'Reilly Forums: Lab #2: Damageenemy() Not Making Any Sense - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lab #2: Damageenemy() Not Making Any Sense

#1 User is offline   Raymii 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 11
  • Joined: 11-October 11

Posted 18 September 2012 - 10:59 AM

Hey there, maybe you guys can help me out with this.

I think I did pretty well with Lab #2. Got the program running just fine. Only problem I got is: My weapons are hitting in all directions all the time, not only in the specified direction. Which sense because the code provided in the book doesn't check the direction and there's some other aspects that just don't make sense to me. That's the code for the DamageEnemy method in Weapon.cs provided by the book:

        protected bool DamageEnemy(Direction direction, int radius, Random random, int damage)
        {
            Point target = game.PlayerLocation;
            for (int distance = 0; distance < radius; distance++)
            {
                foreach (Enemy enemy in game.Enemies)
                {
                    if (Nearby(enemy.Location, target, radius))
                    {
                        enemy.Hit(damage, random);
                        return true;
                  }
                }
                target = Move(direction, target, game.Boundaries);
            }
            return false;            
        }


First: Why is there a For-loop? It Counts up int distance but it never actually uses it because the following foreach-loop uses radius instead. To my believe this outer loop does nothing which is supported by the fact that removing it doesn't do any harm, the code works like before.

Second: Although the direction is passed to the method, it is never achtually checked or used for the attack. It's just passed to the overloaded move method which brings me to:

Third: What is calling the overloaded Move-method supposed to achieve? It can't be moving the enemies after the attack, because that is already taken care in the attack method in the game class. I never wrote the overloaded Move method and commented out the line that's calling it and the code is working just fine.

I don't know if my Problems result from an error in the book's code of from the fact that I maybe solved a few tasks differently than the book suggested. Eg I'm removing used potions or dead enemys from the lists instead of just making them invisible as the book suggests. But that shouldn't be the source of the Problems I'm having I would guess.

Any hints anyone? My believe would be I'd have to rewrite the DamageEnemy method to check for the direction of the enemy before calling enemy.hit but maybe I'm missing something? Any Input is greatly appreciated.
0

#2 User is offline   Raymii 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 11
  • Joined: 11-October 11

Posted 19 September 2012 - 01:03 PM

Hmm. I now wrote my own DamageEnemy() method that only attacks in the specified direction. It needs a new method in Mover.cs that I also wrote. Seems to be working just fine. I still don't get what the original method provided in the book is supposed do. Any Input would still be greatly appreciated :-)

Ok, here's my code:

        protected bool DamageEnemy(Direction direction, int radius, Random random, int damage)
        {
            Point target = game.PlayerLocation;
            foreach (Enemy enemy in game.Enemies)
                {
                if (Nearby(enemy.Location, target, radius))
                {
                    if(direction == EnemyDirection(enemy.Location, target))
                    {
                        enemy.Hit(damage, random);
                        return true;
                    }
                }               
            }
            return false;
        }


And here's the method from Mover.cs for checking if the enemy is not only within the radius but also in the right direction.

        public Direction EnemyDirection(Point enemyLocation, Point playerLocation)
        {
            if (enemyLocation.X < playerLocation.X && Math.Abs(playerLocation.Y - enemyLocation.Y) < (playerLocation.X - enemyLocation.X))
                return Direction.Left;
            else if (enemyLocation.X > playerLocation.X && Math.Abs(playerLocation.Y - enemyLocation.Y) < (enemyLocation.X - playerLocation.X))
                return Direction.Right;
            else if (enemyLocation.Y < playerLocation.Y && (playerLocation.Y - enemyLocation.Y) > Math.Abs(enemyLocation.X - playerLocation.X))
                return Direction.Up;
            else 
                return Direction.Down;

0

#3 User is offline   Lorenco 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 15-October 12

Posted 15 October 2012 - 08:45 AM

Hello, I also think this method doesn't make sense. However you said everything works fine for you but in Mover.cs
Nearby(Point locationToCheck , int distance) --> takes 2 parameters where as in Weapon.cs code provide Nearby take 3 parameters !
if (Nearby(enemy.Location , target, radius)) --> takes 3 parameters how did you sort that out?
Did you write an overloaded method in Mover.cs ?

This post has been edited by Lorenco: 15 October 2012 - 08:46 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users