ParticialNetworkImplementation commit

This commit is contained in:
AnloGames 2023-08-17 21:55:48 +03:00
parent d3a0c64596
commit e04e6f19da
7 changed files with 66 additions and 10 deletions

View file

@ -13,6 +13,7 @@ namespace DangerousD.GameCore
public abstract class GameObject : IDrawableObject public abstract class GameObject : IDrawableObject
{ {
protected Vector2 _pos; protected Vector2 _pos;
public int id = 0;
public Vector2 Pos => _pos; public Vector2 Pos => _pos;
public int Width { get; set; } public int Width { get; set; }
public int Height { get; set; } public int Height { get; set; }
@ -45,10 +46,14 @@ namespace DangerousD.GameCore
GraphicsComponent.LoadContent(); GraphicsComponent.LoadContent();
} }
public virtual void Update(GameTime gameTime) public void PlayAnimation()
{ {
GraphicsComponent.Update(); GraphicsComponent.Update();
} }
public virtual void Update(GameTime gameTime)
{
PlayAnimation();
}
public static Texture2D debugTexture; public static Texture2D debugTexture;
public virtual void Draw(SpriteBatch spriteBatch) public virtual void Draw(SpriteBatch spriteBatch)

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
{ {
@ -53,7 +54,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{ {
if (gameObject is Player) if (gameObject is Player)
{ {
isVisible = false; //isVisible = false;
} }
base.OnCollision(gameObject); base.OnCollision(gameObject);
} }
@ -125,6 +126,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{ {
AnimationJump(); AnimationJump();
} }
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
{ {
@ -19,6 +21,7 @@ namespace DangerousD.GameCore
{ {
public static AppManager Instance { get; private set; } public static AppManager Instance { get; private set; }
public string IpAddress { get; private set; } = "127.0.0.1"; public string IpAddress { get; private set; } = "127.0.0.1";
public List<NetworkTask> NetworkTasks { get; set; } = new List<NetworkTask>();
private GraphicsDeviceManager _graphics; private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch; private SpriteBatch _spriteBatch;
public GameState gameState { get; private set; } public GameState gameState { get; private set; }
@ -188,12 +191,17 @@ 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));
break; break;
case NetworkTaskOperationEnum.GetClientPlayerId: case NetworkTaskOperationEnum.GetClientPlayerId:
GameManager.GetPlayer1.id = networkTask.objId;
break; break;
default: default:
break; break;

View file

@ -8,14 +8,16 @@ 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;
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;
@ -31,6 +33,7 @@ namespace DangerousD.GameCore
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>();
@ -45,14 +48,23 @@ namespace DangerousD.GameCore
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);
GetPlayer1 = players[0]; 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)
@ -88,6 +100,11 @@ namespace DangerousD.GameCore
public void Update(GameTime gameTime) public void Update(GameTime gameTime)
{ {
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)
@ -95,8 +112,22 @@ namespace DangerousD.GameCore
foreach (var item in entities) foreach (var item in entities)
item.Update(gameTime); item.Update(gameTime);
for (int i = 0; i < livingEntities.Count; i++) if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Client)
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);
}
foreach (var item in otherObjects) foreach (var item in otherObjects)
item.Update(gameTime); item.Update(gameTime);

View file

@ -46,7 +46,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) // запустить звук у которого есть позиция
@ -58,7 +58,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,8 @@ 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>