This repository was archived by the owner on Nov 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadState.java
More file actions
174 lines (167 loc) · 6.57 KB
/
LoadState.java
File metadata and controls
174 lines (167 loc) · 6.57 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.*;
import java.io.*;
import java.awt.*;
/**
* State that loads the game map
*/
public class LoadState extends State {
int id;
Player[] players;
Map map;
Loader loader;
String text;
LoadState(Keyboard keyboard, Mouse mouse, Messenger messenger) {
super(keyboard, mouse, messenger);
}
public void setup(Object[] args) {
super.setup();
this.id = (int)args[0];
this.players = (Player[])args[1];
this.map = null;
this.loader = null;
}
public void message(String messageText) {
String[] message = messageText.split(" ");
String command = message[0];
String[] args = Arrays.copyOfRange(message, 1, message.length);
switch (command) {
case "AGENT":
this.agent(args);
break;
case "NAME":
this.name(args);
break;
case "MAP":
this.map(args);
break;
case "START":
this.start(args);
break;
}
}
public void type(char key) {
}
public void click(Mouse.Click click) {
}
public void draw(Graphics g) {
if (this.map != null) {
Text.draw(g, 20, map.getName(), 100, 100);
if (this.map.getRooms() != null) {
Text.draw(g, 20, this.text, 100, 200);
}
} else {
Text.draw(g, 20, "Awaiting server", 100, 100);
}
}
public void close() {
super.close();
this.setNextArgs(new Object[] {
this.id,
this.players,
this.map
});
}
/* Server-Client commands */
private void agent(String[] args) {
this.players[Integer.valueOf(args[0])].setAgent(Agent.valueOf(args[1]));
}
private void name(String[] args) {
String fullName = "";
for (String word: Arrays.copyOfRange(args, 1, args.length)) {
fullName = fullName + " " + word;
}
fullName = fullName.trim();
this.players[Integer.valueOf(args[0])].setName(fullName);
}
private void map(String[] args) {
this.map = new Map();
this.loader = new Loader(args[0]);
new Thread(this.loader).start();
}
private void start(String[] args) {
this.close();
}
private class Loader implements Runnable {
BufferedReader input;
Loader(String mapName) {
try {
this.input = new BufferedReader(new FileReader("assets/Maps/" + mapName + ".txt"));
} catch (Exception e) {
System.out.println("Error accessing map file");
}
}
public void run() {
// Read the map name
map.setName(this.nextLine());
// Read the room count
int roomCount = Integer.valueOf(this.nextLine());
map.setRoomCount(roomCount);
text = "Loading rooms";
// Read the spawn rooms
int defenderSpawn = Integer.valueOf(this.nextLine());
int attackerSpawn = Integer.valueOf(this.nextLine());
// Read the plant sites
HashSet<Integer> bombRooms = new HashSet<Integer>();
for (String roomId: this.nextLine().split(" ")) {
bombRooms.add(Integer.valueOf(roomId));
}
// Load each room
for (int roomId = 0; roomId < roomCount; roomId++) {
Room room;
if (roomId == defenderSpawn || roomId == attackerSpawn) {
room = new SpawnRoom();
} else if (bombRooms.contains(roomId)) {
room = new BombRoom();
} else {
room = new Room();
}
map.addRoom(room);
// Read the room colour
String[] rgb = this.nextLine().split(" ");
room.setColor(new Color(Integer.valueOf(rgb[0]), Integer.valueOf(rgb[1]), Integer.valueOf(rgb[2])));
// Read the obstacle colour
rgb = this.nextLine().split(" ");
room.setObstacleColor(new Color(Integer.valueOf(rgb[0]), Integer.valueOf(rgb[1]), Integer.valueOf(rgb[2])));
// Read the penetrable obstacle colour
rgb = this.nextLine().split(" ");
room.setPenetrableObstacleColor(new Color(Integer.valueOf(rgb[0]), Integer.valueOf(rgb[1]), Integer.valueOf(rgb[2])));
// Read the room width
room.setWidth(Integer.valueOf(this.nextLine()));
// Read the room height
room.setHeight(Integer.valueOf(this.nextLine()));
// Read the obstacle count
int obstacleCount = Integer.valueOf(this.nextLine());
room.setObstacleCount(obstacleCount);
for (int obstacleId = 0; obstacleId < obstacleCount; obstacleId++) {
// Read obstacle details
String[] args = this.nextLine().split(" ");
room.addObstacle(new Obstacle(Integer.valueOf(args[0]), Integer.valueOf(args[1]), Integer.valueOf(args[2]), Integer.valueOf(args[3]), args[4].equals("1")));
}
// Read the door count
int doorCount = Integer.valueOf(this.nextLine());
room.setDoorCount(doorCount);
for (int doorId = 0; doorId < doorCount; doorId++) {
// Read door details
String[] args = this.nextLine().split(" ");
room.addDoor(new Door(Integer.valueOf(args[0]), Integer.valueOf(args[1]), Integer.valueOf(args[2]), Integer.valueOf(args[3]), Integer.valueOf(args[4]), Integer.valueOf(args[5])));
}
if (roomId == defenderSpawn || roomId == attackerSpawn) {
// Read the player spawns
String[] spawns = this.nextLine().split(" ");
((SpawnRoom)room).setSpawnX(new int[] {Integer.valueOf(spawns[0]), Integer.valueOf(spawns[2]), Integer.valueOf(spawns[4])});
((SpawnRoom)room).setSpawnY(new int[] {Integer.valueOf(spawns[1]), Integer.valueOf(spawns[3]), Integer.valueOf(spawns[5])});
}
}
messenger.print("LOADED");
}
private String nextLine() {
try {
if (this.input.ready()) {};
return this.input.readLine();
} catch (Exception e) {
System.out.println("Error reading map file");
}
return null;
}
}
}