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

Trying to build the goals, To fix : issue with bodies placement in general

parent 794d88a4
No related branches found
No related tags found
No related merge requests found
package com.mygdx.game.GameScreens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
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.Shape2D;
import com.badlogic.gdx.math.Vector2;
......@@ -11,12 +15,17 @@ import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2D;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
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.Goal;
import com.mygdx.game.bodies.Joystick;
import com.mygdx.game.bodies.Player;
......@@ -31,9 +40,16 @@ public class GamePlay extends GameScreen
Field field;
World world;
Joystick jleft, jright;
Goal gleft, gright;
BitmapFont font;
Box2DDebugRenderer b2dd;
int scoreLeft, scoreRight;
long duration;
int remaining;
@Override
public void buildStage()
{
......@@ -44,12 +60,88 @@ public class GamePlay extends GameScreen
pright = new Player(world, Player.TYPE.RIGHT);
jleft = new Joystick(world, Joystick.SIDE.LEFT);
jright = new Joystick(world, Joystick.SIDE.RIGHT);
gleft = new Goal(world, Goal.SIDE.LEFT);
gright = new Goal(world, Goal.SIDE.RIGHT);
FreeTypeFontGenerator fGen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Comic_Sans_MS_Bold.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter fp = new FreeTypeFontGenerator.FreeTypeFontParameter();
fp.size = 1024/60;
fp.color = new Color(1f, 1f, 0f, 0.75f);
fp.borderColor = Color.BLACK;
fp.borderWidth = 3f;
font = fGen.generateFont(fp);
fGen.dispose();
scoreLeft = 0;
scoreRight = 0;
duration = 180;
world.setContactListener(new ContactListener() {
@Override
public void beginContact(Contact contact) {
Body ba = contact.getFixtureA().getBody();
Body bb = contact.getFixtureB().getBody();
if(ba.getUserData() != null && bb.getUserData() != null)
{
if(ba.getUserData() instanceof Ball && bb.getUserData() instanceof Goal)
{
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();
if(goal.side == Goal.SIDE.LEFT)
{
score(pright);
}
else {
score(pleft);
}
}
}
}
@Override
public void endContact(Contact contact) {
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
});
remaining = (int)duration;
}
@Override
public void render(float delta) {
beforeRender();
if(remaining <= 0)
{
GameScreenManager.getInstance().showGameScreen(GameScreenEnum.GAME_PLAY);
}
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);
......@@ -62,6 +154,9 @@ public class GamePlay extends GameScreen
jright.draw(batch);
remaining = (int)(duration - (System.currentTimeMillis() - creationDate) / 1000);
displayTexts(batch);
world.step(delta, 1, 1);
super.render(delta);
......@@ -76,4 +171,23 @@ public class GamePlay extends GameScreen
{
return Gdx.graphics.getHeight();
}
private void displayTexts(SpriteBatch batch)
{
font.draw(batch, "" + scoreLeft, width()*0.25f, height()*0.985f);
font.draw(batch, "" + scoreRight, width()*0.75f, height()*0.985f);
font.draw(batch, "" + remaining, width()/2f, height()*0.985f);
}
private void score(Player player)
{
pleft.resetPosition(pleft == player);
pright.resetPosition(pright == player);
if(pleft == player) scoreLeft++; else scoreRight++;
}
private boolean pressed(int key)
{
return Gdx.input.isKeyPressed(key);
}
}
......@@ -17,7 +17,7 @@ public class TableFootball extends Game {
public void create ()
{
GameScreenManager.getInstance().initialize(this); // Initialize the GameScreenManager instance with the current Game
GameScreenManager.getInstance().showGameScreen(GameScreenEnum.GAME_INTRO); // Set the base screen as the intro screen
GameScreenManager.getInstance().showGameScreen(GameScreenEnum.GAME_PLAY); // Set the base screen as the intro screen
}
@Override
......
......@@ -31,6 +31,7 @@ public class Ball
fdef.friction = 1.5f;
tb = new TextureBody(Gdx.files.internal("images/Ballon.png"), world, bdef, fdef, cs.getRadius(), cs.getRadius());
tb.body.setUserData(this);
}
public Texture texture()
......@@ -47,4 +48,9 @@ public class Ball
{
tb.draw(batch);
}
public void resetPosition()
{
tb.body.setTransform(Gdx.graphics.getWidth()/2f, Gdx.graphics.getHeight()/2f, 0);
}
}
package com.mygdx.game.bodies;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class Goal
{
private Body body;
private Fixture fixture;
public Goal.SIDE side;
public Goal(World world, Goal.SIDE side)
{
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
float offset = Gdx.graphics.getWidth() * (1f/80);
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);
this.side = side;
FixtureDef fdef = new FixtureDef();
PolygonShape ps = new PolygonShape();
ps.setAsBox(Gdx.graphics.getWidth() * (1f/40), Gdx.graphics.getHeight() * 0.2f);
fdef.shape = ps;
fdef.density = 0f;
fdef.restitution = 0f;
fdef.friction = 0f;
body = world.createBody(bdef);
body.setUserData(this);
fixture = body.createFixture(fdef);
}
public enum SIDE
{
LEFT, RIGHT
}
}
......@@ -13,6 +13,7 @@ import com.badlogic.gdx.physics.box2d.World;
public class Player
{
private TextureBody tb;
TYPE type;
public Player(World world, Player.TYPE playerType)
{
......@@ -31,6 +32,8 @@ public class Player
fdef.restitution = 0.25f;
fdef.friction = 1.5f;
type = playerType;
tb = new TextureBody(Gdx.files.internal( "images/Joueur" + (playerType == TYPE.RIGHT ? "Droite" : "Gauche") + ".png"), world, bdef, fdef, cs.getRadius(), cs.getRadius());
}
......@@ -49,6 +52,33 @@ public class Player
tb.draw(batch);
}
public void interact(boolean left, boolean right, boolean up, boolean down)
{
if(type == TYPE.AI)
{
}
else
{
int hor = (left ? -10 : 0) + (right ? 10 : 0);
int ver = (down ? -10 : 0) + (up ? 10 : 0);
//tb.body.applyForceToCenter(hor, ver, false);
tb.body.applyLinearImpulse(hor*100, ver*100, tb.width/2, tb.height/2, true);
}
}
public void resetPosition(boolean hasScored)
{
if(type == TYPE.RIGHT)
{
tb.body.setTransform(Gdx.graphics.getWidth() * (hasScored ? 5/8f : 0.75f), Gdx.graphics.getHeight()/2, 0);
}
else
{
tb.body.setTransform(Gdx.graphics.getWidth() * (hasScored ? 3/8f : 0.25f), Gdx.graphics.getHeight()/2, 0);
}
}
public enum TYPE
{
LEFT, RIGHT, AI
......
......@@ -21,12 +21,14 @@ 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 - width/2f, body.getPosition().y - height/2f, width, height);
batch.draw(this, body.getPosition().x, body.getPosition().y, width, height);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment