Merge pull request #109 from progtime-net/Effects

Effects
This commit is contained in:
SergoDobro 2024-09-07 01:48:50 +03:00 committed by GitHub
commit a639dd362e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 236 additions and 49 deletions

View file

@ -133,11 +133,11 @@ namespace ZoFo.GameCore
UpdateShaking();
for (int i = 0; i < gameObjects.Count; i++)
{
gameObjects[i].UpdateAnimations();
gameObjects[i].Update_OnClient();
}
for (int i = 0; i < particles.Count; i++)
{
particles[i].UpdateAnimations();
particles[i].Update_OnClient();
}
networkManager.SendData();//set to ticks
@ -176,6 +176,7 @@ namespace ZoFo.GameCore
internal void UpdatesList(List<UpdateData> updates)
{
//pls, add mutex ot monitor
foreach (var item in updates)
{
GotData(item);
@ -208,22 +209,7 @@ namespace ZoFo.GameCore
else if (update is UpdateGameObjectCreated)
{
//TODO
Entity created_gameObject;
if ((update as UpdateGameObjectCreated).GameObjectType == "Player")
{
created_gameObject = new Player((update as UpdateGameObjectCreated).position.GetVector2());
gameObjects.Add(created_gameObject);
}
else
{
Type t = Type.GetType("ZoFo.GameCore.GameObjects." + (update as UpdateGameObjectCreated).GameObjectType);
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);
}
(gameObjects.Last() as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity);
Update_GameObjectCreated(update as UpdateGameObjectCreated);
}
else if (update is UpdateGameObjectWithoutIdCreated)
@ -285,6 +271,26 @@ namespace ZoFo.GameCore
}
#region UpdatesAnalysis
public void Update_GameObjectCreated(UpdateGameObjectCreated update)
{
if (update.GameObjectType == "Player")
{
Player player = new Player(update.position.GetVector2());
player.SetIdByClient(update.IdEntity);
gameObjects.Add(player);
}
else
{
Type t = Type.GetType("ZoFo.GameCore.GameObjects." + update.GameObjectType);
GameObject gameObject = Activator.CreateInstance(t, update.position.GetVector2()) as GameObject;
if (gameObject is Entity)
(gameObject as Entity).SetIdByClient(update.IdEntity);
gameObjects.Add(gameObject);
}
}
#endregion
public void UpdatePlayerHealth(UpdatePlayerParametrs update)
{
@ -347,6 +353,9 @@ namespace ZoFo.GameCore
public void DeleteObject(GameObject gameObject)
{
if (gameObjects.Contains(gameObject))
gameObjects.Remove(gameObject);
if (gameObject is Entity)
{
DeleteEntity(gameObject as Entity);
@ -368,5 +377,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);
}
}
}

View file

@ -229,7 +229,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
}
return players.ToArray();
}
public Entity[] GetEntities(Rectangle rectangle, Entity entity)
public Entity[] GetEntities(Rectangle rectangle, Entity entityToIgnore = null)
{
List<Entity> entities = new List<Entity>();
@ -240,8 +240,11 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
entities.Add(item);
}
}
if (entities.Contains(entity))
entities.Remove(entity);
if (entityToIgnore!= null)
{
if (entities.Contains(entityToIgnore))
entities.Remove(entityToIgnore);
}
return entities.ToArray();
}
@ -257,7 +260,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
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);
return new Vector2((float)Random.Shared.NextDouble() - 0.5f, (float)Random.Shared.NextDouble() - 0.5f)*2;
}
}

View file

