Package tech.underoaks.coldcase.state
Class Map
java.lang.Object
tech.underoaks.coldcase.state.Map
Represents the game map, which is a 2D array of
Tile objects.
Provides methods for accessing and modifying the map, rendering, and updating the map state.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMap()Default constructor for Map needed for deserialization inMapGeneratorDefault constructor for Map -
Method Summary
Modifier and TypeMethodDescriptionCreates a deep clone of thisMapinstance.voiddispose()Disposes of resources held by all tiles in the map.intgetChildIndex(com.badlogic.gdx.math.Vector2 tile, TileContent tileContent) Returns the child index of the specifiedTileContentwithin the tile at the given position.getTile(int x, int y) Retrieves the tile at the specified coordinates.getTile(com.badlogic.gdx.math.Vector2 position) Retrieves the tile at the given position.intReturns the height of the tile array.intReturns the width of the tile array.getTileContentByIndex(com.badlogic.gdx.math.Vector2 position, int index) Retrieves a childTileContentfrom the tile at the specified position based on the given index.com.badlogic.gdx.math.Vector2getTileContentByType(Class<? extends TileContent> type) Searches the map for a tile containing aTileContentof the specified type.com.badlogic.gdx.math.Vector2isoTo2D(com.badlogic.gdx.math.Vector2 pt) Converts a point in isometric coordinates to normal 2D coordinatesbooleanisOutOfBounds(com.badlogic.gdx.math.Vector2 position) Checks whether the specified position is out of bounds of the tile array.static booleanisPlayerNextToTile(com.badlogic.gdx.math.Vector2 tilePosition) Checks if the player is next to the specified tile position.static booleanisPlayerOnTile(com.badlogic.gdx.math.Vector2 tilePosition) Checks if the player is on the specified tile position.booleanReturns whether the map is a snapshot map.voidrender(com.badlogic.gdx.graphics.g2d.Batch batch, float originX, float originY) Calls the render method of each tile of the map in the correct order and with the correct position in isometric coordinates.voidsetIsSnapshotMap(boolean snapshotMap) Sets the snapshot map status.voidSets the tile at the specified coordinates to the givenTile.com.badlogic.gdx.math.Vector2twoDToIso(com.badlogic.gdx.math.Vector2 pt) Converts a point in normal 2D coordinates to isometric coordinatesstatic com.badlogic.gdx.math.Vector2twoDToIso45(int ex, int why) Converts 2D coordinates to isometric coordinates using a 45-degree rotation transformation.updateMap(InteractionChain chain, Interaction interaction, TileContent handler) Updates the map by attempting to perform an update on eachTileintileArray.voidupdateUntilStable(InteractionChain chain, Interaction interaction, TileContent handler) Continuously updates the map until no further updates are possible.
-
Field Details
-
tileArray
Tiles that store the State of this Map
-
-
Constructor Details
-
Map
public Map()Default constructor for Map needed for deserialization inMapGenerator -
Map
Default constructor for Map- Parameters:
tileArray- Initial Tile-Arrangement
-
-
Method Details
-
getTile
Retrieves the tile at the specified coordinates.- Parameters:
x- the x-coordinate (column index) of the tiley- the y-coordinate (row index) of the tile- Returns:
- the
Tilelocated attileArray[y][x]
-
getTile
Retrieves the tile at the given position.Note that the y-coordinate of the provided position is used as the x-index and the x-coordinate as the y-index when accessing the tile array.
- Parameters:
position- the position of the tile as aVector2- Returns:
- the
Tileat the specified position
-
getTileContentByType
Searches the map for a tile containing aTileContentof the specified type.- Parameters:
type- the class type of theTileContentto locate- Returns:
- a
Vector2representing the position of the tile with the specifiedTileContent, ornullif no such tile is found - Throws:
IllegalArgumentException- if the provided type isnull
-
setTile
Sets the tile at the specified coordinates to the givenTile.- Parameters:
x- the x-coordinate (column index) of the tile positiony- the y-coordinate (row index) of the tile positiontile- theTileto set at the specified position
-
getTileArrayWidth
public int getTileArrayWidth()Returns the width of the tile array.- Returns:
- the number of columns in the tile array
-
getTileArrayHeight
public int getTileArrayHeight()Returns the height of the tile array.- Returns:
- the number of rows in the tile array
-
getChildIndex
Returns the child index of the specifiedTileContentwithin the tile at the given position.- Parameters:
tile- the position of the tile as aVector2(with x as the first index and y as the second index in the tile array)tileContent- theTileContentwhose child index is to be retrieved- Returns:
- the child index corresponding to the provided
TileContent
-
getTileContentByIndex
Retrieves a childTileContentfrom the tile at the specified position based on the given index.- Parameters:
position- the position of the tile as aVector2index- the index of the childTileContentto retrieve- Returns:
- the child
TileContentat the specified index - Throws:
IllegalArgumentException- if the index is -1 or if the tile does not contain anyTileContent
-
isOutOfBounds
public boolean isOutOfBounds(com.badlogic.gdx.math.Vector2 position) Checks whether the specified position is out of bounds of the tile array.- Parameters:
position- the position to check- Returns:
trueif the position is outside the bounds of the tile array,falseotherwise
-
render
public void render(com.badlogic.gdx.graphics.g2d.Batch batch, float originX, float originY) Calls the render method of each tile of the map in the correct order and with the correct position in isometric coordinates.
This calls twoDToIso() to convert the 2D coordinates to isometric coordinates- Parameters:
batch- SpriteBatch to render the maporiginX- X-Coordinate on Screen-SpaceoriginY- Y-Coordinate on Screen-Space
-
isoTo2D
public com.badlogic.gdx.math.Vector2 isoTo2D(com.badlogic.gdx.math.Vector2 pt) Converts a point in isometric coordinates to normal 2D coordinates- Parameters:
pt- Vector2 Point in isometric Coordinates to convert- Returns:
- Converted point as a Vector2
-
twoDToIso
public com.badlogic.gdx.math.Vector2 twoDToIso(com.badlogic.gdx.math.Vector2 pt) Converts a point in normal 2D coordinates to isometric coordinates- Parameters:
pt- Vector2 Point in normal 2D coordinates to convert- Returns:
- Converted point as a Vector2
-
twoDToIso45
public static com.badlogic.gdx.math.Vector2 twoDToIso45(int ex, int why) Converts 2D coordinates to isometric coordinates using a 45-degree rotation transformation.- Parameters:
ex- the x-coordinate in 2D spacewhy- the y-coordinate in 2D space- Returns:
- a
Vector2representing the corresponding isometric coordinates
-
updateUntilStable
public void updateUntilStable(InteractionChain chain, Interaction interaction, TileContent handler) throws GameStateUpdateException, UpdateTileContentException Continuously updates the map until no further updates are possible.Keeps trying to update the map with the given
InteractionChainuntil no more changes occur.- Parameters:
chain- theInteractionChainused to manage interactions and snapshots during updatesinteraction- Interaction that has caused the updatehandler- Handler that has accepted the interaction- Throws:
IllegalStateException- if the maximum iteration limit is exceeded, suggesting a potential cyclic dependency inTileContent.GameStateUpdateException- If a GameStateUpdate has failedUpdateTileContentException- If a TileContent couldn't be updated (due to a failing validation)- "Implementation Note:"
- This method has a limit on the number of iterations to prevent endless loops. If one
TileContenttriggers another in a cyclic manner, the loop may otherwise never terminate.
-
updateMap
public List<TileContent> updateMap(InteractionChain chain, Interaction interaction, TileContent handler) throws GameStateUpdateException, UpdateTileContentException Updates the map by attempting to perform an update on eachTileintileArray.For each Tile with non-null
TileContent, thehandleUpdatemethod is invoked with the givenInteractionChain.- Parameters:
chain- theInteractionChainmanaging interactions and snapshots for updatesinteraction- Interaction that has caused the updatehandler- Handler that has accepted the interaction- Returns:
- List of
TileContentsthat handled the update - Throws:
GameStateUpdateException- If a GameStateUpdate has failedUpdateTileContentException- If a TileContent couldn't be updated (due to a failing validation)- See Also:
-
deepClone
Creates a deep clone of thisMapinstance.- Returns:
- A new
Mapinstance with a deep-clonedTilearray.
-
dispose
public void dispose()Disposes of resources held by all tiles in the map.Iterates over the tile array and calls
dispose()on each tile. -
isSnapshotMap
public boolean isSnapshotMap()Returns whether the map is a snapshot map.- Returns:
- true if the map is a snapshot map, false otherwise.
-
setIsSnapshotMap
public void setIsSnapshotMap(boolean snapshotMap) Sets the snapshot map status.- Parameters:
snapshotMap- true if the map is a snapshot map, false otherwise.
-
isPlayerOnTile
public static boolean isPlayerOnTile(com.badlogic.gdx.math.Vector2 tilePosition) Checks if the player is on the specified tile position.- Parameters:
tilePosition- The position of the tile to check.- Returns:
- true if the player's position is the same as the tile's position, false otherwise.
-
isPlayerNextToTile
public static boolean isPlayerNextToTile(com.badlogic.gdx.math.Vector2 tilePosition) Checks if the player is next to the specified tile position. The player is considered next to the tile if they are adjacent in any of the four cardinal directions or diagonally adjacent.- Parameters:
tilePosition- The position of the tile to check.- Returns:
- true if the player is adjacent to the tile (either to the left, right, above, below, or diagonally), false otherwise.
-