-
Notifications
You must be signed in to change notification settings - Fork 0
/
AreaManager.java
134 lines (113 loc) · 3.47 KB
/
AreaManager.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
package cs383.team1.model.overworld;
import java.util.List;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import cs383.team1.model.overworld.Area;
import cs383.team1.model.overworld.DemoEntity;
import cs383.team1.model.overworld.Entity;
import cs383.team1.model.overworld.Field;
import cs383.team1.model.overworld.Position;
import cs383.team1.model.overworld.Player;
import cs383.team1.model.overworld.Tile;
import cs383.team1.model.overworld.Wall;
public final class AreaManager {
public static final AreaManager instance = new AreaManager();
public List<Area> areas;
public Area current;
private AreaManager() {
if(instance != null) {
Gdx.app.error("AreaManager:AreaManager",
"reinstantiating singleton AreaManager");
throw new IllegalStateException("reinstantiating singleton");
}
Gdx.app.debug("AreaManager:AreaManager", "instantiating class");
areas = new ArrayList<Area>();
current = null;
}
public Area loadArea(String fname) {
boolean passable;
int i;
int x;
int y;
int type;
int hp;
int mp;
int ap;
int numEntities;
int numTiles;
int offset;
Area a;
String fcontents;
Position pos;
Player player;
String[] vals;
FileHandle fin;
List<Entity> entities;
List<Tile> tiles;
offset = 0;
entities = new ArrayList<Entity>();
tiles = new ArrayList<Tile>();
Gdx.app.log("AreaManager:loadArea", "Loading " + fname);
fin = Gdx.files.internal(fname);
fcontents = fin.readString();
vals = fcontents.trim().split("\\s+");
Gdx.app.debug("AreaManager:loadArea", "Loading tiles");
numTiles = Integer.parseInt(vals[offset++]);
for(i = offset; i < offset + (numTiles * 3); i += 3) {
x = Integer.parseInt(vals[i + 0]);
y = Integer.parseInt(vals[i + 1]);
type = Integer.parseInt(vals[i + 2]);
pos = new Position(x, y);
switch(type) {
case 1:
Gdx.app.debug("AreaManager:loadArea",
"Loading Field (" + vals[i] + "," + vals[i + 1] + ")");
tiles.add(new Field(pos));
break;
case 2:
Gdx.app.debug("AreaManager:loadArea",
"Loading Wall (" + vals[i] + "," + vals[i + 1] + ")");
tiles.add(new Wall(pos));
break;
default:
Gdx.app.error("AreaManager:loadArea",
"invalid tile type " + vals[i + 2]);
}
}
offset += numTiles * 3;
Gdx.app.debug("AreaManager:loadArea", "Loading entities");
numEntities = Integer.parseInt(vals[offset++]);
for(i = offset; i < offset + (numEntities * 3); i += 3) {
x = Integer.parseInt(vals[i + 0]);
y = Integer.parseInt(vals[i + 1]);
type = Integer.parseInt(vals[i + 2]);
pos = new Position(x, y);
switch(type) {
case DemoEntity.TYPE:
Gdx.app.debug("AreaManager:loadArea", "Loading DemoEntity");
entities.add(new DemoEntity(pos));
break;
case NPC.TYPE:
Gdx.app.debug("AreaManager:loadArea", "Loading NPC");
entities.add(new NPC(pos));
break;
default:
Gdx.app.error("AreaManager:loadArea",
"invalid entity type " + vals[i + 2]);
}
}
offset += numEntities * 3;
Gdx.app.debug("AreaManager:loadArea", "Loading Player");
x = Integer.parseInt(vals[offset++]);
y = Integer.parseInt(vals[offset++]);
hp = Integer.parseInt(vals[offset++]);
mp = Integer.parseInt(vals[offset++]);
ap = Integer.parseInt(vals[offset++]);
pos = new Position(x, y);
player = new Player(pos, hp, mp, ap);
a = new Area(tiles, entities, player);
areas.add(a);
return a;
}
}