-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteToFile.java
99 lines (89 loc) · 2.13 KB
/
WriteToFile.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
/*
* File: WriteToFile.java
* Author: Luke S. Snyder
* UA ID: 010691128
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class WriteToFile
{
String fileName;
private ArrayList<Beam> beams;
private ArrayList<Missle> missles;
private ArrayList<Enemy> enemies;
private Boss boss;
private Player player;
FileOutputStream fos;
ObjectOutputStream oos;
public WriteToFile(String fileName, ArrayList<Beam> beams, ArrayList<Missle> missles,
ArrayList<Enemy> enemies, Boss boss, Player player)
{
this.fileName = fileName;
this.beams = beams;
this.missles = missles;
this.enemies = enemies;
this.boss = boss;
this.player = player;
createFileStreams();
}
// Try making new file streams.
public void createFileStreams()
{
try
{
fos = new FileOutputStream(fileName + ".ser");
oos = new ObjectOutputStream(fos);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "ERROR: Could not create save file.");
}
}
// Save game to file.
public boolean start()
{
if (fos == null || oos == null)
return false;
try
{
// Write all relevant data to file.
oos.writeInt(Enemy.getEnemiesKilled());
oos.writeInt(SpawningThread.getTotalEnemies());
oos.writeObject(Game.getGameState());
oos.writeObject(SpawningThread.getBossState());
oos.writeObject(SpawningThread.getPhaseLocation());
oos.writeObject(Game.getPhase());
for (Beam beam : beams)
oos.writeObject((Sprite) beam);
for (Missle missle : missles)
oos.writeObject((Sprite) missle);
for (Enemy enemy : enemies)
oos.writeObject((Sprite) enemy);
oos.writeObject((Sprite) boss);
oos.writeObject((Sprite) player);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "ERROR: Exception thrown while writing to file.");
close();
return false;
}
close();
return true;
}
public void close()
{
try
{
fos.close();
oos.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "ERROR: Could not close file stream.");
}
}
}