Refactoring; improved quit event; Merged KeyConfig and Config

v5stable
ondra 11 years ago
parent c991523d0e
commit bf5c1a5b93
  1. 111
      src/mightypork/gamecore/Config.java
  2. 62
      src/mightypork/gamecore/core/BaseApp.java
  3. 3
      src/mightypork/gamecore/core/events/MainLoopRequest.java
  4. 7
      src/mightypork/gamecore/core/events/ShudownRequest.java
  5. 32
      src/mightypork/gamecore/core/events/UserQuitRequest.java
  6. 18
      src/mightypork/gamecore/core/events/UserQuitRequestListener.java
  7. 2
      src/mightypork/gamecore/gui/screens/impl/CrossfadeOverlay.java
  8. 5
      src/mightypork/gamecore/input/InputSystem.java
  9. 138
      src/mightypork/gamecore/input/KeyConfig.java
  10. 2
      src/mightypork/gamecore/resources/AsyncResourceLoader.java
  11. 4
      src/mightypork/rogue/Launcher.java
  12. 12
      src/mightypork/rogue/RogueApp.java
  13. 9
      src/mightypork/rogue/RogueKeys.java
  14. 25
      src/mightypork/rogue/screens/RogueScreen.java
  15. 14
      src/mightypork/rogue/screens/game/ScreenGame.java
  16. 59
      src/mightypork/rogue/screens/layout_testing/LayoutTestScreen.java
  17. 3
      src/mightypork/rogue/screens/menu/ScreenMainMenu.java
  18. 3
      src/mightypork/rogue/screens/select_world/ScreenSelectWorld.java
  19. 2
      src/mightypork/rogue/world/events/PlayerDeathHandler.java
  20. 4
      src/mightypork/rogue/world/events/PlayerKilledEvent.java
  21. 1
      src/mightypork/rogue/world/gen/ScratchMap.java
  22. 20
      src/mightypork/rogue/world/gen/WorldCreator.java

@ -2,7 +2,12 @@ package mightypork.gamecore;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Map;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys;
import mightypork.gamecore.util.files.config.Property;
import mightypork.gamecore.util.files.config.PropertyManager; import mightypork.gamecore.util.files.config.PropertyManager;
@ -21,6 +26,79 @@ public class Config {
void addOptions(PropertyManager prop); void addOptions(PropertyManager prop);
} }
/**
* Key configurator access
*/
public static class KeyOpts {
public void add(String cfgKey, String dataString)
{
KeyProperty kprop = new KeyProperty(prefixKey(cfgKey), KeyStroke.createFromDataString(dataString), null);
strokes.put(prefixKey(cfgKey), kprop);
cfg.putProperty(kprop);
}
}
/**
* Key configurator
*/
public static interface KeySetup {
public void addKeys(KeyOpts keys);
}
/**
* Key property.<br>
* The stored value must be invariant ({@link KeyStroke} is mutable).
*
* @author MightyPork
*/
public static class KeyProperty extends Property<KeyStroke> {
public KeyProperty(String key, KeyStroke defaultValue, String comment)
{
super(key, defaultValue, comment);
}
@Override
public KeyStroke decode(String string, KeyStroke defval)
{
if (string != null) {
// keep it invariant
final int backup_key = getValue().getKey();
final int backup_mod = getValue().getMod();
getValue().fromDataString(string);
if (getValue().getKey() == Keys.NONE) {
getValue().setTo(backup_key, backup_mod);
}
}
return getValue();
}
@Override
public String encode(KeyStroke value)
{
return value.toDataString();
}
@Override
public void setValue(Object value)
{
// keep it invariant
getValue().setTo(((KeyStroke) value).getKey(), ((KeyStroke) value).getMod());
}
}
public static Config.KeyOpts keyOpts = new Config.KeyOpts();
public static Map<String, Config.KeyProperty> strokes = new HashMap<>();
private static PropertyManager cfg; private static PropertyManager cfg;
@ -30,6 +108,13 @@ public class Config {
} }
public static void registerKeys(KeySetup layout)
{
layout.addKeys(keyOpts);
}
public static void registerOptions(ConfigSetup cfgl) public static void registerOptions(ConfigSetup cfgl)
{ {
cfgl.addOptions(cfg); cfgl.addOptions(cfg);
@ -48,15 +133,37 @@ public class Config {
} }
public static <T> T get(String key) public static <T> T getOption(String key)
{ {
return cfg.getValue(key); return cfg.getValue(key);
} }
public static <T> void set(String key, T value) public static <T> void setOption(String key, T value)
{ {
cfg.setValue(key, value); cfg.setValue(key, value);
} }
private static String prefixKey(String cfgKey)
{
return "key." + cfgKey;
}
public static KeyStroke getKey(String cfgKey)
{
final Config.KeyProperty kp = strokes.get(prefixKey(cfgKey));
if (kp == null) throw new IllegalArgumentException("No such stroke: " + cfgKey);
return kp.getValue();
}
public static void setKey(String cfgKey, int key, int mod)
{
final Config.KeyProperty kp = strokes.get(prefixKey(cfgKey));
if (kp == null) throw new IllegalArgumentException("No such stroke: " + cfgKey);
kp.getValue().setTo(key, mod);
}
} }

