Added basic AppManager and inputManager

Deleted Game1, because it is AppManager

Added TODOs

TODO:
Uncomment AbstractGUI when merged with GUIBranch
Uncomment server and client when merged with serverBranch
This commit is contained in:
Сухинов Сережа 2024-08-15 02:26:04 +03:00
parent d6c8b11e6f
commit fc18c517fb
4 changed files with 348 additions and 53 deletions

View file

@ -1,52 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ZoFo;
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}

View file

@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using static System.Collections.Specialized.BitVector32;
namespace ZoFo.GameCore.GameManagers
{
public enum GameState { NotPlaying, HostPlaying, ClientPlaying }
public class AppManager : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public static AppManager Instance { get; private set; }
public GameState gamestate;
//public AbstractGUI currentGUI;
//public Client client;
//public Server server;
#region Managers
public InputManager InputManager;
#endregion
public AppManager()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
Instance = this;
InputManager = new InputManager();
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
InputManager.Update();
//currentGUI.Update();
switch (gamestate)
{
case GameState.NotPlaying:
break;
case GameState.HostPlaying:
//client.Update(GameTime gameTime);
break;
case GameState.ClientPlaying:
//server.Update(GameTime gameTime);
break;
default:
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
//currentGUI.Draw(_spriteBatch);
switch (gamestate)
{
case GameState.ClientPlaying:
case GameState.HostPlaying:
//client.Draw(_spriteBatch);
break;
case GameState.NotPlaying:
default:
break;
}
base.Draw(gameTime);
}
public void ChangeState(GameState gameState)
{
this.gamestate = gameState;
}
public void SetGUI(/*AbstractGUI gui*/)
{
//currentGUI = gui
//TODO
}
public void GameEnded(Dictionary<string, int> lootIGot)
{
//TODO
}
}
}

View file

