Added support code, client instantiation and deletion

This commit is contained in:
SergoDobro 2024-08-25 21:58:15 +03:00
parent aa3cf04cc6
commit fd917022cf
5 changed files with 59 additions and 18 deletions

View file

@ -367,5 +367,15 @@ namespace ZoFo.GameCore
players.Remove(entity as Player); players.Remove(entity as Player);
} }
public void RegisterClientMadeObject(GameObject gameObject)
{
if (gameObject is Particle) particles.Add(gameObject as Particle);
else gameObjects.Add(gameObject);
}
} }
} }

View file

@ -111,11 +111,11 @@ namespace ZoFo.GameCore.GameObjects
public override void DeathEnd() public override void DeathEnd()
{ {
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(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(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(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(new Particle(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20)); Instantiate_OnClient(new Explosion(position - collisionComponent.stopRectangle.Size.ToVector2() / 2 + ExtentionClass.RandomVector() * 20));
base.DeathEnd(); base.DeathEnd();
} }

View file

@ -270,7 +270,7 @@ public class Player : LivingEntity
{ {
for (int i = 3; i <= 3; i++) for (int i = 3; i <= 3; i++)
{ {
Instantiate(new Particle( Instantiate(new Explosion(
((position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * (3 - i) / 3f) + ((position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * (3 - i) / 3f) +
((entity.position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * i / 3f) + ExtentionClass.RandomVector() * 3 ((entity.position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * i / 3f) + ExtentionClass.RandomVector() * 3
)); ));

View file

@ -10,23 +10,27 @@ using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects namespace ZoFo.GameCore.GameObjects
{ {
internal class Particle : GameObject public class Particle : GameObject
{ {
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "explosion_1" }, "explosion_1"); public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "explosion_1" }, "explosion_1");
public Particle(Vector2 position) : base(position) 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<string> { "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);
};
}
}
} }

View file

@ -54,6 +54,33 @@ public abstract class GameObject
#region Client Side #region Client Side
/// <summary>
/// вызывается для создания объектов на клиенте,
/// соответственно используется по большей часте для создания эффектов
/// (например на конец атаки или смерти будут создаваться взирывы, а их бессмысленно передавать каждый раз)
/// </summary>
/// <param name="gameObject"></param>
public void Instantiate_OnClient(GameObject gameObject)
{
if (AppManager.Instance.client != null)
AppManager.Instance.client.RegisterClientMadeObject(gameObject);
}
/// <summary>
/// Используется для локального удаления объектов на клиенте, например, частицы взрыва удаляются после срабатывания,
/// соответственно не надо это очевидное и предсказыемое удаление отправлять с сервераа на клиент
/// </summary>
/// <param name="gameObject"></param>
public void Delete_OnClient(GameObject gameObject)
{
if (AppManager.Instance.client != null)
AppManager.Instance.client.DeleteObject(gameObject);
}
public static Texture2D debugTexture; public static Texture2D debugTexture;
/// <summary> /// <summary>
/// Для клиента /// Для клиента