Skip to content
Snippets Groups Projects
Commit 90eb6ea8 authored by Magnus Segtnan Skjølberg's avatar Magnus Segtnan Skjølberg
Browse files

Combine hud with gameplay

parent f5c00d5c
No related branches found
No related tags found
1 merge request!32Combine hud with gameplay
Showing
with 495 additions and 43 deletions
File added
No preview for this file type
gameplay-skin.png
size: 256, 256
format: RGBA8888
filter: Linear, Linear
repeat: none
fire-button-light.9
rotate: false
xy: 67, 213
size: 32, 32
split: 1, 1, 1, 1
pad: 13, 13, 2, 2
orig: 32, 32
offset: 0, 0
index: -1
fire-button.9
rotate: false
xy: 1, 1
size: 32, 32
split: 1, 1, 1, 1
pad: 13, 13, 1, 1
orig: 32, 32
offset: 0, 0
index: -1
move-left-light.9
rotate: false
xy: 1, 35
size: 43, 43
split: 0, 0, 0, 0
orig: 43, 43
offset: 0, 0
index: -1
move-left.9
rotate: false
xy: 1, 125
size: 43, 43
split: 0, 0, 0, 0
orig: 43, 43
offset: 0, 0
index: -1
move-right-light.9
rotate: false
xy: 1, 80
size: 43, 43
split: 0, 0, 0, 0
orig: 43, 43
offset: 0, 0
index: -1
move-right.9
rotate: false
xy: 1, 170
size: 43, 43
split: 0, 0, 0, 0
orig: 43, 43
offset: 0, 0
index: -1
progress-background.9
rotate: false
xy: 101, 213
size: 32, 32
split: 3, 3, 3, 3
pad: 1, 1, 1, 1
orig: 32, 32
offset: 0, 0
index: -1
progress-knob
rotate: false
xy: 169, 215
size: 30, 30
orig: 30, 30
offset: 0, 0
index: -1
slider-background.9
rotate: false
xy: 135, 213
size: 32, 32
split: 1, 1, 1, 1
orig: 32, 32
offset: 0, 0
index: -1
slider-knob-before.9
rotate: false
xy: 1, 215
size: 64, 30
split: 1, 1, 1, 1
orig: 64, 30
offset: 0, 0
index: -1
slider-knob.9
rotate: false
xy: 46, 177
size: 10, 36
split: 1, 1, 1, 1
orig: 10, 36
offset: 0, 0
index: -1
{
com.badlogic.gdx.graphics.Color: {
black: {
r: 0
g: 0
b: 0
a: 1
}
yellow: {
r: 0.98039216
g: 1
b: 0
a: 1
}
red: {
r: 1
g: 0
b: 0
a: 1
}
white: {
r: 1
g: 1
b: 1
a: 1
}
}
BitmapFont: {
large: {
file: Roboto-Black.ttf
size: 28
}
largest: {
file: Roboto-Black.ttf
size: 30
}
default: {
file: Roboto-Regular.ttf
size: 16
}
}
com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle: {
move-right: {
up: move-right.9
down: move-right-light.9
}
move-left: {
up: move-left.9
down: move-left-light.9
}
}
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
default: {
font: default
fontColor: black
}
large-black: {
font: large
fontColor: black
}
large-white: {
font: large
fontColor: white
}
}
com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle: {
default-horizontal: {
background: progress-background.9
knobBefore: progress-knob
}
}
com.badlogic.gdx.scenes.scene2d.ui.Slider$SliderStyle: {
default-horizontal: {
background: slider-background.9
knob: slider-knob.9
knobBefore: slider-knob-before.9
}
}
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: {
font: largest
fontColor: yellow
up: fire-button.9
down: fire-button-light.9
}
}
com.badlogic.gdx.scenes.scene2d.ui.TextTooltip$TextTooltipStyle: {
default: {
label: default
}
}
}
\ No newline at end of file
frontend/assets/gameplay-skin.png

9.06 KiB

