merge net part 1

This commit is contained in:
SergoDobro 2024-08-20 12:12:27 +03:00
parent d2187b223a
commit f7688a7785
11 changed files with 36 additions and 24 deletions

View file

@ -23,7 +23,6 @@ using System.Web;
using ZoFo.GameCore.GUI;
using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
using ZoFo.GameCore.Graphics;
using Newtonsoft.Json.Linq;
@ -165,14 +164,21 @@ namespace ZoFo.GameCore
}
internal void GotData(List<UpdateData> updates)
internal void UpdatesList(List<UpdateData> updates)
{
foreach (var item in updates)
{
GotData(item);
}
}
internal void GotData(UpdateData update)
{
if (update is UpdateTileCreated)
{
mapObjects.Add(
new MapObject(
(update as UpdateTileCreated).Position,
(update as UpdateTileCreated).Position.GetVector2(),
(update as UpdateTileCreated).Size.GetPoint().ToVector2(),
(update as UpdateTileCreated).sourceRectangle.GetRectangle(),
(update as UpdateTileCreated).tileSetName
@ -182,7 +188,7 @@ namespace ZoFo.GameCore
{
stopObjects.Add(
new StopObject(
(update as UpdateStopObjectCreated).Position,
(update as UpdateStopObjectCreated).Position.GetVector2(),
(update as UpdateStopObjectCreated).Size.GetPoint().ToVector2(),
(update as UpdateStopObjectCreated).sourceRectangle.GetRectangle(),
(update as UpdateStopObjectCreated).tileSetName,
@ -194,7 +200,7 @@ namespace ZoFo.GameCore
Entity created_gameObject;
if ((update as UpdateGameObjectCreated).GameObjectType == "Player")
{
created_gameObject = new Player((update as UpdateGameObjectCreated).position);
created_gameObject = new Player((update as UpdateGameObjectCreated).position.GetVector2());
players.Add(created_gameObject as Player);
myPlayer = players[0];
gameObjects.Add(created_gameObject);
@ -202,7 +208,7 @@ namespace ZoFo.GameCore
else
{
Type t = Type.GetType("ZoFo.GameCore.GameObjects." + (update as UpdateGameObjectCreated).GameObjectType);
GameObject gameObject = Activator.CreateInstance(t, (update as UpdateGameObjectCreated).position) as GameObject;
GameObject gameObject = Activator.CreateInstance(t, (update as UpdateGameObjectCreated).position.GetVector2()) as GameObject;
if (gameObject is Entity)
(gameObject as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity);
gameObjects.Add(gameObject);
@ -213,7 +219,7 @@ namespace ZoFo.GameCore
else if (update is UpdateGameOBjectWithoutIdCreated)
{
Type t = Type.GetType("ZoFo.GameCore.GameObjects." + (update as UpdateGameOBjectWithoutIdCreated).GameObjectClassName);
GameObject gameObject = Activator.CreateInstance(t, (update as UpdateGameOBjectWithoutIdCreated).position) as GameObject;
GameObject gameObject = Activator.CreateInstance(t, (update as UpdateGameOBjectWithoutIdCreated).position.GetVector2()) as GameObject;
if (gameObject is Particle)
particles.Add(gameObject as Particle);
}
@ -222,7 +228,7 @@ namespace ZoFo.GameCore
var ent = FindEntityById(update.IdEntity);
if (ent != null)
ent.position = (update as UpdatePosition).NewPosition;
ent.position = (update as UpdatePosition).NewPosition.GetVector2();
}
else if (update is UpdateAnimation)
{

View file

@ -234,6 +234,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
rectangle.Y += (int)origin.Y;
return rectangle;
}
public static SerializableVector2 Serialize(this Vector2 vector) => new SerializableVector2(vector);
public static Vector2 RandomVector()
{
return new Vector2((float)Random.Shared.NextDouble() - 0.5f, (float)Random.Shared.NextDouble() - 0.5f);

View file

@ -99,7 +99,7 @@ namespace ZoFo.GameCore.GameManagers.MapManager
AppManager.Instance.server.RegisterGameObject(new MapObject(position,
new Vector2(tileSet.TileWidth, tileSet.TileHeight),
sourceRectangle,
"Textures/TileSetImages/" + Path.GetFileName(tileSet.Image).Replace(".png", "")));
"Content/Textures/TileSetImages/" + Path.GetFileName(tileSet.Image).Replace(".png", "")));
break;
case "StopObject":
@ -108,7 +108,7 @@ namespace ZoFo.GameCore.GameManagers.MapManager
AppManager.Instance.server.RegisterGameObject(new StopObject(position,
new Vector2(tileSet.TileWidth, tileSet.TileHeight),
sourceRectangle,
"Textures/TileSetImages/" + Path.GetFileName(tileSet.Image).Replace(".png", ""),
"Content/Textures/TileSetImages/" + Path.GetFileName(tileSet.Image).Replace(".png", ""),
collisionRectangles.ToArray()));
break;

View file

@ -194,7 +194,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
}
void ExecuteDatagramm(Datagramm Dgramm)
{
AppManager.Instance.client.GotData(Dgramm.updateDatas);
AppManager.Instance.client.UpdatesList(Dgramm.updateDatas);
//Достаёт Update и передает в ивент
}

View file

@ -23,4 +23,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO
return new Vector2(X, Y);
}
}
}

View file

@ -34,7 +34,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public delegate void OnDataSend(string data);
public event OnDataSend GetDataSend; // event
Thread serverThread;
int datapackSize = 150;
public ServerNetworkManager() { Init(); }
/// <summary>
@ -114,7 +114,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
if (importantUpdates.Count != 0 || sendedData.Count != 0)
{
dataToSend = new List<UpdateData>();
for (int i = 0; i < 200 && i < importantUpdates.Count; i++)
for (int i = 0; i < datapackSize && i < importantUpdates.Count; i++)
dataToSend.Add(importantUpdates[i]);
for (int i = 0; i < clientsEP.Count; i++)
@ -133,7 +133,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
}
}
currentDatagrammId++;
for (int i = 0; i < 200 && i < dataToSend.Count; i++)
for (int i = 0; i < datapackSize && i < dataToSend.Count; i++)
importantUpdates.RemoveAt(0);
}
Datagramm unImpDgramm = new Datagramm();

View file

@ -11,6 +11,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
{
public UpdateGameOBjectWithoutIdCreated() { UpdateType = "UpdateGameOBjectWithoutIdCreated"; }
public string GameObjectClassName { get; set; }
public Vector2 position { get; set; }
public SerializableVector2 position { get; set; }
}
}

