How to code simple attack logic for 2D game in C#?

1.8k views Asked by At

Also, attack works, however, the main issue is that I'm supposed to make weapon attack IN ONE DIRECTION.

Hi, I am making a turn based action game where the player can either move or attack enemies, using a certain weapon. Enemies just use a simple attack logic of checking if the player is nearby, then making him take damage if so. Nearby() uses Math.Abs to find absolute distance away.

Here is the old Nearby() method used for Enemy attack:

public bool Nearby(Point locationToCheck, int distance)
        {
            if (Math.Abs(location.X - locationToCheck.X) < distance &&
            (Math.Abs(location.Y - locationToCheck.Y) < distance))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

So the Enemy class, which contains the above method, passes Player location into the method, and its own attack range as an int, then checks it against its own location, and damages the player if return true.

However, the player attack is more complicated. His weapon must check for Nearby() distance in a given direction first. So I need to write a new overloaded Nearby() method to work with the Player's Weapon attack logic. I don't know how to do this. Somehow, the Player's Weapon Attack method is supposed to pass parameters into the following DamageEnemy() method:

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

This DamageEnemy method relies on two new overloaded methods, the aforementioned Move() method, and the overloaded Nearby() method. I am doing this as a tutorial in Oreilly Headfirst C#, p. 480, by the way.

*The original inherited Move() method is simple; it just updates the location field of the object that inherits it in a direction based on the input of one of 4 directions from a Direction Enum.

I want to do this myself, but really can't figure out how the attack logic is supposed to work! Can anyone give me some hints to point me in the right direction? I don't know what they want me to make the new Move() overloaded method for. Is the player supposed to jump to the point where he attacks? Or does it have some other functionality? Thanks!

1

There are 1 answers

0
JMP On
for (int distance = 0; distance < radius; distance++) {
        foreach (Enemy enemy in game.Enemies) {
            if (Nearby(enemy.Location, target, distance)) {
                enemy.Hit(damage, random);
                return true;
            }

This is really bad. The 'for' routine is redundant - distance can be got from enemy.Location and target, and it would take forever to check each enemy over every distance. Plus what if there are obstacles in the way. I think 'distance' in the overrided Nearby() function should be 'direction' which would answer your question. I suggest:

    foreach (Enemy enemy in game.Enemies) {
        if (Nearby(enemy.Location, target, radius, direction)) {
            enemy.Hit(damage, random);
            return true;
        }

Also the 'move()' function is called inside the distance' loop, and so its behaviour is unpredictable.