Skip to content

Commit

Permalink
refactor(common): getDefaultPlayerStat takes PlayerStat
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamiell committed Sep 4, 2023
1 parent 5b44386 commit 638b485
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
33 changes: 13 additions & 20 deletions packages/isaacscript-common/src/functions/stats.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { CacheFlag } from "isaac-typescript-definitions";
import { DEFAULT_PLAYER_STAT_MAP } from "../maps/defaultPlayerStatMap";
import { ReadonlySet } from "../types/ReadonlySet";
import { DEFAULT_PLAYER_STATS } from "../objects/defaultPlayerStats";
import { addTearsStat } from "./tears";

const STAT_CACHE_FLAGS_SET = new ReadonlySet<CacheFlag>([
CacheFlag.DAMAGE, // 1 << 0
CacheFlag.FIRE_DELAY, // 1 << 1
CacheFlag.SHOT_SPEED, // 1 << 2
CacheFlag.RANGE, // 1 << 3
CacheFlag.SPEED, // 1 << 4
CacheFlag.LUCK, // 1 << 10
]);

/**
* Helper function to add a stat to a player based on the `CacheFlag` provided. Call this function
* from the `EVALUATE_CACHE` callback.
Expand All @@ -32,12 +22,6 @@ export function addStat(
cacheFlag: CacheFlag,
amount: number,
): void {
if (!STAT_CACHE_FLAGS_SET.has(cacheFlag)) {
error(
`You cannot add a stat to a player with the cache flag of: ${cacheFlag}`,
);
}

switch (cacheFlag) {
// 1 << 0
case CacheFlag.DAMAGE: {
Expand Down Expand Up @@ -74,15 +58,24 @@ export function addStat(
player.Luck += amount;
break;
}

// eslint-disable-next-line isaacscript/require-break
default: {
error(
`You cannot add a stat to a player with the cache flag of: ${cacheFlag}`,
);
}
}
}

/**
* Returns the starting stat that Isaac (the default character) starts with. For example, if you
* pass this function `CacheFlag.DAMAGE`, it will return 3.5.
* pass this function `PlayerStat.DAMAGE`, it will return 3.5.
*
* Note that the default fire delay is represented in the tear stat, not the `MaxFireDelay` value.
*/
export function getDefaultPlayerStat(cacheFlag: CacheFlag): number | undefined {
return DEFAULT_PLAYER_STAT_MAP.get(cacheFlag);
export function getDefaultPlayerStat(
playerStat: keyof typeof DEFAULT_PLAYER_STATS,
): int | float {
return DEFAULT_PLAYER_STATS[playerStat];
}
18 changes: 0 additions & 18 deletions packages/isaacscript-common/src/maps/defaultPlayerStatMap.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/isaacscript-common/src/objects/defaultPlayerStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PlayerStat } from "../enums/PlayerStat";
import { getTearsStat } from "../functions/tears";

const DEFAULT_MAX_FIRE_DELAY = 10;

/** The default fire delay is represented in the tear stat, not the `MaxFireDelay` value. */
export const DEFAULT_PLAYER_STATS = {
[PlayerStat.DAMAGE]: 3.5, // 1 << 0

// The default tears stat is 2.73.
[PlayerStat.FIRE_DELAY]: getTearsStat(DEFAULT_MAX_FIRE_DELAY), // 1 << 1

[PlayerStat.SHOT_SPEED]: 1, // 1 << 2
[PlayerStat.TEAR_RANGE]: 6.5, // 1 << 3
[PlayerStat.MOVE_SPEED]: 1, // 1 << 4
[PlayerStat.LUCK]: 0, // 1 << 10
} as const;

0 comments on commit 638b485

Please sign in to comment.