Refactoring; improved quit event; Merged KeyConfig and Config
This commit is contained in:
@@ -2,7 +2,12 @@ package mightypork.gamecore;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -21,6 +26,79 @@ public class Config {
|
||||
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;
|
||||
|
||||
|
||||
@@ -30,6 +108,13 @@ public class Config {
|
||||
}
|
||||
|
||||
|
||||
public static void registerKeys(KeySetup layout)
|
||||
{
|
||||
layout.addKeys(keyOpts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void registerOptions(ConfigSetup cfgl)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
public static <T> void set(String key, T value)
|
||||
public static <T> void setOption(String key, T 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.ConfigSetup;
|
||||
import mightypork.gamecore.Config.KeySetup;
|
||||
import mightypork.gamecore.WorkDir;
|
||||
import mightypork.gamecore.WorkDir.RouteSetup;
|
||||
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.impl.CrossfadeOverlay;
|
||||
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.SlickLogRedirector;
|
||||
import mightypork.gamecore.logging.writers.LogWriter;
|
||||
@@ -51,19 +50,25 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
|
||||
private String logDir = "log";
|
||||
private String logFilePrefix = "runtime";
|
||||
|
||||
private String screenshotDir = "screenshots";
|
||||
|
||||
private int logArchiveCount = 0;
|
||||
private boolean busLogging = false;
|
||||
|
||||
private String configFile = "settings.cfg";
|
||||
private String configComment = "Main config file";
|
||||
|
||||
public String lockFile = ".lock";
|
||||
|
||||
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<RouteSetup> routeLists = new ArrayList<>();
|
||||
|
||||
private ResourceLoader resourceLoader = new AsyncResourceLoader();
|
||||
private Level logLevel = Level.ALL;
|
||||
public boolean sigleInstance;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -122,6 +127,12 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
{
|
||||
this.screenshotDir = path;
|
||||
}
|
||||
|
||||
|
||||
public void setLockFile(String lockFile)
|
||||
{
|
||||
this.lockFile = lockFile;
|
||||
}
|
||||
}
|
||||
|
||||
// modules
|
||||
@@ -132,6 +143,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
private MainLoop gameLoop;
|
||||
private ScreenRegistry screenRegistry;
|
||||
private boolean started = false;
|
||||
private boolean lockObtained = false;
|
||||
|
||||
// init opt holder
|
||||
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);
|
||||
|
||||
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()
|
||||
{
|
||||
WorkDir.addPath("screenshots", opt.screenshotDir);
|
||||
WorkDir.addPath("config", opt.configFile);
|
||||
WorkDir.addPath("logs", opt.logDir);
|
||||
|
||||
if (opt.sigleInstance) initLock();
|
||||
lockObtained = true;
|
||||
|
||||
for (final RouteSetup rs : opt.routeLists) {
|
||||
WorkDir.registerRoutes(rs);
|
||||
@@ -195,18 +204,18 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
|
||||
// apply configurations
|
||||
Config.init(WorkDir.getFile(opt.configFile), opt.configComment);
|
||||
for (final KeySetup l : opt.keyLists) {
|
||||
KeyConfig.registerKeys(l);
|
||||
}
|
||||
|
||||
// add keys to config
|
||||
Config.registerOptions(KeyConfig.inst());
|
||||
for (final ConfigSetup cfgl : opt.configLists) {
|
||||
Config.registerOptions(cfgl);
|
||||
for (final KeySetup l : opt.keyLists) {
|
||||
Config.registerKeys(l);
|
||||
}
|
||||
|
||||
// add options to config
|
||||
for (final ConfigSetup c : opt.configLists) {
|
||||
Config.registerOptions(c);
|
||||
}
|
||||
Config.load();
|
||||
|
||||
|
||||
/*
|
||||
* Setup logging
|
||||
*/
|
||||
@@ -215,15 +224,12 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
Log.setMainLogger(log);
|
||||
org.newdawn.slick.util.Log.setLogSystem(new SlickLogRedirector(log));
|
||||
|
||||
|
||||
Log.i("=== Starting initialization sequence ===");
|
||||
|
||||
|
||||
// pre-init hook
|
||||
Log.f2("Calling pre-init hook...");
|
||||
preInit();
|
||||
|
||||
|
||||
/*
|
||||
* Event bus
|
||||
*/
|
||||
@@ -235,10 +241,9 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
/*
|
||||
* Ionizables
|
||||
*/
|
||||
Log.f3("initializing ION...");
|
||||
Log.f3("Initializing ION save system...");
|
||||
registerIonizables();
|
||||
|
||||
|
||||
/*
|
||||
* Display
|
||||
*/
|
||||
@@ -246,7 +251,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
displaySystem = new DisplaySystem(this);
|
||||
initDisplay(displaySystem);
|
||||
|
||||
|
||||
/*
|
||||
* Audio
|
||||
*/
|
||||
@@ -254,7 +258,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
soundSystem = new SoundSystem(this);
|
||||
initSoundSystem(soundSystem);
|
||||
|
||||
|
||||
/*
|
||||
* Input
|
||||
*/
|
||||
@@ -262,7 +265,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
inputSystem = new InputSystem(this);
|
||||
initInputSystem(inputSystem);
|
||||
|
||||
|
||||
/*
|
||||
* Prepare main loop
|
||||
*/
|
||||
@@ -271,7 +273,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
gameLoop = createMainLoop();
|
||||
gameLoop.setRootRenderable(screenRegistry);
|
||||
|
||||
|
||||
/*
|
||||
* Load resources
|
||||
*
|
||||
@@ -288,7 +289,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
Res.load(rl);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Screen registry
|
||||
*
|
||||
@@ -381,7 +381,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
*/
|
||||
private void initLock()
|
||||
{
|
||||
final File lock = WorkDir.getFile(".lock");
|
||||
final File lock = WorkDir.getFile(opt.lockFile);
|
||||
if (!InstanceLock.onFile(lock)) {
|
||||
onLockError();
|
||||
return;
|
||||
@@ -399,6 +399,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
* Triggered when lock cannot be obtained.<br>
|
||||
* App should terminate gracefully.
|
||||
*/
|
||||
|
||||
protected void onLockError()
|
||||
{
|
||||
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
|
||||
JOptionPane.showMessageDialog(
|
||||
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",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
@@ -444,9 +445,9 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
}
|
||||
|
||||
|
||||
@DefaultImpl
|
||||
protected void beforeShutdown()
|
||||
{
|
||||
if (lockObtained) Config.save();
|
||||
}
|
||||
|
||||
|
||||
@@ -457,7 +458,6 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
}
|
||||
|
||||
|
||||
@DefaultImpl
|
||||
protected void onCrash(Throwable e)
|
||||
{
|
||||
Log.e("The game has crashed.", e);
|
||||
|
||||
+2
-1
@@ -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.event_flags.SingleReceiverEvent;
|
||||
|
||||
+5
-2
@@ -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.event_flags.NonConsumableEvent;
|
||||
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.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
@SingleReceiverEvent
|
||||
@NonConsumableEvent
|
||||
public class ShudownRequest extends BusEvent<MainLoop> {
|
||||
|
||||
@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.ShudownRequest;
|
||||
import mightypork.gamecore.core.events.ShudownRequest;
|
||||
import mightypork.gamecore.gui.components.painters.QuadPainter;
|
||||
import mightypork.gamecore.gui.events.ScreenRequest;
|
||||
import mightypork.gamecore.gui.screens.Overlay;
|
||||
|
||||
@@ -2,7 +2,8 @@ package mightypork.gamecore.input;
|
||||
|
||||
|
||||
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.events.Updateable;
|
||||
import mightypork.gamecore.input.KeyStroke.Edge;
|
||||
@@ -139,7 +140,7 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
|
||||
}
|
||||
|
||||
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.LinkedBlockingQueue;
|
||||
|
||||
import mightypork.gamecore.core.MainLoopRequest;
|
||||
import mightypork.gamecore.core.events.MainLoopRequest;
|
||||
import mightypork.gamecore.eventbus.BusAccess;
|
||||
import mightypork.gamecore.eventbus.events.Destroyable;
|
||||
import mightypork.gamecore.logging.Log;
|
||||
|
||||
@@ -35,9 +35,9 @@ public class Launcher {
|
||||
}
|
||||
|
||||
if (!localWorkdir) {
|
||||
workdir = OsUtils.getHomeWorkDir(lwdDir);
|
||||
workdir = OsUtils.getHomeWorkDir(".rogue-save");
|
||||
} else {
|
||||
workdir = new File(".rogue-save");
|
||||
workdir = new File(lwdDir);
|
||||
}
|
||||
|
||||
} catch (final ArrayIndexOutOfBoundsException e) {
|
||||
|
||||
@@ -5,11 +5,11 @@ import java.io.File;
|
||||
|
||||
import mightypork.gamecore.Config;
|
||||
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.gui.screens.ScreenRegistry;
|
||||
import mightypork.gamecore.input.InputSystem;
|
||||
import mightypork.gamecore.input.KeyConfig;
|
||||
import mightypork.gamecore.input.KeyStroke.Edge;
|
||||
import mightypork.gamecore.render.DisplaySystem;
|
||||
import mightypork.gamecore.render.events.FullscreenToggleRequest;
|
||||
@@ -20,7 +20,6 @@ import mightypork.rogue.events.RogueStateRequest;
|
||||
import mightypork.rogue.screens.FpsOverlay;
|
||||
import mightypork.rogue.screens.LoadingOverlay;
|
||||
import mightypork.rogue.screens.game.ScreenGame;
|
||||
import mightypork.rogue.screens.layout_testing.LayoutTestScreen;
|
||||
import mightypork.rogue.screens.menu.ScreenMainMenu;
|
||||
import mightypork.rogue.screens.select_world.ScreenSelectWorld;
|
||||
import mightypork.rogue.world.Inventory;
|
||||
@@ -62,7 +61,7 @@ public final class RogueApp extends BaseApp {
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -80,7 +79,6 @@ public final class RogueApp extends BaseApp {
|
||||
screens.addScreen("main_menu", new ScreenMainMenu(this));
|
||||
screens.addScreen("select_world", new ScreenSelectWorld(this));
|
||||
screens.addScreen("game", new ScreenGame(this));
|
||||
screens.addScreen("test.layout", new LayoutTestScreen(this));
|
||||
|
||||
screens.addOverlay(new FpsOverlay(this));
|
||||
screens.addOverlay(new LoadingOverlay(this));
|
||||
@@ -94,14 +92,14 @@ public final class RogueApp extends BaseApp {
|
||||
bindEventToKey(new FullscreenToggleRequest(), "global.fullscreen");
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
public void run()
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package mightypork.rogue;
|
||||
|
||||
|
||||
import mightypork.gamecore.input.KeyConfig.KeyOpts;
|
||||
import mightypork.gamecore.input.KeyConfig.KeySetup;
|
||||
import mightypork.gamecore.Config;
|
||||
import mightypork.gamecore.Config.KeyOpts;
|
||||
import mightypork.gamecore.Config.KeySetup;
|
||||
|
||||
|
||||
public class RogueKeys implements KeySetup {
|
||||
public class RogueKeys implements Config.KeySetup {
|
||||
|
||||
@Override
|
||||
public void addKeys(KeyOpts keys)
|
||||
public void addKeys(Config.KeyOpts keys)
|
||||
{
|
||||
keys.add("global.quit", "CTRL+Q");
|
||||
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 mightypork.gamecore.core.AppAccess;
|
||||
import mightypork.gamecore.core.events.UserQuitRequest;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
import mightypork.gamecore.gui.ActionGroup;
|
||||
import mightypork.gamecore.gui.screens.LayeredScreen;
|
||||
@@ -15,14 +16,15 @@ import mightypork.gamecore.util.math.color.Color;
|
||||
import mightypork.rogue.Const;
|
||||
import mightypork.rogue.RogueStateManager.RogueState;
|
||||
import mightypork.rogue.events.RogueStateRequest;
|
||||
import mightypork.rogue.screens.RogueScreen;
|
||||
import mightypork.rogue.world.PlayerFacade;
|
||||
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.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_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.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);
|
||||
|
||||
// add as actions - enableables.
|
||||
@@ -307,4 +308,11 @@ public class ScreenGame extends LayeredScreen implements PlayerKilledListener {
|
||||
{
|
||||
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.rogue.RogueStateManager.RogueState;
|
||||
import mightypork.rogue.events.RogueStateRequest;
|
||||
import mightypork.rogue.screens.RogueScreen;
|
||||
|
||||
|
||||
/**
|
||||
@@ -30,7 +31,7 @@ import mightypork.rogue.events.RogueStateRequest;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class ScreenMainMenu extends LayeredScreen {
|
||||
public class ScreenMainMenu extends RogueScreen {
|
||||
|
||||
|
||||
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.rogue.RogueStateManager.RogueState;
|
||||
import mightypork.rogue.events.RogueStateRequest;
|
||||
import mightypork.rogue.screens.RogueScreen;
|
||||
|
||||
|
||||
/**
|
||||
@@ -27,7 +28,7 @@ import mightypork.rogue.events.RogueStateRequest;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class ScreenSelectWorld extends LayeredScreen {
|
||||
public class ScreenSelectWorld extends RogueScreen {
|
||||
|
||||
|
||||
public ScreenSelectWorld(AppAccess app)
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.world.events;
|
||||
|
||||
|
||||
public interface PlayerKilledListener {
|
||||
public interface PlayerDeathHandler {
|
||||
|
||||
void onPlayerKilled();
|
||||
}
|
||||
@@ -4,10 +4,10 @@ package mightypork.rogue.world.events;
|
||||
import mightypork.gamecore.eventbus.BusEvent;
|
||||
|
||||
|
||||
public class PlayerKilledEvent extends BusEvent<PlayerKilledListener> {
|
||||
public class PlayerKilledEvent extends BusEvent<PlayerDeathHandler> {
|
||||
|
||||
@Override
|
||||
protected void handleBy(PlayerKilledListener handler)
|
||||
protected void handleBy(PlayerDeathHandler handler)
|
||||
{
|
||||
handler.onPlayerKilled();
|
||||
}
|
||||
|
||||
@@ -682,7 +682,6 @@ public class ScratchMap {
|
||||
|
||||
public boolean addItemInMap(Item item, int tries)
|
||||
{
|
||||
Log.f3("gen bounds: " + genMin + " -> " + genMax);
|
||||
return addItemInArea(item, genMin, genMax, tries);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ public class WorldCreator {
|
||||
public static World createWorld(long seed)
|
||||
{
|
||||
synchronized (rand) {
|
||||
|
||||
Log.f2("Generating a new world...");
|
||||
|
||||
rand.setSeed(seed);
|
||||
final MapTheme theme = new ThemeBrick();
|
||||
|
||||
@@ -36,12 +39,15 @@ public class WorldCreator {
|
||||
|
||||
// build the level rooms
|
||||
for (int floor = 1; floor <= 7; floor++) {
|
||||
|
||||
Log.f3("Placing rooms for level: " + floor);
|
||||
|
||||
final LevelBuilder lb = prepareFloor(rand.nextLong(), floor, theme, floor == 7);
|
||||
|
||||
levelBuilders[floor - 1] = lb;
|
||||
}
|
||||
|
||||
|
||||
Log.f3("Placing items...");
|
||||
final List<ItemModel> weaponsBasic = new ArrayList<>();
|
||||
weaponsBasic.add(Items.ROCK);
|
||||
weaponsBasic.add(Items.BONE);
|
||||
@@ -86,7 +92,6 @@ public class WorldCreator {
|
||||
randomFood.add(Items.MEAT);
|
||||
|
||||
for (int level = 1; level <= 7; level++) {
|
||||
|
||||
final LevelBuilder lb = levelBuilders[level - 1];
|
||||
final Range amount = Range.make(1, level);
|
||||
|
||||
@@ -97,6 +102,8 @@ public class WorldCreator {
|
||||
|
||||
|
||||
// place monsters
|
||||
|
||||
Log.f3("Placing monsters...");
|
||||
for (int level = 1; level <= 7; level++) {
|
||||
|
||||
final LevelBuilder lb = levelBuilders[level - 1];
|
||||
@@ -118,13 +125,18 @@ public class WorldCreator {
|
||||
|
||||
|
||||
// compile levels
|
||||
Log.f3("Building levels...");
|
||||
int i=1;
|
||||
for (final LevelBuilder lb : levelBuilders) {
|
||||
Log.f3("Building level "+i);
|
||||
w.addLevel(lb.build(w));
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
w.createPlayer();
|
||||
|
||||
Log.f2("World generation finished.");
|
||||
|
||||
return w;
|
||||
}
|
||||
}
|
||||
@@ -132,8 +144,6 @@ public class WorldCreator {
|
||||
|
||||
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);
|
||||
|
||||
lb.addRoom(Rooms.ENTRANCE, BuildOrder.FIRST, true);
|
||||
|
||||
Reference in New Issue
Block a user