From c66babad4fa0cf37ea6aaad359fb24c8b629fe5c Mon Sep 17 00:00:00 2001
From: "janetho@stud.ntnu.no" <janeinartho@gmail.com>
Date: Thu, 16 Mar 2023 21:27:50 +0100
Subject: [PATCH] #21 Tank follows terrain path

---
 .../src/com/game/tankwars/model/Bullet.java   |  2 +-
 .../src/com/game/tankwars/model/Tank.java     | 48 ++++++++++++++-----
 .../src/com/game/tankwars/model/Terrain.java  |  4 ++
 .../com/game/tankwars/view/GameScreen.java    | 32 ++++++-------
 4 files changed, 56 insertions(+), 30 deletions(-)

diff --git a/frontend/core/src/com/game/tankwars/model/Bullet.java b/frontend/core/src/com/game/tankwars/model/Bullet.java
index 034b9ec..8bb9d78 100644
--- a/frontend/core/src/com/game/tankwars/model/Bullet.java
+++ b/frontend/core/src/com/game/tankwars/model/Bullet.java
@@ -20,7 +20,7 @@ public class Bullet {
 
     public Bullet(Tank tank) {
         this.position = tank.getPosition();
-        position.y += 0.5;
+        position.y += 0.5f;
         this.tank = tank;
         world = Box2dWorld.getWorld();
         bodyDef.type = BodyDef.BodyType.DynamicBody;
diff --git a/frontend/core/src/com/game/tankwars/model/Tank.java b/frontend/core/src/com/game/tankwars/model/Tank.java
index 4d5ae33..ea7328a 100644
--- a/frontend/core/src/com/game/tankwars/model/Tank.java
+++ b/frontend/core/src/com/game/tankwars/model/Tank.java
@@ -12,26 +12,30 @@ import com.badlogic.gdx.physics.box2d.PolygonShape;
 import com.badlogic.gdx.physics.box2d.World;
 import com.badlogic.gdx.utils.Array;
 import com.game.tankwars.TankWarsGame;
+import com.game.tankwars.view.GameScreen;
 
 public class Tank {
 
     public static final int TANK_MOVESPEED = 10;
     public static final int TANK_WIDTH = 2;
     public static final int TANK_HEIGHT = 1;
-
     private Vector2 position;
-
     private Rectangle bounds;
-
     private Texture texture;
     private Sprite sprite;
     World world;
     Body body;
     BodyDef bodyDef = new BodyDef();
     FixtureDef fixtureDef = new FixtureDef();
-
-    public Tank(Vector2 position, Texture texture) {
-        this.position = position;
+    Terrain terrain;
+    Vector2[] vertices;
+    int posInVertArr;
+
+    public Tank(int posInVertArr, Texture texture, Terrain terrain) {
+        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);
@@ -39,7 +43,7 @@ public class Tank {
 
         world = Box2dWorld.getWorld();
         bodyDef.type = BodyDef.BodyType.KinematicBody;
-        bodyDef.position.set(sprite.getX(), sprite.getY());
+        bodyDef.position.set(position.x, position.y);
         body = world.createBody(bodyDef);
         body.setUserData(sprite);
 
@@ -78,13 +82,33 @@ public class Tank {
 
 
     public void moveRight() {
-        body.setLinearVelocity(TANK_MOVESPEED, 0);
-        position = body.getPosition();
+        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){
+            body.setTransform(newPos.x, newPos.y + 0.11f, angle);
+            position.set(newPos);
+            posInVertArr++;
+        }
+        else {
+            body.setTransform(curPos.x, curPos.y + 0.11f, angle);
+        }
     }
 
     public void moveLeft() {
-        body.setLinearVelocity(- TANK_MOVESPEED, 0);
-        position = body.getPosition();
+        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 >= 0 + 0.075){
+            body.setTransform(newPos.x, newPos.y + 0.11f, angle);
+            position.set(newPos);
+            posInVertArr--;
+        }
+        else {
+            body.setTransform(curPos.x, curPos.y + 0.11f, angle);
+        }
     }
 
     public void stop() {body.setLinearVelocity(0, 0);}
@@ -96,6 +120,4 @@ public class Tank {
     public boolean detectCollisionRight() {
         return TankWarsGame.VIEWPORT_WIDTH - TANK_WIDTH < position.x;
     }
-
-
 }
diff --git a/frontend/core/src/com/game/tankwars/model/Terrain.java b/frontend/core/src/com/game/tankwars/model/Terrain.java
index c7b143e..860cb64 100644
--- a/frontend/core/src/com/game/tankwars/model/Terrain.java
+++ b/frontend/core/src/com/game/tankwars/model/Terrain.java
@@ -44,4 +44,8 @@ public class Terrain {
 
         chainShape.dispose();
     }
+
+    public Vector2[] getVertices() {
+        return vertices;
+    }
 }
diff --git a/frontend/core/src/com/game/tankwars/view/GameScreen.java b/frontend/core/src/com/game/tankwars/view/GameScreen.java
index 1b23b27..7fb55e8 100644
--- a/frontend/core/src/com/game/tankwars/view/GameScreen.java
+++ b/frontend/core/src/com/game/tankwars/view/GameScreen.java
@@ -28,17 +28,17 @@ public class GameScreen implements Screen {
 
     int horizontalScaling;
     int verticalScaling;
-    private SpriteBatch batch;
+    SpriteBatch batch;
 
-    private Tank tank;
-    private Box2dWorld model;
-    private World world;
-    private Terrain terrain;
+    Tank tank;
+    Box2dWorld model;
+    World world;
+    Terrain terrain;
 
-    private OrthographicCamera cam;
-    private Box2DDebugRenderer debugRenderer;
+    OrthographicCamera cam;
+    Box2DDebugRenderer debugRenderer;
 
-    private Bullet bullet;
+    Bullet bullet;
 
     public GameScreen(final TankWarsGame tankWarsGame){
         this.tankWarsGame = tankWarsGame;
@@ -49,13 +49,13 @@ public class GameScreen implements Screen {
         cam.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0);
         cam.update();
         debugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true);
-        Vector2 tankPos = new Vector2(0, cam.position.y/2);
-        tank = new Tank(tankPos, new Texture("tank-khaki.png"));
-        horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
-        verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
 
         terrain = new Terrain();
 
+        int initPos = 50;
+        tank = new Tank(initPos, new Texture("tank-khaki.png"), terrain);
+        horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
+        verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
     }
     @Override
     public void render(float delta) {
@@ -64,14 +64,14 @@ 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.detectCollisionRight()) {
+        if(Gdx.input.isKeyPressed(Input.Keys.D)) {
             tank.moveRight();
         }
-        else if(Gdx.input.isKeyPressed(Input.Keys.A) && !tank.detectCollisionLeft()) {
+        else if(Gdx.input.isKeyPressed(Input.Keys.A)) {
             tank.moveLeft();
-        } else {
-            tank.stop();
         }
+
+
         if(Gdx.input.justTouched()) {
             bullet = new Bullet(tank);
             System.out.println(tank.getPosition());
-- 
GitLab