GLOBAL NETWORK FIX

This commit is contained in:
AnloGames 2023-08-18 00:03:01 +03:00
parent dd4a5c7fb2
commit 3ec1fa3ab8
8 changed files with 88 additions and 54 deletions

View file

@ -14,6 +14,7 @@ namespace DangerousD.GameCore
{
protected Vector2 _pos;
public Vector2 Pos => _pos;
public int id;
public int Width { get; set; }
public int Height { get; set; }
public Rectangle Rectangle => new Rectangle((int)Pos.X, (int)Pos.Y, Width, Height);
@ -38,10 +39,14 @@ namespace DangerousD.GameCore
{
}
public void LoadContent()
public void PlayAnimation()
{
GraphicsComponent.LoadContent();
}
public void LoadContent()
{
PlayAnimation();
}
public virtual void Update(GameTime gameTime)
{

View file

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using DangerousD.GameCore.GameObjects.PlayerDeath;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using DangerousD.GameCore.Network;
namespace DangerousD.GameCore.GameObjects.LivingEntities
{
@ -51,10 +52,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
}
public override void OnCollision(GameObject gameObject)
{
if (gameObject is Player)
{
isVisible = false;
}
base.OnCollision(gameObject);
}
public override void Draw(SpriteBatch spriteBatch)
@ -124,6 +121,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
GraphicsComponent.StartAnimation("ZombieMoveLeft");
}
}
if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.SinglePlayer)
{
NetworkTask task = new NetworkTask(id, Pos);
AppManager.Instance.NetworkTasks.Add(task);
}
}
public void MoveDown()
{

View file

@ -10,6 +10,8 @@ using DangerousD.GameCore.Graphics;
using DangerousD.GameCore.Network;
using MonogameLibrary.UI.Base;
using DangerousD.GameCore.Managers;
using DangerousD.GameCore.GameObjects.LivingEntities;
using DangerousD.GameCore.GameObjects;
namespace DangerousD.GameCore
{
@ -31,6 +33,7 @@ namespace DangerousD.GameCore
IDrawableObject LobbyGUI;
IDrawableObject DeathGUI;
public DebugHUD DebugHUD;
public List<NetworkTask> NetworkTasks = new List<NetworkTask>();
public GameManager GameManager { get; private set; } = new();
public AnimationBuilder AnimationBuilder { get; private set; } = new AnimationBuilder();
@ -56,7 +59,7 @@ namespace DangerousD.GameCore
resolution = SettingsManager.Resolution;
_graphics.PreferredBackBufferWidth = resolution.X;
_graphics.PreferredBackBufferHeight = resolution.Y;
_graphics.IsFullScreen = true;
_graphics.IsFullScreen = false;
gameState = GameState.Menu;
MenuGUI = new MenuGUI();
LoginGUI = new LoginGUI();
@ -211,18 +214,29 @@ namespace DangerousD.GameCore
case NetworkTaskOperationEnum.CreateEntity:
break;
case NetworkTaskOperationEnum.SendPosition:
LivingEntity entity = GameManager.livingEntities.Find(x => x.id == networkTask.objId);
entity.SetPosition(networkTask.position);
break;
case NetworkTaskOperationEnum.ChangeState:
break;
case NetworkTaskOperationEnum.ConnectToHost:
Player connectedPlayer = new Player(Vector2.Zero);
NetworkTasks.Add(new NetworkTask(connectedPlayer.id));
NetworkTask task = new NetworkTask();
NetworkTasks.Add(task.AddConnectedPlayer(GameManager.GetPlayer1.id, GameManager.GetPlayer1.Pos));
break;
case NetworkTaskOperationEnum.GetClientPlayerId:
GameManager.GetPlayer1.id = networkTask.objId;
break;
case NetworkTaskOperationEnum.AddConnectedPlayer:
Player remoteConnectedPlayer = new Player(networkTask.position);
remoteConnectedPlayer.id = networkTask.objId;
GameManager.players.Add(remoteConnectedPlayer);
break;
default:
break;
}
}
}
public void SetMultiplayerState(MultiPlayerStatus multiPlayerStatus)
{

View file

@ -8,14 +8,18 @@ using System;
using System.Collections.Generic;
using System.Text;
using DangerousD.GameCore.GameObjects.LivingEntities.Monsters;
using System.Linq;
using DangerousD.GameCore.GUI;
using DangerousD.GameCore.Network;
namespace DangerousD.GameCore
{
public class GameManager
{
public List<GameObject> GetAllGameObjects { get; private set; }
private int currentEntityId = 0;
public List<LivingEntity> livingEntities;
public List<LivingEntity> livingEntitiesWithoutPlayers;
public List<Entity> entities;
public List<MapObject> mapObjects;
public List<MapObject> BackgroundObjects;
@ -24,34 +28,43 @@ namespace DangerousD.GameCore
public PhysicsManager physicsManager;
public List<Player> players;
public List<GameObject> otherObjects = new();
public Player GetPlayer1 => players[0];
public Player GetPlayer1 { get; private set; }
public GameManager()
{
others = new List<GameObject>();
GetAllGameObjects = new List<GameObject>();
livingEntities = new List<LivingEntity>();
livingEntitiesWithoutPlayers = new List<LivingEntity>();
mapObjects = new List<MapObject>();
BackgroundObjects = new List<MapObject>();
entities = new List<Entity>();
players = new List<Player>();
mapManager = new MapManager(1);
physicsManager = new PhysicsManager();
}
}
internal void Register(GameObject gameObject)
{
GetAllGameObjects.Add(gameObject);
if (gameObject is Entity)
{
gameObject.id = currentEntityId;
currentEntityId++;
}
if (gameObject is Player objPl)
{
livingEntities.Add(gameObject as LivingEntity);
players.Add(objPl);
if (GetPlayer1 is null)
{
GetPlayer1 = players[players.Count - 1];
}
}
else if (gameObject is LivingEntity objLE)
{
livingEntitiesWithoutPlayers.Add(objLE);
livingEntities.Add(objLE);
}
else if (gameObject is Entity objE)
@ -70,36 +83,6 @@ namespace DangerousD.GameCore
otherObjects.Add(gameObject);
}
}
public void Remove(GameObject gameObject)
{
GetAllGameObjects.Remove(gameObject);
if (gameObject is Player objPl)
{
livingEntities.Remove(gameObject as LivingEntity);
players.Remove(objPl);
}
else if (gameObject is LivingEntity objLE)
{
livingEntities.Remove(objLE);
}
else if (gameObject is Entity objE)
{
entities.Remove(objE);
}
else if (gameObject is MapObject obj)
{
if (obj.IsColliderOn)
mapObjects.Remove(obj);
else
BackgroundObjects.Remove(obj);
}
else
{
otherObjects.Remove(gameObject);
}
}
public void Draw(SpriteBatch _spriteBatch)
{
foreach (var item in BackgroundObjects)
@ -116,21 +99,39 @@ namespace DangerousD.GameCore
public void Update(GameTime gameTime)
{
AppManager.Instance.DebugHUD.Set("playerId: ", GetPlayer1.id.ToString());
if (AppManager.Instance.NetworkTasks.Count > 0)
{
AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList());
AppManager.Instance.NetworkTasks.Clear();
}
foreach (var item in BackgroundObjects)
item.Update(gameTime);
foreach (var item in mapObjects)
item.Update(gameTime);
foreach (var item in entities)
item.Update(gameTime);
for (int i = 0; i < livingEntities.Count; i++)
livingEntities[i].Update(gameTime);
if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Client)
{
for (int i = 0; i < livingEntitiesWithoutPlayers.Count; i++)
{
livingEntitiesWithoutPlayers[i].Update(gameTime);
}
GetPlayer1.Update(gameTime);
}
else
{
for (int i = 0; i < livingEntitiesWithoutPlayers.Count; i++)
{
livingEntitiesWithoutPlayers[i].PlayAnimation();
}
GetPlayer1.Update(gameTime);
}
GetPlayer1.Update(gameTime);
foreach (var item in otherObjects)
item.Update(gameTime);
physicsManager.UpdateCollisions(entities, livingEntities, mapObjects, gameTime);
}
}
}
}

