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