-
Notifications
You must be signed in to change notification settings - Fork 21
/
CollectibleItemPoolType.ts
80 lines (68 loc) · 2.7 KB
/
CollectibleItemPoolType.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { ItemPoolType } from "isaac-typescript-definitions";
import { ModCallback, PickupVariant } from "isaac-typescript-definitions";
import { game } from "../../../core/cachedClasses";
import { Exported } from "../../../decorators";
import { ISCFeature } from "../../../enums/ISCFeature";
import { getEntityID } from "../../../functions/entities";
import { isCollectible } from "../../../functions/pickupVariants";
import { getRoomItemPoolType } from "../../../functions/rooms";
import type { PickupIndex } from "../../../types/PickupIndex";
import { Feature } from "../../private/Feature";
import type { PickupIndexCreation } from "./PickupIndexCreation";
const v = {
run: {
collectibleItemPoolTypeMap: new Map<PickupIndex, ItemPoolType>(),
},
};
export class CollectibleItemPoolType extends Feature {
/** @internal */
public override v = v;
private readonly pickupIndexCreation: PickupIndexCreation;
/** @internal */
constructor(pickupIndexCreation: PickupIndexCreation) {
super();
this.featuresUsed = [ISCFeature.PICKUP_INDEX_CREATION];
this.callbacksUsed = [
// 34
[
ModCallback.POST_PICKUP_INIT,
this.postPickupInitCollectible,
[PickupVariant.COLLECTIBLE],
],
];
this.pickupIndexCreation = pickupIndexCreation;
}
// ModCallback.POST_PICKUP_INIT (34)
// PickupVariant.COLLECTIBLE (100)
private readonly postPickupInitCollectible = (collectible: EntityPickup) => {
const pickupIndex = this.pickupIndexCreation.getPickupIndex(collectible);
if (!v.run.collectibleItemPoolTypeMap.has(pickupIndex)) {
const itemPool = game.GetItemPool();
const lastItemPoolType = itemPool.GetLastPool();
v.run.collectibleItemPoolTypeMap.set(pickupIndex, lastItemPoolType);
}
};
/**
* Helper function to get the item pool type that a given collectible came from. Since there is no
* native method in the API to get this, we listen in the `POST_PICKUP_INIT` callback for
* collectibles, use the `ItemPool.GetLastPool` method, and then assume that the collectible
* matches.
*
* In order to use this function, you must upgrade your mod with
* `ISCFeature.COLLECTIBLE_ITEM_POOL_TYPE`.
*
* @public
*/
@Exported
public getCollectibleItemPoolType(collectible: EntityPickup): ItemPoolType {
if (!isCollectible(collectible)) {
const entityID = getEntityID(collectible);
error(
`The "getCollectibleItemPoolType" function was given a non-collectible: ${entityID}`,
);
}
const pickupIndex = this.pickupIndexCreation.getPickupIndex(collectible);
const itemPoolType = v.run.collectibleItemPoolTypeMap.get(pickupIndex);
return itemPoolType ?? getRoomItemPoolType();
}
}