Player †入力を処理する機構はできたので, いよいよFieldController?を拡張する. もともとFieldController?はプレイヤーを表すクラスのスーパークラスとして定義していた. ただし,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で生成する. |