-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttackCommand.java
82 lines (71 loc) · 2.83 KB
/
AttackCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package bork;
/**
* Handles the command to attack something/one else
* @author Woodruff & Christopher Ringham
*/
class AttackCommand extends Command {
private String weaponName;
private String targetName;
/**
* Constructor for the weapon the player uses and the target of the attack
* @param weaponName the name of the weapon the player is attacking with
* @param targetName the name of the target the player is attacking
*/
AttackCommand(String weaponName, String targetName)
{
this.weaponName = weaponName;
this.targetName = targetName;
}
/**
* Returns the String based on the calculated combat exchange
* @return String with lines of combat and messages about the round
*/
@Override
public String execute()
{
try
{
//Player attack phase
int playerScore = GameState.instance().getScore();
int weaponDamage = 0;
Item itemReferredTo = GameState.instance().getItemFromInventoryNamed(weaponName);
if(itemReferredTo instanceof Weapon)
{
Weapon wepReferredTo = (Weapon)itemReferredTo;
weaponDamage = wepReferredTo.getDamage(); //if it is a weapon
} else {
weaponDamage = 1; //if it is not a weapon
}
NPC targetNPC = GameState.instance().getNpcInVicinityNamed(targetName);
boolean willDie = weaponDamage+(playerScore/2) < targetNPC.getHealth();
targetNPC.wound(weaponDamage+(playerScore/2));
if(willDie) //Make sure the NPC isn't dead
{
//NPC attack phase
int NPCScore = targetNPC.getScore();
weaponDamage = targetNPC.getWeapon().getDamage();
GameState.instance().wound(weaponDamage+(NPCScore/2));
}
else
{
String dieString = "The " + targetNPC.getName() + " has been slain.\n";
targetNPC.die();
return dieString;
}
} catch (Item.NoItemException ex) {
return "You do not have the " + this.weaponName + " to attack with.";
} catch (NPC.NoNpcException e) {
return this.targetName + " is not in the room.\n";
} catch (Weapon.NoWeaponException x)
{
try
{
NPC targetNPC = GameState.instance().getNpcInVicinityNamed(targetName);
GameState.instance().wound(1+(targetNPC.getScore()/2));
} catch (NPC.NoNpcException e) {
return this.targetName + " is not in the room.\n";
}
}
return "You've exchanged blows!\n";
}
}