Skip to content

Commit

Permalink
Merge pull request #35 from Our-Magic-Journey/turn-system
Browse files Browse the repository at this point in the history
create turn system
  • Loading branch information
AmonDeShir authored Dec 31, 2023
2 parents a3c67d7 + ba4f283 commit 04070fa
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 19 deletions.
4 changes: 3 additions & 1 deletion assets/assets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ images/planet-field-down.png
images/planet-field-color.png
images/map-done.png
images/board-background.png
images/player.png
images/player0.png
images/player1.png
images/player2.png
animations/planet/tatuine.atlas
animations/planet/centre.atlas
animations/planet/pluto.atlas
Expand Down
Binary file removed assets/images/player.png
Binary file not shown.
Binary file added assets/images/player0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/player1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/player2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions assets/skin/ui.skin.atlas
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ panel
orig: 57, 96
offset: 0, 0
index: -1
white-pixel
rotate: false
xy: 126, 112
size: 1, 1
orig: 1, 1
offset: 0, 0
index: -1
panel-margin
rotate: false
xy: 60, 71
Expand Down
Binary file modified assets/skin/ui.skin.json
Binary file not shown.
Binary file modified assets/skin/ui.skin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion core/src/xyz/magicjourney/nebulaquest/NebulaQuest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class NebulaQuest extends Game {
protected ScreenManager screenManager;
protected MusicManager musicManager;


@Override
public void create () {
this.batch = new SpriteBatch();
Expand Down
17 changes: 17 additions & 0 deletions core/src/xyz/magicjourney/nebulaquest/player/Player.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package xyz.magicjourney.nebulaquest.player;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class Player {
private static int nextID = 0;

protected int id;
protected String name;

public Player(String name) {
this.name = name;
this.id = generateId();
}

private int generateId() {
return Player.nextID++;
}

public String getName() {
return this.name;
}

public TextureRegionDrawable getShip(AssetManager assets) {
return new TextureRegionDrawable(new TextureRegion(assets.get("images/player" + id + ".png", Texture.class)));
}
}
37 changes: 27 additions & 10 deletions core/src/xyz/magicjourney/nebulaquest/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,41 @@
import xyz.magicjourney.nebulaquest.entity.entities.planet.Planet;
import xyz.magicjourney.nebulaquest.entity.entities.planet.PlanetRegion;
import xyz.magicjourney.nebulaquest.music.MusicManager;
import xyz.magicjourney.nebulaquest.player.Player;
import xyz.magicjourney.nebulaquest.ui.panel.InteractivePanel;
import xyz.magicjourney.nebulaquest.ui.panel.MenuPanel;
import xyz.magicjourney.nebulaquest.ui.panel.OptionPanel;
import xyz.magicjourney.nebulaquest.ui.panel.TourPanel;

public class GameScreen extends AbstractScreen {
ArrayList<PlanetRegion> regions;
ArrayList<Entity> entities;
Board board;
InteractivePanel interactivePanel;
OptionPanel optionsPanel;
TourPanel tourPanel;
MenuPanel menuPanel;
Dice dice;
protected Player activePlayer;
protected ArrayList<Player> players;
protected ArrayList<PlanetRegion> regions;
protected ArrayList<Entity> entities;
protected Board board;
protected InteractivePanel interactivePanel;
protected OptionPanel optionsPanel;
protected TourPanel tourPanel;
protected MenuPanel menuPanel;
protected Dice dice;

public GameScreen(SpriteBatch batch, AssetManager assets, ScreenManager screenManager, MusicManager musicManager) {
super(batch, assets, screenManager, musicManager);

this.players = new ArrayList<>();
this.regions = new ArrayList<>();
this.entities = new ArrayList<>();
this.populatePlayers();
this.populateRegions();
this.populateBoard();

this.activePlayer = this.players.get(0);
}

public void populatePlayers() {
this.players.add(new Player("Player 1"));
this.players.add(new Player("Player 2"));
this.players.add(new Player("Player 3"));
}

protected void populateRegions() {
Expand Down Expand Up @@ -115,7 +128,7 @@ public void create() {
this.optionsPanel = new OptionPanel(assets);
this.optionsPanel.setPosition(751, 189);

this.tourPanel = new TourPanel(assets);
this.tourPanel = new TourPanel(assets, this.players);
this.tourPanel.setPosition(751, 72);

this.menuPanel = new MenuPanel(assets);
Expand All @@ -128,7 +141,11 @@ public void create() {
this.board.onFieldSelect().subscribe(this::handleFieldSelect);
this.board.onFieldUnselect().subscribe(this::handlePanelUnselect);

this.tourPanel.onRoll().subscribe(() -> dice.roll((r) -> System.out.println(r)));
this.tourPanel.onRoll().subscribe(() -> dice.roll((r) -> {
System.out.println(r);
this.tourPanel.setTurnEndMode();
}));
this.tourPanel.onTurnStarted().subscribe((player) -> this.activePlayer = player);

this.dice.setPosition(0, 0);
this.stage.addActor(board);
Expand Down
62 changes: 62 additions & 0 deletions core/src/xyz/magicjourney/nebulaquest/ui/dialog/MessageBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package xyz.magicjourney.nebulaquest.ui.dialog;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;

import xyz.magicjourney.nebulaquest.event.Event;
import xyz.magicjourney.nebulaquest.event.EventGetter;
import xyz.magicjourney.nebulaquest.listener.Listener;

public class MessageBox extends Window {
protected Skin skin;
protected Label message;
protected TextButton okBtn;
protected Event acceptedEvent;

public MessageBox(String text, AssetManager assets) {
super("", assets.get("skin/ui.skin.json", Skin.class));

this.skin = super.getSkin();
this.acceptedEvent = new Event();
this.message = new Label(text, this.skin, "small");
this.okBtn = new TextButton("Ok", this.skin, "orange");

this.setWidth(300);
this.setHeight(100);

this.clear();
this.pad(6);
this.add(this.message).expandX().center().row();
this.add(okBtn).minWidth(100).expandX().center().pad(10);

this.okBtn.addListener(new Listener(this::handleOkButtonClick));
}

public EventGetter onAccepted() {
return this.acceptedEvent;
}

public void show(Stage stage) {
stage.addActor(this);

this.center(stage.getWidth(), stage.getHeight());
}

protected void center(float width, float height) {
this.setX((width - this.getWidth()) / 2);
this.setY((height - this.getHeight()) / 2);
}

protected void handleOkButtonClick() {
this.remove();
this.acceptedEvent.emit();
}

public void setText(String text) {
this.message.setText(text);
}
}
60 changes: 53 additions & 7 deletions core/src/xyz/magicjourney/nebulaquest/ui/panel/TourPanel.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,64 @@
package xyz.magicjourney.nebulaquest.ui.panel;

import java.util.ArrayList;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.ui.Cell;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.Queue;

import xyz.magicjourney.nebulaquest.event.Event;
import xyz.magicjourney.nebulaquest.event.EventGetter;
import xyz.magicjourney.nebulaquest.event.ParameterizedEvent;
import xyz.magicjourney.nebulaquest.event.ParameterizedEventGetter;
import xyz.magicjourney.nebulaquest.listener.Listener;
import xyz.magicjourney.nebulaquest.player.Player;
import xyz.magicjourney.nebulaquest.ui.dialog.MessageBox;

public class TourPanel extends Panel {
protected Queue<Player> players;
protected TextButton roll;
protected TextButton endTurn;
protected Cell<?> buttonCell;
protected Label money;
protected Image player;
protected Image playerImage;
protected MessageBox playerTurnMsg;
protected AssetManager assets;

protected Event rollEvent;
protected ParameterizedEvent<Player> turnStartedEvent;

public TourPanel(AssetManager assets) {
public TourPanel(AssetManager assets, ArrayList<Player> players) {
super(207, 115, assets);

this.assets = assets;
this.players = new Queue<>();
players.forEach(this.players::addLast);

this.playerTurnMsg = new MessageBox("", assets);
this.roll = new TextButton("Roll", this.skin, "orange");
this.endTurn = new TextButton("End turn", this.skin);
this.money = new Label("1000$", this.skin);
this.player = new Image(assets.get("images/player.png", Texture.class));
this.playerImage = new Image(this.players.first().getShip(assets));

this.rollEvent = new Event();
this.turnStartedEvent = new ParameterizedEvent<>();

Table playerDescription = new Table();
playerDescription.add(player).expand().center();
playerDescription.add(playerImage).expand().center();
playerDescription.add(money).expand().center();


this.content.add(playerDescription).expand().fill();
this.content.row();
this.content.add(roll).fillX();

this.buttonCell = this.content.add(roll).fillX();
this.roll.addListener(new Listener(rollEvent::emit));
this.endTurn.addListener(new Listener(this::handleTurnEnd));
this.playerTurnMsg.onAccepted().subscribe(() -> turnStartedEvent.emit(this.players.first()));
}

@Override
Expand All @@ -45,4 +69,26 @@ public void act(float delta) {
public EventGetter onRoll() {
return this.rollEvent;
}

public ParameterizedEventGetter<Player> onTurnStarted() {
return this.turnStartedEvent;
}

public void setRollMode() {
this.buttonCell.clearActor();
this.buttonCell.setActor(roll);
}

public void setTurnEndMode() {
this.buttonCell.clearActor();
this.buttonCell.setActor(endTurn);
}

protected void handleTurnEnd() {
this.players.addLast(this.players.removeFirst());
this.playerImage.setDrawable(this.players.first().getShip(assets));
this.playerTurnMsg.setText("It is the turn of " + this.players.first().getName() + "!");
this.playerTurnMsg.show(this.getStage());
this.setRollMode();
}
}

0 comments on commit 04070fa

Please sign in to comment.