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

#42 fixed client-side render of tanks

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