......@@ -96,5 +96,8 @@ project(":core") {
implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
implementation 'com.github.acanthite:freetype-skin:0.4'
implementation 'com.github.raeleus.stripe:freetype:1.4.5'
}
}
......@@ -2,15 +2,8 @@ package com.game.tankwars;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.github.acanthite.gdx.graphics.g2d.FreeTypeSkinLoader;
......@@ -29,6 +22,8 @@ public class ResourceManager {
new AssetDescriptor<>("menu-textures.atlas", TextureAtlas.class);
private final AssetDescriptor<Skin> MENU_SKIN =
new AssetDescriptor<>("menu-textures.json", Skin.class);
private final AssetDescriptor<Skin> GAMEPLAY_SKIN =
new AssetDescriptor<>("gameplay-skin.json", Skin.class);
public ResourceManager() {
manager = new AssetManager();
......@@ -66,6 +61,21 @@ public class ResourceManager {
return null;
}
/**
* Loads the Skin for the gameplay,
* @return loaded Skin object with JSON file, null on loading error
*/
public Skin loadAndGetGameplayHudAssets() {
if (!manager.isLoaded(GAMEPLAY_SKIN)) manager.load(GAMEPLAY_SKIN);
try {
manager.finishLoading();
return manager.get(GAMEPLAY_SKIN);
} catch(GdxRuntimeException error) {
System.out.println(error.getMessage());
}
return null;
}
/**
* Block until all currently loaded assets are finished loading
*/
......
......@@ -4,14 +4,25 @@
package com.game.tankwars;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.game.tankwars.view.FindGameScreen;
import com.game.tankwars.view.LoginScreen;
public class TankWarsGame extends Game {
public static final float SCALE = 10;
public static int VIEWPORT_WIDTH = 320;
public static int VIEWPORT_HEIGHT = 240;
public static final int GAMEPORT_WIDTH = 720;
public static final int GAMEPORT_HEIGHT = 480;
private SpriteBatch batch;
private BitmapFont font;
@Override
public void create() {
this.setScreen(new LoginScreen(this));
......
......@@ -2,29 +2,35 @@ package com.game.tankwars.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.model.Bullet;
import com.game.tankwars.model.Tank;
import java.util.Arrays;
import com.game.tankwars.view.GameHud;
public class GameController {
TankWarsGame tankWarsGame;
Tank tank;
Bullet bullet;
public GameController(Tank tank, TankWarsGame tankWarsGame){
private TankWarsGame tankWarsGame;
private GameHud hud;
private Tank tank;
private Bullet bullet;
private boolean moveRightTouched;
private boolean moveLeftTouched;
public GameController(Tank tank, TankWarsGame tankWarsGame, GameHud hud) {
this.hud = hud;
this.tank = tank;
this.tankWarsGame = tankWarsGame;
}
public void checkKeyInput(Tank tank){
if(Gdx.input.isKeyPressed(Input.Keys.D)) {
tank.moveRight();
}
else if(Gdx.input.isKeyPressed(Input.Keys.A)) {
tank.moveLeft();
}
if(Gdx.input.isKeyPressed(Input.Keys.Q)) {
tank.rotateCannonLeft();
}
......@@ -32,9 +38,68 @@ public class GameController {
tank.rotateCannonRight();
}
if(Gdx.input.justTouched()) {
bullet = new Bullet(tank);
bullet.shoot();
if(moveRightTouched) {
tank.moveRight();
}
else if(moveLeftTouched) {
tank.moveLeft();
}
}
public void handleHudEvents() {
hud.getFireButton().addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
bullet = new Bullet(tank);
bullet.shoot();
//actor.setTouchable(Touchable.disabled);
// TODO: send turn to server + enable touchable when it is players turn
endPlayerTurn();
}
});
hud.getPowerSlider().addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println(hud.getPowerSlider().getValue());
tank.setPower(Math.round(hud.getPowerSlider().getValue()));
}
});
hud.getMoveLeft().addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
moveLeftTouched = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
moveLeftTouched = false;
}
});
hud.getMoveRight().addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
moveRightTouched = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
moveRightTouched = false;
}
});
}
public boolean endPlayerTurn() {
System.out.println(tank.getPower());
System.out.println(tank.getPosition());
// end turn for player and send data to server
return true;
}
}
package com.game.tankwars.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
......@@ -37,6 +38,7 @@ public class Tank {
private Vector2[] vertices;
int posInVertArr;
float cannonAngle = 90;
private int power;
boolean directionLeft;
public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft) {
......@@ -49,6 +51,8 @@ public class Tank {
this.posInVertArr = posInVertArr;
position = vertices[posInVertArr];
this.power = 25;
this.bounds = new Rectangle(position.x, position.y, TANK_WIDTH, TANK_HEIGHT);
this.chassisTexture = chassisTexture;
......@@ -99,7 +103,7 @@ public class Tank {
chassisSprite.flip(true, false);
directionLeft = false;
}
if (chassis.getPosition().x <= VIEWPORT_WIDTH - TANK_WIDTH){
if (chassis.getPosition().x <= TankWarsGame.GAMEPORT_WIDTH - TANK_WIDTH){
setPosition(newPos);
chassis.setTransform(newPos.x, newPos.y + 0.11f, angle);
chassisSprite.setRotation(angle);
......@@ -176,6 +180,10 @@ public class Tank {
public Sprite getChassisSprite() {return chassisSprite;}
public Sprite getCannonSprite() {return cannonSprite;}
public int getPower() {
return power;
}
public void setPosition(Vector2 position) {
this.position = position;
}
......@@ -184,6 +192,10 @@ public class Tank {
this.bounds = bounds;
}
public void setPower(int power) {
this.power = power;
}
public void setChassisTexture(Texture texture) {
this.chassisTexture = texture;
}
......
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;
......@@ -26,8 +21,8 @@ public class Terrain {
float xStart = -5;
float xEnd = 100;
float yMin = 1f;
float yMax = 10;
float yMin = 5f;
float yMax = 15;
int points = 10;
int vertNumber = 1000;
......
package com.game.tankwars.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Container;
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.game.tankwars.ResourceManager;
import com.ray3k.stripe.FreeTypeSkin;
public class GameHud {
private Stage stage;
private Viewport viewport;
private Table table;
private Skin skin;
private ProgressBar healthProgressBarPlayer;
private ProgressBar healthProgressBarOpponent;
private TextButton fireButton;
private HorizontalGroup powerContainer;
private Label powerLabel;
private Slider powerSlider;
private Button moveLeft;
private Button moveRight;
private HorizontalGroup moveContainer;
public GameHud(Viewport viewport, SpriteBatch batch) {
this.viewport = viewport;
stage = new Stage(viewport, batch);
table = new Table();
table.setFillParent(true);
table.setDebug(false);
stage.addActor(table);
skin = ResourceManager.getInstance().loadAndGetGameplayHudAssets();
healthProgressBarPlayer = new ProgressBar(0, 100, 1, false, skin);
healthProgressBarPlayer.setValue(100);
healthProgressBarOpponent = new ProgressBar(0, 100, 1, false, skin);
healthProgressBarOpponent.setValue(100);
Container HpOpponentWrapper = new Container(healthProgressBarOpponent);
HpOpponentWrapper.setTransform(true);
HpOpponentWrapper.setOrigin(HpOpponentWrapper.getPrefWidth() / 2, HpOpponentWrapper.getPrefHeight() / 2);
HpOpponentWrapper.setRotation(180);
table.add(healthProgressBarPlayer).expand().top().left().padTop(10).padLeft(10);
table.add(HpOpponentWrapper).colspan(2).top().right().padTop(10).padRight(10);
table.row();
fireButton = new TextButton("Fire!", skin);
table.add(fireButton).expand().bottom().left().padLeft(10).padBottom(10);
powerLabel = new Label("Power", skin.get("large-white", Label.LabelStyle.class));
powerSlider = new Slider(0, 100, 1, false, skin);
powerContainer = new HorizontalGroup().space(10);
powerContainer.addActor(powerLabel);
powerContainer.addActor(powerSlider);
table.add(powerContainer).expand().bottom().padBottom(10);
moveLeft = new Button(skin.get("move-left", Button.ButtonStyle.class));
moveRight = new Button(skin.get("move-right", Button.ButtonStyle.class));
moveContainer = new HorizontalGroup().space(10);
moveContainer.addActor(moveLeft);
moveContainer.addActor(moveRight);
table.add(moveContainer).expand().bottom().right().padBottom(10).padRight(10);
}
public Stage getStage() {
return stage;
}
public Viewport getViewport() {
return viewport;
}
public Skin getSkin() {
return skin;
}
public TextButton getFireButton() {
return fireButton;
}
public Slider getPowerSlider() {
return powerSlider;
}
public Button getMoveLeft() {
return moveLeft;
}
public Button getMoveRight() {
return moveRight;
}
/**
* Set visible health of player
* @param health new health of player
*/
public void setPlayerHealth(int health) {
healthProgressBarPlayer.setValue(health);
}
/**
* Set visible health of opponent
* @param health new health of opponent
*/
public void setOpponentHealth(int health) {
healthProgressBarOpponent.setValue(health);
}
}
......@@ -18,7 +18,7 @@ import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.controller.GameController;
import com.game.tankwars.model.Box2dWorld;
......@@ -36,10 +36,12 @@ public class GameScreen implements Screen {
ShapeRenderer shapeRender;
Tank myTank;
Tank opponentTank;
GameHud hud;
Box2dWorld model;
World world;
Terrain terrain;
OrthographicCamera cam;
OrthographicCamera worldCam;
OrthographicCamera hudCam;
Box2DDebugRenderer debugRenderer;
Bullet bullet;
GameController controller;
......@@ -56,9 +58,16 @@ public class GameScreen implements Screen {
model = new Box2dWorld();
world = Box2dWorld.getWorld();
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
cam.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0);
cam.update();
worldCam = new OrthographicCamera(scale(TankWarsGame.GAMEPORT_WIDTH), scale(TankWarsGame.GAMEPORT_HEIGHT));
worldCam.position.set(scale(TankWarsGame.GAMEPORT_WIDTH)/2, scale(TankWarsGame.GAMEPORT_HEIGHT)/2, 0);
hudCam = new OrthographicCamera(TankWarsGame.GAMEPORT_WIDTH, TankWarsGame.GAMEPORT_HEIGHT);
hudCam.position.set(TankWarsGame.GAMEPORT_WIDTH/2, TankWarsGame.GAMEPORT_HEIGHT/2, 0);
worldCam.update();
hudCam.update();
debugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true);
terrain = new Terrain();
......@@ -77,18 +86,23 @@ public class GameScreen implements Screen {
terrain,
tankWarsGame, false);
horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
horizontalScaling = Gdx.graphics.getWidth() / TankWarsGame.GAMEPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / TankWarsGame.GAMEPORT_HEIGHT;
hud = new GameHud(new FitViewport(TankWarsGame.GAMEPORT_WIDTH, TankWarsGame.GAMEPORT_HEIGHT, hudCam), batch);
controller = new GameController(myTank, tankWarsGame);
controller = new GameController(myTank, tankWarsGame, hud);
Gdx.input.setInputProcessor(hud.getStage());
controller.handleHudEvents();
}
@Override
public void render(float delta) {
model.logicStep(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(0, 0, 100, 100);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, cam.combined);
shapeRender.setProjectionMatrix(cam.combined);
debugRenderer.render(world, worldCam.combined);
shapeRender.setProjectionMatrix(worldCam.combined);
controller.checkKeyInput(myTank);
......@@ -100,7 +114,7 @@ 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 + 0.25f) * (float) verticalScaling);
s.setPosition(b.getPosition().x * (float) TankWarsGame.SCALE - s.getWidth() / 2, (b.getPosition().y + 0.25f) * (float) TankWarsGame.SCALE);
if (s.equals(myTank.getChassisSprite())) {
s.setRotation(myTank.getAngle());
}
......@@ -128,6 +142,9 @@ public class GameScreen implements Screen {
opponentTank.getChassisSprite().draw(batch);
opponentTank.getCannonSprite().draw(batch);
batch.end();
batch.setProjectionMatrix(hud.getStage().getCamera().combined);
hud.getStage().draw();
}
@Override
......@@ -158,7 +175,12 @@ public class GameScreen implements Screen {
public void dispose() {
myTank.getChassisTexture().dispose();
myTank.getCannonTexture().dispose();
hud.getStage().dispose();
batch.dispose();
shapeRender.dispose();
}
private float scale(float value) {
return value / TankWarsGame.SCALE;
}
}
......@@ -2,15 +2,25 @@ package com.game.tankwars.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Value;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.FocusListener;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
......@@ -114,7 +124,7 @@ public class LoginScreen implements Screen {
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
//Gdx.input.setInputProcessor(null);
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