-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathSolApplication.java
More file actions
434 lines (356 loc) · 15.4 KB
/
SolApplication.java
File metadata and controls
434 lines (356 loc) · 15.4 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* Copyright 2018 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.destinationsol;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2D;
import org.destinationsol.assets.AssetHelper;
import org.destinationsol.assets.Assets;
import org.destinationsol.assets.music.OggMusicManager;
import org.destinationsol.assets.sound.OggSoundManager;
import org.destinationsol.asteroids.components.AsteroidMesh;
import org.destinationsol.body.components.BodyLinked;
import org.destinationsol.common.SolColor;
import org.destinationsol.common.SolMath;
import org.destinationsol.common.SolRandom;
import org.destinationsol.game.DebugOptions;
import org.destinationsol.game.ObjectManager;
import org.destinationsol.game.SaveManager;
import org.destinationsol.game.SolGame;
import org.destinationsol.game.SolNames;
import org.destinationsol.game.WorldConfig;
import org.destinationsol.game.drawables.DrawableLevel;
import org.destinationsol.health.components.Health;
import org.destinationsol.location.components.Angle;
import org.destinationsol.location.components.Velocity;
import org.destinationsol.material.MaterialType;
import org.destinationsol.material.components.Material;
import org.destinationsol.moneyDropping.components.DropsMoneyOnDestruction;
import org.destinationsol.rendering.RenderableElement;
import org.destinationsol.rendering.components.Renderable;
import org.destinationsol.rendering.events.RenderEvent;
import org.destinationsol.entitysystem.ComponentSystemManager;
import org.destinationsol.entitysystem.EntitySystemManager;
import org.destinationsol.entitysystem.SerialisationManager;
import org.destinationsol.game.SolCam;
import org.destinationsol.game.console.adapter.ParameterAdapterManager;
import org.destinationsol.game.context.Context;
import org.destinationsol.game.context.internal.ContextImpl;
import org.destinationsol.game.drawables.DrawableManager;
import org.destinationsol.game.item.ItemManager;
import org.destinationsol.game.item.LootBuilder;
import org.destinationsol.location.components.Position;
import org.destinationsol.menu.MenuScreens;
import org.destinationsol.menu.background.MenuBackgroundManager;
import org.destinationsol.modules.ModuleManager;
import org.destinationsol.rubble.components.CreatesRubbleOnDestruction;
import org.destinationsol.size.components.Size;
import org.destinationsol.ui.DebugCollector;
import org.destinationsol.ui.DisplayDimensions;
import org.destinationsol.ui.FontSize;
import org.destinationsol.ui.ResizeSubscriber;
import org.destinationsol.ui.SolInputManager;
import org.destinationsol.ui.SolLayouts;
import org.destinationsol.ui.UiDrawer;
import org.destinationsol.ui.nui.NUIManager;
import org.destinationsol.util.FramerateLimiter;
import org.destinationsol.util.InjectionHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.entitysystem.component.Component;
import org.terasology.gestalt.entitysystem.component.management.ComponentManager;
import org.terasology.gestalt.entitysystem.entity.EntityRef;
import org.terasology.gestalt.module.sandbox.API;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
@API
public class SolApplication implements ApplicationListener {
private static final Logger logger = LoggerFactory.getLogger(SolApplication.class);
private final float targetFPS;
@SuppressWarnings("FieldCanBeLocal")
private final ModuleManager moduleManager;
private EntitySystemManager entitySystemManager;
private OggMusicManager musicManager;
private OggSoundManager soundManager;
private SolInputManager inputManager;
private MenuBackgroundManager menuBackgroundManager;
private UiDrawer uiDrawer;
private FactionDisplay factionDisplay;
private MenuScreens menuScreens;
private SolLayouts layouts;
private GameOptions options;
private CommonDrawer commonDrawer;
private String fatalErrorMsg;
private String fatalErrorTrace;
private SolGame solGame;
private ParameterAdapterManager parameterAdapterManager;
private NUIManager nuiManager;
private Context context;
// TODO: Make this non-static.
public static DisplayDimensions displayDimensions;
private float timeAccumulator = 0;
private boolean isMobile;
private ComponentManager componentManager;
// TODO: Make this non-static.
private static Set<ResizeSubscriber> resizeSubscribers;
public SolApplication(ModuleManager moduleManager, float targetFPS) {
// Initiate Box2D to make sure natives are loaded early enough
Box2D.init();
this.moduleManager = moduleManager;
this.targetFPS = targetFPS;
resizeSubscribers = new HashSet<>();
}
@Override
public void create() {
context = new ContextImpl();
context.put(SolApplication.class, this);
context.put(ModuleManager.class, moduleManager);
isMobile = Gdx.app.getType() == Application.ApplicationType.Android || Gdx.app.getType() == Application.ApplicationType.iOS;
if (isMobile) {
DebugOptions.read(null);
}
options = new GameOptions(isMobile(), null);
componentManager = new ComponentManager();
AssetHelper helper = new AssetHelper();
helper.init(moduleManager.getEnvironment(), componentManager, isMobile);
Assets.initialize(helper);
context.put(ComponentSystemManager.class, new ComponentSystemManager(moduleManager.getEnvironment(), context));
logger.info("\n\n ------------------------------------------------------------ \n");
moduleManager.printAvailableModules();
displayDimensions = new DisplayDimensions(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
commonDrawer = new CommonDrawer();
uiDrawer = new UiDrawer(commonDrawer);
layouts = new SolLayouts();
SolCam camera = new SolCam();
context.put(SolCam.class, camera);
musicManager = new OggMusicManager(options);
soundManager = new OggSoundManager(context);
inputManager = new SolInputManager(soundManager, context);
musicManager.playMusic(OggMusicManager.MENU_MUSIC_SET, options);
menuBackgroundManager = new MenuBackgroundManager(displayDimensions);
menuScreens = new MenuScreens(layouts, isMobile(), options);
inputManager.setScreen(this, menuScreens.main);
parameterAdapterManager = ParameterAdapterManager.createCore(this);
nuiManager = new NUIManager(this, context, commonDrawer, options);
}
@Override
public void resize(int newWidth, int newHeight) {
displayDimensions.set(newWidth, newHeight);
for (ResizeSubscriber resizeSubscriber : resizeSubscribers) {
resizeSubscriber.resize();
}
}
public void render() {
timeAccumulator += Gdx.graphics.getDeltaTime();
while (timeAccumulator > Const.REAL_TIME_STEP) {
safeUpdate();
timeAccumulator -= Const.REAL_TIME_STEP;
}
FramerateLimiter.synchronizeFPS(Math.round(targetFPS));
try {
draw();
} catch (Throwable t) {
logger.error("Fatal Error:", t);
fatalErrorMsg = "A fatal error occurred:\n" + t.getMessage();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
fatalErrorTrace = sw.toString();
if (!isMobile) {
throw t;
}
}
}
@Override
public void pause() {
}
@Override
public void resume() {
}
private void safeUpdate() {
if (fatalErrorMsg != null) {
return;
}
try {
update();
} catch (Throwable t) {
logger.error("Fatal Error:", t);
fatalErrorMsg = "A fatal error occurred:\n" + t.getMessage();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
fatalErrorTrace = sw.toString();
if (!isMobile) {
throw t;
}
}
}
private void update() {
DebugCollector.update();
if (DebugOptions.SHOW_FPS) {
DebugCollector.debug("Fps", Gdx.graphics.getFramesPerSecond());
}
inputManager.update(this);
nuiManager.update(this);
if (solGame != null) {
solGame.update();
}
SolMath.checkVectorsTaken(null);
}
//TODO remove this line - it is for debugging purposes
private boolean entityCreated = false;
private void draw() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
commonDrawer.begin();
if (solGame != null) {
context.get(DrawableManager.class).draw(solGame, context);
//This event causes each entity with a `Renderable` component to be rendered onscreen
entitySystemManager.sendEvent(new RenderEvent(), new Renderable(), new Position());
//TODO remove this block - it is for debugging purposes
if (!entityCreated) {
Size size = new Size();
size.size = 1;
RenderableElement element = new RenderableElement();
element.texture = SolRandom.randomElement(Assets.listTexturesMatching("engine:asteroid_.*"));
element.relativePosition = new Vector2();
element.drawableLevel = DrawableLevel.BODIES;
element.tint = Color.YELLOW;
element.setSize(size.size);
element.graphicsOffset = new Vector2();
Renderable graphicsComponent = new Renderable();
graphicsComponent.elements.add(element);
Position position = new Position();
position.position = solGame.getHero().getShip().getPosition().cpy();
position.position.y += 1;
Health health = new Health();
health.currentHealth = 1;
Material material = new Material();
material.materialType = MaterialType.ROCK;
EntityRef entityRef = entitySystemManager.getEntityManager().createEntity(graphicsComponent, position, size,
new Angle(), new Velocity(), new AsteroidMesh(), health, new DropsMoneyOnDestruction(),
new CreatesRubbleOnDestruction(), material);
entityRef.setComponent(new BodyLinked());
entityCreated = true;
}
}
uiDrawer.updateMtx();
inputManager.draw(uiDrawer, this);
nuiManager.draw(commonDrawer);
inputManager.drawCursor(uiDrawer);
if (solGame != null) {
solGame.drawDebugUi(uiDrawer);
factionDisplay.drawFactionNames(solGame, uiDrawer, inputManager, solGame.getObjectManager());
}
if (fatalErrorMsg != null) {
uiDrawer.draw(uiDrawer.whiteTexture, displayDimensions.getRatio(), .5f, 0, 0, 0, .25f, 0, SolColor.UI_BG);
uiDrawer.drawString(fatalErrorMsg, displayDimensions.getRatio(), .5f, FontSize.MENU, true, SolColor.WHITE);
uiDrawer.drawString(fatalErrorTrace, .2f * displayDimensions.getRatio(), .6f, FontSize.DEBUG, false, SolColor.WHITE);
}
DebugCollector.draw(uiDrawer);
if (solGame == null) {
uiDrawer.drawString("v" + Const.VERSION, 0.01f, .974f, FontSize.DEBUG, UiDrawer.TextAlignment.LEFT, false, SolColor.WHITE);
}
commonDrawer.end();
}
public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig worldConfig) {
context.get(ComponentSystemManager.class).preBegin();
solGame = new SolGame(shipName, tut, isNewGame, commonDrawer, context, worldConfig);
context.put(SolGame.class, solGame);
context.put(LootBuilder.class, solGame.getLootBuilder());
context.put(ItemManager.class, solGame.getItemMan());
context.put(ObjectManager.class, solGame.getObjectManager());
entitySystemManager = new EntitySystemManager(moduleManager.getEnvironment(), componentManager, context);
InjectionHelper.inject(solGame.getContactListener(), context);
solGame.createUpdateSystems(context);
solGame.startGame(shipName, isNewGame, worldConfig, new SolNames(), entitySystemManager);
// Big, fat, ugly HACK to get a working classloader
// Serialisation and thus a classloader is not needed when there are no components
Iterator<Class<? extends Component>> componentClasses =
moduleManager.getEnvironment().getSubtypesOf(Component.class).iterator();
SerialisationManager serialisationManager = new SerialisationManager(
SaveManager.getResourcePath("entity_store.dat"), entitySystemManager.getEntityManager(),
componentClasses.hasNext() ? componentClasses.next().getClassLoader() : null);
context.put(SerialisationManager.class, serialisationManager);
if (!isNewGame) {
try {
context.get(SerialisationManager.class).deserialise();
} catch (Exception e) {
e.printStackTrace();
}
}
factionDisplay = new FactionDisplay(context.get(SolCam.class));
inputManager.setScreen(this, solGame.getScreens().mainGameScreen);
}
public SolInputManager getInputManager() {
return inputManager;
}
public MenuScreens getMenuScreens() {
return menuScreens;
}
public void dispose() {
commonDrawer.dispose();
if (solGame != null) {
solGame.onGameEnd(context);
}
inputManager.dispose();
}
public SolGame getGame() {
return solGame;
}
public ParameterAdapterManager getParameterAdapterManager() {
return parameterAdapterManager;
}
public SolLayouts getLayouts() {
return layouts;
}
public void finishGame() {
solGame.onGameEnd(context);
solGame = null;
inputManager.setScreen(this, menuScreens.main);
}
public boolean isMobile() {
return DebugOptions.EMULATE_MOBILE || isMobile;
}
public GameOptions getOptions() {
return options;
}
public OggMusicManager getMusicManager() {
return musicManager;
}
public OggSoundManager getSoundManager() {
return soundManager;
}
public MenuBackgroundManager getMenuBackgroundManager() {
return menuBackgroundManager;
}
public Context getContext() {
return context;
}
public NUIManager getNuiManager() {
return nuiManager;
}
// TODO: Make this non-static.
public static void addResizeSubscriber(ResizeSubscriber resizeSubscriber) {
resizeSubscribers.add(resizeSubscriber);
}
}