Package itumulator.world
Class World
java.lang.Object
itumulator.world.World
The World class provides an abstraction for our simulated worlds. A world
contains beneficial abstractions for interacting with the world, some of
which are used by the Simulator itself.
World contains a landscape / 'map' (a three-dimensional array) wherein
objects
can be placed. The world class / object provides abstractions for
manipulating this.
Furthermore, World can contain objects not currently existing on the map,
e.g., when objects are not visible. This is primarily used by the simulator
to ensure all
Actor
can act without being on the
'map'.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds an object to the world without placing it on the map.boolean
Determines whether an object exists in worldboolean
containsNonBlocking
(Location location) Determines whether a specified location contains a non-blocking object.void
Completely removes an object from the world, both from the map and the world's entity list.<T> Set<T>
getAll
(Class<T> type, Collection<Location> locations) Returns all objects at a location which is an instance of the type given, or an empty set if none matches the type.Provides the current location of the world.int
Provides the current time of daystatic int
Provides the amount of day time ticksGet tiles which are not filled surrounding the current location (as defined bygetCurrentLocation
andsetLocation
) andisTileEmpty(Location)
).getEmptySurroundingTiles
(Location location) Retrieves a set of locations immediately surrounding the specified location that are 'empty'.Gets all objects within the world (including those who do not currently reside on the map).getLocation
(Object object) Retrieves the current location of a specified object within the world.getNonBlocking
(Location location) Retrieves a non-blocking object from a specified location.int
getSize()
Provides the size of the world (it is square)Get immediate tiles surrounding the current location (as defined bygetCurrentLocation
andsetLocation
).getSurroundingTiles
(int radius) Get tiles (up to the radius provided) surrounding the current location which are not filled (as defined bygetCurrentLocation
andsetCurrentLocation
).getSurroundingTiles
(Location location) Retrieves a set of locations immediately surrounding the specified location, including diagonals.getSurroundingTiles
(Location location, int radius) Retrieves a set of locations surrounding the specified location within a given radius.Retrieves an object from a specified location, prioritizing blocking objects over non-blocking ones.Object[][][]
getTiles()
Retrieves a copy of the three-dimensional array representing the current state of the world map.static int
Provides the total amount of ticks of a dayboolean
isDay()
Provides an indicator of whether it is day.boolean
isNight()
Provides an indicator of whether it is night.boolean
Determines whether an object is placed on a tileboolean
isTileEmpty
(Location location) Checks if a specified location is empty.void
Moves an object from its current location to a new location.void
Removes an object from its current location on the map, but retains its existence in the world.void
setCurrentLocation
(Location current) Sets the location which is deemed as the 'current' location.void
setDay()
Sets the time to day.void
setNight()
Sets the time of the world to beginning of night, according to IS_DAY_TIME.void
Places a given object at a specified location on the map.void
step()
Increases the step of the world to progress time.
-
Constructor Details
-
World
public World(int size) initializes a World with a map of size. A day is defined in steps (i.e., 20) and day time is decided by before the first 10 steps.- Parameters:
size
- of the map within the world (size defines both x and y).
-
-
Method Details
-
step
public void step()Increases the step of the world to progress time. -
isDay
public boolean isDay()Provides an indicator of whether it is day.- Returns:
- true if it is day time.
-
isNight
public boolean isNight()Provides an indicator of whether it is night.- Returns:
- true if it is night time.
-
getSize
public int getSize()Provides the size of the world (it is square)- Returns:
- the x and y of the world.
-
setDay
public void setDay()Sets the time to day. -
setNight
public void setNight()Sets the time of the world to beginning of night, according to IS_DAY_TIME. -
getCurrentTime
public int getCurrentTime()Provides the current time of day- Returns:
- the current time as an integer
-
getTotalDayDuration
public static int getTotalDayDuration()Provides the total amount of ticks of a day- Returns:
- the total amount of ticks of a day
-
getDayDuration
public static int getDayDuration()Provides the amount of day time ticks- Returns:
- the amount of ticks the world is in day time
-
setCurrentLocation
Sets the location which is deemed as the 'current' location. This is the location used when calling parameterless methods normally requiring a parameter (e.g.,getSurroundingTiles
). Please note that methods likemove
andsetTile
do not update this location. You have to do that manually usingsetCurrentLocation(itumulator.world.Location)
if you want to keep using the parameterless methods.- Parameters:
current
- the location which will be set as currently under examination (can be null). Eg. when the actor is not on the map but exists in the world.- Throws:
IllegalArgumentException
- thrown if the location is out of bounds.
-
getCurrentLocation
Provides the current location of the world. This is the location which the world uses for the overloaded methods (e.g.,getSurroundingTiles()
). This does not provide any object.- Returns:
- the current location.
-
getLocation
Retrieves the current location of a specified object within the world. If the object is in the world but not currently on the map (i.e., its location is null), this method throws an exception.- Parameters:
object
- The object whose location is to be retrieved.- Returns:
- The current location of the object on the map.
- Throws:
IllegalArgumentException
- if the object does not exist in the world, or if it exists but is not currently placed on the map.
-
getSurroundingTiles
Get immediate tiles surrounding the current location (as defined bygetCurrentLocation
andsetLocation
). The method only returns existing locations within the map.- Returns:
- set of immediate surrounding locations.
- Throws:
IllegalStateException
- thrown if current location not set.
-
getSurroundingTiles
Get tiles (up to the radius provided) surrounding the current location which are not filled (as defined bygetCurrentLocation
andsetCurrentLocation
). The method only returns existing locations within the map.- Parameters:
radius
- the number of locations to include in each direction.- Returns:
- set of surrounding locations.
- Throws:
IllegalStateException
- thrown if current location not set.
-
getEmptySurroundingTiles
Get tiles which are not filled surrounding the current location (as defined bygetCurrentLocation
andsetLocation
) andisTileEmpty(Location)
). The method only returns existing locations within the map.- Returns:
- set of surrounding tiles which are empty.
- Throws:
IllegalStateException
- thrown if current location not set.
-
remove
Removes an object from its current location on the map, but retains its existence in the world. This method is particularly useful for temporarily 'hiding' an object from the map without deleting it (if an actor, its act method will continuously be called). Note: this does not delete the object from the world's entity list. Furthermore, remove does not update the current location. Thus, calling overloaded methods (e.g.,getEmptySurroundingTiles()
) will still provide the tiles surrounding the location you removed from.- Parameters:
object
- The object to be removed from the map.- Throws:
IllegalArgumentException
- if the object does not exist in the world, or is not currently placed on the map.
-
delete
Completely removes an object from the world, both from the map and the world's entity list. This method is used when an object is no longer needed in the simulation and should be permanently deleted. If the object is currently on the map, it is first removed usingremove(Object)
method. Then, the object is removed from the world's entity list, ensuring that it no longer exists within the world context.- Parameters:
object
- The object to delete from the world.- Throws:
IllegalArgumentException
- if the object does not exist in the world.
-
setTile
Places a given object at a specified location on the map. If the location already contains an object of the same type (blocking/non-blocking), an exception is thrown. Additionally, if the object already exists elsewhere in the world, it cannot be placed again. Note that setTile does not update the current location. Thus, one cannot after using setTile use the overloaded methods (e.g.,getEmptySurroundingTiles()
) as the current location is not set.- Parameters:
location
- The location where the object will be placed.object
- The object to place at the specified location.- Throws:
IllegalArgumentException
- if the tile is occupied by the same type of object, or if the object already exists on the map, or if the location is out of bounds.
-
move
Moves an object from its current location to a new location.- Parameters:
object
- The object to be moved.to
- The destination location for the object.- Throws:
IllegalArgumentException
- if the object is not in the world, is not on the map, or if the destination tile is occupied by a blocking object.
-
getTile
Retrieves an object from a specified location, prioritizing blocking objects over non-blocking ones. If a blocking object exists at the location, it is returned; otherwise, the non-blocking object is returned. If multiple objects are present (one blocking and one non-blocking), the blocking one is always returned. If no objects are present at the location, null is returned.- Parameters:
location
- The location from which to retrieve the object.- Returns:
- The object at the specified location, prioritizing blocking objects. Returns null if no object is present.
-
add
Adds an object to the world without placing it on the map. The object's location is set to null, indicating it is not currently on the map. This method is useful for adding objects that exist in the world but are not visible or interactable on the map.- Parameters:
object
- The object to add to the world.- Throws:
IllegalArgumentException
- if the object already exists in the world.
-
getNonBlocking
Retrieves a non-blocking object from a specified location. If a non-blocking object is present at the location, it is returned. If no non-blocking object is present, an exception is thrown.- Parameters:
location
- The location to check for a non-blocking object.- Returns:
- The non-blocking object at the specified location.
-
isTileEmpty
Checks if a specified location is empty. A tile is considered empty if it does not contain any blocking objects. Non-blocking objects do not affect the emptiness status of a tile.- Parameters:
location
- The location to check.- Returns:
- true if the tile is empty (no blocking objects present), false otherwise.
-
containsNonBlocking
Determines whether a specified location contains a non-blocking object. This method checks if there is an object at the location that implements the NonBlocking interface.- Parameters:
location
- The location to check.- Returns:
- true if a non-blocking object is present, false otherwise.
-
getSurroundingTiles
Retrieves a set of locations immediately surrounding the specified location, including diagonals. The method calculates the surrounding tiles within a radius of 1 tile. Tiles outside the bounds of the world are excluded.- Parameters:
location
- The central location for which surrounding tiles are to be found.- Returns:
- A set of locations immediately surrounding the specified location.
- Throws:
IllegalArgumentException
- if the specified location is out of bounds.
-
getSurroundingTiles
Retrieves a set of locations surrounding the specified location within a given radius. This method includes all tiles within the radius. Tiles outside the world's bounds are excluded.- Parameters:
location
- The location for which surrounding tiles are to be found.radius
- The radius within which to find surrounding tiles. Must be non-negative.- Returns:
- A set of locations surrounding the specified location within the radius.
- Throws:
IllegalArgumentException
- if the specified location is out of bounds or if the radius is negative.
-
getEmptySurroundingTiles
Retrieves a set of locations immediately surrounding the specified location that are 'empty'. A location is considered 'empty' based on the criteria defined inisTileEmpty(Location)
method, typically meaning it does not contain a blocking object. This method is useful for determining potential movement or placement options around a given location, particularly in scenarios where blocking objects restrict certain actions. It includes all adjacent tiles, both orthogonal and diagonal, within a radius of 1 tile from the given location. Locations outside the bounds of the world are not included in the returned set.- Parameters:
location
- The central location from which to find empty surrounding tiles.- Returns:
- A set of locations around the specified location that are empty, as per the definition in
isTileEmpty(Location)
. - Throws:
IllegalArgumentException
- if the specified location is out of bounds.
-
getTiles
Retrieves a copy of the three-dimensional array representing the current state of the world map. Each element in this array corresponds to a specific location in the world, with the first two dimensions representing the x and y coordinates, and the third dimension representing layers (0 for non-blocking, 1 for blocking objects). Note that the returned array is a copy, ensuring that modifications to it do not affect the actual world state.- Returns:
- A copy of the three-dimensional array representing the world's map.
-
getEntities
Gets all objects within the world (including those who do not currently reside on the map).- Returns:
- Map where keys are the objects and their value is the location they are currently placed on (can be null if not currently on the map). The map returned is a copy of the internal map to avoid concurrent modification exceptions.
-
contains
Determines whether an object exists in world- Parameters:
o
- object to check- Returns:
- true if the object exists in the world
-
isOnTile
Determines whether an object is placed on a tile- Parameters:
o
- object to check- Returns:
- true if the object exists on a tile
-
getAll
Returns all objects at a location which is an instance of the type given, or an empty set if none matches the type.- Parameters:
type
- the class we are looking for.locations
- the locations from where we want to collect all objects of a certain instance- Returns:
- a set of the type we are looking for (decided by the parameter 'type') containing all (if any) objects that are instances of this.
-