Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
teramotodaiki opened this issue Feb 19, 2020 · 0 comments
Open

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

teramotodaiki opened this issue Feb 19, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@teramotodaiki
Copy link
Contributor

現状、 むき に関する表現方法は 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]);
};
@teramotodaiki teramotodaiki added the enhancement New feature or request label Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant