Question
I have 2 classes but because they share similar code I want to make useForce extend from Attack how to do that? Attack.java package starwars.actions;
I have 2 classes but because they share similar code I want to make useForce extend from Attack how to do that?
Attack.java
package starwars.actions;
import edu.monash.fit2099.simulator.userInterface.MessageRenderer; import starwars.Capability; import starwars.SWActionInterface; import starwars.SWActor; import starwars.SWAffordance; import starwars.SWEntityInterface; import starwars.entities.actors.Droid;
/** * Command to attack entities. * * This affordance is attached to all attackable entities * * @author David.Squire@monash.edu (dsquire) */ /* * Change log * 2017/02/03 Fixed the bug where the an actor could attack another actor in the same team (asel) * 2017/02/08 Attack given a priority of 1 in constructor (asel) */ public class Attack extends SWAffordance implements SWActionInterface {
/** * Constructor for the Attack
class. Will initialize the messageRenderer
and * give Attack
a priority of 1 (lowest priority is 0). * * @param theTarget the target being attacked * @param m message renderer to display messages */ public Attack(SWEntityInterface theTarget, MessageRenderer m) { super(theTarget, m); priority = 1; }
/** * Returns the time is takes to perform this Attack
action. * * @return The duration of the Attack action. Currently hard coded to return 1. */ @Override public int getDuration() { return 1; }
/** * A String describing what this Attack
action will do, suitable for display on a user interface * * @return String comprising "attack " and the short description of the target of this Affordance
*/ @Override public String getDescription() { return "attack " + this.target.getShortDescription(); }
/** * Determine whether a particular SWActor a
can attack the target. * * @author dsquire * @param a the SWActor
being queried * @return true any SWActor
can always try an attack, it just won't do much * good unless this SWActor a
has a suitable weapon. */ @Override public boolean canDo(SWActor a) { SWEntityInterface target = this.getTarget(); return !a.isDead() && target.getHitpoints()>0; }
/** * Perform the Attack
command on an entity. *
* This method does not perform any damage (an attack) if, *
- *
- The target of the
Attack
and theSWActor a
are in the sameTeam
* - The
SWActor a
is holding an item without theWEAPON Affordance
*
* else it would damage the entity attacked, tires the attacker, and blunts any weapon used for the attack. * * TODO : check if the weapon has enough hitpoints and the attacker has enough energy before an attack. * * @author dsquire - adapted from the equivalent class in the old Eiffel version * @author Asel - bug fixes. * @param a the SWActor
who is attacking * @pre this method should only be called if the SWActor a
is alive * @pre an Attack
must not be performed on a dead SWActor
* @post if a SWActor
dies in an Attack
their Attack
affordance would be removed * @see starwars.SWActor#isDead() * @see starwars.Team */ @Override public void act(SWActor a) { SWEntityInterface target = this.getTarget(); boolean targetIsActor = target instanceof SWActor; SWActor targetActor = null; int energyForAttackWithWeapon = 1;//the amount of energy required to attack with a weapon if (targetIsActor) { targetActor = (SWActor) target; } if (targetIsActor && (a.getTeam() == targetActor.getTeam())) { //don't attack SWActors in the same team a.say("\t" + a.getShortDescription() + " says: Silly me! We're on the same team, " + target.getShortDescription() + ". No harm done"); } else if (a.isHumanControlled() // a human-controlled player can attack anyone || (targetIsActor && (a.getTeam() != targetActor.getTeam()))) { // others will only attack actors on different teams a.say(a.getShortDescription() + " is attacking " + target.getShortDescription() + "!"); SWEntityInterface itemCarried = a.getItemCarried(); if (itemCarried != null) {//if the actor is carrying an item if (itemCarried.hasCapability(Capability.WEAPON)) { target.takeDamage(itemCarried.getHitpoints() + 1); // blunt weapon won't do much, but it will still do some damage itemCarried.takeDamage(1); // weapon gets blunt a.takeDamage(energyForAttackWithWeapon); // actor uses energy to attack } else {//an attack with a none weapon if (targetIsActor) { targetActor.say("\t" + targetActor.getShortDescription() + " is amused by " + a.getShortDescription() + "'s attempted attack with " + itemCarried.getShortDescription()); } } } else { // attack with bare hands target.takeDamage((a.getHitpoints()/20) + 1); // a bare-handed attack doesn't do much damage. a.takeDamage(2*energyForAttackWithWeapon); // actor uses energy. It's twice as tiring as using a weapon } //After the attack //display if the a
is dead after the attack if (a.isDead()) { a.setLongDescription(a.getLongDescription() + ", that died of exhaustion while attacking someone"); //remove the attack affordance of the dead actor so it can no longer be attacked a.removeAffordance(this); }
//display if the a
is not a droid and has negative hitpoints else if (target.getClass() != Droid.class && target.getHitpoints() <= 0) { target.setLongDescription(target.getLongDescription() + ", that was killed in a fight"); //remove the attack affordance of the dead actor so it can no longer be attacked targetActor.removeAffordance(this); }
//display if the a
is a droid and its hitpoint is below the damage threshold else if (target.getClass() == Droid.class && target.getHitpoints() <= ((Droid)target).getDamageThreshold()){ targetActor.removeAffordance(this); target.setLongDescription(target.getLongDescription() + ", that was destroyed"); }
//display if the a
is a droid and hitpoints is inbetween threshold and 0 else if (target.getClass() == Droid.class && target.getHitpoints() <= 0) { target.setLongDescription(target.getLongDescription() + ", that was disabled"); } } // not game player and different teams } }
useForce.java
package starwars.actions;
import edu.monash.fit2099.simulator.userInterface.MessageRenderer; import starwars.SWActionInterface; import starwars.SWActor; import starwars.SWAffordance; import starwars.SWEntityInterface;
public class UseForce extends Attack { public static final int minUsePoints=50;
public UseForce(SWEntityInterface theTarget, MessageRenderer m) { super(theTarget, m); priority = 1; } @Override public String getDescription() { return "Use The Force on " + this.target.getShortDescription(); }
@Override public boolean canDo(SWActor a) { return a.getForcepoints()>=minUsePoints; } /** * Returns the time is takes to perform this Force
action. * * @return The duration of the Force action. Currently hard coded to return 1. */
@Override public void act(SWActor a) { SWEntityInterface target = this.getTarget(); boolean targetIsActor = target instanceof SWActor; SWActor targetActor = null; int energyForForceAttack = 2;//the amount of energy required to use force if (targetIsActor) { targetActor = (SWActor) target; } if (targetIsActor && (a.getTeam() == targetActor.getTeam())) { //don't attack SWActors in the same team a.say("\t" + a.getShortDescription() + " says: Silly me! We're on the same team, " + target.getShortDescription() + ". No harm done"); } else if (a.isHumanControlled() // a human-controlled player can attack anyone || (targetIsActor && (a.getTeam() != targetActor.getTeam()))) { // others will only attack actors on different teams a.say(a.getShortDescription() + " is attacking " + target.getShortDescription() + "!"); SWEntityInterface itemCarried = a.getItemCarried(); if (itemCarried == null) { //if the actor is not carrying an item a.takeDamage(energyForForceAttack); // actor uses energy to attack with Force(last resort) target.takeDamage((a.getForcepoints()/10) + 1);//If actor has a lot of force points, it is much greater than bare handed attack } //After the attack if (a.isDead()) {//the actor who attacked is dead after the attack a.setLongDescription(a.getLongDescription() + ", that died of exhaustion while attacking someone"); //remove the use force affordance of the dead actor so it can no longer be attacked a.removeAffordance(this); } if (this.getTarget().getHitpoints() <= 0) { // can't use isDead(), as we don't know that the target is an actor target.setLongDescription(target.getLongDescription() + ", that was killed in a fight"); targetActor.removeAffordance(this);
} } } // not game player and different teams
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started