Merge pull request #75 from progtime-net/PlayerLogic

Player logic
This commit is contained in:
SergoDobro 2024-08-18 23:54:11 +03:00 committed by GitHub
commit df49c509ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 99 additions and 37 deletions

View file

@ -52,6 +52,15 @@ namespace ZoFo.GameCore
InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection,
InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection
});
};
AppManager.Instance.InputManager.OnInteract += () =>
{
networkManager.AddData(new UpdateInputInteraction() { });
};
AppManager.Instance.InputManager.ShootEvent += () =>
{
networkManager.AddData(new UpdateInputShoot() { });
};
}
@ -88,6 +97,7 @@ namespace ZoFo.GameCore
List<GameObject> gameObjects = new List<GameObject>();
List<Player> players = new List<Player>();
List<StopObject> stopObjects = new List<StopObject>();
/// <summary>
/// Клиент должен обнговлять игру анимаций
/// </summary>
@ -99,6 +109,8 @@ namespace ZoFo.GameCore
AppManager.Instance.debugHud.Set("GameTime", gameTime.TotalGameTime.ToString());
gameObjects[i].UpdateAnimations();
}
networkManager.SendData();//set to ticks
}
internal void Draw(SpriteBatch spriteBatch)
{

View file

@ -10,6 +10,7 @@ using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using ZoFo.GameCore.GUI;
namespace ZoFo.GameCore.GameManagers
{
@ -27,9 +28,6 @@ namespace ZoFo.GameCore.GameManagers
private Vector2 prevInputMovementDirection;
public Vector2 InputAttackDirection;
private Vector2 prevInputAttackDirection;
public event Action TalkEvent;
public ScopeState currentScopeState; // Положение оружия. Left, Right, Straight, Back, StraightLeft, StraightRight, BackLeft, BackRight.
private ScopeState prevCurrentScopeState;
private bool _cheatsEnabled = false;
@ -48,6 +46,7 @@ namespace ZoFo.GameCore.GameManagers
public InputManager()
{
isInteract = true;
InputMovementDirection = new Vector2(0, 0);
InputAttackDirection = new Vector2(0, 0);
this.isShoot = false;
@ -173,38 +172,46 @@ namespace ZoFo.GameCore.GameManagers
if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.Straight;
InputMovementDirection = new Vector2(0, -1);
}
else if (keyBoardState.IsKeyDown(Keys.Down) || keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.Back;
InputMovementDirection = new Vector2(0, 1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) || keyBoardState.IsKeyDown(Keys.A))
{
currentScopeState = ScopeState.Left;
InputMovementDirection = new Vector2(-1, 0);
}
else if(keyBoardState.IsKeyDown(Keys.Right) || keyBoardState.IsKeyDown(Keys.D))
{
currentScopeState = ScopeState.Right;
InputMovementDirection = new Vector2(1, 0);
}
else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.StraightRight;
InputMovementDirection = new Vector2(1, 1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.StraightLeft;
InputMovementDirection = new Vector2(-1, 1);
}
else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.BackRight;
InputMovementDirection = new Vector2(1, -1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.BackLeft;
InputMovementDirection = new Vector2(-1, -1);
}
#endregion
@ -247,6 +254,8 @@ namespace ZoFo.GameCore.GameManagers
prevInputAttackDirection = InputAttackDirection;
prevCurrentScopeState = currentScopeState;
#endregion
DebugHUD.Instance.Set("controls", currentScopeState.ToString());
}
}
}

View file

@ -42,6 +42,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public void SendData()
{
for (int i = 0; i < updates.Count; i++)
{
AppManager.Instance.server.ProcessIUpdateData(updates[i]);
}
updates.Clear();
return;// TODO remove
byte[] bytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(updates)); //нужно сериализовать
socket.Send(bytes);
}
@ -98,7 +105,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public static IPAddress GetIp()
{
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
/*string hostName = Dns.GetHostName(); // Retrive the Name of HOST
var ipList = Dns.GetHostByName(hostName).AddressList;
foreach (var ip in ipList)
@ -107,8 +114,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
{
return ip;
}
}
return IPAddress.Loopback;
}*/
return IPAddress.Parse("127.0.0.1");
}
//поток 2

View file

