Class Map

  • All Implemented Interfaces:
    com.badlogic.gdx.ai.pfa.Graph<PfNode>, com.badlogic.gdx.ai.pfa.indexed.IndexedGraph<PfNode>


    public class Map
    extends java.lang.Object
    implements com.badlogic.gdx.ai.pfa.indexed.IndexedGraph<PfNode>
    A map stores many chunks in a hash map. It also contains the entities.
    • Field Detail

      • MAPVERSION

        public static final java.lang.Integer MAPVERSION
    • Constructor Detail

      • Map

        public Map​(java.io.File name,
                   int saveslot)
            throws java.io.IOException
        Loads a map using the default generator.
        Parameters:
        name - if available on disk it will be load
        saveslot -
        Throws:
        java.io.IOException - thrown if there is no full read/write access to the map file
      • Map

        public Map​(java.io.File name,
                   int saveSlot,
                   Generator generator)
            throws java.io.IOException
        Loads a map. Loads map and save cVars.
        Parameters:
        name - if available on disk it will load the meta file
        generator - the generator used for generating new chunks
        saveSlot -
        Throws:
        java.io.IOException - thrown if there is no full read/write access to the map file
    • Method Detail

      • setDefaultGenerator

        public static void setDefaultGenerator​(Generator generator)
        Parameters:
        generator -
      • newSaveSlot

        public static int newSaveSlot​(java.io.File path)
        Parameters:
        path - the directory of the map
        Returns:
      • createSaveSlot

        public static void createSaveSlot​(java.io.File path,
                                          int slot)
        Parameters:
        path - the directory of the map
        slot -
      • getSavesCount

        public static int getSavesCount​(java.io.File path)
        Get the amount of save files for this map.
        Parameters:
        path -
        Returns:
      • getBlockConfig

        public static BlockConfig getBlockConfig​()
        Get the currently set block config.
        Returns:
      • setBlockConfig

        public static void setBlockConfig​(BlockConfig blockConfig)
        Set the currently set block config.
        Parameters:
        blockConfig -
      • update

        public void update​(float dt)
        Updates amostly the entities.
        Parameters:
        dt - time in ms
      • postUpdate

        public void postUpdate​(float dt)
        Called after the view update to catch changes caused by the view
        Parameters:
        dt -
      • loadChunk

        public void loadChunk​(int chunkX,
                              int chunkY)
        loads a chunk from disk if not already loaded.
        Parameters:
        chunkX -
        chunkY -
      • loadChunk

        public void loadChunk​(Coordinate coord)
        loads a chunk from disk if not already loaded.
        Parameters:
        coord -
      • getData

        public java.util.HashMap<java.lang.Integer,Chunk> getData​()
        Get the data of the map. From range in X [-chunkDim/2,chunkDim/2]
        Returns:
      • getLoadedChunks

        public java.util.LinkedList<Chunk> getLoadedChunks​()
        Returns:
      • getBlockId

        public byte getBlockId​(int x,
                               int y,
                               int z)
        Returns a block without checking the parameters first. Good for debugging and also faster. O(n)
        Parameters:
        x - coordinate
        y - coordinate
        z - coordinate
        Returns:
        the single block you wanted
      • getBlockId

        public byte getBlockId​(Coordinate coord)
        If the block can not be found returns null pointer.
        Parameters:
        coord -
        Returns:
      • getBlock

        public int getBlock​(Coordinate coord)
        id, value and health
        Parameters:
        coord -
        Returns:
        first byte id, second value, third is health.
      • getBlock

        public int getBlock​(int x,
                            int y,
                            int z)
        Parameters:
        x -
        y -
        z -
        Returns:
      • getHealth

        public byte getHealth​(Coordinate coord)
        Parameters:
        coord -
        Returns:
      • setBlock

        public void setBlock​(Coordinate coord,
                             int block)
        Set id, value and health at a coordinate in the map. This creates a AbstractBlockLogicExtension instance if the block has logic.
        Parameters:
        coord -
        block - id (bit 0-7), value (bit 8-15) and health (bit 16-23)
      • setBlock

        public void setBlock​(Coordinate coord,
                             byte id,
                             byte value)
        Set id and value at a coordinate in the map. This creates a AbstractBlockLogicExtension instance if the block has logic.
        Parameters:
        coord -
        id -
        value -
      • setValue

        public void setValue​(Coordinate coord,
                             byte value)
        Parameters:
        coord -
        value -
      • setHealth

        public void setHealth​(Coordinate coord,
                              byte health)
        Set health of a cell.
        Parameters:
        coord -
        health -
      • getChunkContaining

        public Chunk getChunkContaining​(Coordinate coord)
        get the chunk where the coordinates are on. Usese hashmap so O(1).
        Parameters:
        coord - not altered
        Returns:
        can return null if not loaded
      • getChunkContaining

        public Chunk getChunkContaining​(int x,
                                        int y)
        get the chunk where the coordinates are on
        Parameters:
        x - grid coordinate
        y - grid coordinate
        Returns:
        can return null if not loaded
      • getChunkContaining

        public Chunk getChunkContaining​(Point point)
        Parameters:
        point -
        Returns:
      • getChunk

        public Chunk getChunk​(int chunkX,
                              int chunkY)
        get the chunk with the given chunk coords.

        Runtime: O(1)
        Parameters:
        chunkX - chunk coordinate
        chunkY - chunk coordinate
        Returns:
        if not in memory returns null
      • getEntitiesOnChunk

        public java.util.ArrayList<AbstractEntity> getEntitiesOnChunk​(int xChunk,
                                                                      int yChunk)
        Get every entity on a chunk.
        Parameters:
        xChunk -
        yChunk -
        Returns:
      • getEntitiesOnChunkSavedOnly

        public java.util.ArrayList<AbstractEntity> getEntitiesOnChunkSavedOnly​(int xChunk,
                                                                               int yChunk)
        Get every entity on a chunk which should be saved
        Parameters:
        xChunk -
        yChunk -
        Returns:
      • save

        public boolean save​(int saveSlot)
        saves every chunk on the map
        Parameters:
        saveSlot -
        Returns:
      • save

        public boolean save​()
        save every chunk using the current active save slot. Saves position of the sun and moon at origin.
        Returns:
      • useSaveSlot

        public void useSaveSlot​(int slot)
        uses a specific save slot for loading and saving the map. Loads the save cVars.
        Parameters:
        slot - slot number
      • newSaveSlot

        public int newSaveSlot​()
        Uses a new save slot as the save slot
        Returns:
        the new save slot number
      • hasSaveSlot

        public boolean hasSaveSlot​(int saveSlot)
        Check if the save slot exists.
        Parameters:
        saveSlot -
        Returns:
      • createSaveSlot

        public void createSaveSlot​(int slot)
        Parameters:
        slot -
      • getSavesCount

        public int getSavesCount​()
        checks a map for the amount of save files
        Returns:
        the amount of saves for this map
      • modificationCheck

        public void modificationCheck​()
        should be executed after the update method. Dispatches a message if a change occured.
      • getGenerator

        public Generator getGenerator​()
        Returns:
      • getCurrentSaveSlot

        public int getCurrentSaveSlot​()
        Returns:
      • setGenerator

        public void setGenerator​(Generator generator)
        Set the generator used for generating maps
        Parameters:
        generator -
      • getPath

        public java.io.File getPath​()
        The name of the map on the file.
        Returns:
      • setModified

        public void setModified​()
        set the modified flag to true. usually not manually called.
      • getCenter

        public Point getCenter​()
        Returns a coordinate pointing to the absolute center of the map. Height is half the map's height.
        Returns:
      • getCenter

        public Point getCenter​(float height)
        Returns a coordinate pointing to middle of a 3x3 chunk map.
        Parameters:
        height - You custom height.
        Returns:
      • getEntities

        public java.util.ArrayList<AbstractEntity> getEntities​()
        Returns a copy of the entityList.
        Returns:
        every item on the map
      • addEntities

        public void addEntities​(AbstractEntity... ent)
        Adds entities.
        Parameters:
        ent - entities should be already spawned
      • addEntities

        public void addEntities​(java.util.Collection<AbstractEntity> ent)
        Adds entities.
        Parameters:
        ent - entities should be already spawned
      • disposeEntities

        public void disposeEntities​()
        Disposes every entity on the map and clears the list.
      • getEntitys

        public <T> java.util.LinkedList<T> getEntitys​(java.lang.Class<T> filter)
        Find every instance of a special class. E.g. find every AbstractCharacter. They must be spawned to appear in the results.
        Type Parameters:
        T - the class you want to filter.
        Parameters:
        filter - the class you want to filter.
        Returns:
        a list with the entitys
      • getEntitysOnCoord

        public java.util.LinkedList<AbstractEntity> getEntitysOnCoord​(Coordinate coord)
        Get every entity on a coord.
        Parameters:
        coord -
        Returns:
        a list with the entitys
      • getEntitysOnCoord

        public <T> java.util.LinkedList<T> getEntitysOnCoord​(Coordinate coord,
                                                             java.lang.Class<T> filter)
        Get every entity on a coord of the wanted type
        Type Parameters:
        T - the class you want to filter.
        Parameters:
        coord - the coord where you want to get every entity from
        filter - the class you want to filter.
        Returns:
        a list with the entitys of the wanted type
      • isModified

        public boolean isModified​()
        True if some block has changed in loaded chunks.
        Returns:
        returns the modified flag
      • getConnections

        public com.badlogic.gdx.utils.Array<com.badlogic.gdx.ai.pfa.Connection<PfNode>> getConnections​(PfNode fromNode)
        Specified by:
        getConnections in interface com.badlogic.gdx.ai.pfa.Graph<PfNode>
      • findPath

        public com.badlogic.gdx.ai.pfa.DefaultGraphPath<PfNode> findPath​(Coordinate start,
                                                                         Coordinate goal)
        Parameters:
        start -
        goal -
        Returns:
      • getNodeCount

        public int getNodeCount​()
        Specified by:
        getNodeCount in interface com.badlogic.gdx.ai.pfa.indexed.IndexedGraph<PfNode>
      • isLoading

        public boolean isLoading​(int chunkX,
                                 int chunkY)
        check wether a chunk is currently being loaded.
        Parameters:
        chunkX -
        chunkY -
        Returns:
      • dispose

        public void dispose​(boolean save)
        disposes every chunk
        Parameters:
        save -