Class Map

java.lang.Object
tech.underoaks.coldcase.state.Map

public class Map extends Object
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
    Modifier and Type
    Field
    Description
    Tile[][]
    Tiles that store the State of this Map
  • Constructor Summary

    Constructors
    Constructor
    Description
    Map()
    Default constructor for Map needed for deserialization in MapGenerator
    Map(Tile[][] tileArray)
    Default constructor for Map
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a deep clone of this Map instance.
    void
    Disposes of resources held by all tiles in the map.
    int
    getChildIndex(com.badlogic.gdx.math.Vector2 tile, TileContent tileContent)
    Returns the child index of the specified TileContent within 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.
    int
    Returns the height of the tile array.
    int
    Returns the width of the tile array.
    getTileContentByIndex(com.badlogic.gdx.math.Vector2 position, int index)
    Retrieves a child TileContent from the tile at the specified position based on the given index.
    com.badlogic.gdx.math.Vector2
    Searches the map for a tile containing a TileContent of the specified type.
    com.badlogic.gdx.math.Vector2
    isoTo2D(com.badlogic.gdx.math.Vector2 pt)
    Converts a point in isometric coordinates to normal 2D coordinates
    boolean
    isOutOfBounds(com.badlogic.gdx.math.Vector2 position)
    Checks whether the specified position is out of bounds of the tile array.
    static boolean
    isPlayerNextToTile(com.badlogic.gdx.math.Vector2 tilePosition)
    Checks if the player is next to the specified tile position.
    static boolean
    isPlayerOnTile(com.badlogic.gdx.math.Vector2 tilePosition)
    Checks if the player is on the specified tile position.
    boolean
    Returns whether the map is a snapshot map.
    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.
    void
    setIsSnapshotMap(boolean snapshotMap)
    Sets the snapshot map status.
    void
    setTile(int x, int y, Tile tile)
    Sets the tile at the specified coordinates to the given Tile.
    com.badlogic.gdx.math.Vector2
    twoDToIso(com.badlogic.gdx.math.Vector2 pt)
    Converts a point in normal 2D coordinates to isometric coordinates
    static com.badlogic.gdx.math.Vector2
    twoDToIso45(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 each Tile in tileArray.
    void
    Continuously updates the map until no further updates are possible.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • tileArray

      public Tile[][] tileArray
      Tiles that store the State of this Map
  • Constructor Details

    • Map

      public Map()
      Default constructor for Map needed for deserialization in MapGenerator
    • Map

      public Map(Tile[][] tileArray)
      Default constructor for Map
      Parameters:
      tileArray - Initial Tile-Arrangement
  • Method Details

    • getTile

      public Tile getTile(int x, int y)
      Retrieves the tile at the specified coordinates.
      Parameters:
      x - the x-coordinate (column index) of the tile
      y - the y-coordinate (row index) of the tile
      Returns:
      the Tile located at tileArray[y][x]
    • getTile

      public Tile getTile(com.badlogic.gdx.math.Vector2 position)
      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 a Vector2
      Returns:
      the Tile at the specified position
    • getTileContentByType

      public com.badlogic.gdx.math.Vector2 getTileContentByType(Class<? extends TileContent> type)
      Searches the map for a tile containing a TileContent of the specified type.
      Parameters:
      type - the class type of the TileContent to locate
      Returns:
      a Vector2 representing the position of the tile with the specified TileContent, or null if no such tile is found
      Throws:
      IllegalArgumentException - if the provided type is null
    • setTile

      public void setTile(int x, int y, Tile tile)
      Sets the tile at the specified coordinates to the given Tile.
      Parameters:
      x - the x-coordinate (column index) of the tile position
      y - the y-coordinate (row index) of the tile position
      tile - the Tile to 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

      public int getChildIndex(com.badlogic.gdx.math.Vector2 tile, TileContent tileContent)
      Returns the child index of the specified TileContent within the tile at the given position.
      Parameters:
      tile - the position of the tile as a Vector2 (with x as the first index and y as the second index in the tile array)
      tileContent - the TileContent whose child index is to be retrieved
      Returns:
      the child index corresponding to the provided TileContent
    • getTileContentByIndex

      public TileContent getTileContentByIndex(com.badlogic.gdx.math.Vector2 position, int index)
      Retrieves a child TileContent from the tile at the specified position based on the given index.
      Parameters:
      position - the position of the tile as a Vector2
      index - the index of the child TileContent to retrieve
      Returns:
      the child TileContent at the specified index
      Throws:
      IllegalArgumentException - if the index is -1 or if the tile does not contain any TileContent
    • 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:
      true if the position is outside the bounds of the tile array, false otherwise
    • 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 map
      originX - X-Coordinate on Screen-Space
      originY - 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 space
      why - the y-coordinate in 2D space
      Returns:
      a Vector2 representing 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 InteractionChain until no more changes occur.

      Parameters:
      chain - the InteractionChain used to manage interactions and snapshots during updates
      interaction - Interaction that has caused the update
      handler - Handler that has accepted the interaction
      Throws:
      IllegalStateException - if the maximum iteration limit is exceeded, suggesting a potential cyclic dependency in TileContent.
      GameStateUpdateException - If a GameStateUpdate has failed
      UpdateTileContentException - 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 TileContent triggers another in a cyclic manner, the loop may otherwise never terminate.
    • updateMap

      Updates the map by attempting to perform an update on each Tile in tileArray.

      For each Tile with non-null TileContent, the handleUpdate method is invoked with the given InteractionChain.

      Parameters:
      chain - the InteractionChain managing interactions and snapshots for updates
      interaction - Interaction that has caused the update
      handler - Handler that has accepted the interaction
      Returns:
      List of TileContents that handled the update
      Throws:
      GameStateUpdateException - If a GameStateUpdate has failed
      UpdateTileContentException - If a TileContent couldn't be updated (due to a failing validation)
      See Also:
    • deepClone

      public Map deepClone()
      Creates a deep clone of this Map instance.
      Returns:
      A new Map instance with a deep-cloned Tile array.
    • 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.