UNPKG

nes-emu

Version:

A NES emulator

68 lines (59 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _constants = _interopRequireDefault(require("../../../constants")); var _helpers = require("../../../helpers"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** A sprite containing an id, position, height, a tile id and some attributes. */ class Sprite { constructor(id, x, y, patternTableId, tileId, attributes, is8x16) { this.id = id; this.x = x; this.y = y; this.patternTableId = patternTableId; this.tileId = tileId; this.attributes = attributes; this.is8x16 = is8x16; } /** * Returns the tile id for an `insideY` position. * The bottom part of a 8x16 sprite uses the next tile index. */ tileIdFor(insideY) { let index = +(insideY >= _constants.default.TILE_LENGTH); if (this.is8x16 && this.flipY) index = +!index; return this.tileId + index; } /** Returns whether it should appear in a certain `scanline` or not. */ shouldRenderInScanline(scanline) { const diffY = this.diffY(scanline); return diffY >= 0 && diffY < this.height; } /** Returns the difference between a `scanline` and sprite's Y coordinate. */ diffY(scanline) { return scanline - this.y; } /** Returns the palette id of the sprite. */ get paletteId() { return _constants.default.PALETTE_FOREGROUND_START + _helpers.Byte.getBits(this.attributes, _constants.default.SPRITE_ATTR_PALETTE_BITS_START, _constants.default.SPRITE_ATTR_PALETTE_BITS_SIZE); } /** Returns whether the sprite is in front of background or not. */ get isInFrontOfBackground() { return !_helpers.Byte.getBit(this.attributes, _constants.default.SPRITE_ATTR_PRIORITY_BIT); } /** Returns whether the sprite is horizontally flipped or not. */ get flipX() { return !!_helpers.Byte.getBit(this.attributes, _constants.default.SPRITE_ATTR_HORIZONTAL_FLIP_BIT); } /** Returns whether the sprite is vertically flipped or not. */ get flipY() { return !!_helpers.Byte.getBit(this.attributes, _constants.default.SPRITE_ATTR_VERTICAL_FLIP_BIT); } /** Returns the sprite height. */ get height() { return this.is8x16 ? 16 : 8; } } exports.default = Sprite;