1
\$\begingroup\$

So, I'm a little stuck on what the best way to implement weapons is. The problem I'm running into is, what exactly is a weapon.

I have a Player Class that has an attack method.

public class Character
{
    public int MaxDamage;

    public bool IsAlive = true;

    public Character Attack(Character target)
    {
        Random attackRange = new Random();
        int damageValue = attackRange.Next(0, MaxDamage);

        target.TakeDamage(damageValue);

        Random critCheck = new Random();
        int critCheckValue = critCheck.Next(0, 100);
        //hitChance = dex - target.dex;

         if (critCheckValue <= CritChance) 
         {
          //dmg min = dmg min + (dmg min*crit damage);
          MaxDamage = MaxDamage + (MaxDamage * CritDamage); 
         }
     }
}

So this works. But once I implement a weapon, it gets a little confusing.

  • Does the player still have the attack method and the weapon just modifies the values?
  • Does the weapon really have the attack method and the player just calls it?
  • How would you handle something like a power swing that may hit 3 characters?
  • Does the weapon contain the range or the player?
\$\endgroup\$
  • 3
    \$\begingroup\$ gamedev.stackexchange.com/questions/5626/… Think about using the research function of the site \$\endgroup\$ – nathan Feb 17 '16 at 14:50
  • \$\begingroup\$ @nathan your right, i want to implement xml into a unity game. thats always the answer \$\endgroup\$ – DidIReallyWriteThat Feb 17 '16 at 15:10
  • 2
    \$\begingroup\$ @DidIReallyWriteThat: I think you're missing the point entirely if you're hung up on the XML example in that answer. You can do the same thing with a Unity prefab if that's more your cup of tea, or whatever other data serialization you prefer. \$\endgroup\$ – Sean Middleditch Feb 17 '16 at 15:57
4
\$\begingroup\$

This question is a bit broad, but a few things...

1) You could create a CLASS for weapons, and have the character object own a 'weapon' object, and then you can add in, say "Weapon.damageBonus" to damage dealt

2) Your "Attack" function returns a "Characer" object. I believe you want to return an int or double (or float if for some reason you need a float?) for your 'damage' amount, perhaps?

3) As nathan mentioned in his comment, you really could benefit from searching on this site. There is already a TON of info on here about good ways to implement what you want to accomplish.

Hope this helps.

EDIT:

I realize there was a lot of your question that I didn't address, so here:

The player could have the attack method. You could generalize it more and have it inherit the attack method as well.

The weapon is an item, it doesn't 'do' things, it 'is' things. I would argue that you should have your characters 'do' things (like figure out damage and whatnot) while your items (armor, weapons, etc) just 'are' things, and thus just tweak the formulas a bit.

To handle something like one swing hitting multiple characters, you could simply have the character return the damage they 'intend' to do, and then each thing hit can take that damage and do some math to figure out from that, how much they actually 'take'.

Like my above comment, I would argue for the player to have the range, but the weapon to modify it.

Again, hope this helps.

\$\endgroup\$
  • \$\begingroup\$ that makes sense, let me think about that and see what i come up with \$\endgroup\$ – DidIReallyWriteThat Feb 17 '16 at 15:56
1
\$\begingroup\$

It depends on what you really needs. But if you you use relationship logic, the following statements may explain.

  1. A character wields weapon.

  2. A character attack using weapon or unarmed.

  3. The damage formula differ based on the weapon wield. Swords depends on strength and agility, bows depends on skills etc.

So, basically you have to make something like this :

Solution(s) for case number 1:


  1. The GameCharacter class has an attribute weapon which type is Weapon class.

Solution(s) for case number 2:


  1. The GameCharacter has the method attack(Target t).
  2. The attack method check the character weapon attribute, if the weapon is not present, use unarmed attack.

Solution(s) for case number 3:


  1. So many weapons type but, all of them are weapons anyway. Make the Weapon class abstract, add the abstract method getRawAttackValue() method. Swords, bows, staves etc. are sub-classes.

  2. The damage depends on the wielder status. So, the alter the getRawAttackValue() method into getRawAttackValue(GameCharacter wielder).


The above statements may change depends on the variables added into the formula. Like, for example, if you add the attribute elementalType in the Weapon class. So, the conclusion is, you have to design the classes using paper and pencil first. The methods with empty implementations then write the required codes in the method body later.

These are the code samples, it's just a sample though, I use java syntax :

//the classes are written in different files

public class PlayableCharacter implements Target{
    Weapon weapon;
    Armor armor;
    short strength, endurance, agility, skill, intelligence, spirit;

    public void attack(Target t){
        Random r = new Random();
        int damage;
        if(Weapon!=null)
            damage = weapon.getAttackingPower(this);
        else
            damage = this.strength * (r.nextInt(this.strength)/r.nextInt(this.strength) * 2;
        //determine whether the attack is critical hit or not
        if(r.nextInt(this.spirit)>r.nextInt(t.getSpirit())){
            damage*=2;
        }
        t.getDamage(damage);
    }
}

public abstract class Weapon{
    private short physicalAttack;
    private short magicalAttack;

    Weapon(short p, short m){
        physicalAttack = p;
        magicalAttack = m;
    }

    public short getPhysicalAttack(){
        return physicalAttack;
    }

    public short getMagicalAttack(){
        return magicalAttack;
    }

    public abstract int getAttackingPower(PlayableCharacter c);
}

public class Sword extends Weapon{
    @Override
    public int getAttackingPower(PlayableCharacter c){
        return physicalAttack + physicalAttack/9999 * c.getStrength() * + (physicalAttack + physicalAttack/9999 * c.getStrength()) * c.getAgility() * 0.6;
    }
}

public class Bow extends Weapon{
    @Override
    public int getAttackingPower(PlayableCharacter c){
        return physicalAttack + physicalAttack/9999 * (c.getSkill() + c.getSkill * c.getStrength/999);
    }
}

public class Staff extends Weapon{
    @Override
    public int getAttackingPower(PlayableCharacter c){
        return physicalAttack + physicalAttack/9999 * (c.getMagicalAttack() * 1.2 + c.getPhysicalAttack);
    }
}

I hope my comment will help.

\$\endgroup\$
0
\$\begingroup\$

Your question has lots of parts to it.

Here's a few terms I would add to your vocabulary based on your question:

AOE - Area of Effect - Actions that impact an area rather than a specific target

https://en.wikipedia.org/wiki/Glossary_of_video_game_terms#Area_of_effect

Ranged Weapons - Any weapon that impacts a user at a distance further than hand-to-hand combat (often via an attack NOT connected to the attacker, like a gun or laser or spell)

https://en.wikipedia.org/wiki/Ranged_weapon

Melee Weapons - Any combat item (including body parts) used in close hand-to-hand combat.

https://en.wikipedia.org/wiki/Melee

There are a ton of other relevant terms, but I didn't want to muddy up the answer with other things you weren't asking about.

In the case of a game I created, the TARGET character was a LISTENER, and WEAPONS broadcasted a POTENTIAL impact. The LISTENER actually calculated how much the aggregate impact was based on the totality of the stats and situation. In your model, it looks like you're trying to do the opposite (You're letting the ATTACKER decide all of the damage. I suspect you'll end up shifting your design to more of what I describe as you flush out your concept)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.