broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
113 lines (103 loc) • 4.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _byte = _interopRequireDefault(require("../byte"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var TILE_SIZE_PIXELS = 8;
var PALETTE_FOREGROUND_START = 4;
var SPRITE_ATTR_PALETTE_BITS_START = 0;
var SPRITE_ATTR_PALETTE_BITS_SIZE = 2;
var SPRITE_ATTR_PRIORITY_BIT = 5;
var SPRITE_ATTR_HORIZONTAL_FLIP_BIT = 6;
var SPRITE_ATTR_VERTICAL_FLIP_BIT = 7;
/**
* A sprite containing an id, position, height, a tile id and some attributes.
* Sprites are defined by (y, tileId, attributes, x).
* 76543210
* ||| ++- foregroundPaletteId
* ||+------ priority (0: in front of background, 1: behind background)
* |+------- horizontalFlip
* +-------- verticalFlip
*/
var Sprite = /*#__PURE__*/function () {
function Sprite(ppu, id, x, y, is8x16, patternTableId, topTileId, attributes) {
_classCallCheck(this, Sprite);
this.ppu = ppu;
this.id = id;
this.x = x;
this.y = y;
this.is8x16 = is8x16;
this.patternTableId = patternTableId;
this.tileId = topTileId;
this.attributes = attributes;
}
/**
* Returns the tile id for an `insideY` position.
* The bottom part of a 8x16 sprite uses the next tile index.
*/
_createClass(Sprite, [{
key: "tileIdFor",
value: function tileIdFor(insideY) {
var index = +(insideY >= TILE_SIZE_PIXELS);
if (this.is8x16 && this.flipY) index = +!index;
return this.tileId + index;
}
/** Returns whether it should appear in a certain `scanline` or not. */
}, {
key: "shouldRenderInScanline",
value: function shouldRenderInScanline(scanline) {
var diffY = this.diffY(scanline);
return diffY >= 0 && diffY < this.height;
}
/** Returns the difference between a `scanline` and sprite's Y coordinate. */
}, {
key: "diffY",
value: function diffY(scanline) {
return scanline - this.y;
}
/** Returns the palette id of the sprite. */
}, {
key: "paletteId",
get: function get() {
return PALETTE_FOREGROUND_START + _byte.default.getBits(this.attributes, SPRITE_ATTR_PALETTE_BITS_START, SPRITE_ATTR_PALETTE_BITS_SIZE);
}
/** Returns whether the sprite is in front of background or not. */
}, {
key: "isInFrontOfBackground",
get: function get() {
if (!this.ppu.cpu.unbroken) {
// [!!!]
return true;
}
return !_byte.default.getBit(this.attributes, SPRITE_ATTR_PRIORITY_BIT);
}
/** Returns whether the sprite is horizontally flipped or not. */
}, {
key: "flipX",
get: function get() {
return _byte.default.getFlag(this.attributes, SPRITE_ATTR_HORIZONTAL_FLIP_BIT);
}
/** Returns whether the sprite is vertically flipped or not. */
}, {
key: "flipY",
get: function get() {
return _byte.default.getFlag(this.attributes, SPRITE_ATTR_VERTICAL_FLIP_BIT);
}
/** Returns the sprite height. */
}, {
key: "height",
get: function get() {
return this.is8x16 ? 16 : 8;
}
}]);
return Sprite;
}();
exports.default = Sprite;