-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntity.java
146 lines (131 loc) · 4.57 KB
/
Entity.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
135
136
137
138
139
140
141
142
143
144
145
146
import ansi_terminal.*;
import java.io.PrintWriter;
import java.util.Scanner;
/** Represents a drawable object(Box, Enemy, Player) on the map
*
*/
public class Entity {
// the location of the entity in space
private Position position;
// the character used to draw it
private char display;
// the color used for drawing
private Color color;
// adding the current map/room integer
private int whatMap;
public Entity(int row, int col, char display, Color color) {
position = new Position(row, col);
this.display = display;
this.color = color;
}
public void setPosition(int row, int col) {
position = new Position(row, col);
}
public Position getPosition() {
return position;
}
public int getRow() {
return position.getRow();
}
public int getCol() {
return position.getCol();
}
// translate the entity in space, unless it would hit a wall
public boolean move(int rowChange, int colChange, Room room, Room2 room2, Room3 room3, Room4 room4) {
// find new position
whatMap = World.instance().getRoom();
int newRow = position.getRow() + rowChange;
int newCol = position.getCol() + colChange;
if (whatMap == 1){
if (room.canGo(newRow, newCol)) {
// draw a space where it currently is
Terminal.warpCursor(position.getRow(), position.getCol());
System.out.print(" ");
// eliminating flash
Terminal.reset();
// and then move it
position = new Position(newRow, newCol);
return true;
} else {
return false;
}
} else if (whatMap == 2) {
if (room2.canGo(newRow, newCol)) {
// draw a space where it currently is
Terminal.warpCursor(position.getRow(), position.getCol());
System.out.print(" ");
//eliminating flash
Terminal.reset();
// and then move it
position = new Position(newRow, newCol);
return true;
} else {
return false;
}
} else if (whatMap == 3) {
if (room3.canGo(newRow, newCol)) {
// draw a space where it currently is
Terminal.warpCursor(position.getRow(), position.getCol());
System.out.print(" ");
//eliminating flash
Terminal.reset();
// and then move it
position = new Position(newRow, newCol);
return true;
} else {
return false;
}
} else if (whatMap == 4) {
if (room4.canGo(newRow, newCol)) {
// draw a space where it currently is
Terminal.warpCursor(position.getRow(), position.getCol());
System.out.print(" ");
//eliminating flash
Terminal.reset();
// and then move it
position = new Position(newRow, newCol);
return true;
} else {
return false;
}
}
return false;
}
public void draw() {
Terminal.warpCursor(position.getRow(), position.getCol());
Terminal.setForeground(color);
System.out.print(display);
Terminal.reset();
}
/** Writes the entity's position, display, and color to the save file
*
* @param out the printwriter used to write data to a file
*/
public void save(PrintWriter out) {
out.println("Entity");
position.save(out);
out.println(display);
out.println(color);
}
/** A constructor used for reading in data about the entity's position, display, and color from the save file
*
* @param in the scanner used to read in data from the file
*/
public Entity(Scanner in) {
//skips line in file that says Entity
in.nextLine();
position = new Position(in);
display = in.nextLine().charAt(0);
String c = in.nextLine();
switch (c) {
case "RED" -> color = Color.RED;
case "CYAN" -> color = Color.CYAN;
case "YELLOW" -> color = Color.YELLOW;
case "GREEN" -> color = Color.GREEN;
case "MAGENTA" -> color = Color.MAGENTA;
case "BLUE" -> color = Color.BLUE;
case "BLACK" -> color = Color.BLACK;
case "WHITE" -> color = Color.WHITE;
}
}
}