-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileLevel.java
128 lines (112 loc) · 3.35 KB
/
FileLevel.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
/**
* This class represents a current level. It stores the dates of a character from a file called level.txt and each of
* them is represented on the screen. The time between characters is also measured.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.*;
import java.util.*;
public class FileLevel implements Runnable
{
private int number; // the number of the characters on the screen
private BufferedReader bf;
private char list[]; // the list of characters
/*
* This constructor initialise the file handler of the game. Otherwise, the class will receive precompute
* dates for a minimalise level.
*/
public FileLevel()
{
try
{
bf = new BufferedReader(new FileReader("level.txt"));
number = Integer.parseInt(bf.readLine());
readWholeFile();
}
catch (FileNotFoundException e){
System.out.println("doesn't work");
}
catch (IOException e)
{
System.out.println("doesn't work");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("doesn't work");
}
}
/*
* Getter method for the number of the character.
*/
public int getNumber()
{
return number;
}
private String createName()
{
String[] names = {"Austen","Carmelita","Cerys","Dariela","Dolly","Birch","Morrison","Redmond","Romulus",
"Socrates","Tiberius","Walden","Wilfred"};
Random srand = new Random();
int index = srand.nextInt(13);
return names[index];
}
/*
* This method overload the details about character from the GUI simulators.
*
* @param Character an object that it is shown on the screen (initially it is invisible).
* @return Character a character with modified properties
*/
public Element createElement(String[] strs)
{
Element ch;
switch (strs[0].charAt(0))
{
case 'M': ch = new Monster(createName());break;
case 'W': ch = new Warrior(createName());break;
default: ch = null; break;
}
ch.setX(Integer.parseInt(strs[1]));
ch.setY(Integer.parseInt(strs[2]));
try{
Monster m = (Monster) ch;
m.setAgility(Integer.parseInt(strs[4]));
m.setDamage(Integer.parseInt(strs[3]));
ch = m;
}
catch(IndexOutOfBoundsException e)
{
}
catch (ClassCastException e)
{
}
return ch;
}
public void run()
{
new FileLevel();
}
/*
*
*/
private void readWholeFile() throws IOException
{
int i = 0;
while (true)
{
String str = bf.readLine();
if (str == null)
break;
String[] strs = str.split(" ");
if (strs[0].charAt(0) == 'T')
Element.stopForWhile(Integer.parseInt(strs[1]));
else
{
Element ch = createElement(strs);
Show.bf.addElement(ch);
if (ch instanceof Character)
BattleField.startCharacterMovement((Character)ch);
}
}
}
}