@ -11,6 +11,7 @@ import javax.swing.JOptionPane;
import mightypork.gamecore.Config; import mightypork.gamecore.Config;
import mightypork.gamecore.Config.ConfigSetup; import mightypork.gamecore.Config.ConfigSetup;
import mightypork.gamecore.Config.KeySetup;
import mightypork.gamecore.WorkDir; import mightypork.gamecore.WorkDir;
import mightypork.gamecore.WorkDir.RouteSetup; import mightypork.gamecore.WorkDir.RouteSetup;
import mightypork.gamecore.eventbus.EventBus; import mightypork.gamecore.eventbus.EventBus;
@ -18,8 +19,6 @@ import mightypork.gamecore.eventbus.events.DestroyEvent;
import mightypork.gamecore.gui.screens.ScreenRegistry; import mightypork.gamecore.gui.screens.ScreenRegistry;
import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay; import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay;
import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.input.KeyConfig;
import mightypork.gamecore.input.KeyConfig.KeySetup;
import mightypork.gamecore.logging.Log; import mightypork.gamecore.logging.Log;
import mightypork.gamecore.logging.SlickLogRedirector; import mightypork.gamecore.logging.SlickLogRedirector;
import mightypork.gamecore.logging.writers.LogWriter; import mightypork.gamecore.logging.writers.LogWriter;
@ -51,19 +50,25 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
private String logDir = "log"; private String logDir = "log";
private String logFilePrefix = "runtime"; private String logFilePrefix = "runtime";
private String screenshotDir = "screenshots"; private String screenshotDir = "screenshots";
private int logArchiveCount = 0; private int logArchiveCount = 0;
private boolean busLogging = false; private boolean busLogging = false;
private String configFile = "settings.cfg"; private String configFile = "settings.cfg";
private String configComment = "Main config file"; private String configComment = "Main config file";
public String lockFile = ".lock";
private final List<ResourceSetup> resourceLists = new ArrayList<>(); private final List<ResourceSetup> resourceLists = new ArrayList<>();
private final List<KeySetup> keyLists = new ArrayList<>(); private final List<Config.KeySetup> keyLists = new ArrayList<>();
private final List<ConfigSetup> configLists = new ArrayList<>(); private final List<ConfigSetup> configLists = new ArrayList<>();
private final List<RouteSetup> routeLists = new ArrayList<>(); private final List<RouteSetup> routeLists = new ArrayList<>();
private ResourceLoader resourceLoader = new AsyncResourceLoader(); private ResourceLoader resourceLoader = new AsyncResourceLoader();
private Level logLevel = Level.ALL; private Level logLevel = Level.ALL;
public boolean sigleInstance;
public void setConfigFile(BaseApp baseApp, String filename, String comment) public void setConfigFile(BaseApp baseApp, String filename, String comment)
@ -79,7 +84,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
} }
public void addKeys(KeySetup keys) public void addKeys(Config.KeySetup keys)
{ {
keyLists.add(keys); keyLists.add(keys);
} }
@ -122,6 +127,12 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
{ {
this.screenshotDir = path; this.screenshotDir = path;
} }
public void setLockFile(String lockFile)
{
this.lockFile = lockFile;
}
} }
// modules // modules
@ -132,6 +143,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
private MainLoop gameLoop; private MainLoop gameLoop;
private ScreenRegistry screenRegistry; private ScreenRegistry screenRegistry;
private boolean started = false; private boolean started = false;
private boolean lockObtained = false;
// init opt holder // init opt holder
private final AppInitOptions opt = new AppInitOptions(); private final AppInitOptions opt = new AppInitOptions();
@ -152,13 +164,12 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
} }
public BaseApp(File workdir, boolean singleInstance) public BaseApp(File workdir, boolean singleInstance) {
{
WorkDir.init(workdir); WorkDir.init(workdir);
Log.i("Using workdir: " + WorkDir.getWorkDir()); Log.i("Using workdir: " + WorkDir.getWorkDir());
if (singleInstance) initLock(); opt.sigleInstance = singleInstance;
} }
@ -184,10 +195,8 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
*/ */
protected void initialize() protected void initialize()
{ {
WorkDir.addPath("screenshots", opt.screenshotDir); if (opt.sigleInstance) initLock();
WorkDir.addPath("config", opt.configFile); lockObtained = true;
WorkDir.addPath("logs", opt.logDir);
for (final RouteSetup rs : opt.routeLists) { for (final RouteSetup rs : opt.routeLists) {
WorkDir.registerRoutes(rs); WorkDir.registerRoutes(rs);
@ -195,18 +204,18 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
// apply configurations // apply configurations
Config.init(WorkDir.getFile(opt.configFile), opt.configComment); Config.init(WorkDir.getFile(opt.configFile), opt.configComment);
// add keys to config
for (final KeySetup l : opt.keyLists) { for (final KeySetup l : opt.keyLists) {
KeyConfig.registerKeys(l); Config.registerKeys(l);
} }
// add keys to config // add options to config
Config.registerOptions(KeyConfig.inst()); for (final ConfigSetup c : opt.configLists) {
for (final ConfigSetup cfgl : opt.configLists) { Config.registerOptions(c);
Config.registerOptions(cfgl);
} }
Config.load(); Config.load();
/* /*
* Setup logging * Setup logging
*/ */
@ -215,15 +224,12 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
Log.setMainLogger(log); Log.setMainLogger(log);
org.newdawn.slick.util.Log.setLogSystem(new SlickLogRedirector(log)); org.newdawn.slick.util.Log.setLogSystem(new SlickLogRedirector(log));
Log.i("=== Starting initialization sequence ==="); Log.i("=== Starting initialization sequence ===");
// pre-init hook // pre-init hook
Log.f2("Calling pre-init hook..."); Log.f2("Calling pre-init hook...");
preInit(); preInit();
/* /*
* Event bus * Event bus
*/ */
@ -235,10 +241,9 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
/* /*
* Ionizables * Ionizables
*/ */
Log.f3("initializing ION..."); Log.f3("Initializing ION save system...");
registerIonizables(); registerIonizables();
/* /*
* Display * Display
*/ */
@ -246,7 +251,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
displaySystem = new DisplaySystem(this); displaySystem = new DisplaySystem(this);
initDisplay(displaySystem); initDisplay(displaySystem);
/* /*
* Audio * Audio
*/ */
@ -254,7 +258,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
soundSystem = new SoundSystem(this); soundSystem = new SoundSystem(this);
initSoundSystem(soundSystem); initSoundSystem(soundSystem);
/* /*
* Input * Input
*/ */
@ -262,7 +265,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
inputSystem = new InputSystem(this); inputSystem = new InputSystem(this);
initInputSystem(inputSystem); initInputSystem(inputSystem);
/* /*
* Prepare main loop * Prepare main loop
*/ */
@ -271,7 +273,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
gameLoop = createMainLoop(); gameLoop = createMainLoop();
gameLoop.setRootRenderable(screenRegistry); gameLoop.setRootRenderable(screenRegistry);
/* /*
* Load resources * Load resources
* *
@ -288,7 +289,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
Res.load(rl); Res.load(rl);
} }
/* /*
* Screen registry * Screen registry
* *
@ -381,7 +381,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
*/ */
private void initLock() private void initLock()
{ {
final File lock = WorkDir.getFile(".lock"); final File lock = WorkDir.getFile(opt.lockFile);
if (!InstanceLock.onFile(lock)) { if (!InstanceLock.onFile(lock)) {
onLockError(); onLockError();
return; return;
@ -399,6 +399,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
* Triggered when lock cannot be obtained.<br> * Triggered when lock cannot be obtained.<br>
* App should terminate gracefully. * App should terminate gracefully.
*/ */
protected void onLockError() protected void onLockError()
{ {
Log.e("Could not obtain lock file.\nOnly one instance can run at a time."); Log.e("Could not obtain lock file.\nOnly one instance can run at a time.");
@ -406,7 +407,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
//@formatter:off //@formatter:off
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
"Another instance is already running.", "Another instance is already running.\n(Delete the "+opt.lockFile +" file in the working directory to override)",
"Lock Error", "Lock Error",
JOptionPane.ERROR_MESSAGE JOptionPane.ERROR_MESSAGE
); );
@ -444,9 +445,9 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
} }
@DefaultImpl
protected void beforeShutdown() protected void beforeShutdown()
{ {
if (lockObtained) Config.save();
} }
@ -457,7 +458,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
} }
@DefaultImpl
protected void onCrash(Throwable e) protected void onCrash(Throwable e)
{ {
Log.e("The game has crashed.", e); Log.e("The game has crashed.", e);

@ -1,6 +1,7 @@
package mightypork.gamecore.core; package mightypork.gamecore.core.events;
import mightypork.gamecore.core.MainLoop;
import mightypork.gamecore.eventbus.BusEvent; import mightypork.gamecore.eventbus.BusEvent;
import mightypork.gamecore.eventbus.event_flags.SingleReceiverEvent; import mightypork.gamecore.eventbus.event_flags.SingleReceiverEvent;

@ -1,17 +1,20 @@
package mightypork.gamecore.core; package mightypork.gamecore.core.events;
import mightypork.gamecore.core.MainLoop;
import mightypork.gamecore.eventbus.BusEvent; import mightypork.gamecore.eventbus.BusEvent;
import mightypork.gamecore.eventbus.event_flags.NonConsumableEvent;
import mightypork.gamecore.eventbus.event_flags.SingleReceiverEvent; import mightypork.gamecore.eventbus.event_flags.SingleReceiverEvent;
/** /**
* Shutdown request. Shutdown needs to execute on GL thread for display to * Shutdown request, non-interactive. Shutdown needs to execute on GL thread for display to
* deinit properly. * deinit properly.
* *
* @author MightyPork * @author MightyPork
*/ */
@SingleReceiverEvent @SingleReceiverEvent
@NonConsumableEvent
public class ShudownRequest extends BusEvent<MainLoop> { public class ShudownRequest extends BusEvent<MainLoop> {
@Override @Override

@ -0,0 +1,32 @@
package mightypork.gamecore.core.events;
import mightypork.gamecore.eventbus.BusEvent;
import mightypork.gamecore.eventbus.EventBus;
/**
* User quit request. This event is triggered when user clicks the "close"
* titlebar button, and if no client consumes it, the application will be shut
* down.
*
* @author MightyPork
*/
public class UserQuitRequest extends BusEvent<UserQuitRequestListener> {
@Override
protected void handleBy(UserQuitRequestListener handler)
{
handler.onQuitRequest(this);
}
@Override
public void onDispatchComplete(EventBus bus)
{
if (!isConsumed()) {
bus.send(new ShudownRequest());
}
}
}

@ -0,0 +1,18 @@
package mightypork.gamecore.core.events;
/**
* Quit request listener; implementing client can abort shutdown.
*
* @author MightyPork
*/
public interface UserQuitRequestListener {
/**
* Intercept quit request.<br>
* Consume the event to abort shutdown (ie. ask user to save)
*
* @param event quit request event.
*/
void onQuitRequest(UserQuitRequest event);
}

@ -2,7 +2,7 @@ package mightypork.gamecore.gui.screens.impl;
import mightypork.gamecore.core.AppAccess; import mightypork.gamecore.core.AppAccess;
import mightypork.gamecore.core.ShudownRequest; import mightypork.gamecore.core.events.ShudownRequest;
import mightypork.gamecore.gui.components.painters.QuadPainter; import mightypork.gamecore.gui.components.painters.QuadPainter;
import mightypork.gamecore.gui.events.ScreenRequest; import mightypork.gamecore.gui.events.ScreenRequest;
import mightypork.gamecore.gui.screens.Overlay; import mightypork.gamecore.gui.screens.Overlay;

@ -2,7 +2,8 @@ package mightypork.gamecore.input;
import mightypork.gamecore.core.AppAccess; import mightypork.gamecore.core.AppAccess;
import mightypork.gamecore.core.ShudownRequest; import mightypork.gamecore.core.events.ShudownRequest;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.eventbus.clients.RootBusNode; import mightypork.gamecore.eventbus.clients.RootBusNode;
import mightypork.gamecore.eventbus.events.Updateable; import mightypork.gamecore.eventbus.events.Updateable;
import mightypork.gamecore.input.KeyStroke.Edge; import mightypork.gamecore.input.KeyStroke.Edge;
@ -139,7 +140,7 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
} }
if (Display.isCloseRequested()) { if (Display.isCloseRequested()) {
getEventBus().send(new ShudownRequest()); getEventBus().send(new UserQuitRequest());
} }
} }

@ -1,138 +0,0 @@
package mightypork.gamecore.input;
import java.util.HashMap;
import java.util.Map;
import mightypork.gamecore.Config.ConfigSetup;
import mightypork.gamecore.util.files.config.Property;
import mightypork.gamecore.util.files.config.PropertyManager;
public class KeyConfig implements ConfigSetup {
/**
* Key configurator
*/
public static interface KeySetup {
public void addKeys(KeyOpts keys);
}
/**
* Key configurator access
*/
public static class KeyOpts {
public void add(String cfgKey, String dataString)
{
strokes.put(prefixKey(cfgKey), new KeyProperty(prefixKey(cfgKey), KeyStroke.createFromDataString(dataString), null));
}
}
/**
* Key property.<br>
* The stored value must be invariant ({@link KeyStroke} is mutable).
*
* @author MightyPork
*/
private static class KeyProperty extends Property<KeyStroke> {
public KeyProperty(String key, KeyStroke defaultValue, String comment)
{
super(key, defaultValue, comment);
}
@Override
public KeyStroke decode(String string, KeyStroke defval)
{
if (string != null) {
// keep it invariant
final int backup_key = getValue().getKey();
final int backup_mod = getValue().getMod();
getValue().fromDataString(string);
if (getValue().getKey() == Keys.NONE) {
getValue().setTo(backup_key, backup_mod);
}
}
return getValue();
}
@Override
public String encode(KeyStroke value)
{
return value.toDataString();
}
@Override
public void setValue(Object value)
{
// keep it invariant
getValue().setTo(((KeyStroke) value).getKey(), ((KeyStroke) value).getMod());
}
}
private static KeyConfig inst = new KeyConfig();
public static KeyOpts keyOpts = new KeyOpts();
private static Map<String, KeyProperty> strokes = new HashMap<>();
private static PropertyManager prop;
public static void registerKeys(KeySetup layout)
{
layout.addKeys(keyOpts);
}
public static String prefixKey(String cfgKey)
{
return "key." + cfgKey;
}
/**
* Add key options to a config manager
*/
@Override
public void addOptions(PropertyManager prop)
{
for (final KeyProperty kp : strokes.values()) {
prop.putProperty(kp);
}
this.prop = prop;
}
public static KeyStroke get(String cfgKey)
{
final KeyProperty kp = strokes.get(prefixKey(cfgKey));
if (kp == null) throw new IllegalArgumentException("No such stroke: " + cfgKey);
return kp.getValue();
}
public static void set(String cfgKey, int key, int mod)
{
final KeyProperty kp = strokes.get(prefixKey(cfgKey));
if (kp == null) throw new IllegalArgumentException("No such stroke: " + cfgKey);
kp.getValue().setTo(key, mod);
prop.save();
}
public static KeyConfig inst()
{
return inst;
}
}

@ -5,7 +5,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import mightypork.gamecore.core.MainLoopRequest; import mightypork.gamecore.core.events.MainLoopRequest;
import mightypork.gamecore.eventbus.BusAccess; import mightypork.gamecore.eventbus.BusAccess;
import mightypork.gamecore.eventbus.events.Destroyable; import mightypork.gamecore.eventbus.events.Destroyable;
import mightypork.gamecore.logging.Log; import mightypork.gamecore.logging.Log;

@ -35,9 +35,9 @@ public class Launcher {
} }
if (!localWorkdir) { if (!localWorkdir) {
workdir = OsUtils.getHomeWorkDir(lwdDir); workdir = OsUtils.getHomeWorkDir(".rogue-save");
} else { } else {
workdir = new File(".rogue-save"); workdir = new File(lwdDir);
} }
} catch (final ArrayIndexOutOfBoundsException e) { } catch (final ArrayIndexOutOfBoundsException e) {

@ -5,11 +5,11 @@ import java.io.File;
import mightypork.gamecore.Config; import mightypork.gamecore.Config;
import mightypork.gamecore.core.BaseApp; import mightypork.gamecore.core.BaseApp;
import mightypork.gamecore.core.MainLoopRequest; import mightypork.gamecore.core.events.MainLoopRequest;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.eventbus.BusEvent; import mightypork.gamecore.eventbus.BusEvent;
import mightypork.gamecore.gui.screens.ScreenRegistry; import mightypork.gamecore.gui.screens.ScreenRegistry;
import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.input.KeyConfig;
import mightypork.gamecore.input.KeyStroke.Edge; import mightypork.gamecore.input.KeyStroke.Edge;
import mightypork.gamecore.render.DisplaySystem; import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.events.FullscreenToggleRequest; import mightypork.gamecore.render.events.FullscreenToggleRequest;
@ -20,7 +20,6 @@ import mightypork.rogue.events.RogueStateRequest;
import mightypork.rogue.screens.FpsOverlay; import mightypork.rogue.screens.FpsOverlay;
import mightypork.rogue.screens.LoadingOverlay; import mightypork.rogue.screens.LoadingOverlay;
import mightypork.rogue.screens.game.ScreenGame; import mightypork.rogue.screens.game.ScreenGame;
import mightypork.rogue.screens.layout_testing.LayoutTestScreen;
import mightypork.rogue.screens.menu.ScreenMainMenu; import mightypork.rogue.screens.menu.ScreenMainMenu;
import mightypork.rogue.screens.select_world.ScreenSelectWorld; import mightypork.rogue.screens.select_world.ScreenSelectWorld;
import mightypork.rogue.world.Inventory; import mightypork.rogue.world.Inventory;
@ -62,7 +61,7 @@ public final class RogueApp extends BaseApp {
@Override @Override
protected void initDisplay(DisplaySystem display) protected void initDisplay(DisplaySystem display)
{ {
display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.<Boolean> get("opt.fullscreen"), Const.TITLEBAR); display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.<Boolean> getOption("opt.fullscreen"), Const.TITLEBAR);
display.setTargetFps(Const.FPS_RENDER); display.setTargetFps(Const.FPS_RENDER);
} }
@ -80,7 +79,6 @@ public final class RogueApp extends BaseApp {
screens.addScreen("main_menu", new ScreenMainMenu(this)); screens.addScreen("main_menu", new ScreenMainMenu(this));
screens.addScreen("select_world", new ScreenSelectWorld(this)); screens.addScreen("select_world", new ScreenSelectWorld(this));
screens.addScreen("game", new ScreenGame(this)); screens.addScreen("game", new ScreenGame(this));
screens.addScreen("test.layout", new LayoutTestScreen(this));
screens.addOverlay(new FpsOverlay(this)); screens.addOverlay(new FpsOverlay(this));
screens.addOverlay(new LoadingOverlay(this)); screens.addOverlay(new LoadingOverlay(this));
@ -94,14 +92,14 @@ public final class RogueApp extends BaseApp {
bindEventToKey(new FullscreenToggleRequest(), "global.fullscreen"); bindEventToKey(new FullscreenToggleRequest(), "global.fullscreen");
bindEventToKey(new ScreenshotRequest(), "global.screenshot"); bindEventToKey(new ScreenshotRequest(), "global.screenshot");
bindEventToKey(new RogueStateRequest(RogueState.EXIT), "global.quit"); bindEventToKey(new UserQuitRequest(), "global.quit");
bindEventToKey(new RogueStateRequest(RogueState.MAIN_MENU), "global.menu"); bindEventToKey(new RogueStateRequest(RogueState.MAIN_MENU), "global.menu");
} }
private void bindEventToKey(final BusEvent<?> event, String strokeName) private void bindEventToKey(final BusEvent<?> event, String strokeName)
{ {
getInput().bindKey(KeyConfig.get(strokeName), Edge.RISING, new Runnable() { getInput().bindKey(Config.getKey(strokeName), Edge.RISING, new Runnable() {
@Override @Override
public void run() public void run()

@ -1,14 +1,15 @@
package mightypork.rogue; package mightypork.rogue;
import mightypork.gamecore.input.KeyConfig.KeyOpts; import mightypork.gamecore.Config;
import mightypork.gamecore.input.KeyConfig.KeySetup; import mightypork.gamecore.Config.KeyOpts;
import mightypork.gamecore.Config.KeySetup;
public class RogueKeys implements KeySetup { public class RogueKeys implements Config.KeySetup {
@Override @Override
public void addKeys(KeyOpts keys) public void addKeys(Config.KeyOpts keys)
{ {
keys.add("global.quit", "CTRL+Q"); keys.add("global.quit", "CTRL+Q");
keys.add("global.menu", "CTRL+M"); keys.add("global.menu", "CTRL+M");

@ -0,0 +1,25 @@
package mightypork.rogue.screens;
import mightypork.gamecore.core.AppAccess;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.core.events.UserQuitRequestListener;
import mightypork.gamecore.gui.screens.LayeredScreen;
import mightypork.gamecore.util.annot.DefaultImpl;
import mightypork.rogue.RogueStateManager.RogueState;
import mightypork.rogue.events.RogueStateRequest;
public class RogueScreen extends LayeredScreen implements UserQuitRequestListener {
public RogueScreen(AppAccess app) {
super(app);
}
@Override
@DefaultImpl
public void onQuitRequest(UserQuitRequest event)
{
getEventBus().send(new RogueStateRequest(RogueState.EXIT));
event.consume();
}
}

@ -4,6 +4,7 @@ package mightypork.rogue.screens.game;
import java.io.File; import java.io.File;
import mightypork.gamecore.core.AppAccess; import mightypork.gamecore.core.AppAccess;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.gui.Action; import mightypork.gamecore.gui.Action;
import mightypork.gamecore.gui.ActionGroup; import mightypork.gamecore.gui.ActionGroup;
import mightypork.gamecore.gui.screens.LayeredScreen; import mightypork.gamecore.gui.screens.LayeredScreen;
@ -15,14 +16,15 @@ import mightypork.gamecore.util.math.color.Color;
import mightypork.rogue.Const; import mightypork.rogue.Const;
import mightypork.rogue.RogueStateManager.RogueState; import mightypork.rogue.RogueStateManager.RogueState;
import mightypork.rogue.events.RogueStateRequest; import mightypork.rogue.events.RogueStateRequest;
import mightypork.rogue.screens.RogueScreen;
import mightypork.rogue.world.PlayerFacade; import mightypork.rogue.world.PlayerFacade;
import mightypork.rogue.world.WorldProvider; import mightypork.rogue.world.WorldProvider;
import mightypork.rogue.world.events.PlayerKilledListener; import mightypork.rogue.world.events.PlayerDeathHandler;
import mightypork.rogue.world.events.WorldPauseRequest; import mightypork.rogue.world.events.WorldPauseRequest;
import mightypork.rogue.world.events.WorldPauseRequest.PauseAction; import mightypork.rogue.world.events.WorldPauseRequest.PauseAction;
public class ScreenGame extends LayeredScreen implements PlayerKilledListener { public class ScreenGame extends RogueScreen implements PlayerDeathHandler {
public static final Color COLOR_BTN_GOOD = Color.fromHex(0x28CB2D); public static final Color COLOR_BTN_GOOD = Color.fromHex(0x28CB2D);
public static final Color COLOR_BTN_BAD = Color.fromHex(0xCB2828); public static final Color COLOR_BTN_BAD = Color.fromHex(0xCB2828);
@ -252,7 +254,6 @@ public class ScreenGame extends LayeredScreen implements PlayerKilledListener {
bindKey(new KeyStroke(Keys.L, Keys.MOD_CONTROL), Edge.RISING, actionLoad); bindKey(new KeyStroke(Keys.L, Keys.MOD_CONTROL), Edge.RISING, actionLoad);
bindKey(new KeyStroke(Keys.S, Keys.MOD_CONTROL), Edge.RISING, actionSave); bindKey(new KeyStroke(Keys.S, Keys.MOD_CONTROL), Edge.RISING, actionSave);
bindKey(new KeyStroke(Keys.Q, Keys.MOD_CONTROL), Edge.RISING, actionQuit);
bindKey(new KeyStroke(Keys.ESCAPE), Edge.RISING, actionMenu); bindKey(new KeyStroke(Keys.ESCAPE), Edge.RISING, actionMenu);
// add as actions - enableables. // add as actions - enableables.
@ -307,4 +308,11 @@ public class ScreenGame extends LayeredScreen implements PlayerKilledListener {
{ {
setState(GScrState.DEATH); setState(GScrState.DEATH);
} }
@Override
public void onQuitRequest(UserQuitRequest event)
{
actionQuit.run();
event.consume();
}
} }

@ -1,59 +0,0 @@
package mightypork.rogue.screens.layout_testing;
import mightypork.gamecore.core.AppAccess;
import mightypork.gamecore.gui.AlignX;
import mightypork.gamecore.gui.components.layout.linear.LinearGap;
import mightypork.gamecore.gui.components.layout.linear.LinearLayout;
import mightypork.gamecore.gui.components.layout.linear.LinearSquare;
import mightypork.gamecore.gui.components.painters.QuadPainter;
import mightypork.gamecore.gui.components.painters.TextPainter;
import mightypork.gamecore.gui.screens.LayeredScreen;
import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.gamecore.resources.Res;
import mightypork.gamecore.util.math.color.pal.RGB;
import mightypork.gamecore.util.math.constraints.rect.Rect;
public class LayoutTestScreen extends LayeredScreen {
class Layer1 extends ScreenLayer {
public Layer1(Screen screen)
{
super(screen);
final Rect testRect = root.shrink(root.width().perc(10), root.height().perc(45));
final LinearLayout ll = new LinearLayout(root, AlignX.CENTER);
ll.setRect(testRect);
root.add(ll);
ll.add(new LinearSquare(new QuadPainter(RGB.RED)));
ll.add(new LinearGap(50));
ll.add(new LinearSquare(new QuadPainter(RGB.ORANGE)));
ll.add(new LinearGap(100));
ll.add(new LinearSquare(new QuadPainter(RGB.YELLOW)));
ll.add(new TextPainter(Res.getFont("tiny"), RGB.WHITE, "Text qjf'\"^"));
ll.add(new LinearSquare(new QuadPainter(RGB.GREEN)));
}
@Override
public int getZIndex()
{
return 10;
}
}
public LayoutTestScreen(AppAccess app)
{
super(app);
addLayer(new Layer1(this));
}
}

@ -23,6 +23,7 @@ import mightypork.gamecore.util.math.constraints.num.Num;
import mightypork.gamecore.util.math.constraints.rect.Rect; import mightypork.gamecore.util.math.constraints.rect.Rect;
import mightypork.rogue.RogueStateManager.RogueState; import mightypork.rogue.RogueStateManager.RogueState;
import mightypork.rogue.events.RogueStateRequest; import mightypork.rogue.events.RogueStateRequest;
import mightypork.rogue.screens.RogueScreen;
/** /**
@ -30,7 +31,7 @@ import mightypork.rogue.events.RogueStateRequest;
* *
* @author MightyPork * @author MightyPork
*/ */
public class ScreenMainMenu extends LayeredScreen { public class ScreenMainMenu extends RogueScreen {
public ScreenMainMenu(AppAccess app) public ScreenMainMenu(AppAccess app)

@ -20,6 +20,7 @@ import mightypork.gamecore.util.math.color.pal.RGB;
import mightypork.gamecore.util.math.constraints.rect.Rect; import mightypork.gamecore.util.math.constraints.rect.Rect;
import mightypork.rogue.RogueStateManager.RogueState; import mightypork.rogue.RogueStateManager.RogueState;
import mightypork.rogue.events.RogueStateRequest; import mightypork.rogue.events.RogueStateRequest;
import mightypork.rogue.screens.RogueScreen;
/** /**
@ -27,7 +28,7 @@ import mightypork.rogue.events.RogueStateRequest;
* *
* @author MightyPork * @author MightyPork
*/ */
public class ScreenSelectWorld extends LayeredScreen { public class ScreenSelectWorld extends RogueScreen {
public ScreenSelectWorld(AppAccess app) public ScreenSelectWorld(AppAccess app)

@ -1,7 +1,7 @@
package mightypork.rogue.world.events; package mightypork.rogue.world.events;
public interface PlayerKilledListener { public interface PlayerDeathHandler {
void onPlayerKilled(); void onPlayerKilled();
} }

@ -4,10 +4,10 @@ package mightypork.rogue.world.events;
import mightypork.gamecore.eventbus.BusEvent; import mightypork.gamecore.eventbus.BusEvent;
public class PlayerKilledEvent extends BusEvent<PlayerKilledListener> { public class PlayerKilledEvent extends BusEvent<PlayerDeathHandler> {
@Override @Override
protected void handleBy(PlayerKilledListener handler) protected void handleBy(PlayerDeathHandler handler)
{ {
handler.onPlayerKilled(); handler.onPlayerKilled();
} }

@ -682,7 +682,6 @@ public class ScratchMap {
public boolean addItemInMap(Item item, int tries) public boolean addItemInMap(Item item, int tries)
{ {
Log.f3("gen bounds: " + genMin + " -> " + genMax);
return addItemInArea(item, genMin, genMax, tries); return addItemInArea(item, genMin, genMax, tries);
} }

@ -26,6 +26,9 @@ public class WorldCreator {
public static World createWorld(long seed) public static World createWorld(long seed)
{ {
synchronized (rand) { synchronized (rand) {
Log.f2("Generating a new world...");
rand.setSeed(seed); rand.setSeed(seed);
final MapTheme theme = new ThemeBrick(); final MapTheme theme = new ThemeBrick();
@ -36,12 +39,15 @@ public class WorldCreator {
// build the level rooms // build the level rooms
for (int floor = 1; floor <= 7; floor++) { for (int floor = 1; floor <= 7; floor++) {
Log.f3("Placing rooms for level: " + floor);
final LevelBuilder lb = prepareFloor(rand.nextLong(), floor, theme, floor == 7); final LevelBuilder lb = prepareFloor(rand.nextLong(), floor, theme, floor == 7);
levelBuilders[floor - 1] = lb; levelBuilders[floor - 1] = lb;
} }
Log.f3("Placing items...");
final List<ItemModel> weaponsBasic = new ArrayList<>(); final List<ItemModel> weaponsBasic = new ArrayList<>();
weaponsBasic.add(Items.ROCK); weaponsBasic.add(Items.ROCK);
weaponsBasic.add(Items.BONE); weaponsBasic.add(Items.BONE);
@ -86,7 +92,6 @@ public class WorldCreator {
randomFood.add(Items.MEAT); randomFood.add(Items.MEAT);
for (int level = 1; level <= 7; level++) { for (int level = 1; level <= 7; level++) {
final LevelBuilder lb = levelBuilders[level - 1]; final LevelBuilder lb = levelBuilders[level - 1];
final Range amount = Range.make(1, level); final Range amount = Range.make(1, level);
@ -97,6 +102,8 @@ public class WorldCreator {
// place monsters // place monsters
Log.f3("Placing monsters...");
for (int level = 1; level <= 7; level++) { for (int level = 1; level <= 7; level++) {
final LevelBuilder lb = levelBuilders[level - 1]; final LevelBuilder lb = levelBuilders[level - 1];
@ -118,13 +125,18 @@ public class WorldCreator {
// compile levels // compile levels
Log.f3("Building levels...");
int i=1;
for (final LevelBuilder lb : levelBuilders) { for (final LevelBuilder lb : levelBuilders) {
Log.f3("Building level "+i);
w.addLevel(lb.build(w)); w.addLevel(lb.build(w));
i++;
} }
w.createPlayer(); w.createPlayer();
Log.f2("World generation finished.");
return w; return w;
} }
} }
@ -132,8 +144,6 @@ public class WorldCreator {
public static LevelBuilder prepareFloor(long seed, int floor, MapTheme theme, boolean lastLevel) throws WorldGenError public static LevelBuilder prepareFloor(long seed, int floor, MapTheme theme, boolean lastLevel) throws WorldGenError
{ {
Log.f3("Generating level: " + floor);
final LevelBuilder lb = new LevelBuilder(128, theme, seed); final LevelBuilder lb = new LevelBuilder(128, theme, seed);
lb.addRoom(Rooms.ENTRANCE, BuildOrder.FIRST, true); lb.addRoom(Rooms.ENTRANCE, BuildOrder.FIRST, true);

Loading…
Cancel
Save