Niminim Niminim - 1 year ago 88
Java Question

Java - Access objects

I have a question regarding objects. I develop some kind of a basic RPG. In this game I have a character (let's say this Character object is hero). hero has equipment according to primary hand, body armor etc. This hero wants to attack with the weapon wielded in his primary hand, let's say he has a long sword.

A word regard my Weapon class - the objects in this class have "attack" method. This method gets as an arguments the attacker and the target, so (weapon objects here).attack(hero, hero2) makes hero attack using the weapon object to attack hero2.

My question is as follows - can I avoid using attack(hero, hero2) somehow while the code knows that "hero" is the first objects that calls the attack method ?

That's the attack command:

hero.getEquipment().getPrimaryHand().attack(hero, hero2);

can "some code here" be replaced with something that can replace "hero" in the attack method ?

hero.getEquipment().getPrimaryHand().attack(some code here, hero2);

Edit: Addition of MeleeWeapon Class (subclass of weapon):

public class MeleeWeapon extends Weapon {

boolean throwable;

MeleeWeapon(String name,String reqTraining, boolean oneHaned, int n, int dice, int attackBonus, int damageBonus,double weight, long cost, boolean throwable) {
super(name, reqTraining, weight, cost, oneHaned, n, dice, attackBonus, damageBonus);
this.throwable = throwable;

static ArrayList<MeleeWeapon> meleeWeaponList = new ArrayList<MeleeWeapon>();

meleeWeaponList.add(new MeleeWeapon("Long Sword","Martial", true, 1, 8, 0, 0,8, 10, false));
meleeWeaponList.add(new MeleeWeapon("Short Sword", "Martial", true, 1, 6, 0, 0,5, 5, false));
meleeWeaponList.add(new MeleeWeapon("Dagger","Basic", true, 1, 4, 0, 0,2, 3, true));
meleeWeaponList.add(new MeleeWeapon("Quarter-staff", "Basic", false, 1, 4, 0, 0,3, 2, false));
meleeWeaponList.add(new MeleeWeapon("Shield", "Shield", false, 1, 4, 0, 0,8, 8, false));


public void attack(Character attacker, Character defender){

int attackRoll = DiceRoller.roll(20) + attacker.getBaseAttackBonus() + attacker.getModifier(attacker.getStrength()) + getAttackBonus() ;
System.out.println(attacker.getName() + " attack Roll: " + attackRoll + "AC: " + defender.getArmorClass());

if (attackRoll >= defender.getArmorClass()){
System.out.println("Defender: " + defender.getName() + " had " + defender.getCurrentHp());
int damage = DiceRoller.roll(getN(), getDice()) + attacker.getModifier(attacker.getStrength()) + getDamageBonus() ;
System.out.println("Damage : " + damage);
defender.setCurrentHp(attacker.getCurrentHp() - damage);
System.out.println("Defender: " + defender.getName() + " has " + defender.getCurrentHp());
} else {
System.out.println("Missed Attack");


Answer Source

You can have an attack method in your Character class that would wrap your other attack method :

public void attack (Character other) {
    getEquipment().getPrimaryHand().attack (this, other);

And you call it simply :

hero.attack (otherHero);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download