~zenomat/javatorio

2101319851d1f659f996962e0f8f585669d48785 — Zeno a month ago 3fba92e singleton
Update to use Singleton classes

This commit updates the code to use sigletons for classes,
that every other class need to have access to, like the GameState
or the Terminal output
A app/.settings/org.eclipse.jdt.core.prefs => app/.settings/org.eclipse.jdt.core.prefs +4 -0
@@ 0,0 1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=18
org.eclipse.jdt.core.compiler.compliance=18
org.eclipse.jdt.core.compiler.source=18

M app/src/main/java/javatorio/app/App.java => app/src/main/java/javatorio/app/App.java +27 -37
@@ 3,40 3,30 @@ package javatorio.app;
// import libraries.javatorio.error.Error;
// import libraries.javatorio.number.Number;
//
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;

import libraries.javatorio.event.EventHandler;
import libraries.javatorio.event.EventListener;
import libraries.javatorio.event.KeyEvent;

import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.input.KeyStroke;

public class App {

	// FIXME(zeno): This is not static
	public static Screen terminal;
	public static boolean running;
	public static UIHandler uiHandler;

	public static void main(String[] args) throws Exception, Throwable {
		// All the initalizations
		//		- get Screen
		//		- enter screen mode
		//		- initalize gamemap
		//		- initalize running check
		//		- register UIHandler
		terminal = new DefaultTerminalFactory().createScreen();
		terminal.startScreen();
		running = true;
		//      - initalize Screen
		//      - initalize GameState
		//      - initalize UIHandler
		//      - initalize EventHandler
		Terminal terminal = Terminal.getInstance();
		UIHandler uiHandler = UIHandler.getInstance();
		GameState gameState = GameState.getInstance();
		EventHandler eventHandler = new EventHandler();
		uiHandler = new UIHandler();

		// Inital draw of UI

		// register the Map UI
		uiHandler.registerUIElement(
				new GameMap("GameMap", 0, 0, 128, 20));
		    new GameMap("GameMap", 0, 0, 128, 20));
		uiHandler.findUIElement("GameMap").fillBuffer();
		uiHandler.findUIElement("GameMap").drawBuffer();



@@ 45,32 35,32 @@ public class App {

		// registers a UIElement with buttons
		uiHandler.registerUIElement(
				new UIButtons("ControlButtons", map.getSizeX() + 10, 0, 15, 50, new Button[]{
					new Button('Q', false, false, true, "Quit", "Quits the game", () -> {
						running = false;
					}),
						new Button('n', true, true, false, "New Ores", "Places a new Iron ore", () -> {
							try {
								map.changeField(10, 10, new IronField(50));
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}),
				}
				));
		// TODO(zeno): Figure out a better way without typecasts
		    new UIButtons("ControlButtons", map.getSizeX() + 10, 0, 15, 50, new Button[] {
		new Button('Q', false, false, true, "Quit", "Quits the game", () -> {
			gameState.running = false;
		}),
		new Button('n', true, true, false, "New Ores", "Places a new Iron ore", () -> {
			try {
				map.changeField(10, 10, new IronField(50));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}),
		                  }
		                 ));
		// TODO(zeno): Figure out a better way without typecasts, maybe with instanceof
		eventHandler.registerEvent(KeyEvent.class.getSimpleName(), (EventListener)uiHandler.findUIElement("ControlButtons"));

		uiHandler.findUIElement("ControlButtons").fillBuffer();
		uiHandler.findUIElement("ControlButtons").drawBuffer();


		// Gameloop
		while (running == true) {
			KeyStroke ks = terminal.pollInput();
		while (gameState.running == true) {
			KeyStroke ks = terminal.screen.pollInput();
			// check for keystroke
			if (ks != null) {
				// emit KeyPress event
				eventHandler.emitEvent(new KeyEvent(ks.getCharacter(), ks.isCtrlDown(), ks.isAltDown(), ks.isShiftDown()));
			}
			// game runs at roughly 30 fps, there a 33 ms in a frame


@@ 78,6 68,6 @@ public class App {
			// NOTE(zeno): without sleep, one core pinned at 100% usage
			Thread.sleep(33);
		}
		terminal.stopScreen();
		terminal.screen.stopScreen();
	}
}

M app/src/main/java/javatorio/app/GameMap.java => app/src/main/java/javatorio/app/GameMap.java +1 -1
@@ 61,7 61,7 @@ public class GameMap extends UIElement implements EventListener {
	 */
	public void fillBuffer() {
		// FIXME(zeno): This overrides the whole screen, which sucks, because we don't want to redraw everything
		//				check out buffered screen lanterna
		//              check out buffered screen lanterna
		// App.terminal.clearScreen();
		ArrayList<Object> buffer = new ArrayList<>();
		buffer.add(ControlCodes.SETCURS);

A app/src/main/java/javatorio/app/GameState.java => app/src/main/java/javatorio/app/GameState.java +18 -0
@@ 0,0 1,18 @@
package javatorio.app;

class GameState {
	private static GameState gameState = null;

	public boolean running;

	private GameState() {
		this.running = true;
	}

	public static GameState getInstance() {
		if (gameState == null) {
			gameState = new GameState();
		}
		return gameState;
	}
}

A app/src/main/java/javatorio/app/Terminal.java => app/src/main/java/javatorio/app/Terminal.java +24 -0
@@ 0,0 1,24 @@
package javatorio.app;

import com.googlecode.lanterna.terminal.DefaultTerminalFactory;

import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.input.KeyStroke;

class Terminal {
	private static Terminal terminal = null;

	public Screen screen;

	private Terminal() throws Exception {
		screen = new DefaultTerminalFactory().createScreen();
		screen.startScreen();
	}

	public static Terminal getInstance() throws Exception {
		if (terminal == null) {
			terminal = new Terminal();
		}
		return terminal;
	}
}

M app/src/main/java/javatorio/app/UIButtons.java => app/src/main/java/javatorio/app/UIButtons.java +2 -1
@@ 83,9 83,10 @@ public class UIButtons extends UIElement implements EventListener {

	@Override
	public void receive(KeyEvent event) {
		UIHandler uiHandler = UIHandler.getInstance();
		UIButtons butts;
		try {
			butts = (UIButtons)App.uiHandler.findUIElement("ControlButtons");
			butts = (UIButtons)uiHandler.findUIElement("ControlButtons");
			for (Button but : butts.getButtons()) {
				if (event.key == but.getButtonSymbol() && event.ctrl == but.isCtrl() && event.alt == but.isAlt()) {
					but.exec();

M app/src/main/java/javatorio/app/UIElement.java => app/src/main/java/javatorio/app/UIElement.java +4 -3
@@ 69,11 69,12 @@ public abstract class UIElement {
		// set the cursor coords, so we can use them with the TerminalPosition required by the Screen
		int x = this.getPosX();
		int y = this.getPosY();
		Terminal terminal = Terminal.getInstance();
		for (int i = 0; i < this.buffer.size(); i++) {
			Object obj = this.buffer.get(i);
			// if current object is a string, just output that
			if (obj.getClass() == String.class) {
				TextGraphics textGraphics = App.terminal.newTextGraphics();
				TextGraphics textGraphics = terminal.screen.newTextGraphics();
				textGraphics.putString(x, y, obj.toString());
			}
			// check if object is a ControlCode


@@ 90,11 91,11 @@ public abstract class UIElement {
				}
				// if ControlCode is CLEAR, clear the screen
				if (obj == ControlCodes.CLEAR) {
					App.terminal.clear();
					terminal.screen.clear();
				}
			}
		}
		App.terminal.refresh();
		terminal.screen.refresh();
	}

	public String getLabel() {

M app/src/main/java/javatorio/app/UIHandler.java => app/src/main/java/javatorio/app/UIHandler.java +9 -4
@@ 9,12 9,17 @@ import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
 * The class that manages the ui, e.g. updating, keeping track what UIElements are registered
 */
public class UIHandler {
	private static UIHandler uiHandler = null;
	private ArrayList<UIElement> uiElements = new ArrayList<>();
	public Screen terminal;

	public UIHandler() throws Exception {
		terminal = new DefaultTerminalFactory().createScreen();
		terminal.startScreen();
	private UIHandler() {
	}

	public static UIHandler getInstance() {
		if (uiHandler == null) {
			uiHandler = new UIHandler();
		}
		return uiHandler;
	}

	/**

A libraries/.settings/org.eclipse.jdt.core.prefs => libraries/.settings/org.eclipse.jdt.core.prefs +4 -0
@@ 0,0 1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=18
org.eclipse.jdt.core.compiler.compliance=18
org.eclipse.jdt.core.compiler.source=18