From 1d1535f940a58b485551ad1944115a57f10f8063 Mon Sep 17 00:00:00 2001
From: "janetho@stud.ntnu.no" <janeinartho@gmail.com>
Date: Mon, 20 Mar 2023 10:50:01 +0100
Subject: [PATCH] #23 Fixed tank position and cleaned up controller

---
 .../src/com/game/tankwars/TankWarsGame.java   | 11 ++-
 .../tankwars/controller/GameController.java   | 28 +++++++
 .../src/com/game/tankwars/model/Tank.java     | 81 ++++++++++---------
 .../com/game/tankwars/view/GameScreen.java    | 38 ++++-----
 4 files changed, 97 insertions(+), 61 deletions(-)

diff --git a/frontend/core/src/com/game/tankwars/TankWarsGame.java b/frontend/core/src/com/game/tankwars/TankWarsGame.java
index 2a5d771..34d118b 100644
--- a/frontend/core/src/com/game/tankwars/TankWarsGame.java
+++ b/frontend/core/src/com/game/tankwars/TankWarsGame.java
@@ -69,6 +69,14 @@ public class TankWarsGame extends Game {
 		return font;
 	}
 
+	public int getViewportWidth(){
+		return VIEWPORT_WIDTH;
+	}
+
+	public int getViewportHeight(){
+		return VIEWPORT_HEIGHT;
+	}
+
 	@Override
 	public void render () {
 		super.render();
@@ -76,6 +84,7 @@ public class TankWarsGame extends Game {
 	
 	@Override
 	public void dispose () {
-
+		batch.dispose();
+		font.dispose();
 	}
 }
diff --git a/frontend/core/src/com/game/tankwars/controller/GameController.java b/frontend/core/src/com/game/tankwars/controller/GameController.java
index 2388885..b04047d 100644
--- a/frontend/core/src/com/game/tankwars/controller/GameController.java
+++ b/frontend/core/src/com/game/tankwars/controller/GameController.java
@@ -1,4 +1,32 @@
 package com.game.tankwars.controller;
 
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input;
+import com.game.tankwars.TankWarsGame;
+import com.game.tankwars.model.Bullet;
+import com.game.tankwars.model.Tank;
+
 public class GameController {
+    TankWarsGame tankWarsGame;
+    Tank tank;
+    Bullet bullet;
+    public GameController(Tank tank, TankWarsGame tankWarsGame){
+        this.tank = tank;
+        this.tankWarsGame = tankWarsGame;
+    }
+
+    public void checkKeyInput(){
+        if(Gdx.input.isKeyPressed(Input.Keys.D)) {
+            tank.moveRight();
+        }
+        else if(Gdx.input.isKeyPressed(Input.Keys.A)) {
+            tank.moveLeft();
+        }
+
+        if(Gdx.input.justTouched()) {
+            bullet = new Bullet(tank);
+            System.out.println(tank.getPosition());
+            bullet.shoot();
+        }
+    }
 }
diff --git a/frontend/core/src/com/game/tankwars/model/Tank.java b/frontend/core/src/com/game/tankwars/model/Tank.java
index ea7328a..b67394a 100644
--- a/frontend/core/src/com/game/tankwars/model/Tank.java
+++ b/frontend/core/src/com/game/tankwars/model/Tank.java
@@ -15,10 +15,11 @@ import com.game.tankwars.TankWarsGame;
 import com.game.tankwars.view.GameScreen;
 
 public class Tank {
-
-    public static final int TANK_MOVESPEED = 10;
+    TankWarsGame tankWarsGame;
     public static final int TANK_WIDTH = 2;
     public static final int TANK_HEIGHT = 1;
+    int VIEWPORT_WIDTH;
+    int VIEWPORT_HEIGHT;
     private Vector2 position;
     private Rectangle bounds;
     private Texture texture;
@@ -31,15 +32,22 @@ public class Tank {
     Vector2[] vertices;
     int posInVertArr;
 
-    public Tank(int posInVertArr, Texture texture, Terrain terrain) {
+    public Tank(int posInVertArr, Texture texture, Terrain terrain, TankWarsGame tankWarsGame) {
+        VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
+        VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
+
         this.terrain = terrain;
         vertices = terrain.getVertices();
+
         this.posInVertArr = posInVertArr;
         position = vertices[posInVertArr];
+
         this.bounds = new Rectangle(position.x, position.y, TANK_WIDTH, TANK_HEIGHT);
+
         this.texture = texture;
         sprite = new Sprite(texture);
-        sprite.setPosition(TankWarsGame.VIEWPORT_WIDTH/2, TankWarsGame.VIEWPORT_HEIGHT/2 + TANK_HEIGHT);
+        sprite.setPosition(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2 + TANK_HEIGHT);
+        sprite.setRotation(0);
 
         world = Box2dWorld.getWorld();
         bodyDef.type = BodyDef.BodyType.KinematicBody;
@@ -54,40 +62,14 @@ public class Tank {
         shape.dispose();
     }
 
-    public Vector2 getPosition() {
-        return position;
-    }
-
-    public Rectangle getBounds() {
-        return bounds;
-    }
-
-    public Texture getTexture() {
-        return texture;
-    }
-
-    public Sprite getSprite() {return sprite;}
-
-    public void setPosition(Vector2 position) {
-        this.position = position;
-    }
-
-    public void setBounds(Rectangle bounds) {
-        this.bounds = bounds;
-    }
-
-    public void setTexture(Texture texture) {
-        this.texture = texture;
-    }
-
-
     public void moveRight() {
         Vector2 curPos = vertices[posInVertArr];
         Vector2 newPos = vertices[posInVertArr + 1];
         float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad();
 
-        if (body.getPosition().x <= 80 - 0.075){
+        if (body.getPosition().x <= VIEWPORT_WIDTH - TANK_WIDTH){
             body.setTransform(newPos.x, newPos.y + 0.11f, angle);
+            sprite.setRotation(angle);
             position.set(newPos);
             posInVertArr++;
         }
@@ -101,8 +83,9 @@ public class Tank {
         Vector2 newPos = vertices[posInVertArr - 1];
         float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad();
 
-        if (body.getPosition().x >= 0 + 0.075){
+        if (body.getPosition().x >= TANK_WIDTH){
             body.setTransform(newPos.x, newPos.y + 0.11f, angle);
+            sprite.setRotation(angle);
             position.set(newPos);
             posInVertArr--;
         }
@@ -111,13 +94,35 @@ public class Tank {
         }
     }
 
-    public void stop() {body.setLinearVelocity(0, 0);}
+    public float getAngle(){
+        Vector2 curPos = vertices[posInVertArr];
+        Vector2 newPos = vertices[posInVertArr + 1];
+        return new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleDeg();
+    }
+
+    public Vector2 getPosition() {
+        return position;
+    }
+
+    public Rectangle getBounds() {
+        return bounds;
+    }
+
+    public Texture getTexture() {
+        return texture;
+    }
+
+    public Sprite getSprite() {return sprite;}
+
+    public void setPosition(Vector2 position) {
+        this.position = position;
+    }
 
-    public boolean detectCollisionLeft() {
-        return position.x < 0;
+    public void setBounds(Rectangle bounds) {
+        this.bounds = bounds;
     }
 
-    public boolean detectCollisionRight() {
-        return TankWarsGame.VIEWPORT_WIDTH - TANK_WIDTH < position.x;
+    public void setTexture(Texture texture) {
+        this.texture = texture;
     }
 }
diff --git a/frontend/core/src/com/game/tankwars/view/GameScreen.java b/frontend/core/src/com/game/tankwars/view/GameScreen.java
index 7fb55e8..c87f852 100644
--- a/frontend/core/src/com/game/tankwars/view/GameScreen.java
+++ b/frontend/core/src/com/game/tankwars/view/GameScreen.java
@@ -16,6 +16,7 @@ import com.badlogic.gdx.physics.box2d.World;
 import com.badlogic.gdx.utils.Array;
 import com.badlogic.gdx.utils.ScreenUtils;
 import com.game.tankwars.TankWarsGame;
+import com.game.tankwars.controller.GameController;
 import com.game.tankwars.model.Box2dWorld;
 import com.game.tankwars.model.Bullet;
 import com.game.tankwars.model.Tank;
@@ -23,25 +24,26 @@ import com.game.tankwars.model.Terrain;
 
 public class GameScreen implements Screen {
     final TankWarsGame tankWarsGame;
-    public static int VIEWPORT_WIDTH = 80;
-    public static int VIEWPORT_HEIGHT = 50;
-
+    int VIEWPORT_WIDTH;
+    int VIEWPORT_HEIGHT;
     int horizontalScaling;
     int verticalScaling;
     SpriteBatch batch;
-
     Tank tank;
     Box2dWorld model;
     World world;
     Terrain terrain;
-
     OrthographicCamera cam;
     Box2DDebugRenderer debugRenderer;
-
     Bullet bullet;
+    GameController controller;
 
     public GameScreen(final TankWarsGame tankWarsGame){
         this.tankWarsGame = tankWarsGame;
+
+        VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
+        VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
+
         batch = new SpriteBatch();
         model = new Box2dWorld();
         world = Box2dWorld.getWorld();
@@ -53,9 +55,11 @@ public class GameScreen implements Screen {
         terrain = new Terrain();
 
         int initPos = 50;
-        tank = new Tank(initPos, new Texture("tank-khaki.png"), terrain);
+        tank = new Tank(initPos, new Texture("tank-khaki.png"), terrain, tankWarsGame);
         horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
         verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
+
+        controller = new GameController(tank, tankWarsGame);
     }
     @Override
     public void render(float delta) {
@@ -64,19 +68,7 @@ public class GameScreen implements Screen {
         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         debugRenderer.render(world, cam.combined);
 
-        if(Gdx.input.isKeyPressed(Input.Keys.D)) {
-            tank.moveRight();
-        }
-        else if(Gdx.input.isKeyPressed(Input.Keys.A)) {
-            tank.moveLeft();
-        }
-
-
-        if(Gdx.input.justTouched()) {
-            bullet = new Bullet(tank);
-            System.out.println(tank.getPosition());
-            bullet.shoot();
-        }
+        controller.checkKeyInput();
 
         Array<Body> bodies = new Array<Body>();
         world.getBodies(bodies);
@@ -85,7 +77,8 @@ public class GameScreen implements Screen {
             Sprite s = (Sprite) b.getUserData();
 
             if (s != null) {
-                s.setPosition(b.getPosition().x * (float) horizontalScaling, b.getPosition().y * (float) verticalScaling);
+                s.setPosition(b.getPosition().x * (float) horizontalScaling - s.getWidth()/2 , b.getPosition().y * (float) verticalScaling);
+                s.setRotation(tank.getAngle());
             }
         }
 
@@ -120,6 +113,7 @@ public class GameScreen implements Screen {
 
     @Override
     public void dispose() {
-
+        tank.getTexture().dispose();
+        batch.dispose();
     }
 }
-- 
GitLab