isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
157 lines (156 loc) • 6.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIsaacAPIClassName = getIsaacAPIClassName;
exports.isBomb = isBomb;
exports.isDoor = isDoor;
exports.isEffect = isEffect;
exports.isEntity = isEntity;
exports.isFamiliar = isFamiliar;
exports.isGridEntity = isGridEntity;
exports.isIsaacAPIClass = isIsaacAPIClass;
exports.isIsaacAPIClassOfType = isIsaacAPIClassOfType;
exports.isKnife = isKnife;
exports.isLaser = isLaser;
exports.isNPC = isNPC;
exports.isPickup = isPickup;
exports.isPit = isPit;
exports.isPlayer = isPlayer;
exports.isPoop = isPoop;
exports.isPressurePlate = isPressurePlate;
exports.isProjectile = isProjectile;
exports.isRock = isRock;
exports.isSpikes = isSpikes;
exports.isTNT = isTNT;
exports.isTear = isTear;
exports.isaacAPIClassEquals = isaacAPIClassEquals;
const string_1 = require("./string");
const types_1 = require("./types");
/**
* Helper function to get the name of a class from the Isaac API. This is contained within the
* "__type" metatable key.
*
* For example, a `Vector` class is has a name of "Vector".
*
* Returns undefined if the object is not of type `userdata` or if the "__type" metatable key does
* not exist.
*
* In some cases, Isaac classes can be a read-only. If this is the case, the "__type" field will be
* prepended with "const ". This function will always strip this prefix, if it exists. For example,
* the class name returned for "const Vector" will be "Vector".
*/
function getIsaacAPIClassName(object) {
if (!(0, types_1.isUserdata)(object)) {
return undefined;
}
const metatable = getmetatable(object);
if (metatable === undefined) {
return undefined;
}
const classType = metatable.get("__type");
if (!(0, types_1.isString)(classType)) {
return undefined;
}
return (0, string_1.trimPrefix)(classType, "const ");
}
/** Helper function to detect if a variable is of type `EntityBomb`. */
function isBomb(variable) {
return getIsaacAPIClassName(variable) === "EntityBomb";
}
/** Helper function to detect if a variable is of type `GridEntityDoor`. */
function isDoor(variable) {
return getIsaacAPIClassName(variable) === "GridEntityDoor";
}
/** Helper function to detect if a variable is of type `EntityEffect`. */
function isEffect(variable) {
return getIsaacAPIClassName(variable) === "EntityEffect";
}
/**
* Helper function to detect if a variable is of type `Entity`. This will return false for child
* classes such as `EntityPlayer` or `EntityTear`.
*/
function isEntity(variable) {
return getIsaacAPIClassName(variable) === "Entity";
}
/** Helper function to detect if a variable is of type `EntityFamiliar`. */
function isFamiliar(variable) {
return getIsaacAPIClassName(variable) === "EntityEffect";
}
/** Helper function to detect if a variable is of type `GridEntity`. */
function isGridEntity(variable) {
return getIsaacAPIClassName(variable) === "GridEntity";
}
/**
* Helper function to check if something is an instantiated class from the Isaac API. (All classes
* from the Isaac API have a type of "userdata" in Lua with a metatable key of "__type" equal to the
* name of the class.)
*/
function isIsaacAPIClass(object) {
const isaacAPIClassType = getIsaacAPIClassName(object);
return isaacAPIClassType !== undefined;
}
function isIsaacAPIClassOfType(object, classType) {
const isaacAPIClassType = getIsaacAPIClassName(object);
return (isaacAPIClassType === classType
|| isaacAPIClassType === `const ${classType}`);
}
/** Helper function to detect if a variable is of type `EntityKnife`. */
function isKnife(variable) {
return getIsaacAPIClassName(variable) === "EntityKnife";
}
/** Helper function to detect if a variable is of type `EntityLaser`. */
function isLaser(variable) {
return getIsaacAPIClassName(variable) === "EntityLaser";
}
/** Helper function to detect if a variable is of type `EntityNPC`. */
function isNPC(variable) {
return getIsaacAPIClassName(variable) === "EntityNPC";
}
/** Helper function to detect if a variable is of type `EntityPickup`. */
function isPickup(variable) {
return getIsaacAPIClassName(variable) === "EntityPickup";
}
/** Helper function to detect if a variable is of type `GridEntityPit`. */
function isPit(variable) {
return getIsaacAPIClassName(variable) === "GridEntityPit";
}
/** Helper function to detect if a variable is of type `EntityPlayer`. */
function isPlayer(variable) {
return getIsaacAPIClassName(variable) === "EntityPlayer";
}
/** Helper function to detect if a variable is of type `GridEntityPoop`. */
function isPoop(variable) {
return getIsaacAPIClassName(variable) === "GridEntityPoop";
}
/** Helper function to detect if a variable is of type `GridEntityPressurePlate`. */
function isPressurePlate(variable) {
return getIsaacAPIClassName(variable) === "GridEntityPressurePlate";
}
/** Helper function to detect if a variable is of type `EntityProjectile`. */
function isProjectile(variable) {
return getIsaacAPIClassName(variable) === "EntityProjectile";
}
/** Helper function to detect if a variable is of type `GridEntityRock`. */
function isRock(variable) {
return getIsaacAPIClassName(variable) === "GridEntityRock";
}
/** Helper function to detect if a variable is of type `GridEntitySpikes`. */
function isSpikes(variable) {
return getIsaacAPIClassName(variable) === "GridEntitySpikes";
}
/** Helper function to detect if a variable is of type `GridEntityTNT`. */
function isTNT(variable) {
return getIsaacAPIClassName(variable) === "GridEntityTNT";
}
/** Helper function to detect if a variable is of type `EntityTear`. */
function isTear(variable) {
return getIsaacAPIClassName(variable) === "EntityTear";
}
/**
* Helper function to check if an instantiated Isaac API class is equal to another one of the same
* type. You must provide the list of keys to check for.
*/
function isaacAPIClassEquals(object1, object2, keys) {
const table1 = object1;
const table2 = object2;
return keys.every((key) => table1.get(key) === table2.get(key));
}