Skip to content
Snippets Groups Projects
Commit e99e46ed authored by Jan Einar Thorvaldsen's avatar Jan Einar Thorvaldsen
Browse files

Merge branch '21-have-tank-move-along-terrain' into 'main'

#21 Tank follows terrain path

Closes #21

See merge request !23
parents 635e7112 c66babad
No related branches found
No related tags found
1 merge request!23#21 Tank follows terrain path
Pipeline #209274 failed
......@@ -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;
......
......@@ -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;
}
}
......@@ -44,4 +44,8 @@ public class Terrain {
chainShape.dispose();
}
public Vector2[] getVertices() {
return vertices;
}
}
......@@ -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());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment