Skip to content
Snippets Groups Projects
Commit 1d1535f9 authored by janetho@stud.ntnu.no's avatar janetho@stud.ntnu.no
Browse files

#23 Fixed tank position and cleaned up controller

parent e99e46ed
No related branches found
No related tags found
1 merge request!24#23 Fixed tank position and cleaned up controller
Pipeline #209110 passed
...@@ -69,6 +69,14 @@ public class TankWarsGame extends Game { ...@@ -69,6 +69,14 @@ public class TankWarsGame extends Game {
return font; return font;
} }
public int getViewportWidth(){
return VIEWPORT_WIDTH;
}
public int getViewportHeight(){
return VIEWPORT_HEIGHT;
}
@Override @Override
public void render () { public void render () {
super.render(); super.render();
...@@ -76,6 +84,7 @@ public class TankWarsGame extends Game { ...@@ -76,6 +84,7 @@ public class TankWarsGame extends Game {
@Override @Override
public void dispose () { public void dispose () {
batch.dispose();
font.dispose();
} }
} }
package com.game.tankwars.controller; 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 { 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; ...@@ -15,10 +15,11 @@ import com.game.tankwars.TankWarsGame;
import com.game.tankwars.view.GameScreen; import com.game.tankwars.view.GameScreen;
public class Tank { public class Tank {
TankWarsGame tankWarsGame;
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;
int VIEWPORT_WIDTH;
int VIEWPORT_HEIGHT;
private Vector2 position; private Vector2 position;
private Rectangle bounds; private Rectangle bounds;
private Texture texture; private Texture texture;
...@@ -31,15 +32,22 @@ public class Tank { ...@@ -31,15 +32,22 @@ public class Tank {
Vector2[] vertices; Vector2[] vertices;
int posInVertArr; 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; this.terrain = terrain;
vertices = terrain.getVertices(); vertices = terrain.getVertices();
this.posInVertArr = posInVertArr; this.posInVertArr = posInVertArr;
position = vertices[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);
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(); world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.type = BodyDef.BodyType.KinematicBody;
...@@ -54,40 +62,14 @@ public class Tank { ...@@ -54,40 +62,14 @@ public class Tank {
shape.dispose(); 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() { public void moveRight() {
Vector2 curPos = vertices[posInVertArr]; Vector2 curPos = vertices[posInVertArr];
Vector2 newPos = vertices[posInVertArr + 1]; Vector2 newPos = vertices[posInVertArr + 1];
float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad(); 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); body.setTransform(newPos.x, newPos.y + 0.11f, angle);
sprite.setRotation(angle);
position.set(newPos); position.set(newPos);
posInVertArr++; posInVertArr++;
} }
...@@ -101,8 +83,9 @@ public class Tank { ...@@ -101,8 +83,9 @@ public class Tank {
Vector2 newPos = vertices[posInVertArr - 1]; Vector2 newPos = vertices[posInVertArr - 1];
float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad(); 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); body.setTransform(newPos.x, newPos.y + 0.11f, angle);
sprite.setRotation(angle);
position.set(newPos); position.set(newPos);
posInVertArr--; posInVertArr--;
} }
...@@ -111,13 +94,35 @@ public class Tank { ...@@ -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() { public void setBounds(Rectangle bounds) {
return position.x < 0; this.bounds = bounds;
} }
public boolean detectCollisionRight() { public void setTexture(Texture texture) {
return TankWarsGame.VIEWPORT_WIDTH - TANK_WIDTH < position.x; this.texture = texture;
} }
} }
...@@ -16,6 +16,7 @@ import com.badlogic.gdx.physics.box2d.World; ...@@ -16,6 +16,7 @@ import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils; import com.badlogic.gdx.utils.ScreenUtils;
import com.game.tankwars.TankWarsGame; import com.game.tankwars.TankWarsGame;
import com.game.tankwars.controller.GameController;
import com.game.tankwars.model.Box2dWorld; import com.game.tankwars.model.Box2dWorld;
import com.game.tankwars.model.Bullet; import com.game.tankwars.model.Bullet;
import com.game.tankwars.model.Tank; import com.game.tankwars.model.Tank;
...@@ -23,25 +24,26 @@ import com.game.tankwars.model.Terrain; ...@@ -23,25 +24,26 @@ import com.game.tankwars.model.Terrain;
public class GameScreen implements Screen { public class GameScreen implements Screen {
final TankWarsGame tankWarsGame; final TankWarsGame tankWarsGame;
public static int VIEWPORT_WIDTH = 80; int VIEWPORT_WIDTH;
public static int VIEWPORT_HEIGHT = 50; int VIEWPORT_HEIGHT;
int horizontalScaling; int horizontalScaling;
int verticalScaling; int verticalScaling;
SpriteBatch batch; SpriteBatch batch;
Tank tank; Tank tank;
Box2dWorld model; Box2dWorld model;
World world; World world;
Terrain terrain; Terrain terrain;
OrthographicCamera cam; OrthographicCamera cam;
Box2DDebugRenderer debugRenderer; Box2DDebugRenderer debugRenderer;
Bullet bullet; Bullet bullet;
GameController controller;
public GameScreen(final TankWarsGame tankWarsGame){ public GameScreen(final TankWarsGame tankWarsGame){
this.tankWarsGame = tankWarsGame; this.tankWarsGame = tankWarsGame;
VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
batch = new SpriteBatch(); batch = new SpriteBatch();
model = new Box2dWorld(); model = new Box2dWorld();
world = Box2dWorld.getWorld(); world = Box2dWorld.getWorld();
...@@ -53,9 +55,11 @@ public class GameScreen implements Screen { ...@@ -53,9 +55,11 @@ public class GameScreen implements Screen {
terrain = new Terrain(); terrain = new Terrain();
int initPos = 50; 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; horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT; verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
controller = new GameController(tank, tankWarsGame);
} }
@Override @Override
public void render(float delta) { public void render(float delta) {
...@@ -64,19 +68,7 @@ public class GameScreen implements Screen { ...@@ -64,19 +68,7 @@ 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)) { controller.checkKeyInput();
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();
}
Array<Body> bodies = new Array<Body>(); Array<Body> bodies = new Array<Body>();
world.getBodies(bodies); world.getBodies(bodies);
...@@ -85,7 +77,8 @@ public class GameScreen implements Screen { ...@@ -85,7 +77,8 @@ public class GameScreen implements Screen {
Sprite s = (Sprite) b.getUserData(); Sprite s = (Sprite) b.getUserData();
if (s != null) { 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 { ...@@ -120,6 +113,7 @@ public class GameScreen implements Screen {
@Override @Override
public void dispose() { 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