Skip to content

「ばしょ」を表すクラス #79

@teramotodaiki

Description

@teramotodaiki

2次元座標上の位置を扱いやすくするためのクラスの提案
向きによって決まる位置(前や右手など)をどうするか固まっていないので保留中

import { synonymizeClass, PropertyMissing } from './synonyms/synonymize';
import { synonyms } from './synonyms/location';

/**
 * 2次元座標上の位置を扱いやすくするクラス
 */
export class Location {
  /**
   * このばしょがマップの左端から数えて何マス目か(0から数える)
   */
  public readonly x: number;
  /**
   * このばしょがマップの上端から数えて何マス目か(0から数える)
   */
  public readonly y: number;
  /**
   * このばしょが何という名前のマップにあるか
   */
  public readonly map: string;

  constructor(x: number, y: number, map: string) {
    this.x = x;
    this.y = y;
    this.map = map;
  }

  /**
   * 他のキャラクターがこのばしょにいるかどうかを調べる
   * @param other ばしょを調べたいキャラクターの変数
   */
  equalsTo(other: Location | { location: Location }): boolean {
    return other instanceof Location
      ? this.x === other.x && this.y === other.y && this.map === other.map
      : this.equalsTo(other.location);
  }

  up() {
      return new Location(this.x, this.y - 1, this.map);
  }

  [PropertyMissing](chainName: string) {}
}

export const LocationWithSynonyms = synonymizeClass(Location, synonyms);

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