isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
89 lines (88 loc) • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyKColor = copyKColor;
exports.deserializeKColor = deserializeKColor;
exports.getRandomKColor = getRandomKColor;
exports.isKColor = isKColor;
exports.isSerializedKColor = isSerializedKColor;
exports.kColorEquals = kColorEquals;
exports.serializeKColor = serializeKColor;
const SerializationBrand_1 = require("../enums/private/SerializationBrand");
const isaacAPIClass_1 = require("./isaacAPIClass");
const random_1 = require("./random");
const rng_1 = require("./rng");
const table_1 = require("./table");
const types_1 = require("./types");
const utils_1 = require("./utils");
const OBJECT_NAME = "KColor";
const KEYS = ["Red", "Green", "Blue", "Alpha"];
/** Helper function to copy a `KColor` Isaac API class. */
function copyKColor(kColor) {
if (!isKColor(kColor)) {
error(`Failed to copy a ${OBJECT_NAME} object since the provided object was not a userdata ${OBJECT_NAME} class.`);
}
return KColor(kColor.Red, kColor.Green, kColor.Blue, kColor.Alpha);
}
/**
* Helper function to convert a `SerializedKColor` object to a normal `KColor` object. (This is used
* by the save data manager when reading data from the "save#.dat" file.)
*/
function deserializeKColor(kColor) {
if (!(0, types_1.isTable)(kColor)) {
error(`Failed to deserialize a ${OBJECT_NAME} object since the provided object was not a Lua table.`);
}
const [r, g, b, a] = (0, table_1.getNumbersFromTable)(kColor, OBJECT_NAME, ...KEYS);
(0, utils_1.assertDefined)(r, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: Red`);
(0, utils_1.assertDefined)(g, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: Green`);
(0, utils_1.assertDefined)(b, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: Blue`);
(0, utils_1.assertDefined)(a, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: Alpha`);
return KColor(r, g, b, a);
}
/**
* Helper function to get a random `KColor` object (for use in fonts).
*
* If you want to generate an unseeded object, you must explicitly pass `undefined` to the
* `seedOrRNG` parameter.
*
* @param seedOrRNG The `Seed` or `RNG` object to use. If an `RNG` object is provided, the
* `RNG.Next` method will be called. If `undefined` is provided, it will default to
* a random seed.
* @param alpha Optional. The alpha value to use. Default is 1.
*/
function getRandomKColor(seedOrRNG, alpha = 1) {
const rng = (0, rng_1.isRNG)(seedOrRNG) ? seedOrRNG : (0, rng_1.newRNG)(seedOrRNG);
const r = (0, random_1.getRandom)(rng);
const g = (0, random_1.getRandom)(rng);
const b = (0, random_1.getRandom)(rng);
return KColor(r, g, b, alpha);
}
/** Helper function to check if something is an instantiated `KColor` object. */
function isKColor(object) {
return (0, isaacAPIClass_1.isIsaacAPIClassOfType)(object, OBJECT_NAME);
}
/**
* Used to determine is the given table is a serialized `KColor` object created by the `deepCopy`
* function.
*/
function isSerializedKColor(object) {
if (!(0, types_1.isTable)(object)) {
return false;
}
return ((0, table_1.tableHasKeys)(object, ...KEYS) && object.has(SerializationBrand_1.SerializationBrand.K_COLOR));
}
function kColorEquals(kColor1, kColor2) {
return (0, isaacAPIClass_1.isaacAPIClassEquals)(kColor1, kColor2, KEYS);
}
/**
* Helper function to convert a `KColor` object to a `SerializedKColor` object. (This is used by the
* save data manager when writing data from the "save#.dat" file.)
*/
function serializeKColor(kColor) {
if (!isKColor(kColor)) {
error(`Failed to serialize a ${OBJECT_NAME} object since the provided object was not a userdata ${OBJECT_NAME} class.`);
}
const kColorTable = new LuaMap();
(0, table_1.copyUserdataValuesToTable)(kColor, KEYS, kColorTable);
kColorTable.set(SerializationBrand_1.SerializationBrand.K_COLOR, "");
return kColorTable;
}