diff --git a/src/helpers/chance.ts b/src/helpers/chance.ts new file mode 100644 index 0000000..8f75465 --- /dev/null +++ b/src/helpers/chance.ts @@ -0,0 +1,6 @@ +/** + * Returns true or false. + */ +export function coinflip() { + return Boolean(randi(2)) +} diff --git a/src/helpers/coordinates.ts b/src/helpers/coordinates.ts new file mode 100644 index 0000000..dbedd59 --- /dev/null +++ b/src/helpers/coordinates.ts @@ -0,0 +1,14 @@ +import { coinflip } from './chance' + +/** + * Generate random coordinates outside the camera view. + */ +export function outsideCoordinates(x: number, y: number) { + const halfWidth = width() / 2 + const halfHeight = height() / 2 + const multiplier = coinflip() ? 1 : -1 + return { + x: x + halfWidth * multiplier, + y: y + halfHeight * multiplier, + } +} diff --git a/src/helpers/index.ts b/src/helpers/index.ts new file mode 100644 index 0000000..f25f9aa --- /dev/null +++ b/src/helpers/index.ts @@ -0,0 +1,2 @@ +export * from './chance' +export * from './coordinates' diff --git a/src/scenes/game.ts b/src/scenes/game.ts index d327eee..d7925a4 100644 --- a/src/scenes/game.ts +++ b/src/scenes/game.ts @@ -1,5 +1,6 @@ import { Scene } from '../constants' import { addEnemy, addPlayer } from '../gameobjects' +import { outsideCoordinates } from '../helpers' scene(Scene.Game, () => { const player = addPlayer() @@ -7,24 +8,7 @@ scene(Scene.Game, () => { add([text('Press arrow keys', { width: width() / 2 }), pos(12, 12)]) loop(5, () => { - const { x, y } = coordinates(player.pos.x, player.pos.y) + const { x, y } = outsideCoordinates(player.pos.x, player.pos.y) addEnemy(x, y, player) }) }) - -/** - * Random coordinates outside the camera view. - */ -function coordinates(x: number, y: number) { - const halfWidth = width() / 2 - const halfHeight = height() / 2 - const multiplier = coinflip() ? 1 : -1 - return { - x: x + halfWidth * multiplier, - y: y + halfHeight * multiplier, - } -} - -function coinflip() { - return Boolean(randi(2)) -}