diff --git a/src/mightypork/rogue/Launcher.java b/src/mightypork/rogue/Launcher.java index 96b148a..c0087b7 100644 --- a/src/mightypork/rogue/Launcher.java +++ b/src/mightypork/rogue/Launcher.java @@ -5,36 +5,7 @@ import java.io.File; import java.util.logging.Level; import mightypork.gamecore.core.App; -import mightypork.gamecore.core.DeltaMainLoop; -import mightypork.gamecore.core.MainLoop; -import mightypork.gamecore.core.init.InitTaskCrashHandler; -import mightypork.gamecore.core.init.InitTaskCustom; -import mightypork.gamecore.core.init.InitTaskDisplay; -import mightypork.gamecore.core.init.InitTaskIonizables; -import mightypork.gamecore.core.init.InitTaskLog; -import mightypork.gamecore.core.init.InitTaskLogHeader; -import mightypork.gamecore.core.init.InitTaskMainLoop; -import mightypork.gamecore.core.init.InitTaskResourceLoaderAsync; -import mightypork.gamecore.core.init.InitTaskScreens; -import mightypork.gamecore.core.plugins.screenshot.InitTaskPluginScreenshot; -import mightypork.gamecore.core.plugins.screenshot.ScreenshotRequest; -import mightypork.gamecore.graphics.FullscreenToggleRequest; -import mightypork.gamecore.graphics.Renderable; -import mightypork.gamecore.gui.screens.ScreenRegistry; -import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay; -import mightypork.gamecore.input.Trigger; -import mightypork.rogue.screens.FpsOverlay; -import mightypork.rogue.screens.LoadingOverlay; -import mightypork.rogue.screens.game.ScreenGame; -import mightypork.rogue.screens.menu.ScreenMainMenu; -import mightypork.rogue.screens.select_world.ScreenSelectWorld; -import mightypork.rogue.screens.story.ScreenStory; -import mightypork.rogue.world.Inventory; -import mightypork.rogue.world.WorldProvider; -import mightypork.utils.eventbus.BusEvent; import mightypork.utils.files.OsUtils; -import mightypork.utils.ion.Ion; -import mightypork.utils.logging.Log; public class Launcher { @@ -49,7 +20,6 @@ public class Launcher { // System.out.println("argv = " + Arrays.toString(args)+"\n"); Level llSyso = Level.FINER; - final Level llFile = Level.ALL; File workdir = null; boolean logBus = false; @@ -106,146 +76,10 @@ public class Launcher { System.exit(1); } - final App app = new RogueApp(); + final App app = new RogueApp(workdir, llSyso); - App.bus().detailedLogging = true; + if (logBus) App.bus().detailedLogging = true; - app.addInitTask(new RogueInitWorkdir(workdir)); - app.addInitTask(new RogueInitConfig()); - - app.addInitTask(new InitTaskResourceLoaderAsync()); - app.addInitTask(new RogueInitResources()); - - app.addInitTask(new InitTaskCrashHandler()); - app.addInitTask(new InitTaskPluginScreenshot("screenshots")); - - app.addInitTask(new InitTaskIonizables() { - - @Override - public void after() - { - Ion.register(mightypork.rogue.world.level.Level.class); - Ion.register(Inventory.class); - } - }); - - - final Level logLevelWrite = llFile, logLevelPrint = llSyso; - - app.addInitTask(new InitTaskLog() { - - @Override - public void init() - { - setArchiveCount(5); - setLevels(logLevelWrite, logLevelPrint); - setLogDir("logs"); - setLogName("runtime"); - } - }); - - app.addInitTask(new InitTaskLogHeader() { - - @Override - public void before() - { - Log.i("## Starting Rogue v." + Const.VERSION + " ##"); - } - }); - - - app.addInitTask(new InitTaskDisplay() { - - @Override - public void init() - { - final int w = App.cfg().getValue("display.width"); - final int h = App.cfg().getValue("display.height"); - final boolean fs = App.cfg().getValue("display.fullscreen"); - - setSize(w, h); - setResizable(true); - setFullscreen(fs); - setTitle(Const.TITLEBAR); - setTargetFps(Const.FPS_RENDER); - } - }); - - app.addInitTask(new InitTaskCustom("global_keys", new String[] { "config" }) { - - @Override - public void run() - { - bindEventToKey(new FullscreenToggleRequest(), "global.fullscreen"); - bindEventToKey(new ScreenshotRequest(), "global.screenshot"); - - final Runnable quitTask = new Runnable() { - - @Override - public void run() - { - App.shutdown(); - } - }; - - App.input().bindKey(App.cfg().getKeyStroke("global.quit"), Trigger.RISING, quitTask); - App.input().bindKey(App.cfg().getKeyStroke("global.quit_force"), Trigger.RISING, quitTask); - } - - - private void bindEventToKey(final BusEvent event, String strokeName) - { - App.input().bindKey(App.cfg().getKeyStroke(strokeName), Trigger.RISING, new Runnable() { - - @Override - public void run() - { - App.bus().send(event); - } - }); - } - - }); - - - app.addInitTask(new InitTaskMainLoop() { - - @Override - protected MainLoop getLoopImpl() - { - final MainLoop loop = new DeltaMainLoop(); - return loop; - } - }); - - - app.addInitTask(new InitTaskScreens() { - - @Override - protected Renderable getMainRenderableImpl() - { - final ScreenRegistry screens = new ScreenRegistry(); - app.addChildClient(screens); - - /* game screen references world provider instance */ - App.bus().subscribe(new RogueStateManager()); - App.bus().subscribe(WorldProvider.get()); - - screens.addOverlay(new CrossfadeOverlay()); - - screens.addScreen("main_menu", new ScreenMainMenu()); - screens.addScreen("select_world", new ScreenSelectWorld()); - screens.addScreen("game", new ScreenGame()); - screens.addScreen("story", new ScreenStory()); - - screens.addOverlay(new FpsOverlay()); - screens.addOverlay(new LoadingOverlay()); - - return screens; - } - }); - - app.start(); } diff --git a/src/mightypork/rogue/RogueApp.java b/src/mightypork/rogue/RogueApp.java index 6254a48..60dcfd3 100644 --- a/src/mightypork/rogue/RogueApp.java +++ b/src/mightypork/rogue/RogueApp.java @@ -1,15 +1,36 @@ package mightypork.rogue; +import java.io.File; +import java.util.logging.Level; + import mightypork.gamecore.backends.lwjgl.LwjglBackend; import mightypork.gamecore.core.App; +import mightypork.gamecore.core.DeltaMainLoop; +import mightypork.gamecore.core.MainLoop; import mightypork.gamecore.core.events.MainLoopRequest; +import mightypork.gamecore.core.init.InitTaskCrashHandler; +import mightypork.gamecore.core.init.InitTaskIonizables; +import mightypork.gamecore.core.init.InitTaskLog; +import mightypork.gamecore.core.init.InitTaskLogHeader; +import mightypork.gamecore.core.init.InitTaskMainLoop; +import mightypork.gamecore.core.init.InitTaskResourceLoaderAsync; +import mightypork.gamecore.core.plugins.screenshot.InitTaskPluginScreenshot; import mightypork.gamecore.core.plugins.screenshot.ScreenshotRequestListener; import mightypork.gamecore.gui.events.ViewportChangeEvent; import mightypork.gamecore.gui.events.ViewportChangeListener; import mightypork.gamecore.resources.Res; import mightypork.rogue.RogueStateManager.RogueState; import mightypork.rogue.events.RogueStateRequest; +import mightypork.rogue.init.AddResources; +import mightypork.rogue.init.InitScreens; +import mightypork.rogue.init.SetupConfig; +import mightypork.rogue.init.SetupDisplay; +import mightypork.rogue.init.SetupGlobalKeys; +import mightypork.rogue.init.SetupWorkdir; +import mightypork.rogue.world.Inventory; +import mightypork.utils.ion.Ion; +import mightypork.utils.logging.Log; /** @@ -18,25 +39,80 @@ import mightypork.rogue.events.RogueStateRequest; * @author Ondřej Hruška (MightyPork) */ public final class RogueApp extends App implements ViewportChangeListener, ScreenshotRequestListener { - - public RogueApp() + + public RogueApp(File workdir, final Level logStdout) { super(new LwjglBackend()); + + addInitTask(new InitTaskLog() { + + @Override + public void init() + { + setArchiveCount(5); + setLevels(Level.ALL, logStdout); + setLogDir("logs"); + setLogName("runtime"); + } + }); + + addInitTask(new InitTaskLogHeader() { + + @Override + public void before() + { + Log.i("## Starting Rogue v." + Const.VERSION + " ##"); + } + }); + + addInitTask(new InitTaskCrashHandler()); + addInitTask(new SetupWorkdir(workdir)); + + addInitTask(new SetupConfig()); + + addInitTask(new InitTaskPluginScreenshot("screenshots")); + + addInitTask(new InitTaskIonizables() { + + @Override + public void after() + { + Ion.register(mightypork.rogue.world.level.Level.class); + Ion.register(Inventory.class); + } + }); + + + addInitTask(new SetupDisplay()); + addInitTask(new SetupGlobalKeys()); + addInitTask(new InitTaskResourceLoaderAsync()); + addInitTask(new AddResources()); + + addInitTask(new InitTaskMainLoop() { + + @Override + protected MainLoop getLoopImpl() + { + return new DeltaMainLoop(); + } + }); + + addInitTask(new InitScreens()); } - - + + @Override public void onScreenshotRequest() { Res.sound("gui.shutter").play(0.8); } - - + + @Override protected void postInit() { bus().send(new MainLoopRequest(new Runnable() { - + @Override public void run() { @@ -49,16 +125,16 @@ public final class RogueApp extends App implements ViewportChangeListener, Scree } }, false)); } - - + + @Override public void onViewportChanged(ViewportChangeEvent event) { // save viewport size to config file final boolean fs = gfx().isFullscreen(); - + cfg().setValue("display.fullscreen", fs); - + if (!fs) { cfg().setValue("display.width", gfx().getWidth()); cfg().setValue("display.height", gfx().getHeight()); diff --git a/src/mightypork/rogue/RogueInitResources.java b/src/mightypork/rogue/init/AddResources.java similarity index 98% rename from src/mightypork/rogue/RogueInitResources.java rename to src/mightypork/rogue/init/AddResources.java index 12e28b2..b1c04d4 100644 --- a/src/mightypork/rogue/RogueInitResources.java +++ b/src/mightypork/rogue/init/AddResources.java @@ -1,4 +1,4 @@ -package mightypork.rogue; +package mightypork.rogue.init; import mightypork.gamecore.audio.SoundRegistry; @@ -15,7 +15,7 @@ import mightypork.gamecore.graphics.textures.WrapMode; import mightypork.utils.math.constraints.rect.Rect; -public class RogueInitResources extends InitTaskResources { +public class AddResources extends InitTaskResources { @Override public void addFonts(FontRegistry fonts) diff --git a/src/mightypork/rogue/init/InitScreens.java b/src/mightypork/rogue/init/InitScreens.java new file mode 100644 index 0000000..d7e2df9 --- /dev/null +++ b/src/mightypork/rogue/init/InitScreens.java @@ -0,0 +1,43 @@ +package mightypork.rogue.init; + + +import mightypork.gamecore.core.App; +import mightypork.gamecore.core.init.InitTaskScreens; +import mightypork.gamecore.graphics.Renderable; +import mightypork.gamecore.gui.screens.ScreenRegistry; +import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay; +import mightypork.rogue.RogueStateManager; +import mightypork.rogue.screens.FpsOverlay; +import mightypork.rogue.screens.LoadingOverlay; +import mightypork.rogue.screens.game.ScreenGame; +import mightypork.rogue.screens.menu.ScreenMainMenu; +import mightypork.rogue.screens.select_world.ScreenSelectWorld; +import mightypork.rogue.screens.story.ScreenStory; +import mightypork.rogue.world.WorldProvider; + + +public class InitScreens extends InitTaskScreens { + + @Override + protected Renderable getMainRenderableImpl() + { + final ScreenRegistry screens = new ScreenRegistry(); + app.addChildClient(screens); + + /* game screen references world provider instance */ + App.bus().subscribe(new RogueStateManager()); + App.bus().subscribe(WorldProvider.get()); + + screens.addOverlay(new CrossfadeOverlay()); + + screens.addScreen("main_menu", new ScreenMainMenu()); + screens.addScreen("select_world", new ScreenSelectWorld()); + screens.addScreen("game", new ScreenGame()); + screens.addScreen("story", new ScreenStory()); + + screens.addOverlay(new FpsOverlay()); + screens.addOverlay(new LoadingOverlay()); + + return screens; + } +} diff --git a/src/mightypork/rogue/RogueInitConfig.java b/src/mightypork/rogue/init/SetupConfig.java similarity index 97% rename from src/mightypork/rogue/RogueInitConfig.java rename to src/mightypork/rogue/init/SetupConfig.java index 90715bf..595ffdf 100644 --- a/src/mightypork/rogue/RogueInitConfig.java +++ b/src/mightypork/rogue/init/SetupConfig.java @@ -1,11 +1,11 @@ -package mightypork.rogue; +package mightypork.rogue.init; import mightypork.gamecore.core.config.Config; import mightypork.gamecore.core.config.InitTaskConfig; -public class RogueInitConfig extends InitTaskConfig { +public class SetupConfig extends InitTaskConfig { @Override protected Config buildConfig() diff --git a/src/mightypork/rogue/init/SetupDisplay.java b/src/mightypork/rogue/init/SetupDisplay.java new file mode 100644 index 0000000..9e267a4 --- /dev/null +++ b/src/mightypork/rogue/init/SetupDisplay.java @@ -0,0 +1,25 @@ +package mightypork.rogue.init; + + +import mightypork.gamecore.core.App; +import mightypork.gamecore.core.init.InitTaskDisplay; +import mightypork.rogue.Const; + + +public class SetupDisplay extends InitTaskDisplay { + + @Override + public void init() + { + final int w = App.cfg().getValue("display.width"); + final int h = App.cfg().getValue("display.height"); + final boolean fs = App.cfg().getValue("display.fullscreen"); + + setSize(w, h); + setResizable(true); + setFullscreen(fs); + setTitle(Const.TITLEBAR); + setTargetFps(Const.FPS_RENDER); + } + +} diff --git a/src/mightypork/rogue/init/SetupGlobalKeys.java b/src/mightypork/rogue/init/SetupGlobalKeys.java new file mode 100644 index 0000000..383f277 --- /dev/null +++ b/src/mightypork/rogue/init/SetupGlobalKeys.java @@ -0,0 +1,60 @@ +package mightypork.rogue.init; + + +import mightypork.gamecore.core.App; +import mightypork.gamecore.core.init.InitTask; +import mightypork.gamecore.core.plugins.screenshot.ScreenshotRequest; +import mightypork.gamecore.graphics.FullscreenToggleRequest; +import mightypork.gamecore.input.Trigger; +import mightypork.utils.eventbus.BusEvent; + + +public class SetupGlobalKeys extends InitTask { + + + @Override + public void run() + { + bindEventToKey(new FullscreenToggleRequest(), "global.fullscreen"); + bindEventToKey(new ScreenshotRequest(), "global.screenshot"); + + final Runnable quitTask = new Runnable() { + + @Override + public void run() + { + App.shutdown(); + } + }; + + App.input().bindKey(App.cfg().getKeyStroke("global.quit"), Trigger.RISING, quitTask); + App.input().bindKey(App.cfg().getKeyStroke("global.quit_force"), Trigger.RISING, quitTask); + } + + + private void bindEventToKey(final BusEvent event, String strokeName) + { + App.input().bindKey(App.cfg().getKeyStroke(strokeName), Trigger.RISING, new Runnable() { + + @Override + public void run() + { + App.bus().send(event); + } + }); + } + + + @Override + public String getName() + { + return "global_keys"; + } + + + @Override + public String[] getDependencies() + { + return new String[] { "config" }; + } +} diff --git a/src/mightypork/rogue/RogueInitWorkdir.java b/src/mightypork/rogue/init/SetupWorkdir.java similarity index 69% rename from src/mightypork/rogue/RogueInitWorkdir.java rename to src/mightypork/rogue/init/SetupWorkdir.java index 08aacf0..85a85b5 100644 --- a/src/mightypork/rogue/RogueInitWorkdir.java +++ b/src/mightypork/rogue/init/SetupWorkdir.java @@ -1,4 +1,4 @@ -package mightypork.rogue; +package mightypork.rogue.init; import java.io.File; @@ -6,9 +6,9 @@ import java.io.File; import mightypork.gamecore.core.init.InitTaskWorkdir; -public class RogueInitWorkdir extends InitTaskWorkdir { +public class SetupWorkdir extends InitTaskWorkdir { - public RogueInitWorkdir(File workdir) + public SetupWorkdir(File workdir) { super(workdir, true); } diff --git a/src/mightypork/rogue/screens/RogueScreen.java b/src/mightypork/rogue/screens/RogueScreen.java index c2d0601..aa9d581 100644 --- a/src/mightypork/rogue/screens/RogueScreen.java +++ b/src/mightypork/rogue/screens/RogueScreen.java @@ -11,13 +11,13 @@ import mightypork.utils.annotations.Stub; public class RogueScreen extends LayeredScreen implements ShutdownListener { - - + + @Override @Stub public void onShutdown(ShutdownEvent event) { App.bus().send(new RogueStateRequest(RogueState.EXIT)); - event.consume(); + //event.consume(); FIXME we need a "shutdown request" event AND "shutdown" event. } }