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

#25 created rotating cannon

parent f24afea4
No related branches found
No related tags found
1 merge request!27#25 created rotating cannon
Pipeline #211531 passed
frontend/assets/cannon.png

106 B

...@@ -23,6 +23,13 @@ public class GameController { ...@@ -23,6 +23,13 @@ public class GameController {
tank.moveLeft(); tank.moveLeft();
} }
if(Gdx.input.isKeyPressed(Input.Keys.Q)) {
tank.rotateCannonLeft();
}
else if (Gdx.input.isKeyPressed(Input.Keys.E)) {
tank.rotateCannonRight();
}
if(Gdx.input.justTouched()) { if(Gdx.input.justTouched()) {
bullet = new Bullet(tank); bullet = new Bullet(tank);
System.out.println(tank.getPosition()); System.out.println(tank.getPosition());
......
package com.game.tankwars.model; package com.game.tankwars.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
...@@ -10,29 +9,36 @@ import com.badlogic.gdx.physics.box2d.BodyDef; ...@@ -10,29 +9,36 @@ import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape; import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.physics.box2d.joints.RevoluteJoint;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
import com.game.tankwars.TankWarsGame; import com.game.tankwars.TankWarsGame;
import com.game.tankwars.view.GameScreen;
public class Tank { public class Tank {
TankWarsGame tankWarsGame; TankWarsGame tankWarsGame;
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;
public static final float CANNON_WIDTH = 0.4f;
public static final float CANNON_HEIGHT = 1;
int VIEWPORT_WIDTH; int VIEWPORT_WIDTH;
int VIEWPORT_HEIGHT; int VIEWPORT_HEIGHT;
private Vector2 position; private Vector2 position;
private Rectangle bounds; private Rectangle bounds;
private Texture texture; private Texture chassisTexture;
private Sprite sprite; private Texture cannonTexture;
private Sprite chassisSprite;
private Sprite cannonSprite;
World world; World world;
Body body; Body chassis;
Body cannon;
BodyDef bodyDef = new BodyDef(); BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef(); FixtureDef fixtureDef = new FixtureDef();
RevoluteJoint joint;
Terrain terrain; Terrain terrain;
Vector2[] vertices; Vector2[] vertices;
int posInVertArr; int posInVertArr;
float cannonAngle = 90;
public Tank(int posInVertArr, Texture texture, Terrain terrain, TankWarsGame tankWarsGame) { public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame) {
VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight(); VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth(); VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
...@@ -44,21 +50,35 @@ public class Tank { ...@@ -44,21 +50,35 @@ public class Tank {
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.chassisTexture = chassisTexture;
sprite = new Sprite(texture); chassisSprite = new Sprite(chassisTexture);
sprite.setPosition(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2 + TANK_HEIGHT); chassisSprite.scale(0.4f);
sprite.setRotation(0);
this.cannonTexture = 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;
bodyDef.position.set(position.x, position.y); bodyDef.position.set(position.x, position.y);
body = world.createBody(bodyDef);
body.setUserData(sprite);
PolygonShape shape = new PolygonShape(); PolygonShape shape = new PolygonShape();
shape.setAsBox(TANK_WIDTH, TANK_HEIGHT); shape.setAsBox(TANK_WIDTH, TANK_HEIGHT);
fixtureDef.shape = shape; fixtureDef.shape = shape;
body.createFixture(fixtureDef);
//Chassis
chassis = world.createBody(bodyDef);
chassis.setUserData(chassisSprite);
chassis.createFixture(fixtureDef);
//Cannon
shape.setAsBox(CANNON_WIDTH, CANNON_HEIGHT);
cannonSprite.setOrigin(0, cannonSprite.getHeight()/2);
cannon = world.createBody(bodyDef);
cannon.setUserData(cannonSprite);
shape.dispose(); shape.dispose();
} }
...@@ -67,14 +87,15 @@ public class Tank { ...@@ -67,14 +87,15 @@ 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 <= VIEWPORT_WIDTH - TANK_WIDTH){ if (chassis.getPosition().x <= VIEWPORT_WIDTH - TANK_WIDTH){
body.setTransform(newPos.x, newPos.y + 0.11f, angle); chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
sprite.setRotation(angle); chassisSprite.setRotation(angle);
position.set(newPos); position.set(newPos);
updateCannonPos();
posInVertArr++; posInVertArr++;
} }
else { else {
body.setTransform(curPos.x, curPos.y + 0.11f, angle); chassis.setTransform(curPos.x, curPos.y + 0.11f, angle);
} }
} }
...@@ -83,14 +104,15 @@ public class Tank { ...@@ -83,14 +104,15 @@ 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 >= TANK_WIDTH){ if (chassis.getPosition().x >= TANK_WIDTH){
body.setTransform(newPos.x, newPos.y + 0.11f, angle); chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
sprite.setRotation(angle); chassisSprite.setRotation(angle);
position.set(newPos); position.set(newPos);
updateCannonPos();
posInVertArr--; posInVertArr--;
} }
else { else {
body.setTransform(curPos.x, curPos.y + 0.11f, angle); chassis.setTransform(curPos.x, curPos.y + 0.11f, angle);
} }
} }
...@@ -100,6 +122,27 @@ public class Tank { ...@@ -100,6 +122,27 @@ public class Tank {
return new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleDeg(); return new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleDeg();
} }
public void updateCannonPos(){
Vector2 chassisPos = chassis.getPosition();
chassisPos.add(0, 1);
cannon.setTransform(chassisPos, cannon.getAngle());
}
public void rotateCannonRight(){
if(cannonAngle > -90) {
cannonAngle--;
}
}
public void rotateCannonLeft(){
if(cannonAngle < 90) {
cannonAngle++;
}
}
public float getCannonAngle(){
return cannonAngle;
}
public Vector2 getPosition() { public Vector2 getPosition() {
return position; return position;
} }
...@@ -108,11 +151,15 @@ public class Tank { ...@@ -108,11 +151,15 @@ public class Tank {
return bounds; return bounds;
} }
public Texture getTexture() { public Texture getChassisTexture() {
return texture; return chassisTexture;
}
public Texture getCannonTexture() {
return cannonTexture;
} }
public Sprite getSprite() {return sprite;} public Sprite getChassisSprite() {return chassisSprite;}
public Sprite getCannonSprite() {return cannonSprite;}
public void setPosition(Vector2 position) { public void setPosition(Vector2 position) {
this.position = position; this.position = position;
...@@ -122,7 +169,10 @@ public class Tank { ...@@ -122,7 +169,10 @@ public class Tank {
this.bounds = bounds; this.bounds = bounds;
} }
public void setTexture(Texture texture) { public void setChassisTexture(Texture texture) {
this.texture = texture; this.chassisTexture = texture;
}
public void setCannonTexture(Texture texture) {
this.cannonTexture = texture;
} }
} }
...@@ -55,7 +55,7 @@ public class GameScreen implements Screen { ...@@ -55,7 +55,7 @@ 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, tankWarsGame); tank = new Tank(initPos, new Texture("tank-khaki.png"), new Texture("cannon.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;
...@@ -73,17 +73,25 @@ public class GameScreen implements Screen { ...@@ -73,17 +73,25 @@ public class GameScreen implements Screen {
Array<Body> bodies = new Array<Body>(); Array<Body> bodies = new Array<Body>();
world.getBodies(bodies); world.getBodies(bodies);
//Rendering Bullet bodies
for (Body b : bodies) { for (Body b : bodies) {
Sprite s = (Sprite) b.getUserData(); Sprite s = (Sprite) b.getUserData();
if (s != null) { if (s != null) {
s.setPosition(b.getPosition().x * (float) horizontalScaling - s.getWidth()/2 , b.getPosition().y * (float) verticalScaling); s.setPosition(b.getPosition().x * (float) horizontalScaling - s.getWidth()/2 , b.getPosition().y * (float) verticalScaling);
s.setRotation(tank.getAngle()); if(s.equals(tank.getChassisSprite())){
s.setRotation(tank.getAngle());
}
if(s.equals(tank.getCannonSprite())){
s.setOrigin(s.getWidth()/2, 0);
s.setRotation(tank.getCannonAngle());
}
} }
} }
batch.begin(); batch.begin();
tank.getSprite().draw(batch); tank.getChassisSprite().draw(batch);
tank.getCannonSprite().draw(batch);
batch.end(); batch.end();
} }
...@@ -113,7 +121,8 @@ public class GameScreen implements Screen { ...@@ -113,7 +121,8 @@ public class GameScreen implements Screen {
@Override @Override
public void dispose() { public void dispose() {
tank.getTexture().dispose(); tank.getChassisTexture().dispose();
tank.getCannonTexture().dispose();
batch.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