mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Legger til en timer som starter når en spiller er den siste som programmerer, og gir spilleren et tilfeldig program om den går ut Closes #72
This commit is contained in:
parent
cb678b419b
commit
b02e665e86
@ -34,4 +34,11 @@ public interface RoboRallyUI {
|
||||
* @return The server of the game
|
||||
*/
|
||||
RoboRallyServer getServer();
|
||||
|
||||
/**
|
||||
* Sets whether the client should hurry with whatever it's doing
|
||||
*
|
||||
* @param shouldHurry True if the user should hurry
|
||||
*/
|
||||
void setShouldHurry(boolean shouldHurry);
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ public class RoboRallyWrapper extends Game implements RoboRallyUI {
|
||||
public RoboRallyGame roboRallyGame;
|
||||
public RoboRallyServer server;
|
||||
public RoboRallyClient client;
|
||||
public boolean shouldHurry = false;
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
@ -59,6 +60,11 @@ public class RoboRallyWrapper extends Game implements RoboRallyUI {
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShouldHurry(boolean shouldHurry) {
|
||||
this.shouldHurry = shouldHurry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Quits the game
|
||||
*/
|
||||
|
@ -39,6 +39,7 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
|
||||
private final ShapeRenderer shapeRenderer;
|
||||
private final List<CardRectangle> chosenCards;
|
||||
private final int maxCards;
|
||||
private long timerStarted;
|
||||
|
||||
/**
|
||||
* Instantiates a new card choice screen
|
||||
@ -213,10 +214,23 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
|
||||
roboRallyWrapper.batch.begin();
|
||||
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Press TAB to toggle the board", 10,
|
||||
viewport.getWorldHeight() - 50);
|
||||
int timerSize = 30;
|
||||
if (timerStarted != 0) {
|
||||
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Time left: " + String.valueOf(timerSize -
|
||||
(System.currentTimeMillis() - timerStarted) / 1000), viewport.getWorldWidth() - 150,
|
||||
viewport.getWorldHeight() - 50);
|
||||
}
|
||||
renderCardText();
|
||||
roboRallyWrapper.batch.end();
|
||||
stage.draw();
|
||||
stage.act();
|
||||
if (roboRallyWrapper.shouldHurry && timerStarted == 0) {
|
||||
timerStarted = System.currentTimeMillis();
|
||||
}
|
||||
if (System.currentTimeMillis() > timerStarted + 1000 * timerSize && timerStarted > 0) {
|
||||
fillProgramWithRandomCards();
|
||||
confirmCards(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -251,6 +265,23 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
|
||||
roboRallyWrapper.batch.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the user's program with enough cards to complete the program
|
||||
*/
|
||||
private void fillProgramWithRandomCards() {
|
||||
int missingCards = maxCards - getCards().size();
|
||||
for (int i = 0; i < missingCards; i++) {
|
||||
for (CardRectangle rectangle : cardRectangles) {
|
||||
if (!rectangle.selected) {
|
||||
rectangle.selected = true;
|
||||
rectangle.selectable = false;
|
||||
chosenCards.add(rectangle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the text displayed on cards
|
||||
*/
|
||||
|
@ -6,6 +6,7 @@ import inf112.fiasko.roborally.elementproperties.GameState;
|
||||
import inf112.fiasko.roborally.gamewrapper.RoboRallyUI;
|
||||
import inf112.fiasko.roborally.networking.containers.ErrorResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.GameStartInfoResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.HurryResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.OkayResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.PowerDownContainerResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.ProgramsContainerResponse;
|
||||
@ -67,6 +68,8 @@ class RoboRallyClientListener extends Listener {
|
||||
new Thread(() -> wrapper.getGame().receiveStayInPowerDown((PowerDownContainerResponse) object)).start();
|
||||
} else if (object instanceof OkayResponse) {
|
||||
this.lastRequestState = RequestState.SENT_OKAY;
|
||||
} else if (object instanceof HurryResponse) {
|
||||
this.wrapper.setShouldHurry(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.Listener;
|
||||
import inf112.fiasko.roborally.elementproperties.RobotID;
|
||||
import inf112.fiasko.roborally.networking.containers.ErrorResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.HurryResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.OkayResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.PowerDownContainerResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
|
||||
@ -169,6 +170,13 @@ class RoboRallyServerListener extends Listener {
|
||||
}
|
||||
server.sendToAllClients(new ProgramsContainerResponse(program, powerDown));
|
||||
programs = new HashMap<>();
|
||||
} else {
|
||||
List<Connection> notReceivedFrom = playersNotYetReceivedFrom(programs);
|
||||
if (notReceivedFrom.size() != 1) {
|
||||
return;
|
||||
}
|
||||
Connection hurryUp = notReceivedFrom.get(0);
|
||||
hurryUp.sendTCP(new HurryResponse());
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,6 +193,20 @@ class RoboRallyServerListener extends Listener {
|
||||
return data.keySet().containsAll(connections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list containing all connections the data has not been received from
|
||||
*
|
||||
* @param data The data map to check
|
||||
* @param <K> The type of data contained in the map
|
||||
* @return All active connections for which the map has no data
|
||||
*/
|
||||
private <K> List<Connection> playersNotYetReceivedFrom(Map<Connection, K> data) {
|
||||
Set<Connection> connections = clients.keySet();
|
||||
connections.removeAll(deadPlayers);
|
||||
connections.removeAll(data.keySet());
|
||||
return new ArrayList<>(connections);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connected(Connection connection) {
|
||||
//Prevent players from joining after the game has started
|
||||
|
@ -0,0 +1,7 @@
|
||||
package inf112.fiasko.roborally.networking.containers;
|
||||
|
||||
/**
|
||||
* A response telling the client to hurry up the action it's doing because other clients are waiting
|
||||
*/
|
||||
public class HurryResponse {
|
||||
}
|
@ -5,6 +5,7 @@ import inf112.fiasko.roborally.elementproperties.Action;
|
||||
import inf112.fiasko.roborally.elementproperties.RobotID;
|
||||
import inf112.fiasko.roborally.networking.containers.ErrorResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.GameStartInfoResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.HurryResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.OkayResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.PowerDownContainerResponse;
|
||||
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
|
||||
@ -44,5 +45,6 @@ public final class NetworkUtil {
|
||||
kryo.register(HashMap.class);
|
||||
kryo.register(UsernameRequest.class);
|
||||
kryo.register(OkayResponse.class);
|
||||
kryo.register(HurryResponse.class);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user