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

Merge branch '42-render-the-two-tanks-correctly' into 'main'

#42 fixed client-side render of tanks

Closes #42

See merge request !52
parents ae1833ac 12b1e855
No related branches found
No related tags found
1 merge request!52#42 fixed client-side render of tanks
Pipeline #215247 failed
frontend/assets/camo-tank-1.png

444 B

frontend/assets/camo-tank-barrel.png

852 B

......@@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.game.tankwars.view.FindGameScreen;
import com.game.tankwars.view.GameScreen;
import com.game.tankwars.view.LoginScreen;
public class TankWarsGame extends Game {
......@@ -25,7 +26,7 @@ public class TankWarsGame extends Game {
@Override
public void create() {
this.setScreen(new LoginScreen(this));
this.setScreen(new GameScreen(this));
}
public int getViewportWidth(){
......
......@@ -37,11 +37,13 @@ public class Tank {
Terrain terrain;
private Vector2[] vertices;
int posInVertArr;
float cannonAngle = 90;
float cannonAngle;
private int power;
boolean directionLeft;
int fuel = 150;
int health = 100;
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft) {
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft, float cannonAngle) {
VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
......@@ -52,16 +54,15 @@ public class Tank {
position = vertices[posInVertArr];
this.power = 25;
this.cannonAngle = cannonAngle;
this.bounds = new Rectangle(position.x, position.y, TANK_WIDTH, TANK_HEIGHT);
this.chassisTexture = chassisTexture;
chassisSprite = new Sprite(chassisTexture);
chassisSprite.scale(0.4f);
this.cannonTexture = cannonTexture;
cannonSprite = new Sprite(cannonTexture);
cannonSprite.scale(-0.1f);
world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.KinematicBody;
......@@ -75,11 +76,12 @@ public class Tank {
chassis = world.createBody(bodyDef);
chassis.setUserData(chassisSprite);
chassis.createFixture(fixtureDef);
chassisSprite.scale(0.2f);
//Cannon
shape.setAsBox(CANNON_WIDTH, CANNON_HEIGHT);
cannonSprite.setOrigin(0, cannonSprite.getHeight()/2);
cannonSprite.setOrigin(1, cannonSprite.getHeight()/2);
cannon = world.createBody(bodyDef);
cannon.setUserData(cannonSprite);
......@@ -103,12 +105,13 @@ public class Tank {
chassisSprite.flip(true, false);
directionLeft = false;
}
if (chassis.getPosition().x <= TankWarsGame.GAMEPORT_WIDTH - TANK_WIDTH){
if (chassis.getPosition().x <= TankWarsGame.GAMEPORT_WIDTH/TankWarsGame.SCALE - TANK_WIDTH && fuel > 0){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
chassisSprite.setRotation(angle);
updateCannonPos();
posInVertArr++;
fuel--;
}
else {
chassis.setTransform(curPos.x, curPos.y + 0.11f, angle);
......@@ -125,12 +128,13 @@ public class Tank {
directionLeft = true;
}
if (chassis.getPosition().x >= TANK_WIDTH){
if (chassis.getPosition().x >= TANK_WIDTH && fuel > 0){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
chassisSprite.setRotation(angle);
updateCannonPos();
posInVertArr--;
fuel--;
}
else {
chassis.setTransform(curPos.x, curPos.y + 0.11f, angle);
......@@ -145,18 +149,18 @@ public class Tank {
public void updateCannonPos(){
Vector2 chassisPos = chassis.getPosition();
chassisPos.add(0, 1);
chassisPos.add(0, 0.35f);
cannon.setTransform(chassisPos, cannon.getAngle());
}
public void rotateCannonRight(){
if(cannonAngle > -90) {
if(cannonAngle > 90) {
cannonAngle--;
}
}
public void rotateCannonLeft(){
if(cannonAngle < 90) {
if(cannonAngle < 270) {
cannonAngle++;
}
}
......@@ -179,23 +183,20 @@ public class Tank {
}
public Sprite getChassisSprite() {return chassisSprite;}
public Sprite getCannonSprite() {return cannonSprite;}
public int getPower() {
return power;
}
public int getFuel(){return fuel;}
public void setPosition(Vector2 position) {
this.position = position;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public void setPower(int power) {
this.power = power;
}
public void setChassisTexture(Texture texture) {
this.chassisTexture = texture;
}
......
......@@ -26,6 +26,8 @@ import com.game.tankwars.model.Bullet;
import com.game.tankwars.model.Tank;
import com.game.tankwars.model.Terrain;
import java.util.Arrays;
public class GameScreen implements Screen {
final TankWarsGame tankWarsGame;
int VIEWPORT_WIDTH;
......@@ -43,9 +45,7 @@ public class GameScreen implements Screen {
OrthographicCamera worldCam;
OrthographicCamera hudCam;
Box2DDebugRenderer debugRenderer;
Bullet bullet;
GameController controller;
Mesh groundMesh;
public GameScreen(final TankWarsGame tankWarsGame){
this.tankWarsGame = tankWarsGame;
......@@ -72,19 +72,22 @@ public class GameScreen implements Screen {
terrain = new Terrain();
int myPos = 100;
int opponentPos = terrain.getVertices().length - 250;
int myPos = 50;
int opponentPos = terrain.getVertices().length - 220;
myTank = new Tank(myPos,
new Texture("tank-khaki.png"),
new Texture("cannon.png"),
new Texture("camo-tank-1.png"),
new Texture("camo-tank-barrel.png"),
terrain,
tankWarsGame, true);
tankWarsGame, true,
120);
opponentTank = new Tank(opponentPos,
new Texture("tank-khaki.png"),
new Texture("cannon.png"),
new Texture("camo-tank-1.png"),
new Texture("camo-tank-barrel.png"),
terrain,
tankWarsGame, false);
tankWarsGame,
false,
225);
horizontalScaling = Gdx.graphics.getWidth() / TankWarsGame.GAMEPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / TankWarsGame.GAMEPORT_HEIGHT;
......@@ -114,19 +117,22 @@ public class GameScreen implements Screen {
Sprite s = (Sprite) b.getUserData();
if (s != null) {
s.setPosition(b.getPosition().x * (float) TankWarsGame.SCALE - s.getWidth() / 2, (b.getPosition().y + 0.25f) * (float) TankWarsGame.SCALE);
s.setPosition(b.getPosition().x * (float) TankWarsGame.SCALE - s.getWidth() / 2,
(b.getPosition().y * (float) TankWarsGame.SCALE) - s.getHeight()/2);
if (s.equals(myTank.getChassisSprite())) {
s.setOrigin(s.getWidth() / 2, s.getHeight());
s.setRotation(myTank.getAngle());
}
if (s.equals(opponentTank.getChassisSprite())) {
s.setOrigin(s.getWidth() / 2, s.getHeight());
s.setRotation(opponentTank.getAngle());
}
if (s.equals(myTank.getCannonSprite())) {
s.setOrigin(s.getWidth() / 2, 0);
s.setOrigin(s.getWidth() / 2, s.getHeight());
s.setRotation(myTank.getCannonAngle());
}
if (s.equals(opponentTank.getCannonSprite())) {
s.setOrigin(s.getWidth() / s.getWidth(), 0);
s.setOrigin(s.getWidth() / 2 , s.getHeight());
s.setRotation(opponentTank.getCannonAngle());
}
}
......
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