isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
72 lines (71 loc) • 2.95 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PressInput = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const decorators_1 = require("../../../decorators");
const playerIndex_1 = require("../../../functions/playerIndex");
const Feature_1 = require("../../private/Feature");
const v = {
run: {
buttonActionPairs: [],
},
};
class PressInput extends Feature_1.Feature {
/** @internal */
v = v;
/** @internal */
constructor() {
super();
this.callbacksUsed = [
// 13
[
isaac_typescript_definitions_1.ModCallback.INPUT_ACTION,
this.isActionTriggered,
[isaac_typescript_definitions_1.InputHook.IS_ACTION_TRIGGERED],
],
];
}
// ModCallback.INPUT_ACTION (13)
// InputHook.IS_ACTION_TRIGGERED (1)
isActionTriggered = (entity, _inputHook, buttonAction) => {
if (entity === undefined) {
return undefined;
}
const player = entity.ToPlayer();
if (player === undefined) {
return undefined;
}
const playerIndex = (0, playerIndex_1.getPlayerIndex)(player);
for (let i = v.run.buttonActionPairs.length - 1; i >= 0; i--) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const pair = v.run.buttonActionPairs[i];
if (pair.playerIndex === playerIndex
&& pair.buttonAction === buttonAction) {
v.run.buttonActionPairs.splice(i);
return true;
}
}
return undefined;
};
/**
* Helper function to press an arbitrary `ButtonAction` on the next possible input poll. In most
* cases, this will be equivalent to if the first player pressed the corresponding input. It
* usually takes 1 frame for the input to take effect.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.PRESS_INPUT`.
*/
pressInput(player, buttonAction) {
const playerIndex = (0, playerIndex_1.getPlayerIndex)(player);
v.run.buttonActionPairs.push({ playerIndex, buttonAction });
}
}
exports.PressInput = PressInput;
__decorate([
decorators_1.Exported
], PressInput.prototype, "pressInput", null);