Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 63 additions & 11 deletions core/src/nl/openweb/confetti/GridManager.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package nl.openweb.confetti;

import lombok.Data;
import nl.openweb.confetti.model.GridCell;
import nl.openweb.confetti.model.Player;
import nl.openweb.confetti.dialog.GameNotification;
import nl.openweb.confetti.exception.GridOutOfBoundsException;
import nl.openweb.confetti.model.*;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static nl.openweb.confetti.model.GridCell.GRID_CELL_SIZE;
import static nl.openweb.confetti.model.GridCell.GRID_DIMENSION;

@Data
public class GridManager {
public static final int PLAYER_MOVE_DELAY = 500;
private final List<GridCell> gridCells;

private float gridStartX;
private float gridStartY;
private List<Player> players;
private List<PlayerActor> players;
private String activePlayerId;

private boolean gameFinished;

private static GridManager gridManager;

public static GridManager getInstance() {
Expand Down Expand Up @@ -50,24 +56,70 @@ public void setActivePlayer(Player player) {
this.activePlayerId = player.getId();
}

public Player getNextActivePlayer() {
public PlayerActor getNextActivePlayer(boolean resetToStart) {
int nextPlayerIndex = players.indexOf(getActivePlayer()) + 1;
if(nextPlayerIndex < players.size()) {
Player player = players.get(nextPlayerIndex);
activePlayerId = player.getId();
return player;
long playersAliveCount = players.stream().filter(PlayerActor::isAlive).count();
long playersNoMovesCount = players.stream().filter(playerActor -> playerActor.getMoves().size() == 0).count();

if(playersAliveCount > 1) {
if (playersNoMovesCount == players.size()) {
GameNotification.getInstance().showNotification("Next round!");
}
if (nextPlayerIndex < players.size()) {
PlayerActor player = players.get(nextPlayerIndex);
activePlayerId = player.getId();

if (!player.isAlive()) {
return getNextActivePlayer(true);
} else {
return player;
}
} else {
if (resetToStart) {
return resetActivePlayer();
} else {
return null;
}
}
} else {
Optional<PlayerActor> winningPlayerOpt = players.stream().filter(PlayerActor::isAlive).findFirst();
if (winningPlayerOpt.isPresent()) {
gameFinished = true;
activePlayerId = winningPlayerOpt.get().getId();
System.out.println("Only one player left, player has WON the game: " + winningPlayerOpt.get().getName());
GameNotification.getInstance().showNotification("Winner " + winningPlayerOpt.get().getName(), true);
}
return null;
}
}

public Player resetActivePlayer() {
Player player = players.get(0);
public PlayerActor getNextActivePlayer() {
return getNextActivePlayer(false);
}

public Point2D getCellCenterCoordinates(GridCoordinates gridCoordinates) {
if (gridCoordinates.getRow() >= GRID_DIMENSION || gridCoordinates.getRow() < 0) throw new GridOutOfBoundsException("Requested cell row does not exist: " + gridCoordinates.getRow());
if (gridCoordinates.getColumn() >= GRID_DIMENSION || gridCoordinates.getColumn() < 0) throw new GridOutOfBoundsException("Requested cell column does not exist: " + gridCoordinates.getColumn());

final float cellCenterStartX = GridManager.getInstance().getGridStartX() + (gridCoordinates.getColumn() * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f);
final float cellCenterStartY = GridManager.getInstance().getGridStartY() + (gridCoordinates.getRow() * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f);

return new Point2D.Float(cellCenterStartX, cellCenterStartY);
}

public void performPlayerMove() {
PlayerActor activePlayer = getActivePlayer();
Move move = activePlayer.popMove();
if (move != null) activePlayer.applyMove(move);
}

public PlayerActor resetActivePlayer() {
PlayerActor player = players.stream().filter(PlayerActor::isAlive).findFirst().get();
activePlayerId = player.getId();
return player;
}

public Player getActivePlayer() {
public PlayerActor getActivePlayer() {
return this.players.stream().filter(player -> player.getId().equals(activePlayerId)).findAny().orElseThrow();
}
}
9 changes: 5 additions & 4 deletions core/src/nl/openweb/confetti/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import nl.openweb.confetti.model.GridCell;
import nl.openweb.confetti.model.Move;
import nl.openweb.confetti.model.Player;
import nl.openweb.confetti.model.PlayerActor;

import java.sql.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -62,7 +63,7 @@ private void addPlayer(String playerName, String position, String textureFilenam
player.execute("INSERT INTO PLAYERS (id, name, texture, position) VALUES ('" + playerId + "', '" + playerName + "', '" + textureFilename + "', '" + position + "')");
}

public void addMoves(Player player, List<Move> moves) {
public void addMoves(PlayerActor player, List<Move> moves) {
final AtomicInteger moveId = new AtomicInteger(0);
moves.forEach(move -> {
Statement addMovesStatement;
Expand Down Expand Up @@ -96,15 +97,15 @@ public List<Move> getMoves(String playerId) {
return moves;
}

public List<Player> getPlayers() {
List<Player> players = new ArrayList<>();
public List<PlayerActor> getPlayers() {
List<PlayerActor> players = new ArrayList<>();
try {
ResultSet resultSet = conn.createStatement().executeQuery("SELECT * FROM PLAYERS");
while (resultSet.next()) {
String[] positions = resultSet.getString("position").split("-");
Texture texture = new Texture(Gdx.files.internal(resultSet.getString("texture")));

Player player = new Player(
PlayerActor player = new PlayerActor(
resultSet.getString("id"),
resultSet.getString("name"),
texture,
Expand Down
75 changes: 62 additions & 13 deletions core/src/nl/openweb/confetti/dialog/GameNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,47 @@
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.utils.Align;
import lombok.Getter;
import nl.openweb.confetti.ConfettiGame;

public class GameNotification {
private static final int MARGIN = 10;

private final ShapeRenderer notificationRenderer;
private final GlyphLayout glyphLayout;
private final ConfettiGame game;
private final SpriteBatch batch;
private final float duration;
private static GameNotification gameNotification;

private ShapeRenderer notificationRenderer;
private GlyphLayout normalTextLayout;
private GlyphLayout subTextLayout;
private ConfettiGame game;
private SpriteBatch batch;
private float duration;
private float width;
private float height;
private float startX;
private float startY;
private DialogEvent dialogEvent;
private BitmapFont font;
private BitmapFont subFont;
private String text;

@Getter
private boolean waitForKeyPress;

@Getter
private boolean visible;

private float timePassed = Float.MAX_VALUE;
private Runnable executeFunction;

public static GameNotification getInstance() {
if (gameNotification == null) {
gameNotification = new GameNotification();
}

public GameNotification(ConfettiGame game, String text, int duration, int width, int height, DialogEvent dialogEvent) {
return gameNotification;
}

public void init(ConfettiGame game, String text, int duration, int width, int height, DialogEvent dialogEvent) {
this.game = game;
this.text = text;
this.duration = duration;
Expand All @@ -38,7 +56,8 @@ public GameNotification(ConfettiGame game, String text, int duration, int width,
this.startX = game.getCenterX() - (width / 2f);
this.startY = game.getCenterY() - (height / 2f);
this.notificationRenderer = new ShapeRenderer();
this.glyphLayout = new GlyphLayout();
this.normalTextLayout = new GlyphLayout();
this.subTextLayout = new GlyphLayout();
this.dialogEvent = dialogEvent;
this.batch = new SpriteBatch();
this.createBitmapFont();
Expand All @@ -48,8 +67,8 @@ public void drawNotification() {
float deltaTime = Gdx.graphics.getDeltaTime();
timePassed += deltaTime;

if (timePassed < duration) {
width = glyphLayout.width + 200;
if (timePassed < duration || waitForKeyPress) {
width = normalTextLayout.width + 200;
startX = game.getCenterX() - (width / 2f);

notificationRenderer.begin(ShapeRenderer.ShapeType.Filled);
Expand All @@ -65,11 +84,21 @@ public void drawNotification() {

batch.begin();
batch.setProjectionMatrix(game.getCamera().combined);
font.draw(batch, glyphLayout, game.getCenterX() - (glyphLayout.width / 2f) , game.getCenterY() + 16);
font.draw(batch, normalTextLayout, game.getCenterX() - (normalTextLayout.width / 2f) , game.getCenterY() + 16);

if (waitForKeyPress) {
subFont.draw(batch, subTextLayout, game.getCenterX() - (subTextLayout.width / 2f), game.getCenterY() - 72);
}

batch.end();
} else if (visible) {
visible = false;
waitForKeyPress = false;
dialogEvent.dialogClosed();

if (executeFunction != null) {
executeFunction.run();
}
}
}

Expand All @@ -81,12 +110,23 @@ public void setDialogEvent(DialogEvent dialogEvent) {
this.dialogEvent = dialogEvent;
}

public void setText(final String text) {
public void showNotification(final String text) {
showNotification(text, false);
}

public void showNotification(final String text, boolean waitForKeyPress) {
showNotification(text, waitForKeyPress, null);
}

public void showNotification(final String text, boolean waitForKeyPress, Runnable executeFunction) {
this.text = text;
this.timePassed = 0;
this.visible = true;
this.waitForKeyPress = waitForKeyPress;
this.executeFunction = executeFunction;

glyphLayout.setText(font, text);
normalTextLayout.setText(font, text);
subTextLayout.setText(subFont, "< ENTER >");
}

private void createBitmapFont() {
Expand All @@ -101,6 +141,15 @@ private void createBitmapFont() {
parameter.shadowColor = new Color(0, 0.5f, 0, 0.75f);

font = generator.generateFont(parameter);

parameter.size = 16;

subFont = generator.generateFont(parameter);
}

public void close() {
this.waitForKeyPress = false;
this.visible = false;
}

public void dispose() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nl.openweb.confetti.exception;

public class GridOutOfBoundsException extends RuntimeException {
public GridOutOfBoundsException(String message) {
super(message);
}
}
5 changes: 1 addition & 4 deletions core/src/nl/openweb/confetti/model/GridCell.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package nl.openweb.confetti.model;

public class GridCell {
public class GridCell extends GridCoordinates {
public static final int GRID_DIMENSION = 5;
public static final int GRID_CELL_SIZE = 100;

int column;
int row;
float startX;
float startY;

Expand Down
31 changes: 31 additions & 0 deletions core/src/nl/openweb/confetti/model/GridCoordinates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nl.openweb.confetti.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import nl.openweb.confetti.GridManager;

import java.awt.geom.Point2D;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GridCoordinates {
protected int row;
protected int column;

public GridCoordinates applyMove(Move move) {
column += move.getDeltaX();
row += move.getDeltaY();
return this;
}

public void setCoordinates(GridCoordinates coordinates) {
this.column = coordinates.getColumn();
this.row = coordinates.getRow();
}

public Point2D getXYCenterCoordinates() {
return GridManager.getInstance().getCellCenterCoordinates(this);
}
}
Loading