目次へもどる

Player

入力を処理する機構はできたので, いよいよFieldController?を拡張する.

もともとFieldController?はプレイヤーを表すクラスのスーパークラスとして定義していた. ただし,CPUによる操作も考慮すると以下のような構造になると思われる.

  • FieldController?
    • Human
      • Player1
      • Player2
      • ...
    • CPU
      • ...(CPUの戦略アルゴリズムごとのクラス)

今回実行したのはHumanとそのサブクラスまで.CPUはとりあえず無視しておく.

class Human < FieldController
  def initialize(ipt_ctrl, x, y, row_s, line_s, block_s)
    super(x, y, row_s, line_s, block_s)
    @ipt_ctrl = ipt_ctrl
  end

  def human?; true; end
  def cpu?; false; end

  def update
    @ipt_ctrl.update
    super
  end

  def input_move_right?
    @ipt_ctrl.press?(:right)
  end
  def input_move_left?
    @ipt_ctrl.press?(:left)
  end
  def input_rotate_right?
    @ipt_ctrl.press?(:button1)
  end
  def input_rotate_left?
    @ipt_ctrl.trigger?(:button2)
  end
  def input_fastfall_right?
    @ipt_ctrl.press?(:down)
  end
end

HumanクラスはFieldController?InputController?を持たせただけである. 入力の情報をupdateメソッドで更新してコントロールブロックに対する操作をチェックできる.

class Player1 < Human
  def initialize(x, y, row_s, line_s, block_s)
    super(InputController.new(1), x, y, row_s, line_s, block_s)
  end
end

Player1はInputController?をプレイヤー番号1で生成する.


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2013-08-30 (金) 22:01:50 (3885d)