View file

@ -11,9 +11,9 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
{
internal class UpdateStopObjectCreated : UpdateData
{
public UpdateStopObjectCreated() { UpdateType = "UpdateStopObjectCreated"; }
public UpdateStopObjectCreated() { UpdateType = "UpdateStopObjectCreated"; isImportant = true; }
public Texture2D TextureTile { get; set; }
public Vector2 Position { get; set; }
public SerializableVector2 Position { get; set; }
public SerializablePoint Size { get; set; }
public SerializableRectangle sourceRectangle { get; set; }
public string tileSetName { get; set; }

View file

@ -15,14 +15,16 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates
[JsonDerivedType(typeof(UpdateGameEnded))]
[JsonDerivedType(typeof(UpdateGameObjectCreated))]
[JsonDerivedType(typeof(UpdateGameObjectDeleted))]
[JsonDerivedType(typeof(UpdateInteraction))]
[JsonDerivedType(typeof(UpdateInteractionReady))]
[JsonDerivedType(typeof(UpdateLoot))]
[JsonDerivedType(typeof(UpdateGameOBjectWithoutIdCreated))]
[JsonDerivedType(typeof(UpdatePlayerParametrs))]
[JsonDerivedType(typeof(UpdatePosition))]
[JsonDerivedType(typeof(UpdateStopObjectCreated))]
[JsonDerivedType(typeof(UpdateTileCreated))]
[JsonDerivedType(typeof(UpdateInput))]
[JsonDerivedType(typeof(UpdatePlayerExit))]
[JsonDerivedType(typeof(UpdateInteractionReady))]
[JsonDerivedType(typeof(UpdateInteraction))]
public class UpdateData
{

View file

@ -9,7 +9,7 @@ using ZoFo.GameCore.GUI;
namespace ZoFo.GameCore.GameObjects;
public class Collectable : Interactable
{
protected static readonly string _path = "Textures/icons/Collectables/";
protected static readonly string _path = "Content/Textures/icons/Collectables/";
public Collectable(Vector2 position) : base(position) {
graphicsComponent.ObjectDrawRectangle.Width = 20;

View file

@ -187,7 +187,7 @@ namespace ZoFo.GameCore
{
AddData(new UpdateStopObjectCreated()
{
Position = (gameObject as StopObject).position,
Position = (gameObject as StopObject).position.Serialize(),
sourceRectangle = new SerializableRectangle((gameObject as StopObject).sourceRectangle),
Size = new SerializablePoint((gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size),
tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName,
@ -218,7 +218,7 @@ namespace ZoFo.GameCore
AddData(new UpdateGameOBjectWithoutIdCreated()
{
GameObjectClassName = gameObject.GetType().Name,
position = gameObject.position
position = gameObject.position.Serialize()
});
return;
}
@ -229,7 +229,7 @@ namespace ZoFo.GameCore
{
GameObjectType = gameObject.GetType().Name,
IdEntity = entity.Id,
position = gameObject.position
position = gameObject.position.Serialize()
});
collisionManager.Register(entity.collisionComponent);
entities.Add(entity);
@ -238,7 +238,7 @@ namespace ZoFo.GameCore
AddData(new UpdateGameObjectCreated()
{
GameObjectType = gameObject.GetType().Name,
position = gameObject.position
position = gameObject.position.Serialize()
});