All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mmb.PlayAWorld Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

There is a newer version: 0.6
Show newest version
/**
 * 
 */
package mmb;

import java.awt.EventQueue;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

import com.fasterxml.jackson.databind.JsonNode;
import com.pploder.events.Event;
import com.pploder.events.SimpleEvent;

import mmb.engine.MMBUtils;
import mmb.engine.debug.Debugger;
import mmb.engine.files.Save;
import mmb.engine.json.JsonTool;
import mmb.engine.window.FullScreen;
import mmb.engine.worlds.universe.Universe;
import mmb.menu.main.MainMenu;
import mmb.menu.world.window.WorldWindow;

/**
 * Controls the game loading process
 * @author oskar
 */
public class PlayAWorld {
	private static final Debugger debug = new Debugger("SELECT A SAVE");
	
	/** Triggered when a world finishes loading */
	@NN public final Event<@Nil Throwable> onLoad = new SimpleEvent<>();

	private static void fullPlay0(Save s, WorldWindow ww){
		boolean fail = true;
		try(InputStream in = s.file.getInputStream()) {
			debug.printl("Opened a file");
			String loadedData = IOUtils.toString(in, StandardCharsets.UTF_8);
			debug.printl("Loaded a file");
			Universe world = new Universe();
			@SuppressWarnings("null")
			JsonNode node = JsonTool.parse(loadedData);
			if(node == null || node.isMissingNode()) {
				debug.printl("Failed to parse JSON data");
				EventQueue.invokeLater(ww::dispose); //Control given back to EDT to close unnecessary window
			}
			debug.printl("Parsed file");
			world.load(node);
			debug.printl("Loaded");
			ww.setWorld(s, world);
			fail = false;
		}catch(Exception e) {
			debug.pstm(e, "Failed to load the world");
			MMBUtils.shoot(e);
		}catch(OutOfMemoryError e) {
			debug.pstm(e, "Ran out of memory while loading");
		}catch(Throwable e) {
			debug.printerrl("Fatal error while world loading");
			Main.crash(e);
		}finally {
			if(fail) {
				EventQueue.invokeLater(() -> {
					ww.dispose();
					FullScreen.setWindow(MainMenu.INSTANCE);
				});//Control given back to EDT to close unnecessary window
			}
		}
	}
	private void fullPlay(Save s, WorldWindow ww) {
		try {
			fullPlay0(s, ww);
			onLoad.trigger(null);
		}catch(Throwable e) {
			onLoad.trigger(e);
			MMBUtils.shoot(e);
		}
	}

	/**
	 * Plays a specific world
	 * @param s file to play
	 * @return a newly creates world window
	 */
	public WorldWindow play(Save s) {
		WorldWindow ww = new WorldWindow();
		FullScreen.setWindow(ww);
		new Thread(() -> fullPlay(s, ww)).start();
		return ww;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy