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 { ...@@ -20,7 +20,7 @@ public class Bullet {
public Bullet(Tank tank) { public Bullet(Tank tank) {
this.position = tank.getPosition(); this.position = tank.getPosition();
position.y += 0.5; position.y += 0.5f;
this.tank = tank; this.tank = tank;
world = Box2dWorld.getWorld(); world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.type = BodyDef.BodyType.DynamicBody;
......
...@@ -12,26 +12,30 @@ import com.badlogic.gdx.physics.box2d.PolygonShape; ...@@ -12,26 +12,30 @@ import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.game.tankwars.TankWarsGame; import com.game.tankwars.TankWarsGame;
import com.game.tankwars.view.GameScreen;
public class Tank { public class Tank {
public static final int TANK_MOVESPEED = 10; public static final int TANK_MOVESPEED = 10;
public static final int TANK_WIDTH = 2; public static final int TANK_WIDTH = 2;
public static final int TANK_HEIGHT = 1; public static final int TANK_HEIGHT = 1;
private Vector2 position; private Vector2 position;
private Rectangle bounds; private Rectangle bounds;
private Texture texture; private Texture texture;
private Sprite sprite; private Sprite sprite;
World world; World world;
Body body; Body body;
BodyDef bodyDef = new BodyDef(); BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef(); FixtureDef fixtureDef = new FixtureDef();
Terrain terrain;
public Tank(Vector2 position, Texture texture) { Vector2[] vertices;
this.position = position; 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.bounds = new Rectangle(position.x, position.y, TANK_WIDTH, TANK_HEIGHT);
this.texture = texture; this.texture = texture;
sprite = new Sprite(texture); sprite = new Sprite(texture);
...@@ -39,7 +43,7 @@ public class Tank { ...@@ -39,7 +43,7 @@ public class Tank {
world = Box2dWorld.getWorld(); world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.type = BodyDef.BodyType.KinematicBody;
bodyDef.position.set(sprite.getX(), sprite.getY()); bodyDef.position.set(position.x, position.y);
body = world.createBody(bodyDef); body = world.createBody(bodyDef);
body.setUserData(sprite); body.setUserData(sprite);
...@@ -78,13 +82,33 @@ public class Tank { ...@@ -78,13 +82,33 @@ public class Tank {
public void moveRight() { public void moveRight() {
body.setLinearVelocity(TANK_MOVESPEED, 0); Vector2 curPos = vertices[posInVertArr];
position = body.getPosition(); 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() { public void moveLeft() {
body.setLinearVelocity(- TANK_MOVESPEED, 0); Vector2 curPos = vertices[posInVertArr];
position = body.getPosition(); 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);} public void stop() {body.setLinearVelocity(0, 0);}
...@@ -96,6 +120,4 @@ public class Tank { ...@@ -96,6 +120,4 @@ public class Tank {
public boolean detectCollisionRight() { public boolean detectCollisionRight() {
return TankWarsGame.VIEWPORT_WIDTH - TANK_WIDTH < position.x; return TankWarsGame.VIEWPORT_WIDTH - TANK_WIDTH < position.x;
} }
} }
...@@ -44,4 +44,8 @@ public class Terrain { ...@@ -44,4 +44,8 @@ public class Terrain {
chainShape.dispose(); chainShape.dispose();
} }
public Vector2[] getVertices() {
return vertices;
}
} }
...@@ -28,17 +28,17 @@ public class GameScreen implements Screen { ...@@ -28,17 +28,17 @@ public class GameScreen implements Screen {
int horizontalScaling; int horizontalScaling;
int verticalScaling; int verticalScaling;
private SpriteBatch batch; SpriteBatch batch;
private Tank tank; Tank tank;
private Box2dWorld model; Box2dWorld model;
private World world; World world;
private Terrain terrain; Terrain terrain;
private OrthographicCamera cam; OrthographicCamera cam;
private Box2DDebugRenderer debugRenderer; Box2DDebugRenderer debugRenderer;
private Bullet bullet; Bullet bullet;
public GameScreen(final TankWarsGame tankWarsGame){ public GameScreen(final TankWarsGame tankWarsGame){
this.tankWarsGame = tankWarsGame; this.tankWarsGame = tankWarsGame;
...@@ -49,13 +49,13 @@ public class GameScreen implements Screen { ...@@ -49,13 +49,13 @@ public class GameScreen implements Screen {
cam.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0); cam.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0);
cam.update(); cam.update();
debugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true); 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(); 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 @Override
public void render(float delta) { public void render(float delta) {
...@@ -64,14 +64,14 @@ public class GameScreen implements Screen { ...@@ -64,14 +64,14 @@ public class GameScreen implements Screen {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, cam.combined); debugRenderer.render(world, cam.combined);
if(Gdx.input.isKeyPressed(Input.Keys.D) && !tank.detectCollisionRight()) { if(Gdx.input.isKeyPressed(Input.Keys.D)) {
tank.moveRight(); tank.moveRight();
} }
else if(Gdx.input.isKeyPressed(Input.Keys.A) && !tank.detectCollisionLeft()) { else if(Gdx.input.isKeyPressed(Input.Keys.A)) {
tank.moveLeft(); tank.moveLeft();
} else {
tank.stop();
} }
if(Gdx.input.justTouched()) { if(Gdx.input.justTouched()) {
bullet = new Bullet(tank); bullet = new Bullet(tank);
System.out.println(tank.getPosition()); 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