OpenSiv3D icon indicating copy to clipboard operation
OpenSiv3D copied to clipboard

UnrealEngineのようなInputAxis機能の実装

Open yunba28 opened this issue 2 years ago • 2 comments

追加する機能の内容 | Describe the solution you'd like

事前にハッシュテーブルに名前付きのInputAxisを登録することで入力関連の処理を簡易的かつ直感的にします。

その機能の追加によって解決する問題 | Is your feature request related to a problem? Please describe.

ゲーム制作にかかわらずアプリケーションに必須な入力処理をプログラマが理解できやすい形でコーディングすることが可能になります。特にプレイヤーの移動やカメラ操作、カメラコンフィグ等では役に立ちます。

備考 | Additional context

簡易実装

class InputAxis_impl final
{
public:

    struct State
    {
        Input input;
        double scale;
        bool operator==(const State& other)const noexcept 
        {
            return (input == other.input) && (scale == other.scale); 
        }
    };

    InputAxis_impl() = default;

    InputAxis_impl& add(const Input& input, double scale)
    {
        State state{ input,scale };
        if(auto found = std::ranges::find(m_states, state); found == m_states.end())
        {
            m_states << state;
        }
        return *this;
    }

    void remove(const Input& input, double scale)
    {
        m_states.remove({ input,scale });
    }

    double value()const
    {
        double value{ 0 };
        for(auto& state : m_states)
        {
            if(state.input.pressed())
            {
                value += state.scale;
            }
        }
        return value;
    }

private:

    Array<State> m_states;

};

inline HashTable<String, InputAxis_impl> InputAxis;

使用例

void SetupInputAxis()
{
    InputAxis[U"MoveX"]
        .add(KeyA, -1)
        .add(KeyLeft, -1)
        .add(KeyD, 1)
        .add(KeyRight, 1);

    InputAxis[U"MoveY"]
        .add(KeyW, -1)
        .add(KeyUp, -1)
        .add(KeyS, 1)
        .add(KeyDown, 1);
}

void MovePlayer(Circle& body)
{
    Vec2 axis{ 0,0 };
    {
        axis.x = InputAxis[U"MoveX"].value();
        axis.y = InputAxis[U"MoveY"].value();
    }

    if(axis.isZero())
        return;

    axis.normalize();

    body.center.moveBy(axis * 200.0 * Scene::DeltaTime());
}

void Main()
{
    Circle player{ 400,300,10 };
    
    SetupInputAxis();
    
    while(System::Update())
    {
        MovePlayer(player);
        player.draw();
    }
}

yunba28 avatar Nov 14 '22 00:11 yunba28

既存の機能を使うと次のようなコードでも書けるので、差別化が難しい気がします。

# include <Siv3D.hpp> // OpenSiv3D v0.6.5

const auto InputMoveLeft = (KeyA | KeyLeft);
const auto InputMoveRight = (KeyD | KeyRight);
const auto InputMoveUp = (KeyW | KeyUp);
const auto InputMoveDown = (KeyS | KeyDown);

void Main()
{
	Circle player{ 400, 300, 10 };

	while (System::Update())
	{
		const Vec2 move = Vec2{ (InputMoveRight.pressed() - InputMoveLeft.pressed()),
			(InputMoveDown.pressed() - InputMoveUp.pressed()) }.setLength(200 * Scene::DeltaTime());

		player.moveBy(move);

		player.draw();
	}
}

参考

  • https://zenn.dev/reputeless/books/siv3d-documentation/viewer/tutorial-keyboard#16.6-%E3%82%AD%E3%83%BC%E3%82%B3%E3%83%B3%E3%83%95%E3%82%A3%E3%82%B0

Reputeless avatar Nov 14 '22 00:11 Reputeless

お返事ありがとうございます。

今回提案したクラスの強みはどちらかというと視覚的なわかりやすさと、txtデータからでもある程度の編集が可能ということです。 そういう意味で言うとKeyConfigクラスとも言えます。

確かにサンプルコードのような書き方もありますが、bool型を暗黙の変換で整数型に変換するのも不安がありますし、冗長な気もします。

初心者の方が慣れない中で試行錯誤する手助けにもなると思いますので、気が向いたらもう一度検討していただけないでしょうか?

2022年11月14日(月) 9:50 Ryo Suzuki @.***>:

既存の機能を使うと次のようなコードでも書けるので、差別化が難しい気がします。

include <Siv3D.hpp> // OpenSiv3D v0.6.5

const auto InputMoveLeft = (KeyA | KeyLeft); const auto InputMoveRight = (KeyD | KeyRight); const auto InputMoveUp = (KeyW | KeyUp); const auto InputMoveDown = (KeyS | KeyDown);

void Main()

{

Circle player{ 400, 300, 10 };

while (System::Update())

{

  const Vec2 move = Vec2{ (InputMoveRight.pressed() - InputMoveLeft.pressed()),

  	(InputMoveDown.pressed() - InputMoveUp.pressed()) }.setLength(200 * Scene::DeltaTime());



  player.moveBy(move);



  player.draw();

}

}

参考

https://zenn.dev/reputeless/books/siv3d-documentation/viewer/tutorial-keyboard#16.6-%E3%82%AD%E3%83%BC%E3%82%B3%E3%83%B3%E3%83%95%E3%82%A3%E3%82%B0

— Reply to this email directly, view it on GitHub https://github.com/Siv3D/OpenSiv3D/issues/912#issuecomment-1312894379, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQZ3F3PWTSFC5B5X5NL3K5LWIGEEFANCNFSM6AAAAAAR7JK36E . You are receiving this because you authored the thread.Message ID: @.***>

yunba28 avatar Nov 15 '22 04:11 yunba28