@ -66,8 +66,13 @@ namespace ZoFo.GameCore.GameManagers
sound.SoundEffect.IsLooped = false;
sound.SoundEffect.Volume = sound.baseVolume * AppManager.Instance.SettingsManager.SoundEffectsVolume * AppManager.Instance.SettingsManager.MainVolume;
sound.SoundEffect.Pitch = pitch;
sound.SoundEffect.Play();
PlayingSounds.Add(sound);
//TODO add sound importance, important will be allways played, non-important - not allways
if (PlayingSounds.Count<50) //Exceptino when many sounds
{
sound.SoundEffect.Play();
PlayingSounds.Add(sound);
}
/*/ if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host)
{

View file

@ -32,10 +32,10 @@ namespace ZoFo.GameCore.GameObjects
public virtual void Update()
{
}
public override void UpdateLogic()
public override void UpdateLogic_OnServer()
{
Update();
base.UpdateLogic();
base.UpdateLogic_OnServer();
}
public void StartAnimation(string animationId)

View file

@ -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();
}

View file

@ -34,9 +34,9 @@ public class LivingEntity : Entity
}
public override void UpdateAnimations()
public override void Update_OnClient()
{
base.UpdateAnimations();
base.Update_OnClient();
}
protected Vector2 prevPosition_forClient;
public override void Draw(SpriteBatch spriteBatch)

View file

@ -266,13 +266,12 @@ public class Player : LivingEntity
{
for (int i = 3; i <= 3; i++)
{
Instantiate(new Particle(
Instantiate(new Granade(
((position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * (3 - i) / 3f) +
((entity.position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * i / 3f) + ExtentionClass.RandomVector() * 3
));
}
(entity as Enemy).TakeDamage(1);
}
}
}

View file

@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
@ -10,23 +11,157 @@ 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<string> { "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 += _ => {
}
}
public class Explosion : Particle
{
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "explosion_1" }, "explosion_1");
if (AppManager.Instance.client!=null)
public Explosion(Vector2 position) : base(position)
{
if (AppManager.Instance.client != null)//remove
{
AppManager.Instance.client.AddShaking(0.05f);
}
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);
};
}
}
/// <summary>
/// TODO: change from particle to throwable, it is not a particle anymore
/// </summary>
public class Granade : GameObject
{
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "explosion_1" }, "explosion_1");
/// <summary>
/// TODO updates with directed effect
/// </summary>
/// <param name="positionTo"></param>
/// <param name="positionFrom"></param>
public Granade(Vector2 positionTo) : base(positionTo)
{
if (AppManager.Instance.client.myPlayer != null)
this.positionFrom = AppManager.Instance.client.myPlayer.position;
this.positionTo = positionTo;
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);
};
}
Vector2 positionFrom;
Vector2 positionTo;
float dt = 0;
public override void UpdateLogic_OnServer()
{
if (dt >= 1)
{
FlightEndedOnServer();
return;
}
float m = Math.Min(positionFrom.Y, positionTo.Y) - 40;
position.X = (1 - dt) * positionFrom.X + dt * positionTo.X;
position.Y = (2 * positionTo.Y + 2 * positionFrom.Y - 4 * m) * dt * dt +
(4 * m - positionTo.Y - 3 * positionFrom.Y) * dt + positionFrom.Y;
dt += 0.05f;
base.UpdateLogic_OnServer();
}
private void FlightEndedOnServer()
{
var rect = GetDamageRectangle();
var entities = (AppManager.Instance.server.collisionManager.GetEntities(rect).ToList());
foreach (var item in entities)
{
if (item is Enemy)
{
AppManager.Instance.client.DeleteObject(this);
(item as Enemy).TakeDamage(1);
}
}
}
public override void Update_OnClient()
{
if (dt >= 1)
{
//Granade Finished the flight
Instantiate_OnClient(new Explosion(position + ExtentionClass.RandomVector()*10));
Delete_OnClient(this);
base.Update_OnClient();
for (int i = 0; i < 10; i++)
{
if (Random.Shared.NextDouble() < 0.1) continue;
float angl = i / 10f * (float)Math.PI * 2;
Instantiate_OnClient(new Explosion(position +
new Vector2((float)Math.Cos(angl), (float)Math.Sin(angl)
) * 30));
}
};
}
var rect = GetDamageRectangle();
var entities = (AppManager.Instance.server.collisionManager.GetEntities(rect).ToList());
foreach (var item in entities)
{
if (item is Enemy)
{
(item as Enemy).TakeDamage(1);
}
}
return;
}
float m = Math.Min(positionFrom.Y, positionTo.Y)-40;
position.X = (1 - dt) * positionFrom.X + dt * positionTo.X;
position.Y = (2 * positionTo.Y + 2 * positionFrom.Y - 4 * m) * dt * dt +
(4 * m - positionTo.Y - 3 * positionFrom.Y) * dt + positionFrom.Y;
dt += 0.05f;
//position =
//base.Update_OnClient();
graphicsComponent.ObjectDrawRectangle.X = (int)position.X; //Move To place where Updates Sets your position
graphicsComponent.ObjectDrawRectangle.Y = (int)position.Y;
PlayAnimation_OnClient();
}
public override void Draw(SpriteBatch spriteBatch)
{
DrawDebugRectangle(spriteBatch, GetDamageRectangle(), Color.Red);
base.Draw(spriteBatch);
}
public Rectangle GetDamageRectangle()
{
var rect = graphicsComponent.ObjectDrawRectangle;
int size = 10;
rect.X -= size;
rect.Y -= size;
rect.Width += 2 * size;
rect.Height += 2 * size;
return rect;
}
}
}

View file

@ -26,7 +26,7 @@ public abstract class GameObject
positionDraw = position;
}
public virtual void UpdateLogic()
public virtual void UpdateLogic_OnServer()
{
PlayAnimation_OnServer();
@ -54,6 +54,33 @@ public abstract class GameObject
#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;
/// <summary>
/// Для клиента
@ -82,7 +109,7 @@ public abstract class GameObject
/// Для клиента
/// Обновление, которое вызывается у клиента, для просмотра анимаций
/// </summary>
public virtual void UpdateAnimations()
public virtual void Update_OnClient()
{
positionDraw = (position * 0.15f + positionDraw*0.85f);
graphicsComponent.ObjectDrawRectangle.X = (int)positionDraw.X; //Move To place where Updates Sets your position
@ -103,7 +130,7 @@ public abstract class GameObject
}
public void DrawDebugRectangle(SpriteBatch spriteBatch, Rectangle _rectangle, Nullable<Color> color = null)
{
return;
//return; TODO normal disable and enable via debug modder
if (color is null) color = new Color(1, 0, 0, 0.1f);
if (color.Value.A == 255) color = new Color(color.Value, 0.25f);
spriteBatch.Draw(debugTexture,
@ -112,7 +139,6 @@ public abstract class GameObject
_rectangle.Width * GraphicsComponent.scaling,
_rectangle.Height * GraphicsComponent.scaling), color.Value);
//TODO: debugTexture
}
#endregion
}

View file

@ -171,7 +171,7 @@ namespace ZoFo.GameCore
{
for (int i = 0; i < gameObjects.Count; i++)
{
gameObjects[i].UpdateLogic();
gameObjects[i].UpdateLogic_OnServer();
}
collisionManager.ResolvePhysics();
ticks = 0;