Skip to content

むきを分かりやすく enum にする #70

@teramotodaiki

Description

@teramotodaiki

現状、 むき に関する表現方法は 0~3 の数値、 Vector2Vector2 を返す関数が入り混じっている。これでは比較や計算がしづらいし、利用者にとって分かりやすくない

比較が容易にできる enum を実装し、0~3 の数値で表現されているコードを置き換える。また、 「Vector2 を返す関数」を関数の引数に与えるのをやめて、これも enum に置き換える
ただし、内部的にも enum を持っておき、 forward などのプロパティは Vector2 インスタンスを生成して返す getter にした方が良いだろう

import Vector2 from './math/vector2';

export enum Direction {
  Up = 'up',
  Right = 'right',
  Down = 'down',
  Left = 'left',
  RightHand = 'right hand',
  LeftHand = 'left hand',
  Behind = 'behind',
  Random = 'random',
  RandomDiagonal = 'random diagonal'
}

/**
 *
 * @param current 現在の向き(ベクトル)
 * @param dir
 */
export function getDirection() {}

const m = -1;

/**
 *   0
 * 3   1
 *   2
 */
const _xRandom4 = [0, 1, 0, m];
const _yRandom4 = [m, 0, 1, 0];
export const random4 = () => {
  const n = (Math.random() * 4) >> 0;
  return new Vector2(_xRandom4[n], _yRandom4[n]);
};
export const random = random4;

/**
 * 0 1 2
 * 7   3
 * 6 5 4
 */
const _xRandom8 = [m, 0, 1, 1, 1, 0, m, m];
const _yRandom8 = [m, m, m, 0, 1, 1, 1, 0];
export const random8 = () => {
  const n = (Math.random() * 8) >> 0;
  return new Vector2(_xRandom8[n], _yRandom8[n]);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions