From b2ed066bffd3d2116ee2dbedd3a52cc5c43980da Mon Sep 17 00:00:00 2001
From: Jan Einar Thorvaldsen <janetho@stud.ntnu.no>
Date: Thu, 9 Mar 2023 12:48:26 +0100
Subject: [PATCH] Resolve "Screen logic"

---
 .../src/com/game/tankwars/TankWarsGame.java   | 33 ++++-----
 .../game/tankwars/view/GameOverScreen.java    | 51 ++++++++++++++
 .../com/game/tankwars/view/GameScreen.java    | 70 +++++++++++++++++++
 .../src/com/game/tankwars/view/GameView.java  |  4 --
 .../game/tankwars/view/MainMenuScreen.java    | 54 ++++++++++++++
 5 files changed, 187 insertions(+), 25 deletions(-)
 create mode 100644 frontend/core/src/com/game/tankwars/view/GameOverScreen.java
 create mode 100644 frontend/core/src/com/game/tankwars/view/GameScreen.java
 delete mode 100644 frontend/core/src/com/game/tankwars/view/GameView.java
 create mode 100644 frontend/core/src/com/game/tankwars/view/MainMenuScreen.java

diff --git a/frontend/core/src/com/game/tankwars/TankWarsGame.java b/frontend/core/src/com/game/tankwars/TankWarsGame.java
index 9811df0..eb93a26 100644
--- a/frontend/core/src/com/game/tankwars/TankWarsGame.java
+++ b/frontend/core/src/com/game/tankwars/TankWarsGame.java
@@ -1,6 +1,10 @@
+/* NOTE: This class now extends the GDX Game-class.
+*  Previous input handling code is moved to the GameScreen-Class (for now).
+*/
 package com.game.tankwars;
 
 import com.badlogic.gdx.ApplicationAdapter;
+import com.badlogic.gdx.Game;
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Input;
 import com.badlogic.gdx.graphics.Texture;
@@ -8,37 +12,24 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.math.Vector2;
 import com.badlogic.gdx.utils.ScreenUtils;
 import com.game.tankwars.model.Tank;
+import com.game.tankwars.view.MainMenuScreen;
 
-public class TankWarsGame extends ApplicationAdapter {
-	SpriteBatch batch;
+public class TankWarsGame extends Game {
 
-	Tank tank;
-	
 	@Override
-	public void create () {
-		batch = new SpriteBatch();
-		tank = new Tank(new Vector2(50, 50), new Texture("tank-khaki.png"));
+	public void create() {
+
+		MainMenuScreen mainMenuScreen = new MainMenuScreen(this);
+		this.setScreen(mainMenuScreen);
 	}
 
 	@Override
 	public void render () {
-
-		if(Gdx.input.isKeyPressed(Input.Keys.D) && !tank.detectCollisionRight()) {
-			tank.moveRight();
-		}
-		if(Gdx.input.isKeyPressed(Input.Keys.A) && !tank.detectCollisionLeft()) {
-			tank.moveLeft();
-		}
-
-		ScreenUtils.clear(0, 0, 0, 1);
-		batch.begin();
-		batch.draw(tank.getTexture(), tank.getPosition().x, tank.getPosition().y, Tank.TANK_WIDTH, Tank.TANK_HEIGHT);
-		batch.end();
+		super.render();
 	}
 	
 	@Override
 	public void dispose () {
-		batch.dispose();
-		tank.getTexture().dispose();
+
 	}
 }
diff --git a/frontend/core/src/com/game/tankwars/view/GameOverScreen.java b/frontend/core/src/com/game/tankwars/view/GameOverScreen.java
new file mode 100644
index 0000000..077d285
--- /dev/null
+++ b/frontend/core/src/com/game/tankwars/view/GameOverScreen.java
@@ -0,0 +1,51 @@
+package com.game.tankwars.view;
+
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.game.tankwars.TankWarsGame;
+
+public class GameOverScreen implements Screen {
+
+    final TankWarsGame tankWarsGame;
+    OrthographicCamera camera;
+
+    public GameOverScreen(final TankWarsGame tankWarsGame){
+        this.tankWarsGame = tankWarsGame;
+        camera = new OrthographicCamera();
+    }
+
+    @Override
+    public void show() {
+
+    }
+
+    @Override
+    public void render(float delta) {
+
+    }
+
+    @Override
+    public void resize(int width, int height) {
+
+    }
+
+    @Override
+    public void pause() {
+
+    }
+
+    @Override
+    public void resume() {
+
+    }
+
+    @Override
+    public void hide() {
+
+    }
+
+    @Override
+    public void dispose() {
+
+    }
+}
diff --git a/frontend/core/src/com/game/tankwars/view/GameScreen.java b/frontend/core/src/com/game/tankwars/view/GameScreen.java
new file mode 100644
index 0000000..d92f2fb
--- /dev/null
+++ b/frontend/core/src/com/game/tankwars/view/GameScreen.java
@@ -0,0 +1,70 @@
+package com.game.tankwars.view;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.math.Vector2;
+import com.badlogic.gdx.utils.ScreenUtils;
+import com.game.tankwars.TankWarsGame;
+import com.game.tankwars.model.Tank;
+
+public class GameScreen implements Screen {
+    final TankWarsGame tankWarsGame;
+    OrthographicCamera camera;
+    SpriteBatch batch;
+    Tank tank;
+
+    public GameScreen(final TankWarsGame tankWarsGame){
+        this.tankWarsGame = tankWarsGame;
+        camera = new OrthographicCamera();
+
+        batch = new SpriteBatch();
+        tank = new Tank(new Vector2(50, 50), new Texture("tank-khaki.png"));
+    }
+    @Override
+    public void render(float delta) {
+        if(Gdx.input.isKeyPressed(Input.Keys.D)) {
+            tank.moveRight();
+        }
+        if(Gdx.input.isKeyPressed(Input.Keys.A)) {
+            tank.moveLeft();
+        }
+
+        ScreenUtils.clear(0, 0, 0, 1);
+        batch.begin();
+        batch.draw(tank.getTexture(), tank.getPosition().x, tank.getPosition().y, Tank.TANK_WIDTH, Tank.TANK_HEIGHT);
+        batch.end();
+    }
+
+    @Override
+    public void show() {
+
+    }
+    @Override
+    public void resize(int width, int height) {
+
+    }
+
+    @Override
+    public void pause() {
+
+    }
+
+    @Override
+    public void resume() {
+
+    }
+
+    @Override
+    public void hide() {
+
+    }
+
+    @Override
+    public void dispose() {
+
+    }
+}
diff --git a/frontend/core/src/com/game/tankwars/view/GameView.java b/frontend/core/src/com/game/tankwars/view/GameView.java
deleted file mode 100644
index 3745607..0000000
--- a/frontend/core/src/com/game/tankwars/view/GameView.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.game.tankwars.view;
-
-public class GameView {
-}
diff --git a/frontend/core/src/com/game/tankwars/view/MainMenuScreen.java b/frontend/core/src/com/game/tankwars/view/MainMenuScreen.java
new file mode 100644
index 0000000..82fccd7
--- /dev/null
+++ b/frontend/core/src/com/game/tankwars/view/MainMenuScreen.java
@@ -0,0 +1,54 @@
+package com.game.tankwars.view;
+
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.game.tankwars.TankWarsGame;
+
+public class MainMenuScreen implements Screen {
+    final TankWarsGame tankWarsGame;
+    OrthographicCamera camera;
+
+    public MainMenuScreen(final TankWarsGame tankWarsGame){
+        this.tankWarsGame = tankWarsGame;
+        camera = new OrthographicCamera();
+
+        //NB! Temporarily directing to GameScreen, this is subject to change
+        GameScreen gameScreen = new GameScreen(this.tankWarsGame);
+        tankWarsGame.setScreen(gameScreen);
+    }
+
+    @Override
+    public void render(float delta) {
+
+    }
+
+    @Override
+    public void show() {
+
+    }
+
+    @Override
+    public void resize(int width, int height) {
+
+    }
+
+    @Override
+    public void pause() {
+
+    }
+
+    @Override
+    public void resume() {
+
+    }
+
+    @Override
+    public void hide() {
+
+    }
+
+    @Override
+    public void dispose() {
+
+    }
+}
-- 
GitLab