upgrade InputManager

This commit is contained in:
Lev 2024-08-16 13:05:49 +03:00
parent a8492abe60
commit ba757265f2
2 changed files with 81 additions and 88 deletions

View file

@ -114,9 +114,8 @@ namespace ZoFo.GameCore.GameManagers
GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.Clear(Color.CornflowerBlue);
debugHud.Draw(_spriteBatch);
currentGUI.Draw(_spriteBatch); currentGUI.Draw(_spriteBatch);
debugHud.Draw(_spriteBatch);
switch (gamestate) switch (gamestate)
{ {
case GameState.ClientPlaying: case GameState.ClientPlaying:

View file

@ -3,63 +3,59 @@ using Microsoft.Xna.Framework.Input;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Formats.Tar;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ZoFo.GameCore.GameManagers namespace ZoFo.GameCore.GameManagers
{ {
public enum ScopeState { Up, Middle, Down } public enum ScopeState { Left, Right, Straight, Back, StraightLeft, StraightRight, BackLeft, BackRight }
public enum ControlsState { Gamepad, Keyboard }
public class InputManager public class InputManager
{ {
public delegate void Delegat(); public delegate void Delegat();
public event Delegat MovEventJump; public event Delegat ShootEvent; // событие удара(когда нажат X, событие срабатывает)
public event Delegat MovEventDown;
public event Delegat ShootEvent; public event Delegat OnInteract; // событие взаимодействия с collectable(например, лутом)
//с помощью кнопки E.
public event Delegat TalkEvent;
Vector2 vectorMovementDirection; Vector2 vectorMovementDirection;
ScopeState scopeState; // Положение оружия. Up, Middle, Down. ScopeState currentScopeState; // Положение оружия. Left, Right, Straight, Back, StraightLeft, StraightRight, BackLeft, BackRight.
ControlsState controlsState;
private bool _overrideControls = false;
private bool _cheatsEnabled = false; private bool _cheatsEnabled = false;
public bool InvincibilityCheat { get; private set; } = false; public bool InvincibilityCheat { get; private set; } = false;
public bool CollisionsCheat { get; private set; } = false; public bool CollisionsCheat { get; private set; } = false;
public bool InfiniteAmmoCheat { get; private set; } = false; public bool InfiniteAmmoCheat { get; private set; } = false;
private bool isJumpDown; // Блокирует физическое нажатие прыжка и спуска
private bool isShoot; private bool isShoot;
private bool isInteract;
private KeyboardState lastKeyboardState; private KeyboardState lastKeyboardState;
private GamePadState lastGamePadState; private GamePadState lastGamePadState;
public Vector2 VectorMovementDirection { get => vectorMovementDirection; } public Vector2 VectorMovementDirection { get => vectorMovementDirection; }
public ScopeState ScopeState { get => scopeState; } public ScopeState ScopeState { get => currentScopeState; }
public string currentControlsState; public string currentControlsState;
public ScopeState CurrentScopeState { get => currentScopeState; } // получить текущее состояние
public InputManager() public InputManager()
{ {
this.isJumpDown = false;
this.isShoot = false; this.isShoot = false;
scopeState = ScopeState.Middle; currentScopeState = ScopeState.Straight;
controlsState = ControlsState.Keyboard;
vectorMovementDirection = new Vector2(0, 0); vectorMovementDirection = new Vector2(0, 0);
} }
public void Update() public void Update()
{ {
if (_cheatsEnabled) if (_cheatsEnabled)
{ {
//AppManager.Instance.DebugHUD.Set("cheats", _cheatsEnabled.ToString()); AppManager.Instance.debugHud.Set("cheats", _cheatsEnabled.ToString());
//AppManager.Instance.DebugHUD.Set("invincible", InvincibilityCheat.ToString()); AppManager.Instance.debugHud.Set("invincible", InvincibilityCheat.ToString());
//AppManager.Instance.DebugHUD.Set("infinite ammo", InfiniteAmmoCheat.ToString()); //TODO AppManager.Instance.debugHud.Set("infinite ammo", InfiniteAmmoCheat.ToString()); //TODO
} }
#region Работа с GamePad #region Работа с GamePad
if (_overrideControls ? controlsState == ControlsState.Gamepad : GamePad.GetState(0).IsConnected)
{
controlsState = ControlsState.Gamepad;
#region Обработка гейм-пада. Задает Vector2 vectorMovementDirection являющийся вектором отклонения левого стика. #region Обработка гейм-пада. Задает Vector2 vectorMovementDirection являющийся вектором отклонения левого стика.
GamePadState gamePadState = GamePad.GetState(0); GamePadState gamePadState = GamePad.GetState(0);
vectorMovementDirection = gamePadState.ThumbSticks.Left; vectorMovementDirection = gamePadState.ThumbSticks.Left;
@ -78,56 +74,57 @@ namespace ZoFo.GameCore.GameManagers
} }
#endregion // Cheats #endregion // Cheats
#region Обработка нажатия прыжка и спуска. Вызывает события MovEvent.
if (vectorMovementDirection.Y < -0.2 && gamePadState.Buttons.A == ButtonState.Pressed && !isJumpDown)
{
isJumpDown = true;
MovEventDown?.Invoke();
Debug.WriteLine("Спуск");
}
else if (gamePadState.Buttons.A == ButtonState.Pressed && lastGamePadState.Buttons.A == ButtonState.Released)
{
MovEventJump?.Invoke();
Debug.WriteLine("Прыжок");
}
#endregion
#region Обработка положения оружия. Задает значение полю scopeState. #region Обработка положения оружия. Задает значение полю scopeState.
if (vectorMovementDirection.Y >= 0.7) if (vectorMovementDirection.Y >= 0.6)
{ {
scopeState = ScopeState.Up; currentScopeState = ScopeState.Straight;
} }
else if (vectorMovementDirection.Y <= -0.7 && !isJumpDown) else if(vectorMovementDirection.Y <= 0.6)
{ {
scopeState = ScopeState.Down; currentScopeState = ScopeState.Back;
} }
else else if(vectorMovementDirection.X >= 0.6)
{ {
scopeState = ScopeState.Middle; currentScopeState = ScopeState.Right;
}
else if(vectorMovementDirection.X <= 0.6)
{
currentScopeState = ScopeState.Left;
}
else if(vectorMovementDirection.Y >= 0.6 && vectorMovementDirection.X >= 0.6)
{
currentScopeState = ScopeState.StraightRight;
}
else if(vectorMovementDirection.Y >= 0.6 && vectorMovementDirection.X <= 0.6)
{
currentScopeState = ScopeState.StraightLeft;
}
else if(vectorMovementDirection.Y <= 0.6 && vectorMovementDirection.X >= 0.6)
{
currentScopeState = ScopeState.BackRight;
}
else if(vectorMovementDirection.Y <= 0.6 && vectorMovementDirection.X <= 0.6)
{
currentScopeState = ScopeState.BackLeft;
} }
#endregion #endregion
#region Обработка нажатия выстрела. Вызывает событие ShootEvent #region Обработка нажатия выстрела. Вызывает событие ShootEvent
if (gamePadState.Buttons.X == ButtonState.Pressed && !isJumpDown && !isShoot) if (gamePadState.Buttons.X == ButtonState.Pressed && !isShoot)
{ {
isShoot = true; isShoot = true;
ShootEvent?.Invoke(); ShootEvent?.Invoke();
Debug.WriteLine("Выстрел"); Debug.WriteLine("Выстрел");
} }
else if (gamePadState.Buttons.X == ButtonState.Released && !isJumpDown) else if (gamePadState.Buttons.X == ButtonState.Released)
{ {
isShoot = false; isShoot = false;
} }
#endregion #endregion
lastGamePadState = gamePadState; lastGamePadState = gamePadState;
}
#endregion #endregion
#region Работа с KeyBoard #region Работа с KeyBoard
else
{
controlsState = ControlsState.Keyboard;
#region Состояние клавиатуры #region Состояние клавиатуры
KeyboardState keyBoardState = Keyboard.GetState(); // Состояние клавиатуры KeyboardState keyBoardState = Keyboard.GetState(); // Состояние клавиатуры
#endregion #endregion
@ -154,76 +151,73 @@ namespace ZoFo.GameCore.GameManagers
} }
#endregion // Cheats #endregion // Cheats
#region Обработка движения вправо-влево. Меняет у вектора vectorMovementDirection значение X на -1/0/1. #region Обработка состояния объекта. Задает значение полю scopeState.
if (keyBoardState.IsKeyDown(Keys.Left)) if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W))
{ {
vectorMovementDirection.X = -1; currentScopeState = ScopeState.Straight;
} }
else if (keyBoardState.IsKeyDown(Keys.Right)) else if (keyBoardState.IsKeyDown(Keys.Down) || keyBoardState.IsKeyDown(Keys.S))
{ {
vectorMovementDirection.X = 1; currentScopeState = ScopeState.Back;
} }
else else if(keyBoardState.IsKeyDown(Keys.Left) || keyBoardState.IsKeyDown(Keys.A))
{ {
vectorMovementDirection.X = 0; currentScopeState = ScopeState.Left;
} }
#endregion else if(keyBoardState.IsKeyDown(Keys.Right) || keyBoardState.IsKeyDown(Keys.D))
#region Обработка прыжка и спуска. Вызываются события MovEvent.
if (keyBoardState.IsKeyDown(Keys.LeftShift) && !isJumpDown && keyBoardState.IsKeyDown(Keys.Down))
{ {
isJumpDown = true; currentScopeState = ScopeState.Right;
MovEventDown?.Invoke();
Debug.WriteLine("Спуск");
} }
else if (keyBoardState.IsKeyDown(Keys.LeftShift) && !isJumpDown) else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.W))
{ {
isJumpDown = true; currentScopeState = ScopeState.StraightRight;
MovEventJump?.Invoke();
Debug.WriteLine("Прыжок");
} }
else if (keyBoardState.IsKeyUp(Keys.LeftShift)) else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.W))
{ {
isJumpDown = false; currentScopeState = ScopeState.StraightLeft;
} }
#endregion else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.S))
#region Обработка положения оружия. Задает значение полю scopeState.
if (keyBoardState.IsKeyDown(Keys.Up))
{ {
scopeState = ScopeState.Up; currentScopeState = ScopeState.BackRight;
} }
else if (keyBoardState.IsKeyDown(Keys.Down) && !isJumpDown) else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.S))
{ {
scopeState = ScopeState.Down; currentScopeState = ScopeState.BackLeft;
}
else
{
scopeState = ScopeState.Middle;
} }
#endregion #endregion
#region Обработка нажатия выстрела. Вызывает событие ShootEvent #region Обработка нажатия выстрела. Вызывает событие ShootEvent
if (keyBoardState.IsKeyDown(Keys.X) && !isJumpDown && !isShoot) if (keyBoardState.IsKeyDown(Keys.P) && !isShoot)
{ {
isShoot = true; isShoot = true;
ShootEvent?.Invoke(); ShootEvent?.Invoke();
Debug.WriteLine("Выстрел"); Debug.WriteLine("Выстрел");
} }
else if (keyBoardState.IsKeyUp(Keys.X) && !isJumpDown) else if (keyBoardState.IsKeyUp(Keys.P))
{ {
isShoot = false; isShoot = false;
} }
#endregion #endregion
SetState(ControlsState.Keyboard); #region Обработка взаимодействия с collectable(например лутом). Вызывает событие OnInteract
lastKeyboardState = keyBoardState; if (keyBoardState.IsKeyDown(Keys.E) && !isInteract)
{
OnInteract?.Invoke();
Debug.WriteLine("взаимодействие с Collectable");
}
else if (keyBoardState.IsKeyUp(Keys.E))
{
isInteract = false;
} }
#endregion #endregion
} lastKeyboardState = keyBoardState;
public void SetState(ControlsState controlsState)
{ #endregion
currentControlsState = controlsState.ToString();
} }
} }
} }