forked from Jleetrahan/bork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttackCommand.java
87 lines (82 loc) · 1.73 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
83
84
85
86
87
/**
*
*/
/** AttackCommand deals with any Command relating to the attacking of an NPC or a player.
* @author Team Red
*
*/
class AttackCommand extends Command{
private Item weapon;
private boolean weaponProvided;
private NPC victim;
private boolean victimProvided;
private boolean victimIsPlayer;
/** Instantiates an AttackCommand using the parameter weapon
*
* @param weapon Weapon that is being used to attack
* @param victim NPC that is being attacked, "player" implies that the player is being attacked
*/
AttackCommand(String weapon, String victim)
{
GameState state = GameState.instance();
if(weapon != null)
{
try{
this.weapon = state.getItemFromInventoryNamed(weapon);
}catch(Item.NoItemException e){
this.weapon = null;
}
weaponProvided = true;
}
else
{
weaponProvided = false;
}
if(victim == null)
{
victimProvided = false;
}
else if(!victim.equals("player"))
{
try{
this.victim = state.getNPCInVicinityNamed(victim);
}catch(NPC.NoNPCException e){
this.victim = null;
}
victimIsPlayer = false;
victimProvided = true;
}
else
{
victimIsPlayer = true;
victimProvided = true;
}
}
/**
* @override From Command
*/
String execute()
{
if(!weaponProvided && !victimProvided)
{
return "What do you want to attack, and with what?\n";
}
if(!weaponProvided && victim != null)
{
return "Attack "+ victim +" with what?\n";
}
if(weapon == null)
{
return "You are not carrying that item.\n";
}
if(!weapon.getIsAWeapon())
{
return "You cannot attack with that.\n";
}
if(victim == null && !victimIsPlayer)
{
return "You can't attack what isn't there!\n";
}
return Combat.attack(weapon, victim);
}
}