저는 .NET으로 비디오 게임을 개발하고 있습니다. 그러나 명령 대기열을 올바르게 구현 한 다음 즉시 실행하는 방법에 대해 고심하고 있습니다.명령 패턴 및 키 바인딩을 구현하는 방법은 무엇입니까?
내 비디오 게임은 간단합니다. 움직이는 항공기입니다. 어떻게 제대로 명령 패턴을 구현할 수
public abstract class ICommand {
Category CategoryProperty { get; set; }
public abstract void Execute();
}
public class MoveAircraftCommand : ICommand
{
private Vector2f _velocity;
Aircraft aircraft;
public Category CategoryProperty {
get {
return Category.PlayerAircraft;
}
}
public MoveAircraftCommand (float vx, float vy, Aircraft v_aircraft) {
_velocity = new Vector2f(vx, vy);
aircraft = v_aircraft;
}
public override void Execute()
{
aircraft.Accelerate(_velocity);
}
}
//Then, There is the Player class that binds keys to actions, and actions to Commands.
public class Player
{
public enum ActionMove {
MoveLeft,
MoveRight,
MoveUp,
MoveDown,
ActionCount
}
private IDictionary<Keyboard.Key, ActionMove> _keyBinding;
private IDictionary<ActionMove,ICommand> _actionBinding;
public Player()
{
_keyBinding = new Dictionary<Keyboard.Key, ActionMove>();
_keyBinding.Add(Keyboard.Key.Left,ActionMove.MoveLeft);
_keyBinding.Add(Keyboard.Key.Right,ActionMove.MoveRight);
_keyBinding.Add(Keyboard.Key.Up,ActionMove.MoveUp);
_keyBinding.Add(Keyboard.Key.Down,ActionMove.MoveDown);
/** Dunno how to bind the actions to commands without instantiating the command, Hard-Coding the parameters at start. Also Yet I don't have instantiated the aircraft object**/
float playerSpeed = 200f;
_actionBinding.Add(ActionMove.MoveRight,new MoveAircraftCommand(+playerSpeed,0f,aircraft));
_actionBinding.Add(ActionMove.MoveUp,new MoveAircraftCommand(0f,-playerSpeed, aircraft));
_actionBinding.Add(ActionMove.MoveDown,new MoveAircraftCommand(0f,+playerSpeed,aircraft));
/** **/
/**This function pushes the Commands to a queue, in order to process them in order at once**/
public void HandleRealTimeInput(CommandQueue commands) {
foreach (KeyValuePair<Keyboard.Key,ActionMove> entry in _keyBinding) {
if (Keyboard.IsKeyPressed(entry.Key) && isRealTimeAction(entry.Value)) {
commands.Push(_factory.GetCommand(_keyBinding[entry.Key]));
}
}
}
을 그들이 때 제대로 모든 매개 변수가이 명령을 인스턴스화 : 명령 패턴의 내 구현은 내가 Player 클래스에이 명령의 관리를 구현, 다음과 같다 필수?
는
답장을 보내 주셔서 감사합니다. 그러나 앞으로 더 많은 명령을 추가하려면 어떻게해야합니까? Player 클래스가 너무 크고 다른 구성 요소와 결합되어 있을까요? –
@ DavidJiménezMartínez 가장 간단한 해결책은 생성자를 통해 플레이어 클래스에 사전 인스턴스를 제공하는 것입니다. 그렇게하면 명령과 바인딩을 다른 곳에 설치할 수 있으며 플레이어 클래스를 결합 할 필요가 없습니다. 그러면 조롱 된 명령을 제공 할 수있을 때 단위 테스트가 더 쉬워집니다. –
@ DavidJiménezMartínez 그렇다면, 클로저 동작의 이점 (즉, 항공기의 현재 가치와 명령에 대한 플레이어 위치의 전달)을 잃어 버리면 공공 게터를 통해 액세스 할 수있게됩니다. –