From 685011fd6120b9513f25e0cb2b6329e2a3f5e94d Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Mon, 26 Aug 2024 17:50:51 +0300 Subject: [PATCH] Added Granade Currently damages enemies but based on client side -> need fixing + HOW TO UPDATE POSITION? Need to rethink granade generation anyway --- .../CollisionManager/CollisionManager.cs | 11 +- ZoFo/GameCore/GameObjects/Entities/Entity.cs | 4 +- .../Entities/LivingEntities/Player/Player.cs | 3 +- .../Entities/Particles/Particle.cs | 123 ++++++++++++++++-- ZoFo/GameCore/GameObjects/GameObject.cs | 5 +- ZoFo/GameCore/Server.cs | 2 +- 6 files changed, 126 insertions(+), 22 deletions(-) diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs index 234e912..7faaca4 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -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 entities = new List(); @@ -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; } } diff --git a/ZoFo/GameCore/GameObjects/Entities/Entity.cs b/ZoFo/GameCore/GameObjects/Entities/Entity.cs index fc7cea8..3a97d3b 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Entity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Entity.cs @@ -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) diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 85771f9..fae6f27 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -270,13 +270,12 @@ public class Player : LivingEntity { for (int i = 3; i <= 3; i++) { - Instantiate(new Explosion( + 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); } } } diff --git a/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs b/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs index c641896..065f704 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Particles/Particle.cs @@ -1,4 +1,5 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +14,7 @@ namespace ZoFo.GameCore.GameObjects public class Particle : GameObject { public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "explosion_1" }, "explosion_1"); - + public Particle(Vector2 position) : base(position) { } @@ -24,41 +25,143 @@ namespace ZoFo.GameCore.GameObjects 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 += _ => { + (graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ => + { Delete_OnClient(this); }; } } - public class Granade : Particle + /// + /// TODO: change from particle to throwable, it is not a particle anymore + /// + public class Granade : GameObject { public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "explosion_1" }, "explosion_1"); - public Granade(Vector2 positionTo, Vector2? positionFrom = null) : base(positionTo) - { - + /// + /// TODO updates with directed effect + /// + /// + /// + 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 += _ => { + (graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ => + { - Delete_OnClient(this); + //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) + { + (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 = (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(); + //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; } } } diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index 56ce1ba..4c977d2 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -26,7 +26,7 @@ public abstract class GameObject positionDraw = position; } - public virtual void UpdateLogic() + public virtual void UpdateLogic_OnServer() { PlayAnimation_OnServer(); @@ -130,7 +130,7 @@ public abstract class GameObject } public void DrawDebugRectangle(SpriteBatch spriteBatch, Rectangle _rectangle, Nullable 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, @@ -139,7 +139,6 @@ public abstract class GameObject _rectangle.Width * GraphicsComponent.scaling, _rectangle.Height * GraphicsComponent.scaling), color.Value); - //TODO: debugTexture } #endregion } \ No newline at end of file diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index 3668d76..fba46a3 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -170,7 +170,7 @@ namespace ZoFo.GameCore { for (int i = 0; i < gameObjects.Count; i++) { - gameObjects[i].UpdateLogic(); + gameObjects[i].UpdateLogic_OnServer(); } collisionManager.ResolvePhysics(); ticks = 0;