isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
146 lines (145 loc) • 4.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultMapGetNPC = defaultMapGetNPC;
exports.defaultMapSetNPC = defaultMapSetNPC;
exports.mapDeleteNPC = mapDeleteNPC;
exports.mapGetNPC = mapGetNPC;
exports.mapHasNPC = mapHasNPC;
exports.mapSetNPC = mapSetNPC;
exports.setAddNPC = setAddNPC;
exports.setDeleteNPC = setDeleteNPC;
exports.setHasNPC = setHasNPC;
/**
* Helper function to make using default maps with an index of `PtrHash` easier. Use this instead of
* the `DefaultMap.getAndSetDefault` method if you have a default map of this type.
*
* For example:
*
* ```ts
* const v = {
* run: {
* npcsSpeedBoost: new DefaultMap<PtrHash, int>(0),
* },
* };
*
* function npcUpdate(npc: EntityNPC) {
* const speedBoost = defaultMapGetNPC(v.run.npcsSpeedBoost, npc);
* // Do something with the speed boost.
* }
* ```
*
* Note that not all NPCs should be stored in a map with a `PtrHash` as an index, so only use this
* in the situations where that would be okay. (For example, Dark Esau should never be stored in a
* map like this, because the scope of `PtrHash` is per room and Dark Esau is persistent between
* rooms.)
*/
function defaultMapGetNPC(map, npc, ...extraArgs) {
const ptrHash = GetPtrHash(npc);
return map.getAndSetDefault(ptrHash, ...extraArgs);
}
/**
* Helper function to make using maps with an index of `PtrHash` easier. Use this instead of the
* `Map.set` method if you have a map of this type.
*
* Since `Map` and `DefaultMap` set values in the same way, this function is simply an alias for the
* `mapSetNPC` helper function.
*/
function defaultMapSetNPC(
// eslint-disable-next-line complete/prefer-readonly-parameter-types
map, npc, value) {
mapSetNPC(map, npc, value);
}
/**
* Helper function to make using maps with an type of `PtrHash` easier. Use this instead of the
* `Map.delete` method if you have a set of this type.
*/
function mapDeleteNPC(
// eslint-disable-next-line complete/prefer-readonly-parameter-types
map, npc) {
const ptrHash = GetPtrHash(npc);
return map.delete(ptrHash);
}
/**
* Helper function to make using maps with an index of `PtrHash` easier. Use this instead of the
* `Map.get` method if you have a map of this type.
*
* For example:
*
* ```ts
* const v = {
* run: {
* npcsSpeedBoost: new Map<PtrHash, int>(),
* },
* };
*
* function incrementSpeedBoost(npc: EntityNPC) {
* const oldSpeedBoost = mapGetNPC(v.run.npcsSpeedBoost, npc);
* const newSpeedBoost = oldSpeedBoost + 0.1;
* mapSetNPC(v.run.npcsSpeedBoost, npc);
* }
* ```
*/
function mapGetNPC(map, npc) {
const ptrHash = GetPtrHash(npc);
return map.get(ptrHash);
}
/**
* Helper function to make using maps with an index of `PtrHash` easier. Use this instead of the
* `Map.has` method if you have a map of this type.
*/
function mapHasNPC(map, npc) {
const ptrHash = GetPtrHash(npc);
return map.has(ptrHash);
}
/**
* Helper function to make using maps with an index of `PtrHash` easier. Use this instead of the
* `Map.set` method if you have a map of this type.
*
* For example:
*
* ```ts
* const v = {
* run: {
* npcsSpeedBoost: new Map<PtrHash, int>(),
* },
* };
*
* function incrementSpeedBoost(npc: EntityNPC) {
* const oldSpeedBoost = mapGetNPC(v.run.npcsSpeedBoost, npc);
* const newSpeedBoost = oldSpeedBoost + 0.1;
* mapSetNPC(v.run.npcsSpeedBoost, npc);
* }
* ```
*/
function mapSetNPC(
// eslint-disable-next-line complete/prefer-readonly-parameter-types
map, npc, value) {
const ptrHash = GetPtrHash(npc);
map.set(ptrHash, value);
}
/**
* Helper function to make using sets with an type of `PtrHash` easier. Use this instead of the
* `Set.add` method if you have a set of this type.
*/
// eslint-disable-next-line complete/prefer-readonly-parameter-types
function setAddNPC(set, npc) {
const ptrHash = GetPtrHash(npc);
set.add(ptrHash);
}
/**
* Helper function to make using sets with an type of `PtrHash` easier. Use this instead of the
* `Set.delete` method if you have a set of this type.
*/
// eslint-disable-next-line complete/prefer-readonly-parameter-types
function setDeleteNPC(set, npc) {
const ptrHash = GetPtrHash(npc);
return set.delete(ptrHash);
}
/**
* Helper function to make using sets with an type of `PtrHash` easier. Use this instead of the
* `Set.has` method if you have a set of this type.
*/
function setHasNPC(set, npc) {
const ptrHash = GetPtrHash(npc);
return set.has(ptrHash);
}