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

Merge branch '29-render-opponent-tank' into 'main'

#29 Render opponent tank

Closes #29

See merge request !30
parents af749d59 403fd1b0
Branches 30-get-opponent-json
No related tags found
1 merge request!30#29 Render opponent tank
Pipeline #213693 failed
......@@ -17,7 +17,7 @@ public class GameController {
this.tankWarsGame = tankWarsGame;
}
public void checkKeyInput(){
public void checkKeyInput(Tank tank){
if(Gdx.input.isKeyPressed(Input.Keys.D)) {
tank.moveRight();
}
......
......@@ -33,13 +33,13 @@ public class Tank {
Body cannon;
BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef();
RevoluteJoint joint;
Terrain terrain;
private Vector2[] vertices;
int posInVertArr;
float cannonAngle = 90;
boolean directionLeft;
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame) {
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft) {
VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
......@@ -80,6 +80,14 @@ public class Tank {
cannon.setUserData(cannonSprite);
shape.dispose();
updateCannonPos();
moveLeft();
if(directionLeft){
moveRight();
}
else{
moveLeft();
}
}
public void moveRight() {
......@@ -87,6 +95,10 @@ public class Tank {
Vector2 newPos = new Vector2(vertices[posInVertArr + 1]);
float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad();
if(directionLeft){
chassisSprite.flip(true, false);
directionLeft = false;
}
if (chassis.getPosition().x <= VIEWPORT_WIDTH - TANK_WIDTH){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
......@@ -104,6 +116,11 @@ public class Tank {
Vector2 newPos = new Vector2(vertices[posInVertArr - 1]);
float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad();
if(!directionLeft){
chassisSprite.flip(true, false);
directionLeft = true;
}
if (chassis.getPosition().x >= TANK_WIDTH){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
......
......@@ -32,7 +32,8 @@ public class GameScreen implements Screen {
int verticalScaling;
SpriteBatch batch;
ShapeRenderer shapeRender;
Tank tank;
Tank myTank;
Tank opponentTank;
Box2dWorld model;
World world;
Terrain terrain;
......@@ -50,6 +51,7 @@ public class GameScreen implements Screen {
batch = new SpriteBatch();
shapeRender = new ShapeRenderer();
model = new Box2dWorld();
world = Box2dWorld.getWorld();
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
......@@ -59,12 +61,24 @@ public class GameScreen implements Screen {
terrain = new Terrain();
int initPos = 50;
tank = new Tank(initPos, new Texture("tank-khaki.png"), new Texture("cannon.png"),terrain, tankWarsGame);
int myPos = 50;
int opponentPos = terrain.getVertices().length - 150;
myTank = new Tank(myPos,
new Texture("tank-khaki.png"),
new Texture("cannon.png"),
terrain,
tankWarsGame, true);
opponentTank = new Tank(opponentPos,
new Texture("tank-khaki.png"),
new Texture("cannon.png"),
terrain,
tankWarsGame, false);
horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
controller = new GameController(tank, tankWarsGame);
controller = new GameController(myTank, tankWarsGame);
}
@Override
public void render(float delta) {
......@@ -74,7 +88,7 @@ public class GameScreen implements Screen {
debugRenderer.render(world, cam.combined);
shapeRender.setProjectionMatrix(cam.combined);
controller.checkKeyInput();
controller.checkKeyInput(myTank);
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
......@@ -85,12 +99,19 @@ public class GameScreen implements Screen {
if (s != null) {
s.setPosition(b.getPosition().x * (float) horizontalScaling - s.getWidth() / 2, (b.getPosition().y + 0.25f) * (float) verticalScaling);
if (s.equals(tank.getChassisSprite())) {
s.setRotation(tank.getAngle());
if (s.equals(myTank.getChassisSprite())) {
s.setRotation(myTank.getAngle());
}
if (s.equals(tank.getCannonSprite())) {
if (s.equals(opponentTank.getChassisSprite())) {
s.setRotation(opponentTank.getAngle());
}
if (s.equals(myTank.getCannonSprite())) {
s.setOrigin(s.getWidth() / 2, 0);
s.setRotation(tank.getCannonAngle());
s.setRotation(myTank.getCannonAngle());
}
if (s.equals(opponentTank.getCannonSprite())) {
s.setOrigin(s.getWidth() / s.getWidth(), 0);
s.setRotation(opponentTank.getCannonAngle());
}
}
}
......@@ -100,8 +121,10 @@ public class GameScreen implements Screen {
shapeRender.end();
batch.begin();
tank.getChassisSprite().draw(batch);
tank.getCannonSprite().draw(batch);
myTank.getChassisSprite().draw(batch);
myTank.getCannonSprite().draw(batch);
opponentTank.getChassisSprite().draw(batch);
opponentTank.getCannonSprite().draw(batch);
batch.end();
}
......@@ -131,8 +154,8 @@ public class GameScreen implements Screen {
@Override
public void dispose() {
tank.getChassisTexture().dispose();
tank.getCannonTexture().dispose();
myTank.getChassisTexture().dispose();
myTank.getCannonTexture().dispose();
batch.dispose();
shapeRender.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