All files / src/model commentDeployer.ts

100% Statements 52/52
100% Branches 41/41
100% Functions 13/13
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                                                                                                                                        22x 16x 16x       16x       16x     16x 16x 16x 16x 16x       2x       13x 13x 12x 1x     1x 1x           4x       66x       64x       12x       12x 12x   12x 8x         60x 60x 60x 60x 789x 744x     60x 452x 33x 33x     27x 26x   1x         60x               60x 2223x 3x 3x 3x 3x   2220x 2220x   60x 60x      
import { CommentSupplier } from './commentSupplier'
 
/**
 * 画面上に流れるコメントをゲーム画面に配置します.
 *
 * {@link CommentDeployerBuilder} を使ってインスタンスを作成してください.
 */
export interface CommentDeployer {
  /**
   * コメントを配置したとき発火されるトリガ
   */
  readonly onDeploy: g.Trigger<g.E>
 
  /**
   * コメントが削除されたとき発火されるトリガ
   */
  readonly onFadeOut: g.Trigger<g.E>
 
  /**
   * コメントを配置するエンティティ.
   *
   * コメントは指定されたエンティティの子として登録されます.
   */
  container?: g.E
 
  /**
   * コメントが流れる速度. 1フレームあたりの移動座標数
   */
  readonly speed: number
 
  /**
   * コメントの表示間隔 (y座標値)
   */
  readonly intervalY: number
 
  /**
   * コメントの表示に使うフォント
   */
  readonly font: g.Font
 
  /**
   * 配置対象のコメント生成器一覧を取得します.
   */
  readonly suppliers: readonly CommentSupplier[]
 
  /**
   * ライブラリ利用者が自由に使えるフィールドです.
   */
  vars?: unknown
 
  /**
   * コメント内容を生成するインスタンスを追加します.
   *
   * @param supplier 配置したいコメントを生成するインスタンス
   */
  subscribe (supplier: CommentSupplier): void
 
  /**
   * コメントを画面上に配置します.
   *
   * ライブラリ利用者はこれを呼び出す必要はありません. 自動的に呼び出されます.
   *
   * @param text コメント本文
   * @internal
   */
  deploy (text: string): void
}
 
export interface CommentDeployerOptions {
  scene: g.Scene
  speed: number
  intervalY: number
  font: g.Font
  vars: unknown
}
 
export class CommentDeployerImpl implements CommentDeployer {
  readonly onDeploy = new g.Trigger<g.E>()
  readonly onFadeOut = new g.Trigger<g.E>()
  vars?: unknown
  private readonly scene: g.Scene
  private readonly _font: g.Font
  private readonly views: { row: number, view: g.E }[] = []
  private _container?: g.E
  private readonly _speed: number
  private readonly _intervalY: number
  private readonly _suppliers = new Set<CommentSupplier>()
 
  constructor ({ scene, font, speed, intervalY, vars }: CommentDeployerOptions) {
    this.scene = scene
    this._font = font
    this._speed = speed
    this._intervalY = intervalY
    this.vars = vars
  }
 
  get container (): g.E | undefined {
    return this._container
  }
 
  set container (container: g.E) {
    this._container = container
    if (this._container) {
      for (const { view } of this.views) {
        this._container.append(view)
      }
    } else {
      for (const { view } of this.views) {
        view.remove()
      }
    }
  }
 
  get speed (): number {
    return this._speed
  }
 
  get intervalY (): number {
    return this._intervalY
  }
 
  get font (): g.Font {
    return this._font
  }
 
  get suppliers (): readonly CommentSupplier[] {
    return [...this._suppliers]
  }
 
  subscribe (supplier: CommentSupplier) {
    if (!this._suppliers.has(supplier)) {
      this._suppliers.add(supplier)
    }
    if (!supplier.deployers.some(d => d === this)) {
      supplier.addDeployer(this)
    }
  }
 
  deploy (text: string): void {
    const left = this._container?.width ?? this.scene.game.width
    const bottom = this._container?.height ?? this.scene.game.height
    const full = new Set<number>()
    for (const e of this.views) {
      if (e.view.x + e.view.width > left) {
        full.add(e.row)
      }
    }
    for (let row = 0; row * this._intervalY < bottom; row++) {
      if (!full.has(row)) {
        this.onDeploy.fire(this.createView(row, text))
        return
      }
    }
    if (this.views.length > 0) {
      this.onDeploy.fire(this.createView(this.views[this.views.length - 1].row + 1, text))
    } else {
      this.onDeploy.fire(this.createView(0, text))
    }
  }
 
  private createView (row: number, text: string): g.E {
    const view = new g.Label({
      scene: this.scene,
      parent: this._container,
      font: this.font,
      x: this._container?.width ?? this.scene.game.width,
      y: (row % (Math.ceil(this._container?.height ?? this.scene.game.height) / this.intervalY)) * this._intervalY,
      text
    })
    view.onUpdate.add(() => {
      if (view.x + view.width < 0) {
        view.destroy()
        this.views.splice(this.views.findIndex(e => e.view === view), 1)
        this.onFadeOut.fire(view)
        return true
      }
      view.x -= this._speed
      view.modified()
    })
    this.views.push({ row, view })
    return view
  }
}