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

Merge branch '23-fixing-tank-body-behaviour' into 'main'

#23 Fixed tank position and cleaned up controller

Closes #23

See merge request !24
parents e99e46ed 1d1535f9
No related branches found
No related tags found
1 merge request!24#23 Fixed tank position and cleaned up controller
Pipeline #209304 failed
......@@ -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();
}
}
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();
}
}
}
......@@ -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;
}
}
......@@ -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();
}
}
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