diff --git a/core/src/com/mygdx/game/GameScreens/GamePlay.java b/core/src/com/mygdx/game/GameScreens/GamePlay.java index e43d10d4677839d3de195a5b90409396d7345d86..6dc8db24261dc260af0693ad0601199349df3bb5 100644 --- a/core/src/com/mygdx/game/GameScreens/GamePlay.java +++ b/core/src/com/mygdx/game/GameScreens/GamePlay.java @@ -1,8 +1,12 @@ 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); + } } diff --git a/core/src/com/mygdx/game/TableFootball.java b/core/src/com/mygdx/game/TableFootball.java index d070bfa148e77a064375a0457e18d951d3517c5f..9e823dfb6e5ee72cf0698d44632cd51bc2dd8e44 100644 --- a/core/src/com/mygdx/game/TableFootball.java +++ b/core/src/com/mygdx/game/TableFootball.java @@ -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 diff --git a/core/src/com/mygdx/game/bodies/Ball.java b/core/src/com/mygdx/game/bodies/Ball.java index b6015a7f54a4c008139ddaf182938d27056c8217..ff998eb56f438828919205781506a846dbe1060f 100644 --- a/core/src/com/mygdx/game/bodies/Ball.java +++ b/core/src/com/mygdx/game/bodies/Ball.java @@ -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); + } } diff --git a/core/src/com/mygdx/game/bodies/Goal.java b/core/src/com/mygdx/game/bodies/Goal.java new file mode 100644 index 0000000000000000000000000000000000000000..232876628947dd67389db8f87b8fad057c4cf07c --- /dev/null +++ b/core/src/com/mygdx/game/bodies/Goal.java @@ -0,0 +1,51 @@ +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 + } +} diff --git a/core/src/com/mygdx/game/bodies/Player.java b/core/src/com/mygdx/game/bodies/Player.java index 413c5cb8eef7478f1f9efa845f978f963f3322a3..c4c15b86b211a5c675373f5d26c06165986925a7 100644 --- a/core/src/com/mygdx/game/bodies/Player.java +++ b/core/src/com/mygdx/game/bodies/Player.java @@ -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 diff --git a/core/src/com/mygdx/game/bodies/TextureBody.java b/core/src/com/mygdx/game/bodies/TextureBody.java index e430d05aeabe0fe60ef32ec3ce92e1589c84a919..7fd02324c942bd2cccb31e6746c0bd731f4f6650 100644 --- a/core/src/com/mygdx/game/bodies/TextureBody.java +++ b/core/src/com/mygdx/game/bodies/TextureBody.java @@ -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); } }