-
-
Save todorok1/84f7434cd5a71059cff5ba194c782b47 to your computer and use it in GitHub Desktop.
シンプルRPGチュートリアル第62回 操作キャラの移動制御を行うクラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using UnityEngine; | |
| namespace SimpleRpg | |
| { | |
| /// <summary> | |
| /// 操作キャラの移動制御を行うクラスです。 | |
| /// </summary> | |
| public class PlayerMover : CharacterMover | |
| { | |
| /// <summary> | |
| /// 敵キャラクターとのエンカウントを管理するクラスへの参照です。 | |
| /// </summary> | |
| EncounterManager _encounterManager; | |
| void Update() | |
| { | |
| CheckMoveInput(); | |
| } | |
| /// <summary> | |
| /// 参照を取得します。 | |
| /// </summary> | |
| void GetReference() | |
| { | |
| if (_encounterManager == null) | |
| { | |
| _encounterManager = FindAnyObjectByType<EncounterManager>(); | |
| } | |
| } | |
| /// <summary> | |
| /// キー入力を確認します。 | |
| /// </summary> | |
| void CheckMoveInput() | |
| { | |
| // 既に移動中の場合は移動キーの入力を確認せず抜けます。 | |
| if (_isMoving) | |
| { | |
| return; | |
| } | |
| // 移動のポーズフラグがtrueなら処理を抜けます。 | |
| if (_isMovingPaused) | |
| { | |
| return; | |
| } | |
| var moveDirection = Vector2Int.zero; | |
| MoveAnimationDirection animDirection = MoveAnimationDirection.Front; | |
| // 斜め移動は行わないため、上下左右のいずれかを移動対象とします。 | |
| if (Input.GetKey(KeyCode.DownArrow)) | |
| { | |
| moveDirection = Vector2Int.down; | |
| animDirection = MoveAnimationDirection.Front; | |
| } | |
| else if (Input.GetKey(KeyCode.RightArrow)) | |
| { | |
| moveDirection = Vector2Int.right; | |
| animDirection = MoveAnimationDirection.Right; | |
| } | |
| else if (Input.GetKey(KeyCode.UpArrow)) | |
| { | |
| moveDirection = Vector2Int.up; | |
| animDirection = MoveAnimationDirection.Back; | |
| } | |
| else if (Input.GetKey(KeyCode.LeftArrow)) | |
| { | |
| moveDirection = Vector2Int.left; | |
| animDirection = MoveAnimationDirection.Left; | |
| } | |
| MoveCharacter(moveDirection, animDirection); | |
| } | |
| /// <summary> | |
| /// キャラクター移動後の処理です。 | |
| /// </summary> | |
| protected override void PostMove() | |
| { | |
| GetReference(); | |
| if (_encounterManager != null) | |
| { | |
| // エンカウントの確認を行います。 | |
| _encounterManager.CheckEncounter(); | |
| } | |
| else | |
| { | |
| SimpleLogger.Instance.LogError("EncounterManagerが見つかりませんでした。"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment