mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2025-10-30 08:11:25 +00:00
135 lines
3.2 KiB
Haxe
135 lines
3.2 KiB
Haxe
package src;
|
|
|
|
import hxd.Key;
|
|
import src.MarbleGame;
|
|
import hxd.Window;
|
|
import haxe.DynamicAccess;
|
|
import sys.io.File;
|
|
import src.ResourceLoader;
|
|
import haxe.Json;
|
|
|
|
typedef Score = {
|
|
var name:String;
|
|
var time:Float;
|
|
}
|
|
|
|
typedef OptionsSettings = {
|
|
var screenWidth:Int;
|
|
var screenHeight:Int;
|
|
var isFullScreen:Bool;
|
|
var videoDriver:Int;
|
|
var colorDepth:Int;
|
|
var shadows:Bool;
|
|
var musicVolume:Float;
|
|
var soundVolume:Float;
|
|
}
|
|
|
|
typedef ControlsSettings = {
|
|
var forward:Int;
|
|
var backward:Int;
|
|
var left:Int;
|
|
var right:Int;
|
|
var camForward:Int;
|
|
var camBackward:Int;
|
|
var camLeft:Int;
|
|
var camRight:Int;
|
|
var jump:Int;
|
|
var powerup:Int;
|
|
var freelook:Int;
|
|
var alwaysFreeLook:Bool;
|
|
var cameraSensitivity:Float;
|
|
var invertYAxis:Bool;
|
|
}
|
|
|
|
class Settings {
|
|
public static var highScores:Map<String, Array<Score>> = [];
|
|
|
|
public static var optionsSettings:OptionsSettings = {
|
|
screenWidth: 1280,
|
|
screenHeight: 720,
|
|
isFullScreen: false,
|
|
videoDriver: 0,
|
|
colorDepth: 1,
|
|
shadows: false,
|
|
musicVolume: 0,
|
|
soundVolume: 0,
|
|
};
|
|
|
|
public static var controlsSettings:ControlsSettings = {
|
|
forward: Key.W,
|
|
backward: Key.S,
|
|
left: Key.A,
|
|
right: Key.D,
|
|
camForward: Key.UP,
|
|
camBackward: Key.DOWN,
|
|
camLeft: Key.LEFT,
|
|
camRight: Key.RIGHT,
|
|
jump: Key.SPACE,
|
|
powerup: Key.MOUSE_LEFT,
|
|
freelook: Key.MOUSE_RIGHT,
|
|
alwaysFreeLook: true,
|
|
cameraSensitivity: 0.6,
|
|
invertYAxis: false
|
|
};
|
|
|
|
public static function applySettings() {
|
|
Window.getInstance().resize(optionsSettings.screenWidth, optionsSettings.screenHeight);
|
|
Window.getInstance().displayMode = optionsSettings.isFullScreen ? FullscreenResize : Windowed;
|
|
|
|
MarbleGame.canvas.render(MarbleGame.canvas.scene2d);
|
|
save();
|
|
}
|
|
|
|
public static function saveScore(mapPath:String, score:Score) {
|
|
if (highScores.exists(mapPath)) {
|
|
var scoreList = highScores.get(mapPath);
|
|
scoreList.push(score);
|
|
scoreList.sort((a, b) -> a.time == b.time ? 0 : (a.time > b.time ? 1 : -1));
|
|
} else {
|
|
highScores.set(mapPath, [score]);
|
|
}
|
|
save();
|
|
}
|
|
|
|
public static function getScores(mapPath:String) {
|
|
if (highScores.exists(mapPath)) {
|
|
return highScores.get(mapPath).copy();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public static function save() {
|
|
var outputData = {
|
|
highScores: highScores,
|
|
options: optionsSettings,
|
|
controls: controlsSettings
|
|
};
|
|
var json = Json.stringify(outputData);
|
|
File.saveContent("settings.json", json);
|
|
}
|
|
|
|
public static function load() {
|
|
if (ResourceLoader.fileSystem.exists("settings.json")) {
|
|
var json = Json.parse(ResourceLoader.fileSystem.get("settings.json").getText());
|
|
var highScoreData:DynamicAccess<Array<Score>> = json.highScores;
|
|
for (key => value in highScoreData) {
|
|
highScores.set(key, value);
|
|
}
|
|
optionsSettings = json.options;
|
|
controlsSettings = json.controls;
|
|
}
|
|
}
|
|
|
|
public static function init() {
|
|
load();
|
|
Window.getInstance().resize(optionsSettings.screenWidth, optionsSettings.screenHeight);
|
|
Window.getInstance().displayMode = optionsSettings.isFullScreen ? FullscreenResize : Windowed;
|
|
Window.getInstance().addResizeEvent(() -> {
|
|
var wnd = Window.getInstance();
|
|
Settings.optionsSettings.screenWidth = wnd.width;
|
|
Settings.optionsSettings.screenHeight = wnd.height;
|
|
MarbleGame.canvas.render(MarbleGame.canvas.scene2d);
|
|
});
|
|
}
|
|
}
|