-
Notifications
You must be signed in to change notification settings - Fork 11
/
SceneManager.java
221 lines (188 loc) · 5.28 KB
/
SceneManager.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import greenfoot.*;
import java.util.*;
/**
* This class manages the game scenes
*
* @author Tony Alexander Hild
* @version 0.1a
*/
public final class SceneManager
{
public static final int EDGE = 5;
/**
* Entity buffer. This buffer may be in Game class?
*/
private static Map<String, Entity> entities = new HashMap<String, Entity>();
private static Map<String, Scene> scenes = new HashMap<String, Scene>();
/**
* Current world.
*/
private static World currentWorld;
/**
* Action world;
*/
private static ActionWorld actionWorld;
/**
* Current active hero.
*/
private static PlayerCharacter currentPc;
/**
* Current active scene.
*/
private static Scene currentScene;
/**
* Singleton object
*/
private final static SceneManager instance = new SceneManager();
/**
* Constructor for objects of class SceneManager
*/
private SceneManager() {
}
public static SceneManager getInstance() {
return instance;
}
public void update() {
for(Map.Entry<String, Entity> entry : entities.entrySet()) {
if(currentScene.getEntities().contains(entry.getValue())) return;
entry.getValue().act();
}
}
/**
* Add a entity to the game
*
* @param entity to be added to the game
*/
public void addEntity(Entity entity) {
entities.put(entity.getName(), entity);
}
/**
* Gets an entity from the game by a name
*
* @param name The entity's name
* @return Returns a entity
*/
public <T extends Entity> T getEntity(String name) {
return (T)entities.get(name);
}
/**
* Set the current game world
*
* @param world the world to be set
*/
public void setCurrentWorld(World world) {
currentWorld = world;
Greenfoot.setWorld(world);
}
/**
* Gets the current game world.
*
* @return A World object.
*/
public World getCurrentWorld() {
return currentWorld;
}
/**
* Gets the current action world.
*
* @return A ActionWorld object.
*/
public ActionWorld getActionWorld() {
return actionWorld;
}
/**
* Sets the current action world.
*
*/
public void setActionWorld(ActionWorld world) {
currentWorld = world;
actionWorld = world;
}
public void resumeToActionWorld() {
currentWorld = actionWorld;
if(Game.isRunning()) {
SoundManager.getInstance().playSound(getScene().getSoundToPlay(), true);
}
Greenfoot.setWorld(actionWorld);
}
/**
* Gets the current game player character.
*
* @return A PlayerCharacter object.
*/
public PlayerCharacter getPc() {
return currentPc;
}
/**
* Sets the current active player character.
*
* @param playerCharacter The player character to be set.
*/
public void setPc(PlayerCharacter pc) {
currentPc = pc;
}
public void setScene(Scene scene) {
if (!scenes.containsKey(scene.getName())) {
scenes.put(scene.getName(), scene);
}
if(currentScene != null) {
currentScene.setEntities(getAllEntities());
for (Entity e : currentScene.getEntities()) {
e.reset();
}
}
currentScene = scene;
clearWorld();
for(Entity e : currentScene.getEntities())
{
currentWorld.addObject(e,
e.getX(),
e.getY());
}
currentWorld.setBackground(scene.getBackground());
}
public void setScene(Direction direction) {
Scene scene = currentScene.getNeighbourScene(direction);
if(scene != null) {
switch(direction) {
case NORTH:
currentPc.setY(currentWorld.getHeight() - EDGE);
break;
case SOUTH:
currentPc.setY(EDGE);
break;
case WEST:
currentPc.setX(currentWorld.getWidth() - EDGE);
break;
case EAST:
currentPc.setX(EDGE);
break;
}
setScene(scene);
}
}
public void addScene(Scene scene) {
scenes.put(scene.getName(), scene);
if(currentWorld != null) {
scene.setWidth(currentWorld.getWidth());
scene.setHeight(currentWorld.getHeight());
}
}
public Scene getScene() {
return currentScene;
}
public void playMusic() {
SoundManager.getInstance().playSound(getScene().getSoundToPlay(), true);
}
public void pauseMusic() {
SoundManager.getInstance().pauseSound(getScene().getSoundToPlay());
}
private void clearWorld() {
if(currentWorld == actionWorld) {
currentWorld.removeObjects(currentWorld.getObjects(null));
}
}
private List<Entity> getAllEntities() {
return (List<Entity>)currentWorld.getObjects(Entity.class);
}
}