@ -0,0 +1,229 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZoFo.GameCore.GameManagers
{
public enum ScopeState { Up, Middle, Down }
public enum ControlsState { Gamepad, Keyboard }
public class InputManager
{
public delegate void Delegat();
public event Delegat MovEventJump;
public event Delegat MovEventDown;
public event Delegat ShootEvent;
Vector2 vectorMovementDirection;
ScopeState scopeState; // Положение оружия. Up, Middle, Down.
ControlsState controlsState;
private bool _overrideControls = false;
private bool _cheatsEnabled = false;
public bool InvincibilityCheat { get; private set; } = false;
public bool CollisionsCheat { get; private set; } = false;
public bool InfiniteAmmoCheat { get; private set; } = false;
private bool isJumpDown; // Блокирует физическое нажатие прыжка и спуска
private bool isShoot;
private KeyboardState lastKeyboardState;
private GamePadState lastGamePadState;
public Vector2 VectorMovementDirection { get => vectorMovementDirection; }
public ScopeState ScopeState { get => scopeState; }
public string currentControlsState;
public InputManager()
{
this.isJumpDown = false;
this.isShoot = false;
scopeState = ScopeState.Middle;
controlsState = ControlsState.Keyboard;
vectorMovementDirection = new Vector2(0, 0);
}
public void Update()
{
if (_cheatsEnabled)
{
//AppManager.Instance.DebugHUD.Set("cheats", _cheatsEnabled.ToString());
//AppManager.Instance.DebugHUD.Set("invincible", InvincibilityCheat.ToString());
//AppManager.Instance.DebugHUD.Set("infinite ammo", InfiniteAmmoCheat.ToString()); //TODO
}
#region Работа с GamePad
if (_overrideControls ? controlsState == ControlsState.Gamepad : GamePad.GetState(0).IsConnected)
{
controlsState = ControlsState.Gamepad;
#region Обработка гейм-пада. Задает Vector2 vectorMovementDirection являющийся вектором отклонения левого стика.
GamePadState gamePadState = GamePad.GetState(0);
vectorMovementDirection = gamePadState.ThumbSticks.Left;
#endregion
#region читы
if (gamePadState.Triggers.Left >= 0.9 && gamePadState.Triggers.Right >= 0.9)
_cheatsEnabled = true;
if (_cheatsEnabled)
{
if (gamePadState.Buttons.Y == ButtonState.Pressed && lastGamePadState.Buttons.Y == ButtonState.Released)
InvincibilityCheat = !InvincibilityCheat;
if (gamePadState.Buttons.B == ButtonState.Pressed && lastGamePadState.Buttons.B == ButtonState.Released)
CollisionsCheat = !CollisionsCheat;
//TODO: infinite ammo cheat by gamepad
}
#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.
if (vectorMovementDirection.Y >= 0.7)
{
scopeState = ScopeState.Up;
}
else if (vectorMovementDirection.Y <= -0.7 && !isJumpDown)
{
scopeState = ScopeState.Down;
}
else
{
scopeState = ScopeState.Middle;
}
#endregion
#region Обработка нажатия выстрела. Вызывает событие ShootEvent
if (gamePadState.Buttons.X == ButtonState.Pressed && !isJumpDown && !isShoot)
{
isShoot = true;
ShootEvent?.Invoke();
Debug.WriteLine("Выстрел");
}
else if (gamePadState.Buttons.X == ButtonState.Released && !isJumpDown)
{
isShoot = false;
}
#endregion
lastGamePadState = gamePadState;
}
#endregion
#region Работа с KeyBoard
else
{
controlsState = ControlsState.Keyboard;
#region Состояние клавиатуры
KeyboardState keyBoardState = Keyboard.GetState(); // Состояние клавиатуры
#endregion
#region читы
if (keyBoardState.IsKeyDown(Keys.LeftShift) && keyBoardState.IsKeyDown(Keys.RightShift))
_cheatsEnabled = true;
if (_cheatsEnabled)
{
if (keyBoardState.IsKeyDown(Keys.I) && lastKeyboardState.IsKeyUp(Keys.I))
InvincibilityCheat = !InvincibilityCheat;
if (keyBoardState.IsKeyDown(Keys.C) && lastKeyboardState.IsKeyUp(Keys.C))
CollisionsCheat = !CollisionsCheat;
if (keyBoardState.IsKeyDown(Keys.N) && lastKeyboardState.IsKeyUp(Keys.N))
InfiniteAmmoCheat = !InfiniteAmmoCheat;
List<Keys> lvls = new List<Keys>() { Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9 };
for (int i = 0; i < lvls.Count; i++)
{
//if (keyBoardState.IsKeyDown(lvls[i]) && lastKeyboardState.IsKeyUp(lvls[i])) //TODO
// AppManager.Instance.Restart($"lvl{i}");
}
}
#endregion // Cheats
#region Обработка движения вправо-влево. Меняет у вектора vectorMovementDirection значение X на -1/0/1.
if (keyBoardState.IsKeyDown(Keys.Left))
{
vectorMovementDirection.X = -1;
}
else if (keyBoardState.IsKeyDown(Keys.Right))
{
vectorMovementDirection.X = 1;
}
else
{
vectorMovementDirection.X = 0;
}
#endregion
#region Обработка прыжка и спуска. Вызываются события MovEvent.
if (keyBoardState.IsKeyDown(Keys.LeftShift) && !isJumpDown && keyBoardState.IsKeyDown(Keys.Down))
{
isJumpDown = true;
MovEventDown?.Invoke();
Debug.WriteLine("Спуск");
}
else if (keyBoardState.IsKeyDown(Keys.LeftShift) && !isJumpDown)
{
isJumpDown = true;
MovEventJump?.Invoke();
Debug.WriteLine("Прыжок");
}
else if (keyBoardState.IsKeyUp(Keys.LeftShift))
{
isJumpDown = false;
}
#endregion
#region Обработка положения оружия. Задает значение полю scopeState.
if (keyBoardState.IsKeyDown(Keys.Up))
{
scopeState = ScopeState.Up;
}
else if (keyBoardState.IsKeyDown(Keys.Down) && !isJumpDown)
{
scopeState = ScopeState.Down;
}
else
{
scopeState = ScopeState.Middle;
}
#endregion
#region Обработка нажатия выстрела. Вызывает событие ShootEvent
if (keyBoardState.IsKeyDown(Keys.X) && !isJumpDown && !isShoot)
{
isShoot = true;
ShootEvent?.Invoke();
Debug.WriteLine("Выстрел");
}
else if (keyBoardState.IsKeyUp(Keys.X) && !isJumpDown)
{
isShoot = false;
}
#endregion
SetState(ControlsState.Keyboard);
lastKeyboardState = keyBoardState;
}
#endregion
}
public void SetState(ControlsState controlsState)
{
currentControlsState = controlsState.ToString();
}
}
}

View file

@ -1,2 +1,2 @@
using var game = new ZoFo.Game1(); using var game = new ZoFo.GameCore.GameManagers.AppManager();
game.Run(); game.Run();