LoginSignup
1
1

More than 5 years have passed since last update.

オープンフィールドなRPG風ゲームを作る 5ワールド目

Posted at

プレイヤーの状態を管理する

前回作成した状態管理系のクラスを使って、プレイヤーの状態を管理していきます

今のプレイヤーは、一定時間ごとに違うモーションを取るだけですが、
これを1つのステート(状態)として分離します
コードはこんな感じ

Player.cs
using UnityEngine;

public class Player
{
    //! @brief  プレイヤー作成情報
    public struct PlayerCreateParam
    {
        public GameObject   model;      //!< モデル
        public Animator     anim;       //!< アニメーション制御
    }

    //! @brief  待機ステート
    private class StayState : IFSMState
    {
        private const float             SP_STAY_ANIM_TIME       = 5.0f;                                 //!< 特殊待機モーションを再生するまでの時間
        private static readonly int     ANIM_TRIG_SP_STAY       = Animator.StringToHash("spStay");      //!< 特殊待機モーション再生アニメーショントリガー

        public string   name    { get; }        //!< ステート名

        public StayState(string name, Player player)
        {
            this.name   = name;
            _player     = player;
        }

        //! @brief  開始処理
        //! @return なし
        public void entry()
        {
            _stayTimer = SP_STAY_ANIM_TIME;
        }

        //! @brief  実行処理
        //! @param  [in]    fsm     ステートマシン
        //! @return なし
        public void execute(FSMSystem fsm)
        {
            _stayTimer -= Time.deltaTime;
            if( _stayTimer > 0.0f ) return;

            if( _player != null ) {
                _player.setAnimTrigger(ANIM_TRIG_SP_STAY);
            }
            _stayTimer = SP_STAY_ANIM_TIME;
        }

        //! @brief  終了処理
        //! @return なし
        public void exit()
        {
        }

        private Player  _player     = null;     //!< プレイヤー
        private float   _stayTimer  = 0.0f;     //!< 待機時間タイマー
    }

    //! @brief  ステート
    private static class State
    {
        public static string    Stay    { get { return "stay"; } }
    }

    //! @brief  コンストラクタ
    public Player()
    {
        _fsm.initialize(new IFSMState[] {
            new StayState(State.Stay, this),
        });
    }

    //! @brief  作成
    //! @param  [in]    param   プレイヤー作成情報
    //! @return なし
    public void create(PlayerCreateParam param)
    {
        _model  = param.model;
        _anim   = param.anim;
    }

    //! @brief  初期化
    //! @return なし
    public void initialize()
    {
    }

    //! @brief  更新
    //! @return なし
    public void update()
    {
        _fsm.execute();
    }

    //! @brief  アニメーショントリガーの設定
    //! @param  [in]    id      パラメーターID
    //! @return なし
    private void setAnimTrigger(int id)
    {
        if( _anim == null ) return;

        _anim.SetTrigger(id);
    }

    private GameObject      _model      = null;                 //!< プレイヤーモデル
    private Animator        _anim       = null;                 //!< アニメーション制御

    private FSMSystem       _fsm        = new FSMSystem();      //!< ステートマシン
}

ゲーム的な動作には何も変わりはありませんが、
プレイヤー自身が待機中に何をするのかを意識しなくていい実装になりました

次回は移動ステートを実装して、フィールドを歩き回れるようにしてみます

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1