Fixed random token assigning and realisation

Token class now has two functions: assignRandomType and realiseRandomType
The former can be used to assign any random type to the token including the random type
The latter can be used to assign a concrete random type to the token (not the random type)

#story[1245]
This commit is contained in:
William Muir
2017-09-20 12:02:12 +12:00
parent 52d3cea592
commit 6cde016401
4 changed files with 39 additions and 19 deletions
@@ -283,10 +283,9 @@ public class GameState implements Runnable {
Random random = new Random(); Random random = new Random();
tokensInPlay.clear(); tokensInPlay.clear();
//Get a random token location //Get a random token location with random type
Token token = allTokens.get(random.nextInt(allTokens.size())); Token token = allTokens.get(random.nextInt(allTokens.size()));
//Set a random type to this token token.assignRandomType();
token.setTokenType(TokenType.values()[random.nextInt(TokenType.values().length)]);
tokensInPlay.add(token); tokensInPlay.add(token);
} }
@@ -389,6 +388,7 @@ public class GameState implements Runnable {
//Yacht Collision //Yacht Collision
ServerYacht collidedYacht = checkYachtCollision(serverYacht); ServerYacht collidedYacht = checkYachtCollision(serverYacht);
Mark collidedMark = checkMarkCollision(serverYacht); Mark collidedMark = checkMarkCollision(serverYacht);
Token collidedToken = checkTokenPickUp(serverYacht);
if (collidedYacht != null) { if (collidedYacht != null) {
GeoPoint originalLocation = serverYacht.getLocation(); GeoPoint originalLocation = serverYacht.getLocation();
@@ -433,8 +433,10 @@ public class GameState implements Runnable {
} }
//Token Collision //Token Collision
Token collidedToken = checkTokenPickUp(serverYacht);
if (collidedToken != null) { if (collidedToken != null) {
if (collidedToken.getTokenType() == TokenType.RANDOM) {
collidedToken.realiseRandom();
}
sendServerMessage(serverYacht.getSourceId(), sendServerMessage(serverYacht.getSourceId(),
serverYacht.getBoatName() + " has picked up a " + collidedToken.getTokenType() serverYacht.getBoatName() + " has picked up a " + collidedToken.getTokenType()
.getName() + " token"); .getName() + " token");
+21 -2
View File
@@ -1,5 +1,9 @@
package seng302.model.token; package seng302.model.token;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import seng302.model.GeoPoint; import seng302.model.GeoPoint;
/** /**
@@ -9,6 +13,7 @@ import seng302.model.GeoPoint;
public class Token extends GeoPoint { public class Token extends GeoPoint {
private TokenType tokenType; private TokenType tokenType;
private Random random = new Random();
public Token(TokenType tokenType, double lat, double lng) { public Token(TokenType tokenType, double lat, double lng) {
super(lat, lng); super(lat, lng);
@@ -19,7 +24,21 @@ public class Token extends GeoPoint {
return tokenType; return tokenType;
} }
public void setTokenType(TokenType tokenType) { /**
this.tokenType = tokenType; * Assigns a random type to the token (including the random type token)
*/
public void assignRandomType() {
tokenType = TokenType.values()[random.nextInt(TokenType.values().length)];
} }
/**
* Assigns a random, concrete type to the token (cannot be the random type)
*/
public void realiseRandom() {
List<TokenType> tokenTypeList = new ArrayList<>(Arrays.asList(TokenType.values()));
tokenTypeList.remove(TokenType.RANDOM);
tokenType = tokenTypeList.get(random.nextInt(tokenTypeList.size()));
}
} }
@@ -8,8 +8,8 @@ public enum TokenType {
BOOST(0, "Boost"), BOOST(0, "Boost"),
HANDLING(1, "Handling"), HANDLING(1, "Handling"),
BUMPER(2, "Bumper"), BUMPER(2, "Bumper"),
RANDOM(3, "Random"), WIND_WALKER(3, "Wind Walker"),
WIND_WALKER(4, "Wind Walker"); RANDOM(4, "Random");
private int value; private int value;
private String name; private String name;
@@ -26,15 +26,4 @@ public enum TokenType {
public String getName() { public String getName() {
return name; return name;
} }
public static TokenType getToken(int value) {
switch (value) {
case 0:
return BOOST;
case 1:
return HANDLING;
default:
return BOOST;
}
}
} }
@@ -446,6 +446,16 @@ public class GameClient {
// TODO: 11/09/17 wmu16 - Add in functionality to viually indicate a pickup to a user // TODO: 11/09/17 wmu16 - Add in functionality to viually indicate a pickup to a user
private void showTokenPickUp(TokenType tokenType) { private void showTokenPickUp(TokenType tokenType) {
Sounds.playTokenPickupSound(); Sounds.playTokenPickupSound();
switch (tokenType) {
case BOOST:
break;
case HANDLING:
break;
case WIND_WALKER:
break;
case BUMPER:
break;
}
} }
private void formatAndSendChatMessage(String rawChat) { private void formatAndSendChatMessage(String rawChat) {