This commit is contained in:
AnloGames 2023-08-14 18:49:32 +03:00
commit 293ad8f0e1
3 changed files with 160 additions and 5 deletions

View file

@ -13,15 +13,15 @@ namespace DangerousD.GameCore.Graphics
private List<Texture2D> textures;
private List<string> texturesNames;
private AnimationContainer currentAnimation;
private SpriteBatch _spriteBatch;
//private SpriteBatch _spriteBatch;
private string lastAnimationId;
private int currentFrame;
private int interval;
private int lastInterval;
private Rectangle sourceRectangle;
public GraphicsComponent(List<string> animationsId, SpriteBatch _spriteBatch)
public GraphicsComponent(List<string> animationsId)
{
this._spriteBatch = _spriteBatch;
//this._spriteBatch = _spriteBatch;
currentFrame = 0;
lastInterval = 1;
lastAnimationId = null;
@ -52,7 +52,7 @@ namespace DangerousD.GameCore.Graphics
}
}
}
public void DrawAnimation(Rectangle destinationRectangle, string animationId)
public void DrawAnimation(Rectangle destinationRectangle, string animationId, SpriteBatch _spriteBatch)
{
if (animationId != lastAnimationId)

View file

@ -0,0 +1,156 @@
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.InteropServices.WindowsRuntime;
namespace DangerousD.GameCore
{
public enum ScopeState { Up, Middle, Down }
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.
private bool isJumpDown; // Блокирует физическое нажатие прыжка и спуска
private bool isShoot;
public Vector2 VectorMovementDirection { get => vectorMovementDirection; }
public ScopeState ScopeState { get => scopeState; }
public InputManager()
{
this.isJumpDown = false;
this.isShoot = false;
scopeState = ScopeState.Middle;
vectorMovementDirection = new Vector2(0, 0);
}
public void Update()
{
// Работа с GamePad
if (GamePad.GetState(0).IsConnected)
{
// Обработка гейм-пада. Задает Vector2 vectorMovementDirection являющийся вектором отклонения левого стика.
GamePadState gamePadState = GamePad.GetState(0);
vectorMovementDirection = gamePadState.ThumbSticks.Left;
// Обработка нажатия прыжка и спуска. Вызывает события 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 && !isJumpDown)
{
isJumpDown = true;
MovEventJump?.Invoke();
Debug.WriteLine("Прыжок");
}
else if (gamePadState.Buttons.A == ButtonState.Released)
{
isJumpDown = false;
}
// Обработка положения оружия. Задает значение полю scopeState.
if (vectorMovementDirection.Y >= 0.7)
{
scopeState = ScopeState.Up;
}
else if (vectorMovementDirection.Y <= -0.7 && !isJumpDown)
{
scopeState = ScopeState.Down;
}
else
{
scopeState = ScopeState.Middle;
}
// Обработка нажатия выстрела. Вызывает событие 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;
}
}
// Работа с KeyBoard
else
{
KeyboardState keyBoardState = Keyboard.GetState(); // Состояние клавиатуры
// Обработка движения вправо-влево. Меняет у вектора 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;
}
// Обработка прыжка и спуска. Вызываются события 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;
}
// Обработка положения оружия. Задает значение полю scopeState.
if (keyBoardState.IsKeyDown(Keys.Up))
{
scopeState = ScopeState.Up;
}
else if (keyBoardState.IsKeyDown(Keys.Down) && !isJumpDown)
{
scopeState = ScopeState.Down;
}
else
{
scopeState = ScopeState.Middle;
}
// Обработка нажатия выстрела. Вызывает событие ShootEvent
if (keyBoardState.IsKeyDown(Keys.X) && !isJumpDown && !isShoot)
{
isShoot = true;
ShootEvent?.Invoke();
Debug.WriteLine("Выстрел");
}
else if (keyBoardState.IsKeyUp(Keys.X) && !isJumpDown)
{
isShoot = false;
}
}
}
}
}

View file

@ -46,7 +46,6 @@ namespace DangerousD.GameCore
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
switch (gameState)
{
case GameState.Menu: