ParticialNetworkImplementation commit
This commit is contained in:
parent
d3a0c64596
commit
e04e6f19da
7 changed files with 66 additions and 10 deletions
|
@ -13,6 +13,7 @@ namespace DangerousD.GameCore
|
|||
public abstract class GameObject : IDrawableObject
|
||||
{
|
||||
protected Vector2 _pos;
|
||||
public int id = 0;
|
||||
public Vector2 Pos => _pos;
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
|
@ -45,10 +46,14 @@ namespace DangerousD.GameCore
|
|||
GraphicsComponent.LoadContent();
|
||||
}
|
||||
|
||||
public virtual void Update(GameTime gameTime)
|
||||
public void PlayAnimation()
|
||||
{
|
||||
GraphicsComponent.Update();
|
||||
}
|
||||
public virtual void Update(GameTime gameTime)
|
||||
{
|
||||
PlayAnimation();
|
||||
}
|
||||
|
||||
public static Texture2D debugTexture;
|
||||
public virtual void Draw(SpriteBatch spriteBatch)
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
@ -53,7 +54,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
|
|||
{
|
||||
if (gameObject is Player)
|
||||
{
|
||||
isVisible = false;
|
||||
//isVisible = false;
|
||||
}
|
||||
base.OnCollision(gameObject);
|
||||
}
|
||||
|
@ -125,6 +126,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
|
|||
{
|
||||
AnimationJump();
|
||||
}
|
||||
if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.SinglePlayer)
|
||||
{
|
||||
NetworkTask task = new NetworkTask(id, Pos);
|
||||
AppManager.Instance.NetworkTasks.Add(task);
|
||||
}
|
||||
}
|
||||
public void MoveDown()
|
||||
{
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
@ -19,6 +21,7 @@ namespace DangerousD.GameCore
|
|||
{
|
||||
public static AppManager Instance { get; private set; }
|
||||
public string IpAddress { get; private set; } = "127.0.0.1";
|
||||
public List<NetworkTask> NetworkTasks { get; set; } = new List<NetworkTask>();
|
||||
private GraphicsDeviceManager _graphics;
|
||||
private SpriteBatch _spriteBatch;
|
||||
public GameState gameState { get; private set; }
|
||||
|
@ -188,12 +191,17 @@ 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));
|
||||
break;
|
||||
case NetworkTaskOperationEnum.GetClientPlayerId:
|
||||
GameManager.GetPlayer1.id = networkTask.objId;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
|
@ -8,14 +8,16 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using DangerousD.GameCore.GameObjects.LivingEntities.Monsters;
|
||||
using System.Linq;
|
||||
|
||||
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;
|
||||
|
@ -31,6 +33,7 @@ namespace DangerousD.GameCore
|
|||
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>();
|
||||
|
@ -45,14 +48,23 @@ namespace DangerousD.GameCore
|
|||
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);
|
||||
GetPlayer1 = players[0];
|
||||
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)
|
||||
|
@ -88,6 +100,11 @@ namespace DangerousD.GameCore
|
|||
|
||||
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)
|
||||
item.Update(gameTime);
|
||||
foreach (var item in mapObjects)
|
||||
|
@ -95,8 +112,22 @@ namespace DangerousD.GameCore
|
|||
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);
|
||||
}
|
||||
foreach (var item in otherObjects)
|
||||
item.Update(gameTime);
|
||||
|
||||
|
|
|
@ -46,7 +46,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) // запустить звук у которого есть позиция
|
||||
|
@ -58,7 +58,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() // остановка всех звуков
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -19,6 +19,8 @@ namespace DangerousD.GameCore.Network
|
|||
public Vector2 velocity { get; set; }
|
||||
public Type type { get; set; }
|
||||
|
||||
|
||||
public NetworkTask() { }
|
||||
/// <summary>
|
||||
/// Нанести урон сущности
|
||||
/// </summary>
|
||||
|
|
Loading…
Add table
Reference in a new issue