From fd917022cfb488441f34b1c75276c1bc59794567 Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Sun, 25 Aug 2024 21:58:15 +0300 Subject: [PATCH] Added support code, client instantiation and deletion --- ZoFo/GameCore/Client.cs | 10 +++++++ .../Entities/LivingEntities/Enemies/Zombie.cs | 10 +++---- .../Entities/LivingEntities/Player/Player.cs | 2 +- .../Entities/Particles/Particle.cs | 28 +++++++++++-------- ZoFo/GameCore/GameObjects/GameObject.cs | 27 ++++++++++++++++++ 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 1d8561b..ae83a7f 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -367,5 +367,15 @@ namespace ZoFo.GameCore players.Remove(entity as Player); } + public void RegisterClientMadeObject(GameObject gameObject) + { + + if (gameObject is Particle) particles.Add(gameObject as Particle); + else gameObjects.Add(gameObject); + + + + } + } } \ No newline at end of file diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs index 7762de9..31c22a8 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs @@ -111,11 +111,11 @@ namespace ZoFo.GameCore.GameObjects public override void DeathEnd() { - Instantiate(new Particle(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); - Instantiate(new Particle(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); - Instantiate(new Particle(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); - Instantiate(new Particle(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); - Instantiate(new Particle(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); + Instantiate_OnClient(new Explosion(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); + Instantiate_OnClient(new Explosion(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); + Instantiate_OnClient(new Explosion(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); + Instantiate_OnClient(new Explosion(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); + Instantiate_OnClient(new Explosion(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); base.DeathEnd(); } diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index bb7aa24..85771f9 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -270,7 +270,7 @@ public class Player : LivingEntity { for (int i = 3; i <= 3; i++) { - Instantiate(new Particle( + Instantiate(new Explosion( ((position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * (3 - i) / 3f) + ((entity.position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * i / 3f) + ExtentionClass.RandomVector() * 3 )); diff --git a/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs b/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs index a165996..6067c7e 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs @@ -10,23 +10,27 @@ using ZoFo.GameCore.Graphics; namespace ZoFo.GameCore.GameObjects { - internal class Particle : GameObject + public class Particle : GameObject { public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "explosion_1" }, "explosion_1"); public Particle(Vector2 position) : base(position) { - graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0,60,60).SetOrigin(position); - AppManager.Instance.SoundManager.StartSound("gun-gunshot-01", Vector2.Zero, Vector2.Zero, 0.5f, (float)(Random.Shared.NextDouble()*2-1)); - (graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ => { - - if (AppManager.Instance.client!=null) - { - AppManager.Instance.client.DeleteObject(this); - - } - - }; } } + public class Explosion : Particle + { + public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "explosion_1" }, "explosion_1"); + + public Explosion(Vector2 position) : base(position) + { + graphicsComponent.ObjectDrawRectangle = new Rectangle(-30, -30, 60, 60).SetOrigin(position); + AppManager.Instance.SoundManager.StartSound("gun-gunshot-01", Vector2.Zero, Vector2.Zero, 0.5f, (float)(Random.Shared.NextDouble() * 2 - 1)); + (graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ => { + + Delete_OnClient(this); + + }; + } + } } diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index 1a5c1f9..ecd051d 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -54,6 +54,33 @@ public abstract class GameObject #region Client Side + + /// + /// вызывается для создания объектов на клиенте, + /// соответственно используется по большей часте для создания эффектов + /// (например на конец атаки или смерти будут создаваться взирывы, а их бессмысленно передавать каждый раз) + /// + /// + public void Instantiate_OnClient(GameObject gameObject) + { + if (AppManager.Instance.client != null) + AppManager.Instance.client.RegisterClientMadeObject(gameObject); + + } + + /// + /// Используется для локального удаления объектов на клиенте, например, частицы взрыва удаляются после срабатывания, + /// соответственно не надо это очевидное и предсказыемое удаление отправлять с сервераа на клиент + /// + /// + public void Delete_OnClient(GameObject gameObject) + { + if (AppManager.Instance.client != null) + AppManager.Instance.client.DeleteObject(gameObject); + + } + + public static Texture2D debugTexture; /// /// Для клиента