UGS - UQdeco2800/2022-studio-1 GitHub Wiki
The UGS class was created based off of the Observer design pattern, and is important to the studio given that it maintains all relevant methods for maintaining the state of the UGS.
The UGS class has various getter/setter methods which are outlined below:
- getTileType takes a coordinate (formatted as a string), and returns the associated Tile type
- getEntity takes a coordinate, and returns the associated entity
- setTileType takes a coordinate and a Tile Type. Sets the associated Tile's tile type
- setEntity takes a coordinate, and an Entity 'entity'. Sets the associated Tile's entity to 'entity'
setLargeEntity method: Allows setting multiple Tile's entity parameter to reference one entity, effectively allowing the UGS to be updated to store entities greater than 1x1 Tiles.
/**
* Function for setting / updating tiles for an entity whose size is greater than 1x1.
*
* Function takes an x,y dimension and will set/update the coordinates within those dimensions from the x,y
* origin to contain the entity.
*
* e.g.
* origin is 1,0
* dimensionX = 2
* dimensionY = 2
*
* _ _ _ _ _ _
* |_|_|_| |_|x|x|
* |_|_|_| -> |_|x|x|
* |_|_|_| |_|_|_|
*
* @param origin String
* @param entity Entity
* @param dimensionX Int
* @param dimensionY Int
*/
public void setLargeEntity(String origin, Entity entity, int dimensionX, int dimensionY) {
String[] originCoords = origin.split(",");
int originX = Integer.parseInt(originCoords[0]);
int originY = Integer.parseInt(originCoords[1]);
for (int x = 0; x < dimensionX; x++) {
for (int y = 0; y < dimensionY; y++) {
int coordX = originX + x;
int coordY = originY + y;
String coordinate = generateCoordinate(coordX, coordY);
this.setEntity(coordinate, entity);
}
}
}
generateCoordinate: Takes two integers, each representing either a x or y coordinate, and returns a formatted String representation of the coordinate.
/**
* Generates a coordinate given an x and y value
* @param x integer
* @param y integer
* @return String
*/
public static String generateCoordinate(int x, int y) {
return String.format("%d,%d", x, y);
}
moveEntity: Method for moving an entity's current position.
/**
*
* --------------------Does not account for tile type--------------------
* Checks whether an entity can move in the given direction, and returns True if the move was carried
* out correctly. If the move can be carried out, removes the entity from the current tile and adds it
* to the new tile.
*
* Takes a coordinate string as the currentPosition, and a boolean for yDirection and xDirection.
* yDirection: True if moving up (i.e. pressing w key) else false for moving down (s key)
* xDirection: True if moving to the right (i.e. pressing d key ) else false for moving to the left (a key)
*
* E.g. currentPosition: "3,3"
* yDirection: True
* xDirection: False
*
* Moves from 3,3 -> "2,2"
* <x-1, y-1>
* @param currentPosition String
* @param yDirection Boolean
* @param xDirection Boolean
*/
public boolean moveEntity(Entity entity, String currentPosition, boolean xDirection, boolean yDirection) {
String[] currentCoords = currentPosition.split(",");
int currentX = Integer.parseInt(currentCoords[0]);
int currentY = Integer.parseInt(currentCoords[1]);
if (xDirection) { //If xDirection is true then x coordinate increases [+1]
if (yDirection) { //If yDirection is true then y coordinate decreases [-1]
int newX = currentX + 1;
int newY = currentY - 1;
String coordinate = generateCoordinate(newX, newY);
if (getEntity(coordinate) == null) { //Check no entity in new tile
setEntity(currentPosition, null); //Clear entity from currentPosition
setEntity(coordinate, entity); //Update entity to new position
} else {
return false;
}
} else {
//xDirection true [+1], yDirection false [+1]
int newX = currentX + 1;
int newY = currentY + 1;
String coordinate = generateCoordinate(newX, newY);
if (getEntity(coordinate) == null) { //Check no entity in new tile
setEntity(currentPosition, null); //Clear entity from currentPosition
setEntity(coordinate, entity); //Update entity to new position
} else {
return false;
}
}
} else { //If xDirection is false then x coordinate decreases [-1]
if (yDirection) { //If yDirection is true then y coordinate decreases [-1]
int newX = currentX - 1;
int newY = currentY - 1;
String coordinate = generateCoordinate(newX, newY);
if (getEntity(coordinate) == null) { //Check no entity in new tile
setEntity(currentPosition, null); //Clear entity from currentPosition
setEntity(coordinate, entity); //Update entity to new position
} else {
return false;
}
} else {
//xDirection false [-1], yDirection false [+1]
int newX = currentX - 1;
int newY = currentY + 1;
String coordinate = generateCoordinate(newX, newY);
if (getEntity(coordinate) == null) { //Check no entity in new tile
setEntity(currentPosition, null); //Clear entity from currentPosition
setEntity(coordinate, entity); //Update entity to new position
} else {
return false;
}
}
}
return false;
}
generateUGS: Creates the initial UGS grid, generating a 120x120 grid and initialising a Tile object in each coordinate.
/**
* Generates a Mapsize x Mapsize sized grid and initialises a Tile object
* in each coordinate.
*/
public void generateUGS() {
for (int x = 0; x < MAPSIZE; x++) {
for (int y = 0; y < MAPSIZE; y++) {
Tile tile = new Tile();
String coordinate = generateCoordinate(x, y);
this.add(coordinate,tile);
}
}
}
The unit testing which was performed on the UGS class can be found here.
This UML diagram was based off of the Observer Design Pattern, and shows the interaction between the Tile and UGS class, as well as the interaction between the UGS class and the serviceLocator class.
In the future further research amongst developers could be carried out to understand whether the UGS should store any more information. This can then be implemented by editing the Tile class to add more class attributes. This would add additional complexity and depth to the system.