ユーザーがオブジェクトをグリッドに配置してゲームのレベルを作成できるようにする C#/Winforms のアプリケーションがあります。タイル/ライト/ドア/エンティティなどを配置するためのツールがいくつかあります。現在、現在選択されているツールを格納するために列挙型を使用し、各ツール コードを実行するための switch ステートメントを使用しています。アプリケーションにツールを追加していると、コードが重複してスパゲッティのようになり始めています。
これは、私のエディター クラスのマウス ダウン機能のカットダウン バージョンです。
public void OnEditorViewMouseDown(Point mousePos)
{
// Check if the click is out of bounds.
if (IsLocationOOB(mousePos)) return;
if (CurrentTool == ToolType.GroundTile)
{
// Allow drags along whole tiles only.
m_DragManager.DragType = DragManager.DragTypeEnum.Tile;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.WallTile)
{
// Allow drags along grid edges only.
m_DragManager.DragType = DragManager.DragTypeEnum.Edge;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.PostTile)
{
// Allow drags along grid points only.
m_DragManager.DragType = DragManager.DragTypeEnum.Point;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.AreaLight)
{
// Allow drags anywhere. ie. not snapped to the grid in some way.
m_DragManager.DragType = DragManager.DragTypeEnum.FreeForm;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.PointLight)
{
m_CurrentWorld.AddLight(TranslateToWorldCoords(mousePos));
}
else if (CurrentTool == ToolType.PlaceEntity)
{
m_CurrentWorld.PlaceEntity(TranslateToWorldCoords(mousePos));
}
}
スイッチは他のいくつかの機能 (OnMouseMove、OnMouseUp) で使用されており、設計が悪いようです (いくつかの機能で大きなスイッチがコピーされています)。このようなものをよりクリーンで拡張可能な方法でリファクタリングするためのアドバイスはありますか? 私は現在、基本Tool
クラスを持ち、各ツールが使用する関数 (OnMouseDown() など) をオーバーライドする独自のクラスを持つことを考えています。これは理にかなっていますか?
読んでくれてありがとう。