Skip to content
Snippets Groups Projects
Commit 1fb4f390 authored by ALGLAVE Ivan's avatar ALGLAVE Ivan
Browse files

Bodies and textures positions are fixed, added field boundaries

parent 4d2fe7eb
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,15 @@ package com.mygdx.game.GameScreens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
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.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Shape2D;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
......@@ -25,6 +28,7 @@ import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.game.bodies.Ball;
import com.mygdx.game.bodies.Field;
import com.mygdx.game.bodies.FieldBounds;
import com.mygdx.game.bodies.Goal;
import com.mygdx.game.bodies.Joystick;
import com.mygdx.game.bodies.Player;
......@@ -38,13 +42,16 @@ public class GamePlay extends GameScreen
Ball ball;
Player pleft, pright;
Field field;
FieldBounds fieldBounds;
World world;
Joystick jleft, jright;
Goal gleft, gright;
BitmapFont font;
OrthographicCamera camera;
Box2DDebugRenderer b2dd;
Matrix4 debugMatrix;
int scoreLeft, scoreRight;
long duration;
......@@ -53,8 +60,14 @@ public class GamePlay extends GameScreen
@Override
public void buildStage()
{
b2dd = new Box2DDebugRenderer();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
debugMatrix = new Matrix4(camera.combined);
world = new World(new Vector2(0, 0), true);
field = new Field(world);
fieldBounds = new FieldBounds(world);
ball = new Ball(world, new Vector2(Gdx.graphics.getWidth()/2f, Gdx.graphics.getHeight()/2f));
pleft = new Player(world, Player.TYPE.LEFT);
pright = new Player(world, Player.TYPE.RIGHT);
......@@ -88,25 +101,25 @@ public class GamePlay extends GameScreen
{
if(ba.getUserData() instanceof Ball && bb.getUserData() instanceof Goal)
{
Goal goal = (Goal)bb.getUserData();
/*Goal goal = (Goal)bb.getUserData();
if(goal.side == Goal.SIDE.LEFT)
{
score(pright);
}
else {
score(pleft);
}
}*/
}
else if(bb.getUserData() instanceof Ball && ba.getUserData() instanceof Goal)
{
Goal goal = (Goal)ba.getUserData();
/*Goal goal = (Goal)ba.getUserData();
if(goal.side == Goal.SIDE.LEFT)
{
score(pright);
}
else {
score(pleft);
}
}*/
}
}
}
......@@ -139,24 +152,19 @@ public class GamePlay extends GameScreen
}
pleft.interact(pressed(Input.Keys.Q), pressed(Input.Keys.D), pressed(Input.Keys.Z), pressed(Input.Keys.S));
pright.interact(pressed(Input.Keys.J), pressed(Input.Keys.L), pressed(Input.Keys.I), pressed(Input.Keys.K));
field.draw(batch);
pleft.draw(batch);
pright.draw(batch);
ball.draw(batch);
jleft.draw(batch);
jright.draw(batch);
remaining = (int)(duration - (System.currentTimeMillis() - creationDate) / 1000);
displayTexts(batch);
b2dd.render(world, debugMatrix);
world.step(delta, 1, 1);
super.render(delta);
......
......@@ -30,8 +30,8 @@ public abstract class GameScreen extends Stage implements Screen
protected void beforeRender()
{
// Clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Gdx.gl.glClearColor(0, 0, 0, 1);
//Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
}
......
......@@ -23,14 +23,14 @@ public class Ball
FixtureDef fdef = new FixtureDef();
CircleShape cs = new CircleShape();
cs.setRadius(Gdx.graphics.getWidth() * (1f/85f));
cs.setRadius(Gdx.graphics.getWidth() * (1f/85f) / 2f);
fdef.shape = cs;
fdef.density = 1f;
fdef.restitution = 0.5f;
fdef.friction = 1.5f;
tb = new TextureBody(Gdx.files.internal("images/Ballon.png"), world, bdef, fdef, cs.getRadius(), cs.getRadius());
tb = new TextureBody(Gdx.files.internal("images/Ballon.png"), world, bdef, fdef, cs.getRadius()*2, cs.getRadius()*2);
tb.body.setUserData(this);
}
......
package com.mygdx.game.bodies;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class FieldBounds
{
Body bodies[];
public FieldBounds(World world)
{
float vertices[] = {7.52f, 15.63f, 8.79f, 13.93f, 91.11f, 13.93f, 92.38f, 15.63f, 92.38f, 41.14f, 96.29f, 41.15f, 96.29f, 54.04f, 92.38f, 54.04f, 92.38f, 84.24f, 91.11f, 85.94f, 8.79f, 85.94f, 7.52f, 84.24f, 7.52f, 54.04f, 3.61f, 54.04f, 3.61f, 41.15f, 7.52f, 41.15f};
bodies = new Body[vertices.length/2];
for(int i = 0; i < vertices.length/2; i++)
{
float w = Gdx.graphics.getWidth()*0.8f;
float h = Gdx.graphics.getHeight();
float x_off = Gdx.graphics.getWidth()*0.1f;
float ox = (vertices[(i*2)%vertices.length]/100f)*w+x_off ;
float oy = (vertices[((i*2)+1)%vertices.length]/100f)*h;
float dx = (vertices[((i+1)*2)%vertices.length]/100f)*w+x_off;
float dy = (vertices[(((i+1)*2)+1)%vertices.length]/100f)*h;
float v[] = {ox, oy, ox+0.1f, oy+0.1f, dx, dy, dx+0.1f, dy+0.1f};
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(0, 0);
FixtureDef fdef = new FixtureDef();
PolygonShape ps = new PolygonShape();
ps.set(v);
fdef.shape = ps;
fdef.density = 0f;
fdef.restitution = 0f;
fdef.friction = 0f;
bodies[i] = world.createBody(bdef);
bodies[i].createFixture(fdef);
}
}
}
......@@ -21,7 +21,7 @@ public class Goal
{
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
float offset = Gdx.graphics.getWidth() * (1f/80);
float offset = Gdx.graphics.getWidth() * (1f/20);
Vector2 pos = side == Goal.SIDE.RIGHT ? new Vector2(Gdx.graphics.getWidth()*0.90f - offset, Gdx.graphics.getHeight()/2f) : new Vector2(Gdx.graphics.getWidth()*0.10f + offset, Gdx.graphics.getHeight()/2f);
bdef.position.set(pos);
......@@ -30,7 +30,7 @@ public class Goal
FixtureDef fdef = new FixtureDef();
PolygonShape ps = new PolygonShape();
ps.setAsBox(Gdx.graphics.getWidth() * (1f/40), Gdx.graphics.getHeight() * 0.2f);
ps.setAsBox(Gdx.graphics.getWidth() * (1f/120), Gdx.graphics.getHeight() * 0.1f);
fdef.shape = ps;
fdef.density = 0f;
......
......@@ -25,7 +25,7 @@ public class Player
FixtureDef fdef = new FixtureDef();
CircleShape cs = new CircleShape();
cs.setRadius(Gdx.graphics.getWidth() * (1f/40f));
cs.setRadius(Gdx.graphics.getWidth() * (1f/40f) / 2f);
fdef.shape = cs;
fdef.density = 1f;
......@@ -34,7 +34,7 @@ public class Player
type = playerType;
tb = new TextureBody(Gdx.files.internal( "images/Joueur" + (playerType == TYPE.RIGHT ? "Droite" : "Gauche") + ".png"), world, bdef, fdef, cs.getRadius(), cs.getRadius());
tb = new TextureBody(Gdx.files.internal( "images/Joueur" + (playerType == TYPE.RIGHT ? "Droite" : "Gauche") + ".png"), world, bdef, fdef, cs.getRadius()*2, cs.getRadius()*2);
}
public Texture texture()
......
......@@ -21,14 +21,12 @@ public class TextureBody extends Texture
width = w;
height = h;
bdef.position.x -= w/2f;
bdef.position.y -= h/2f;
body = world.createBody(bdef);
fixture = body.createFixture(fdef);
}
public void draw(SpriteBatch batch)
{
batch.draw(this, body.getPosition().x, body.getPosition().y, width, height);
batch.draw(this, body.getPosition().x - width/2, body.getPosition().y - height/2, width, height);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment