Skip to content
Snippets Groups Projects
Commit 39c8d0a8 authored by Snorre Skjellestad Kristiansen's avatar Snorre Skjellestad Kristiansen
Browse files

Merge branch '26-render-terrain' into 'main'

#26 Added terrain rendering

Closes #26

See merge request !28
parents a2b0c8f6 3c921531
No related branches found
No related tags found
1 merge request!28#26 Added terrain rendering
Pipeline #212992 passed
......@@ -6,6 +6,8 @@ import com.game.tankwars.TankWarsGame;
import com.game.tankwars.model.Bullet;
import com.game.tankwars.model.Tank;
import java.util.Arrays;
public class GameController {
TankWarsGame tankWarsGame;
Tank tank;
......@@ -32,7 +34,6 @@ public class GameController {
if(Gdx.input.justTouched()) {
bullet = new Bullet(tank);
System.out.println(tank.getPosition());
bullet.shoot();
}
}
......
......@@ -12,19 +12,17 @@ public class Bullet {
BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef();
World world;
Body body;
Tank tank;
Vector2 position;
private Vector2 position;
public Bullet(Tank tank) {
this.position = tank.getPosition();
position.y += 0.5f;
this.tank = tank;
//this.position = new Vector2(tank.getPosition().add(0, 1f));
world = Box2dWorld.getWorld();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(this.position);
bodyDef.position.set(new Vector2(tank.getPosition().add(0, 1f)));
body = world.createBody(bodyDef);
CircleShape shape = new CircleShape();
......@@ -40,8 +38,12 @@ public class Bullet {
}
public void shoot() {
body.applyLinearImpulse(0.4f, 0.3f, position.x-1, position.y-2, true);
System.out.println(this.position);
body.applyLinearImpulse(
0.2f,
0.3f,
new Vector2(tank.getPosition()).x-1,
new Vector2(tank.getPosition()).y-2,
false);
}
}
......@@ -22,6 +22,7 @@ public class Tank {
int VIEWPORT_WIDTH;
int VIEWPORT_HEIGHT;
private Vector2 position;
private Vector2 cannonPosition;
private Rectangle bounds;
private Texture chassisTexture;
private Texture cannonTexture;
......@@ -34,7 +35,7 @@ public class Tank {
FixtureDef fixtureDef = new FixtureDef();
RevoluteJoint joint;
Terrain terrain;
Vector2[] vertices;
private Vector2[] vertices;
int posInVertArr;
float cannonAngle = 90;
......@@ -78,19 +79,18 @@ public class Tank {
cannon = world.createBody(bodyDef);
cannon.setUserData(cannonSprite);
shape.dispose();
}
public void moveRight() {
Vector2 curPos = vertices[posInVertArr];
Vector2 newPos = vertices[posInVertArr + 1];
Vector2 curPos = new Vector2(vertices[posInVertArr]);
Vector2 newPos = new Vector2(vertices[posInVertArr + 1]);
float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad();
if (chassis.getPosition().x <= VIEWPORT_WIDTH - TANK_WIDTH){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
chassisSprite.setRotation(angle);
position.set(newPos);
updateCannonPos();
posInVertArr++;
}
......@@ -100,14 +100,14 @@ public class Tank {
}
public void moveLeft() {
Vector2 curPos = vertices[posInVertArr];
Vector2 newPos = vertices[posInVertArr - 1];
Vector2 curPos = new Vector2(vertices[posInVertArr]);
Vector2 newPos = new Vector2(vertices[posInVertArr - 1]);
float angle = new Vector2(newPos.x - curPos.x, newPos.y - curPos.y).angleRad();
if (chassis.getPosition().x >= TANK_WIDTH){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
chassisSprite.setRotation(angle);
position.set(newPos);
updateCannonPos();
posInVertArr--;
}
......@@ -144,20 +144,18 @@ public class Tank {
return cannonAngle;
}
public Vector2 getPosition() {
return position;
return new Vector2(position);
}
public Vector2 getCannonPosition(){ return new Vector2(cannonPosition); }
public Rectangle getBounds() {
return bounds;
}
public Texture getChassisTexture() {
return chassisTexture;
}
public Texture getCannonTexture() {
return cannonTexture;
}
public Sprite getChassisSprite() {return chassisSprite;}
public Sprite getCannonSprite() {return cannonSprite;}
......
package com.game.tankwars.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
......@@ -15,7 +22,7 @@ public class Terrain {
final Vector2[] vertices;
Vector2 position;
public Terrain(){
public Terrain() {
float xStart = -5;
float xEnd = 100;
......@@ -27,7 +34,7 @@ public class Terrain {
this.world = Box2dWorld.getWorld();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(0,0);
vertices = new TerrainGenerator(xStart, xEnd, yMin, yMax, points).generateVertices(vertNumber);
......@@ -46,6 +53,22 @@ public class Terrain {
}
public Vector2[] getVertices() {
return vertices;
return this.vertices.clone();
}
public void draw(ShapeRenderer sp) {
sp.setColor(Color.GREEN);
for (int i = 0; i < vertices.length-1; i++) {
sp.triangle(vertices[i].x, vertices[i].y,
vertices[i + 1].x, vertices[i + 1].y,
vertices[i].x, 0);
sp.triangle(vertices[i + 1].x, vertices[i + 1].y,
vertices[i].x, 0,
vertices[i + 1].x, 0);
}
}
}
......@@ -4,11 +4,13 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
......@@ -29,6 +31,7 @@ public class GameScreen implements Screen {
int horizontalScaling;
int verticalScaling;
SpriteBatch batch;
ShapeRenderer shapeRender;
Tank tank;
Box2dWorld model;
World world;
......@@ -37,6 +40,7 @@ public class GameScreen implements Screen {
Box2DDebugRenderer debugRenderer;
Bullet bullet;
GameController controller;
Mesh groundMesh;
public GameScreen(final TankWarsGame tankWarsGame){
this.tankWarsGame = tankWarsGame;
......@@ -45,6 +49,7 @@ public class GameScreen implements Screen {
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
batch = new SpriteBatch();
shapeRender = new ShapeRenderer();
model = new Box2dWorld();
world = Box2dWorld.getWorld();
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
......@@ -64,9 +69,10 @@ public class GameScreen implements Screen {
@Override
public void render(float delta) {
model.logicStep(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(0f, 0f, 0f, 1);
Gdx.gl.glClearColor(0, 0, 100, 100);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, cam.combined);
shapeRender.setProjectionMatrix(cam.combined);
controller.checkKeyInput();
......@@ -78,17 +84,21 @@ public class GameScreen implements Screen {
Sprite s = (Sprite) b.getUserData();
if (s != null) {
s.setPosition(b.getPosition().x * (float) horizontalScaling - s.getWidth()/2 , b.getPosition().y * (float) verticalScaling);
if(s.equals(tank.getChassisSprite())){
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(tank.getCannonSprite())){
s.setOrigin(s.getWidth()/2, 0);
if (s.equals(tank.getCannonSprite())) {
s.setOrigin(s.getWidth() / 2, 0);
s.setRotation(tank.getCannonAngle());
}
}
}
shapeRender.begin(ShapeRenderer.ShapeType.Filled);
terrain.draw(shapeRender);
shapeRender.end();
batch.begin();
tank.getChassisSprite().draw(batch);
tank.getCannonSprite().draw(batch);
......@@ -124,5 +134,6 @@ public class GameScreen implements Screen {
tank.getChassisTexture().dispose();
tank.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