@ -52,17 +52,16 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
/// <returns></returns>
public static IPAddress GetIp()
{
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
var ipList = Dns.GetHostEntry(hostName).AddressList;
/*string hostName = Dns.GetHostName(); // Retrive the Name of HOST
var ipList = Dns.GetHostByName(hostName).AddressList;
foreach (var ip in ipList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return ip;
}
}
return IPAddress.Loopback;
}*/
return IPAddress.Parse("127.0.0.1");
}
/// <summary>

View file

@ -0,0 +1,12 @@
using System;
using ZoFo.GameCore.GameManagers.NetworkManager;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
/// <summary>
/// уведомляет сервер о том, что игрок взаимодействует
/// </summary>
public class UpdateInputInteraction : UpdateData
{
public UpdateInputInteraction() { UpdateType = "UpdateInputInteraction"; }
}

View file

@ -0,0 +1,9 @@
using System;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
public class UpdateInputShoot : UpdateData
{
public UpdateInputShoot() { UpdateType = "UpdateInputShoot"; }
}

View file

@ -2,6 +2,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
/// <summary>
/// При попытке взаимодействия с объектом
/// отправляет пользователю разрешение на взаимодействие
/// TODO: Вероятно убрать(обсудить)
/// </summary>
public class UpdateInteraction : UpdateData
{

View file

@ -18,17 +18,19 @@ public class Player : LivingEntity
/// <summary>
/// Факт того, что плеер в этом апдейте пытается стрелять
/// </summary>
public bool IsTryingToShoot { get; set; }
//public bool IsTryingToShoot { get; set; }
private float speed;
private int health;
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "player_look_down" }, "player_look_down");
private LootData lootData;
//public bool isTryingToInteract { get; set; }
public Player(Vector2 position) : base(position)
{
//InputWeaponRotation = new Vector2(0, 0);
//InputPlayerRotation = new Vector2(0, 0);
graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100);
collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100);
speed = 10;
//isTryingToInteract = false;
//IsTryingToShoot = false;
StartAnimation("player_look_down");
}
@ -39,21 +41,21 @@ public class Player : LivingEntity
MovementLogic();
}
float t;
public void MovementLogic()
{
IsTryingToShoot = true; //gslkjfsnblkjsdfnnlkjbn;zkcjnb;kkjnzx;cjkb;kzjxb;kSErgo
//velocity.X = 3+(float)Math.Sin(t);
t++;
if (InputPlayerRotation.X > 0.9)
{
}
if (Keyboard.GetState().IsKeyDown(Keys.D)) velocity.X += 5;
if (Keyboard.GetState().IsKeyDown(Keys.A)) velocity.X += -5;
if (Keyboard.GetState().IsKeyDown(Keys.S)) velocity.Y += 5;
if (Keyboard.GetState().IsKeyDown(Keys.W)) velocity.Y += -5;
velocity = InputPlayerRotation * speed;
}
public void HandleNewInput(UpdateInput updateInput)
{
InputPlayerRotation = updateInput.InputMovementDirection;
InputWeaponRotation = updateInput.InputAttackDirection;
}
public void HandleInteract(UpdateInputInteraction updateInputInteraction)
{
//isTryingToInteract = true;
}
public void HandleShoot(UpdateInputShoot updateInputShoot)
{
}

View file

@ -12,6 +12,7 @@ using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.MapManager;
using ZoFo.GameCore.GameManagers.NetworkManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameObjects;
using ZoFo.GameCore.GameObjects.Entities;
@ -78,11 +79,17 @@ namespace ZoFo.GameCore
break;
case "UpdatePlayerParametrs":
break;
case "UpdatePosition":
case "UpdateInput":
players[0].HandleNewInput(updateData as UpdateInput);
break;
case "UpdateTileCreated":
break;
case "UpdateInputInteraction":
players[0].HandleInteract(updateData as UpdateInputInteraction);
break;
case "UpdateInputShoot":
players[0].HandleShoot(updateData as UpdateInputShoot);
break;
}
}
@ -131,6 +138,9 @@ namespace ZoFo.GameCore
//AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(0, 0)));
AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(740, 140)));
AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1000, 1000)));
AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1300, 1000)));
AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1500, 1000)));
AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1700, 1000)));
AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(140, 440)));
AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(240, 440)));
}