Gravwell-Online/common/src/algorithms/processCard.ts
2022-06-28 23:59:56 -06:00

72 lines
No EOL
1.8 KiB
TypeScript

import { Board, GamePiece } from "../types/GameBoard";
import { FuelCard } from "../types/FuelCard";
/** @internal */
export function tractorBeam(board: Board, origin: number, magnitude: number) {
let delta = 0;
// let movedShips: GamePiece[] = [];
while (true) {
delta++;
let behind = origin - delta;
let ahead = origin + delta;
let negativeNewPosition = behind;
let positiveNewPosition = ahead;
let behindShip = board[behind];
let aheadShip = board[ahead];
/*
Check for a ship behind the origin, if there is a ship there then pull
it towards the origin (forwards). If it were to collide with another
ship, then drift until it no longer collides.
*/
if (
behind >= 0
&& behindShip != null
) {
let localMagnitude = magnitude;
while (board[behind + localMagnitude] != null) {
localMagnitude++;
};
negativeNewPosition = behind + localMagnitude;
board[behind] = null;
};
/*
Check for a ship ahead of the origin, if there is a ship there then
pull it towards the origin (backwards). If it were to collide with
another ship, then drift until it no longer collides.
*/
if (
ahead < board.length
&& aheadShip
) {
let localMagnitude = magnitude;
while (board[ahead - localMagnitude] != null) {
localMagnitude++;
};
positiveNewPosition = ahead - localMagnitude;
board[ahead] = null;
};
// Simultaneous movement
if (aheadShip) {
board[positiveNewPosition] = aheadShip;
};
if (behindShip) {
board[negativeNewPosition] = behindShip;
};
if (behind < 0 && ahead >= board.length) {
break;
};
};
};
/** @internal */
export function moveShip(board: Board, shipLocation: number, fuel: FuelCard) {};
export function processCard(board: Board, shipLocation: number, fuel: FuelCard) {};