-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemy.java
100 lines (88 loc) · 2.7 KB
/
Enemy.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
// Enemy.java
import java.util.Random;
import ansi_terminal.*;
import java.io.PrintWriter;
import java.util.Scanner;
/** Creates an enemy object that can walk around the room
*
*/
public class Enemy extends Character {
private String name;
private int damage;
private int protection;
private static Random rng;
private boolean battleActive;
public Enemy(String name, int row, int col, int hp, int damage, int protection) {
super(row, col, '*', Color.RED, hp);
this.name = name;
this.damage = damage;
this.protection = protection;
this.battleActive = false;
rng = new Random();
}
@Override
public int getDamage() {
return damage;
}
@Override
public int getProtection() {
return protection;
}
@Override
public String getName() {
return name;
}
public void setBattleActive() {
battleActive = true;
}
// randomly move the enemy in the room
public void walk(Room room, Room2 room2, Room3 room3, Room4 room4) {
// if a battle is active with this enemy, they DON'T walk right after
if (battleActive) {
battleActive = false;
return;
}
// loop forever until we move correctly
while (true) {
int choice = rng.nextInt(4);
switch (choice) {
case 0:
if (move(0, 1, room, room2, room3, room4)) return;
break;
case 1:
if (move(0, -1, room, room2, room3, room4)) return;
break;
case 2:
if (move(1, 0, room, room2, room3, room4)) return;
break;
case 3:
if (move(-1, 0, room, room2, room3, room4)) return;
break;
}
}
}
/** Writes the enemy's Entity and Character data, along with its name, strength, and defense to the save file
*
* @param out the printwriter used to write data to a file
*/
public void save(PrintWriter out) {
super.save(out);
out.println(name);
out.println(damage);
out.println(protection);
}
/** A constructor used for reading in the enemy's Entity and Character data, along with its name, strength, and
* defense from the save file
*
* @param in the scanner used to read in data from the file
*/
public Enemy(Scanner in) {
super(in);
name = in.nextLine();
damage = in.nextInt();
// added to read the rest of the line
in.nextLine();
protection = in.nextInt();
in.nextLine();
}
}