From 14a5c2362373814e9db1e821529b0481306afdb7 Mon Sep 17 00:00:00 2001
From: ALGLAVE Ivan <ivan.alglave8@etu.univ-lorraine.fr>
Date: Mon, 28 Dec 2020 11:03:52 +0100
Subject: [PATCH] Fixed text positions and size in the GamePlay screen (scores
and time)
---
.../com/mygdx/game/GameScreens/GamePlay.java | 34 +++++++++---
.../mygdx/game/GameScreens/GameScreen.java | 4 +-
.../game/GameScreens/GameScreenEnum.java | 11 +++-
core/src/com/mygdx/game/TableFootball.java | 2 +-
core/src/com/mygdx/game/bodies/Joystick.java | 14 +++++
.../mygdx/game/singletons/FontManager.java | 52 -------------------
6 files changed, 54 insertions(+), 63 deletions(-)
delete mode 100644 core/src/com/mygdx/game/singletons/FontManager.java
diff --git a/core/src/com/mygdx/game/GameScreens/GamePlay.java b/core/src/com/mygdx/game/GameScreens/GamePlay.java
index 311bf37..83c46e8 100644
--- a/core/src/com/mygdx/game/GameScreens/GamePlay.java
+++ b/core/src/com/mygdx/game/GameScreens/GamePlay.java
@@ -57,6 +57,16 @@ public class GamePlay extends GameScreen
long duration;
int remaining;
+ boolean isAI;
+
+ float text_left_x, text_right_x, text_time_x, text_y, text_w;
+
+ public GamePlay(boolean is2ndPlayerAI)
+ {
+ super();
+ isAI = is2ndPlayerAI;
+ }
+
@Override
public void buildStage()
{
@@ -69,7 +79,7 @@ public class GamePlay extends GameScreen
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);
+ pleft = isAI ? new Player(world, Player.TYPE.AI) : new Player(world, Player.TYPE.LEFT);
pright = new Player(world, Player.TYPE.RIGHT);
jleft = new Joystick(world, Joystick.SIDE.LEFT);
jright = new Joystick(world, Joystick.SIDE.RIGHT);
@@ -79,7 +89,7 @@ public class GamePlay extends GameScreen
FreeTypeFontGenerator fGen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Comic_Sans_MS_Bold.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter fp = new FreeTypeFontGenerator.FreeTypeFontParameter();
- fp.size = 1024/60;
+ fp.size = (int)(width()/1024f * 60f);
fp.color = new Color(1f, 1f, 0f, 0.75f);
fp.borderColor = Color.BLACK;
fp.borderWidth = 3f;
@@ -87,6 +97,12 @@ public class GamePlay extends GameScreen
font = fGen.generateFont(fp);
fGen.dispose();
+ text_left_x = width()*0.25f - width()*0.10f;
+ text_time_x = width()*0.5f - width()*0.10f;
+ text_right_x = width()*0.75f - width()*0.10f;
+ text_y = height()*0.97f;
+ text_w = width()*0.20f;
+
scoreLeft = 0;
scoreRight = 0;
duration = 180;
@@ -163,7 +179,7 @@ public class GamePlay extends GameScreen
remaining = (int)(duration - (System.currentTimeMillis() - creationDate) / 1000);
displayTexts(batch);
- b2dd.render(world, debugMatrix);
+ //b2dd.render(world, debugMatrix);
world.step(delta, 1, 1);
@@ -182,9 +198,10 @@ public class GamePlay extends GameScreen
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);
+
+ font.draw(batch, "" + scoreLeft, text_left_x, text_y, text_w, 1, true);
+ font.draw(batch, "" + scoreRight, text_right_x, text_y, text_w, 1, true);
+ font.draw(batch, "" + remaining, text_time_x, text_y, text_w, 1, true);
}
private void score(Player player)
@@ -198,4 +215,9 @@ public class GamePlay extends GameScreen
{
return Gdx.input.isKeyPressed(key);
}
+
+ @Override
+ public void resize(int width, int height) {
+ super.resize(width, height);
+ }
}
diff --git a/core/src/com/mygdx/game/GameScreens/GameScreen.java b/core/src/com/mygdx/game/GameScreens/GameScreen.java
index d85fe99..4778e69 100644
--- a/core/src/com/mygdx/game/GameScreens/GameScreen.java
+++ b/core/src/com/mygdx/game/GameScreens/GameScreen.java
@@ -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();
}
diff --git a/core/src/com/mygdx/game/GameScreens/GameScreenEnum.java b/core/src/com/mygdx/game/GameScreens/GameScreenEnum.java
index beda66c..8dcbc13 100644
--- a/core/src/com/mygdx/game/GameScreens/GameScreenEnum.java
+++ b/core/src/com/mygdx/game/GameScreens/GameScreenEnum.java
@@ -25,13 +25,20 @@ public enum GameScreenEnum
},
/**
- * Play screen
+ * Play screen with 2 players
*/
GAME_PLAY {
public GameScreen getScreen(Object... args)
{
- return new GamePlay();
+ return new GamePlay(false);
}
+ },
+
+ /**
+ * Play screen with AI
+ */
+ GAME_PLAY_AI {
+ public GameScreen getScreen(Object... args) { return new GamePlay(true); }
};
/**
diff --git a/core/src/com/mygdx/game/TableFootball.java b/core/src/com/mygdx/game/TableFootball.java
index 9e823df..fb931a3 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_PLAY); // Set the base screen as the intro screen
+ GameScreenManager.getInstance().showGameScreen(GameScreenEnum.GAME_PLAY_AI); // Set the base screen as the intro screen
}
@Override
diff --git a/core/src/com/mygdx/game/bodies/Joystick.java b/core/src/com/mygdx/game/bodies/Joystick.java
index 4a9f4ca..10c5028 100644
--- a/core/src/com/mygdx/game/bodies/Joystick.java
+++ b/core/src/com/mygdx/game/bodies/Joystick.java
@@ -7,10 +7,12 @@ import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
+import com.badlogic.gdx.graphics.Color;
public class Joystick
{
private TextureBody tb;
+ private SIDE side;
public Joystick(World world, SIDE side)
{
@@ -29,6 +31,8 @@ public class Joystick
fdef.restitution = 0f;
fdef.friction = 0f;
+ this.side = side;
+
bdef.active = false;
tb = new TextureBody(Gdx.files.internal( "images/Pad.png"), world, bdef, fdef, Gdx.graphics.getWidth()*0.1f, Gdx.graphics.getWidth()*0.1f);
@@ -36,7 +40,17 @@ public class Joystick
public void draw(SpriteBatch batch)
{
+ Color or = batch.getColor();
+ if(side == SIDE.LEFT)
+ {
+ batch.setColor(Color.YELLOW);
+ }
+ else
+ {
+ batch.setColor(Color.CYAN);
+ }
tb.draw(batch);
+ batch.setColor(or);
}
public enum SIDE
diff --git a/core/src/com/mygdx/game/singletons/FontManager.java b/core/src/com/mygdx/game/singletons/FontManager.java
deleted file mode 100644
index 5dd091d..0000000
--- a/core/src/com/mygdx/game/singletons/FontManager.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package com.mygdx.game.singletons;
-
-import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.assets.AssetManager;
-import com.badlogic.gdx.graphics.Color;
-import com.badlogic.gdx.graphics.g2d.BitmapFont;
-import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
-import com.badlogic.gdx.scenes.scene2d.ui.Label;
-
-public class FontManager
-{
- private static FontManager fm = new FontManager();
-
- private AssetManager am;
-
- private FontManager()
- {
- am = new AssetManager();
- }
-
- public static FontManager getInstance()
- {
- return fm;
- }
-
- public BitmapFont generateFont()
- {
- /*FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("HFF Ice Bergman.ttf"));
- FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
- parameter.size = 30;
- parameter.borderWidth = 1;
- parameter.color = Color.BLUE;
- parameter.shadowOffsetX = 3;
- parameter.shadowOffsetY = 3;
- parameter.shadowColor = new Color(0, 0.5f, 0, 0.75f);
- BitmapFont font = generator.generateFont(parameter); // font size 24 pixels
- generator.dispose();
-
- return font;*/
- return null;
- }
-
- public Label.LabelStyle getLabelStyle(Color fontColor)
- {
- /*Label.LabelStyle l = new Label.LabelStyle();
- l.font = generateFont();
- l.fontColor = fontColor;
- return l;*/
-
- return null;
- }
-}
--
GitLab