View file

@ -47,7 +47,7 @@ namespace DangerousD.GameCore
PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host)
{
AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName));
AppManager.Instance.NetworkTasks.Add(new Network.NetworkTask(Vector2.Zero, soundName));
}
}
public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция
@ -59,7 +59,7 @@ namespace DangerousD.GameCore
PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host)
{
AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(soundPos, soundName));
AppManager.Instance.NetworkTasks.Add(new Network.NetworkTask(soundPos, soundName));
}
}
public void StopAllSounds() // остановка всех звуков

View file

@ -25,7 +25,7 @@ namespace DangerousD.GameCore.Network
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(IpAddress);
int port = 8000;
int port = 51873;
endPoint = new IPEndPoint(address, port);
}
catch { }
@ -71,6 +71,7 @@ namespace DangerousD.GameCore.Network
Thread acceptThread = new Thread(AcceptSockets);
acceptThread.Start();
state = "Host";
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host);
}
catch { }
}
@ -84,10 +85,13 @@ namespace DangerousD.GameCore.Network
Thread.Sleep(10);
Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
ReceivingThread.Start();
NetworkTask connectionTask = new NetworkTask("Player");
AppManager.Instance.NetworkTasks.Add(connectionTask);
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client);
}
catch { }
}
public void SendMsg(NetworkTask networkTask)
public void SendMsg(List<NetworkTask> networkTask)
{
byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask));
int count = Data.Length;

View file

@ -19,6 +19,7 @@ namespace DangerousD.GameCore.Network
public Vector2 velocity { get; set; }
public Type type { get; set; }
public NetworkTask() { }
/// <summary>
/// Нанести урон сущности
/// </summary>
@ -126,5 +127,12 @@ namespace DangerousD.GameCore.Network
this.velocity = velocity;
this.type = type;
}
public NetworkTask AddConnectedPlayer(int connectedPlayerId, Vector2 playerPosition)
{
operation = NetworkTaskOperationEnum.AddConnectedPlayer;
objId = connectedPlayerId;
position = playerPosition;
return this;
}
}
}

View file

@ -9,6 +9,6 @@ namespace DangerousD.GameCore.Network
[Serializable]
public enum NetworkTaskOperationEnum
{
TakeDamage, SendSound, CreateEntity, SendPosition, ChangeState, ConnectToHost, GetClientPlayerId
TakeDamage, SendSound, CreateEntity, SendPosition, ChangeState, ConnectToHost, GetClientPlayerId, AddConnectedPlayer
}
}