Skip to content

Commit

Permalink
refactor: getRandomOffsetPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamiell committed Dec 3, 2023
1 parent 829adb0 commit 64afd09
Showing 1 changed file with 9 additions and 40 deletions.
49 changes: 9 additions & 40 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CardType,
CoinSubType,
CollectibleType,
Direction,
DisplayFlag,
EffectVariant,
EntityFlag,
Expand All @@ -28,13 +29,15 @@ import {
GAME_FRAMES_PER_SECOND,
VectorZero,
addRoomDisplayFlag,
directionToVector,
doesEntityExist,
findFreePosition,
game,
getCollectibleMaxCharges,
getEntities,
getPlayerFromEntity,
getRandomArrayElement,
getRandomEnumValue,
getRandomInt,
getRoomsInsideGrid,
hasCollectible,
Expand Down Expand Up @@ -141,47 +144,13 @@ export function getRandomOffsetPosition(
offsetSize: int,
seed: Seed,
): Vector {
const offsetDirection = getRandomInt(1, 4, seed);
const randomDirection = getRandomEnumValue(Direction, seed, [
Direction.NO_DIRECTION,
]);
const vector = directionToVector(randomDirection);
const offset = vector.mul(offsetSize);

let offsetX: int;
let offsetY: int;
switch (offsetDirection) {
// Bottom right
case 1: {
offsetX = offsetSize;
offsetY = offsetSize;
break;
}

// Top right
case 2: {
offsetX = offsetSize;
offsetY = offsetSize * -1;
break;
}

// Bottom left
case 3: {
offsetX = offsetSize * -1;
offsetY = offsetSize;
break;
}

// Top left
case 4: {
offsetX = offsetSize * -1;
offsetY = offsetSize * -1;
break;
}

default: {
return error(
`The offset direction was an unknown value of: ${offsetDirection}`,
);
}
}

return Vector(position.X + offsetX, position.Y + offsetY);
return position.add(offset);
}

export function giveCollectibleAndRemoveFromPools(
Expand Down

0 comments on commit 64afd09

Please sign in to comment.