-
Notifications
You must be signed in to change notification settings - Fork 21
/
CustomGridEntities.ts
424 lines (383 loc) · 15.4 KB
/
CustomGridEntities.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import {
ActiveSlot,
CollectibleType,
GridCollisionClass,
GridEntityType,
ModCallback,
UseFlag,
} from "isaac-typescript-definitions";
import { game } from "../../../core/cachedClasses";
import { Exported } from "../../../decorators";
import { ISCFeature } from "../../../enums/ISCFeature";
import { ModCallbackCustom } from "../../../enums/ModCallbackCustom";
import {
removeGridEntity,
spawnGridEntityWithVariant,
} from "../../../functions/gridEntities";
import { getPlayerFromPtr } from "../../../functions/players";
import { getRoomListIndex } from "../../../functions/roomData";
import { isNumber } from "../../../functions/types";
import { isVector } from "../../../functions/vector";
import { GridEntityCustomData } from "../../../interfaces/GridEntityCustomData";
import { DefaultMap } from "../../DefaultMap";
import { Feature } from "../../private/Feature";
import { RunInNFrames } from "../other/RunInNFrames";
export class CustomGridEntities extends Feature {
/** @internal */
public override v = {
level: {
/** Indexed by room list index and grid index. */
customGridEntities: new DefaultMap<int, Map<int, GridEntityCustomData>>(
() => new Map(),
),
},
room: {
genericPropPtrHashes: new Set<PtrHash>(),
manuallyUsingShovel: false,
},
};
private runInNFrames: RunInNFrames;
/** @internal */
constructor(runInNFrames: RunInNFrames) {
super();
this.featuresUsed = [ISCFeature.RUN_IN_N_FRAMES];
this.callbacksUsed = [
// 23
[
ModCallback.PRE_USE_ITEM,
this.preUseItemWeNeedToGoDeeper,
[CollectibleType.WE_NEED_TO_GO_DEEPER],
],
];
this.customCallbacksUsed = [
[ModCallbackCustom.POST_NEW_ROOM_REORDERED, this.postNewRoomReordered],
];
this.runInNFrames = runInNFrames;
}
// ModCallback.PRE_USE_ITEM (23)
// CollectibleType.WE_NEED_TO_GO_DEEPER (84)
private preUseItemWeNeedToGoDeeper = (
_collectibleType: CollectibleType,
_rng: RNG,
player: EntityPlayer,
_useFlags: BitFlags<UseFlag>,
_activeSlot: ActiveSlot,
_customVarData: int,
): boolean | undefined => {
// If a player uses We Need to Go Deeper on top of a custom grid entity, then they will always
// get a crawlspace, due to how custom grids are implemented with decorations. Thus, remove the
// custom grid entity to prevent this from happening if needed.
const room = game.GetRoom();
const roomListIndex = getRoomListIndex();
const roomCustomGridEntities =
this.v.level.customGridEntities.get(roomListIndex);
if (roomCustomGridEntities === undefined) {
return undefined;
}
const gridIndex = room.GetGridIndex(player.Position);
const customGridEntity = roomCustomGridEntities.get(gridIndex);
if (customGridEntity === undefined) {
return undefined;
}
// If the custom grid entity has collision, then the player should not be able to be standing on
// top of it.
if (customGridEntity.gridCollisionClass !== GridCollisionClass.NONE) {
return undefined;
}
removeGridEntity(customGridEntity.gridIndex, false);
const entityPtr = EntityPtr(player);
this.runInNFrames.runNextGameFrame(() => {
const futurePlayer = getPlayerFromPtr(entityPtr);
if (futurePlayer === undefined) {
return;
}
this.v.room.manuallyUsingShovel = true;
futurePlayer.UseActiveItem(CollectibleType.WE_NEED_TO_GO_DEEPER);
this.v.room.manuallyUsingShovel = false;
});
// Cancel the original effect.
return true;
};
// ModCallbackCustom.POST_NEW_ROOM_REORDERED
private postNewRoomReordered = (): void => {
// When we re-enter a room, the graphics for any custom entities will be reverted back to that
// of a normal decoration. Thus, we must re-apply the anm2.
const roomListIndex = getRoomListIndex();
const roomCustomGridEntities =
this.v.level.customGridEntities.get(roomListIndex);
if (roomCustomGridEntities === undefined) {
return;
}
const room = game.GetRoom();
for (const [gridIndex, data] of roomCustomGridEntities) {
const decoration = room.GetGridEntity(gridIndex);
if (decoration === undefined) {
roomCustomGridEntities.delete(gridIndex);
continue;
}
if (data.anm2Path !== undefined) {
const sprite = decoration.GetSprite();
sprite.Load(data.anm2Path, true);
const animationToPlay =
data.defaultAnimation === undefined
? sprite.GetDefaultAnimation()
: data.defaultAnimation;
sprite.Play(animationToPlay, true);
}
}
};
/**
* Helper function to spawn a custom grid entity. Custom grid entities are persistent in that they
* will reappear if the player leaves and re-enters the room. (It will be manually respawned in
* the `POST_NEW_ROOM` callback.)
*
* In order to use this function, you must upgrade your mod with
* `ISCFeature.CUSTOM_GRID_ENTITIES`.
*
* Custom grid entities are built on top of real grid entities. You can use any existing grid
* entity type as a base. For example, if you want to create a custom rock that would be breakable
* like a normal rock, then you should specify `GridEntityType.ROCK` as the base grid entity type.
*
* Once a custom grid entity is spawned, you can take advantage of the custom grid callbacks such
* as `POST_GRID_ENTITY_CUSTOM_UPDATE`. Note that the "normal" grid entities callbacks will not
* fire for custom entities. For example, if you had a custom grid entity based on
* `GridEntityType.ROCK`, and you also had a subscription to the `POST_GRID_ENTITY_UPDATE`
* callback, the callback would only fire for normal rocks and not the custom entity.
*
* Custom grid entities are an IsaacScript feature because the vanilla game does not support any
* custom grid entities.
*
* For example, this would be code to create a custom rock called a "Silver Rock" that produces a
* dime when destroyed:
*
* ```ts
* // This is local to the mod and can safely overlap with the values of `GridEntityType` (or
* // values chosen by other mods).
* const GridEntityTypeCustom = {
* SILVER_ROCK: 0 as GridEntityType,
* } as const;
*
* // This is copied from "gfx/grid/grid_rock.anm2" with some tweaks to make it look special.
* const SILVER_ROCK_ANM2_PATH = "gfx/grid/grid_rock_silver.anm2";
*
* export function silverRockInit(mod: ModUpgraded): void {
* mod.AddCallbackCustom(
* ModCallbackCustom.POST_GRID_ENTITY_CUSTOM_BROKEN,
* postGridEntityCustomBrokenSilverRock,
* GridEntityTypeCustom.SILVER_ROCK,
* );
* }
*
* function postGridEntityCustomBrokenSilverRock(gridEntity: GridEntity) {
* spawnCoin(CoinSubType.DIME, gridEntity.Position);
* }
*
* export function spawnSilverRock(mod: ModUpgraded, gridIndex: int): GridEntity {
* return mod.spawnCustomGridEntity(
* GridEntityTypeCustom.SILVER_ROCK,
* gridIndex,
* undefined,
* SILVER_ROCK_ANM2_PATH,
* undefined,
* GridEntityType.ROCK,
* );
* }
* ```
*
* @param gridEntityTypeCustom An integer that identifies what kind of grid entity you are
* creating. It should correspond to a local enum value created in
* your mod. The integer can be any unique value and will not
* correspond to the actual grid entity type used. (This integer is
* used in the various custom grid entity callbacks.)
* @param gridIndexOrPosition The grid index or position in the room that you want to spawn the
* grid entity at. If a position is specified, the closest grid index
* will be used.
* @param gridCollisionClass Optional. The collision class that you want the custom grid entity to
* have. If not specified, the grid collision class from the base grid
* entity will be used.
* @param anm2Path Optional. The path to the ANM2 file to use for the sprite. If not specified,
* the normal sprite from the base grid entity will be used.
* @param defaultAnimation Optional. The name of the animation to play after the sprite is
* initialized and after the player re-enters a room with this grid entity
* in it. If not specified, the default animation in the anm2 will be
* used.
* @param baseGridEntityType Optional. The type of the grid entity to use as a "base" for this
* custom grid entity. Default is `GridEntityType.DECORATION`.
* @param baseGridEntityVariant Optional. The variant of the grid entity to use as a "base" for
* this custom grid entity. Default is 0.
*/
@Exported
public spawnCustomGridEntity(
gridEntityTypeCustom: GridEntityType,
gridIndexOrPosition: int | Vector,
gridCollisionClass?: GridCollisionClass,
anm2Path?: string,
defaultAnimation?: string,
baseGridEntityType = GridEntityType.DECORATION,
baseGridEntityVariant = 0,
): GridEntity {
const room = game.GetRoom();
const roomListIndex = getRoomListIndex();
const gridIndex = isVector(gridIndexOrPosition)
? room.GetGridIndex(gridIndexOrPosition)
: gridIndexOrPosition;
const customGridEntity = spawnGridEntityWithVariant(
baseGridEntityType,
baseGridEntityVariant,
gridIndexOrPosition,
);
if (customGridEntity === undefined) {
error("Failed to spawn a custom grid entity.");
}
if (gridCollisionClass !== undefined) {
customGridEntity.CollisionClass = gridCollisionClass;
}
if (anm2Path !== undefined) {
const sprite = customGridEntity.GetSprite();
sprite.Load(anm2Path, true);
const animationToPlay =
defaultAnimation === undefined
? sprite.GetDefaultAnimation()
: defaultAnimation;
sprite.Play(animationToPlay, true);
}
const customGridEntityData: GridEntityCustomData = {
gridEntityTypeCustom,
roomListIndex,
gridIndex,
anm2Path,
defaultAnimation,
gridCollisionClass,
};
const roomCustomGridEntities =
this.v.level.customGridEntities.getAndSetDefault(roomListIndex);
roomCustomGridEntities.set(gridIndex, customGridEntityData);
return customGridEntity;
}
/**
* Helper function to remove a custom grid entity created by the `spawnCustomGrid` function.
*
* In order to use this function, you must upgrade your mod with
* `ISCFeature.CUSTOM_GRID_ENTITIES`.
*
* @param gridIndexOrPositionOrGridEntity You can specify the custom grid entity to remove by
* providing the grid index, the room position, or the grid entity
* itself.
* @param updateRoom Optional. Whether or not to update the room after the grid entity is removed.
* Default is true. This is generally a good idea because if the room is not
* updated, you will be unable to spawn another grid entity on the same tile
* until a frame has passed. However, doing this is expensive, since it involves
* a call to `Isaac.GetRoomEntities`, so set it to false if you need to run this
* function multiple times.
* @returns The grid entity that was removed. Returns undefined if no grid entity was found at the
* given location or if the given grid entity was not a custom grid entity.
*/
@Exported
public removeCustomGridEntity(
gridIndexOrPositionOrGridEntity: int | Vector | GridEntity,
updateRoom = true,
): GridEntity | undefined {
const room = game.GetRoom();
const roomListIndex = getRoomListIndex();
let decoration: GridEntity;
if (typeof gridIndexOrPositionOrGridEntity === "number") {
const gridIndex = gridIndexOrPositionOrGridEntity;
const gridEntity = room.GetGridEntity(gridIndex);
if (gridEntity === undefined) {
return undefined;
}
decoration = gridEntity;
} else if (isVector(gridIndexOrPositionOrGridEntity)) {
const position = gridIndexOrPositionOrGridEntity;
const gridEntity = room.GetGridEntityFromPos(position);
if (gridEntity === undefined) {
return undefined;
}
decoration = gridEntity;
} else {
decoration = gridIndexOrPositionOrGridEntity;
}
const gridIndex = decoration.GetGridIndex();
const roomCustomGridEntities =
this.v.level.customGridEntities.getAndSetDefault(roomListIndex);
const exists = roomCustomGridEntities.has(gridIndex);
if (!exists) {
return undefined;
}
roomCustomGridEntities.delete(gridIndex);
removeGridEntity(decoration, updateRoom);
return decoration;
}
/**
* Helper function to get the custom grid entities in the current room. Returns an array of tuples
* containing the raw decoration grid entity and the associated entity data.
*
* In order to use this function, you must upgrade your mod with
* `ISCFeature.CUSTOM_GRID_ENTITIES`.
*/
@Exported
public getCustomGridEntities(): Array<
[gridEntity: GridEntity, data: GridEntityCustomData]
> {
const roomListIndex = getRoomListIndex();
const roomCustomGridEntities =
this.v.level.customGridEntities.get(roomListIndex);
if (roomCustomGridEntities === undefined) {
return [];
}
const room = game.GetRoom();
const customGridEntities: Array<[GridEntity, GridEntityCustomData]> = [];
for (const [gridIndex, data] of roomCustomGridEntities) {
const gridEntity = room.GetGridEntity(gridIndex);
if (gridEntity !== undefined) {
customGridEntities.push([gridEntity, data]);
}
}
return customGridEntities;
}
/**
* Helper function to get the custom `GridEntityType` from a `GridEntity` or grid index. Returns
* undefined if the provided `GridEntity` is not a custom grid entity, or if there was not a grid
* entity on the provided grid index.
*
* In order to use this function, you must upgrade your mod with
* `ISCFeature.CUSTOM_GRID_ENTITIES`.
*/
@Exported
public getCustomGridEntityType(
gridEntityOrGridIndex: GridEntity | int,
): GridEntityType | undefined {
if (!this.initialized) {
return undefined;
}
const gridIndex = isNumber(gridEntityOrGridIndex)
? gridEntityOrGridIndex
: gridEntityOrGridIndex.GetGridIndex();
const roomListIndex = getRoomListIndex();
const roomCustomGridEntities =
this.v.level.customGridEntities.get(roomListIndex);
if (roomCustomGridEntities === undefined) {
return undefined;
}
for (const [_gridIndex, data] of roomCustomGridEntities) {
if (data.gridIndex === gridIndex) {
return data.gridEntityTypeCustom;
}
}
return undefined;
}
/**
* Helper function to check if a `GridEntity` is a custom grid entity or if a grid index has a
* custom grid entity.
*
* In order to use this function, you must upgrade your mod with
* `ISCFeature.CUSTOM_GRID_ENTITIES`.
*/
@Exported
public isCustomGridEntity(gridEntityOrGridIndex: GridEntity | int): boolean {
const gridEntityTypeCustom = this.getCustomGridEntityType(
gridEntityOrGridIndex,
);
return gridEntityTypeCustom !== undefined;
}
}