Repository: Baekalfen/PyBoy
File: Source/pyboy/botsupport/tile.py (Python)

Code before:

class Tile:
    def __init__(self, mb, identifier=None, index=None):
        """
        The Game Boy uses tiles as the building block for all graphics on the screen. This base-class is used for
        `pyboy.pyboy.PyBoy.get_tile`, `pyboy.botsupport.sprite.Sprite` and `pyboy.botsupport.tilemap.TileMap`, when
        refering to graphics.

        This class is not meant to be instantiated by developers reading this documentation, but it will be created
        internaly and returned by `pyboy.botsupport.sprite.Sprite.tiles` and
        `pyboy.botsupport.tilemap.TileMap.get_tile`.

        The data of this class is static, apart from the image data, which is loaded from the Game Boy's memory when
        needed. Be aware, that the graphics for the tile can change between each call to `pyboy.pyboy.PyBoy.tick`.
        """

        self.mb = mb

        if index is not None:
            assert isinstance(index, tuple), "Index parameters needs to be a tuple of (int:index, bool:signed)"
            index, signed = index
            assert 0 <= index <= 0xFF, "index out of range"
            assert isinstance(signed, bool), "signed is not instance of bool"
            self._signed_tile_data = signed
            self._index = index
            offset = HIGH_TILEDATA if signed else LOW_TILEDATA
            self._data_address = offset + (16*index) # _OFFSET + self._index * 2
        elif identifier is not None:
            assert 0 <= identifier <= 0xFF+0x80, "Identifier out of range"
            self._signed_tile_data = identifier > 0xFF
            self._index = identifier
            if self._signed_tile_data:
                self._index -= 0xFF
            self._data_address = LOW_TILEDATA + (16*identifier)
        else:
            raise Exception("No identifier or index specified")

    def get_identifier(self):
        """
        The Game Boy has a slightly complicated indexing system for tiles. Use this attribute instead to get a unified
        and easy way to compare and identify tiles. If you want to modify the state of the tilemap or tile data
        directly in the emulator, you'll need to look at `pyboy.botsupport.tile.Tile.get_index`.

        Returns:
            int: Unique identifier for the tile
        """
        return (self._data_address - LOW_TILEDATA) // 16
    identifier = property(get_identifier)

    def get_data_address(self):
        """

Review comment (suggestion):
This could be cleaner. `<128` instead of `<=127` to be consistent, and `LOW_TILEDATA_LENGTH` should be in bytes like the rest of the constants unless its name is explicit, like `LOW_TILEDATA_NTILES` or something (and use literal numbers instead of a constant like the surrounding code).

Code after:

class Tile:
    def __init__(self, mb, identifier=None, index=None):
        """
        The Game Boy uses tiles as the building block for all graphics on the screen. This base-class is used for
        `pyboy.pyboy.PyBoy.get_tile`, `pyboy.botsupport.sprite.Sprite` and `pyboy.botsupport.tilemap.TileMap`, when
        refering to graphics.

        This class is not meant to be instantiated by developers reading this documentation, but it will be created
        internaly and returned by `pyboy.botsupport.sprite.Sprite.tiles` and
        `pyboy.botsupport.tilemap.TileMap.get_tile`.

        The data of this class is static, apart from the image data, which is loaded from the Game Boy's memory when
        needed. Be aware, that the graphics for the tile can change between each call to `pyboy.pyboy.PyBoy.tick`.
        """

        self.mb = mb

        if index is not None:
            assert isinstance(index, tuple), "Index parameters needs to be a tuple of (int:index, bool:signed)"
            index, signed = index
            assert isinstance(signed, bool), "signed is not instance of bool"
            if signed:
                assert -128 <= index <= 127, "index out of range"
            else:
                assert 0 <= index < LOW_TILEDATA_LENGTH, "index out of range"
            self._signed_tile_data = signed
            self._index = index
            # The offset is not HIGH_TILEDATA, as it start 128 tiles after LOW_TILEDATA. We need the middle of
            # the high tile data area -- i.e. 256 tiles of 16 bytes into LOW_TILEDATA.
            offset = LOW_TILEDATA + 16*LOW_TILEDATA_LENGTH if signed else LOW_TILEDATA
            self._data_address = offset + (16*index) # _OFFSET + self._index * 2
        elif identifier is not None:
            assert 0 <= identifier < 384, "Identifier out of range"
            self._signed_tile_data = identifier >= LOW_TILEDATA_LENGTH
            self._index = identifier
            if self._signed_tile_data:
                self._index -= LOW_TILEDATA_LENGTH
            self._data_address = LOW_TILEDATA + (16*identifier)
        else:
            raise Exception("No identifier or index specified")

    def get_identifier(self):
        """
        The Game Boy has a slightly complicated indexing system for tiles. Use this attribute instead to get a unified
        and easy way to compare and identify tiles. If you want to modify the state of the tilemap or tile data
        directly in the emulator, you'll need to look at `pyboy.botsupport.tile.Tile.get_index`.

        Returns:
            int: Unique identifier for the tile
        """