-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacter.java
134 lines (126 loc) · 3.35 KB
/
Character.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Iterator;
import java.util.Scanner;
/**
* Abstract class Character - fields that all characters have,
* methods to override CharactersDo methods that don't vary between types of
* characters (move and attack). Includes accessors and mutators for the
* benefit of child classes.
*
* @author Ari
* @version 2019-12-19
*/
public abstract class Character implements CharactersDo
{
private String character;
private int attack;
private int location;
private int health;
private int maxHealth;
private Scanner reader = new Scanner(System.in);
/**
* Constructor for objects of class Character
*/
public Character(String character, int attack, int maxHealth)
{
this.character = character;
this.attack = attack;
this.maxHealth = maxHealth;
health = maxHealth;
}
/**
* Take input and either move to that direction or attack that direction
*
* @param input the number in which direction to move/attack
*/
@Override
public void move(int input)
{
int oldLocation = location;
int direction = input;
int newLocation = Main.map.move(character, oldLocation, direction);
if(!(newLocation == oldLocation) &&
(Main.map.getCharacter(newLocation).equals(".") ||
Main.map.getCharacter(newLocation).equals(character))) {
location = newLocation;
} else {
attack(newLocation);
}
}
/**
* Lower health of Character being attacked. Trigger death if health<=0
*
* @param location the location to be attacked
*/
@Override
public void attack(int location)
{
Iterator it = Main.characters.iterator();
boolean found = false;
while(!found && it.hasNext()) {
//Downcasting found from https://www.leepoint.net/data/collections/iterators.html
Character character = (Character)it.next();
if(character.getLocation() == location) {
character.setHealth(character.getHealth() - attack);
System.out.println("Ouch!");
if(character.getHealth() <= 0) {
character.die();
}
found = true;
}
}
}
/**
* @return the character
*/
public String getCharacter() {
return character;
}
/**
* @param character the character to set
*/
public void setCharacter(String character) {
this.character = character;
}
/**
* @return the location
*/
public int getLocation() {
return location;
}
/**
* @param location the location to set
*/
public void setLocation(int location) {
this.location = location;
}
/**
* @return the health
*/
public int getHealth() {
return health;
}
/**
* @param health the health to set
*/
public void setHealth(int health) {
this.health = health;
}
/**
* @return the maxHealth
*/
public int getMaxHealth() {
return maxHealth;
}
/**
* @param maxHealth the maxHealth to set
*/
public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}
/**
* @return the reader
*/
public Scanner getReader() {
return reader;
}
}