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; protected Vector2 _pos;
public Vector2 Pos => _pos; public Vector2 Pos => _pos;
public int id;
public int Width { get; set; } public int Width { get; set; }
public int Height { get; set; } public int Height { get; set; }
public Rectangle Rectangle => new Rectangle((int)Pos.X, (int)Pos.Y, Width, Height); 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(); GraphicsComponent.LoadContent();
} }
public void LoadContent()
{
PlayAnimation();
}
public virtual void Update(GameTime gameTime) public virtual void Update(GameTime gameTime)
{ {

View file

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

View file

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

View file

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

View file

@ -47,7 +47,7 @@ namespace DangerousD.GameCore
PlayingSounds.Add(sound); PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host) 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) // запустить звук у которого есть позиция public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция
@ -59,7 +59,7 @@ namespace DangerousD.GameCore
PlayingSounds.Add(sound); PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host) 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() // остановка всех звуков public void StopAllSounds() // остановка всех звуков

View file

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

View file

@ -19,6 +19,7 @@ namespace DangerousD.GameCore.Network
public Vector2 velocity { get; set; } public Vector2 velocity { get; set; }
public Type type { get; set; } public Type type { get; set; }
public NetworkTask() { }
/// <summary> /// <summary>
/// Нанести урон сущности /// Нанести урон сущности
/// </summary> /// </summary>
@ -126,5 +127,12 @@ namespace DangerousD.GameCore.Network
this.velocity = velocity; this.velocity = velocity;
this.type = type; 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] [Serializable]
public enum NetworkTaskOperationEnum public enum NetworkTaskOperationEnum
{ {
TakeDamage, SendSound, CreateEntity, SendPosition, ChangeState, ConnectToHost, GetClientPlayerId TakeDamage, SendSound, CreateEntity, SendPosition, ChangeState, ConnectToHost, GetClientPlayerId, AddConnectedPlayer
} }
} }