Class Chunk

  • All Implemented Interfaces:
    com.badlogic.gdx.ai.msg.Telegraph


    public class Chunk
    extends java.lang.Object
    implements com.badlogic.gdx.ai.msg.Telegraph
    A Chunk is filled with many Blocks and is a part of the map.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.String CHUNKFILESUFFIX
      The suffix of a chunk files.
    • Constructor Summary

      Constructors 
      Constructor Description
      Chunk​(Map map, int coordX, int coordY)
      Creates a Chunk filled with empty cells (likely air).
      Chunk​(Map map, java.io.File path, int coordX, int coordY, Generator generator)
      Creates a chunk by trying to load and if this fails it generates a new one.
    • Field Detail

      • CHUNKFILESUFFIX

        protected static final java.lang.String CHUNKFILESUFFIX
        The suffix of a chunk files.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Chunk

        public Chunk​(Map map,
                     int coordX,
                     int coordY)
        Creates a Chunk filled with empty cells (likely air).
        Parameters:
        map -
        coordX -
        coordY -
      • Chunk

        public Chunk​(Map map,
                     java.io.File path,
                     int coordX,
                     int coordY,
                     Generator generator)
        Creates a chunk by trying to load and if this fails it generates a new one.
        Parameters:
        map -
        coordX - the chunk coordinate
        coordY - the chunk coordinate
        path - filename, can be null to skip file loading
        generator - used for generating if laoding fails
    • Method Detail

      • getBlocksX

        public static int getBlocksX​()
        The amount of blocks in X direction
        Returns:
      • getBlocksY

        public static int getBlocksY​()
        The amount of blocks in Y direction
        Returns:
      • getBlocksZ

        public static int getBlocksZ​()
        The amount of blocks in Z direction
        Returns:
      • getViewWidth

        public static int getViewWidth​()
        Not scaled.
        Returns:
      • getViewDepth

        public static int getViewDepth​()
        Not scaled.
        Returns:
      • getGameWidth

        public static int getGameWidth​()
        x axis
        Returns:
      • getGameDepth

        public static int getGameDepth​()
        y axis
        Returns:
      • getGameHeight

        public static int getGameHeight​()
        The height of the map. z axis
        Returns:
        in game size
      • update

        public void update​(float dt)
        Updates the chunk. should be called once per frame.
        Parameters:
        dt - time since last frame in game time
      • processModification

        public void processModification​()
        checks if the chunk got modified and if that is the case calls the modification methods
      • fill

        public void fill​(Generator generator)
        Fills the chunk's block using a generator.
        Parameters:
        generator -
      • restoreFromRoot

        public boolean restoreFromRoot​(java.io.File path,
                                       int saveSlot,
                                       int coordX,
                                       int coordY)
        copies something
        Parameters:
        path -
        saveSlot -
        coordX -
        coordY -
        Returns:
      • retrieveEntities

        public java.util.ArrayList<AbstractEntity> retrieveEntities​()
        Returns entitiesinSaveFile spawned on this chunk. Can only called once.
        Returns:
        list of entitiesinSaveFile on this chunk, can be null if empty
      • save

        public boolean save​(Map map,
                            java.io.File path,
                            int saveSlot)
                     throws java.io.IOException
        Save this chunk on storage.
        Parameters:
        map - the map of which this chunk is a part of
        path - the map name on storage
        saveSlot -
        Returns:
        Throws:
        java.io.IOException
      • getData

        public byte[][][] getData​()
        Returns the data of the chunk. each block uses three bytes, id, value and health
        Returns:
      • hasCoord

        public boolean hasCoord​(Coordinate coord)
        Check if the chunk has the coordinate inside. Only checks x and y.
        O(1)
        Parameters:
        coord - the coordinate to be checked
        Returns:
        true if coord is inside.
      • hasPoint

        public boolean hasPoint​(Point point)
        Check if the coordinate has the coordinate inside.
        Parameters:
        point - the coordinate to be checked
        Returns:
        true if coord is inside.
      • toString

        public java.lang.String toString​()
        print the chunk to console
        Overrides:
        toString in class java.lang.Object
        Returns:
      • getChunkX

        public int getChunkX​()
        Get the chunk coordinate of this chunk.
        Returns:
      • getChunkY

        public int getChunkY​()
        Get the chunk coordinate of this chunk.
        Returns:
      • getTopLeftCoordinateX

        public int getTopLeftCoordinateX​()
        Returns:
        not copy safe
      • getTopLeftCoordinateY

        public int getTopLeftCoordinateY​()
        Returns:
        not copy safe
      • setBlock

        public void setBlock​(int x,
                             int y,
                             int z,
                             byte id)
        Almost lowest level method to set a block in the map. If the block has logic a new AbstractBlockLogicExtension instance will be created. Health set to 100 and value set to 0
        Parameters:
        x -
        y -
        z -
        id -
      • setBlock

        public void setBlock​(Coordinate coord,
                             byte id)
        Almost lowest level method to set a block in the map. If the block has logic a new AbstractBlockLogicExtension instance will be created. Health set to 100 and value set to 0
        Parameters:
        coord -
        id -
      • setBlock

        public void setBlock​(Coordinate coord,
                             byte id,
                             byte value)
        Almost lowest level method to set a block in the map. If the block has logic a new AbstractBlockLogicExtension instance will be created. Sets health to 100.
        Parameters:
        coord -
        id -
        value -
      • setBlock

        public void setBlock​(Coordinate coord,
                             byte id,
                             byte value,
                             byte health)
        Almost lowest level method to set a block in the map. If the block has logic a new logicinstance will be created.
        Parameters:
        coord - The position where you insert the block. Must be inside the bounds of the chunk.
        id -
        value -
        health -
      • 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 - 0-100.
      • dispose

        public void dispose​(Map map,
                            java.io.File path)
        disposes the chunk
        Parameters:
        map -
        path - if null, does not save the file
      • handleMessage

        public boolean handleMessage​(com.badlogic.gdx.ai.msg.Telegram msg)
        Specified by:
        handleMessage in interface com.badlogic.gdx.ai.msg.Telegraph
      • getBlockId

        public byte getBlockId​(int x,
                               int y,
                               int z)
        Parameters:
        x - coordinate
        y - coordinate
        z - coordinate
        Returns:
        can be null-pointer
      • getBlockValue

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

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

        public int getBlock​(int x,
                            int y,
                            int z)
        Get the block data at this coordinate.
        Parameters:
        x - global coordinates
        y - global coordinates
        z - global coordinates
        Returns:
        first byte id, second value, third is health.
      • getBlockByIndex

        public int getBlockByIndex​(int x,
                                   int y,
                                   int z)
        Get the block data at this index position.
        Parameters:
        x - only valid index
        y - only valid index
        z - only valid index
        Returns:
        first byte id, second value, third is health.