From 51ece0112c277cd0a0b6fd76eb384dfc4fb65e5e Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Sun, 18 Aug 2024 19:47:03 +0300 Subject: [PATCH] Physics: added triggers --- ZoFo/GameCore/Client.cs | 4 +- ZoFo/GameCore/GUI/DebugHUD.cs | 12 +++++ .../CollisionManager/CollisionComponent.cs | 27 ++++++---- .../CollisionManager/CollisionManager.cs | 53 ++++++++++++++++--- .../Interactables/Collectables/Ammo.cs | 12 ++++- .../Interactables/Collectables/Antiradine.cs | 2 +- .../Collectables/BottleOfWater.cs | 2 +- .../Interactables/Collectables/Collectable.cs | 2 +- .../Interactables/Collectables/Peeble.cs | 2 +- .../Collectables/PureBottleOfWater.cs | 2 +- .../Interactables/Collectables/RottenFlesh.cs | 2 +- .../Interactables/Collectables/Steel.cs | 2 +- .../Interactables/Collectables/Wood.cs | 2 +- .../Entities/Interactables/Door.cs | 2 +- .../Entities/Interactables/Interactable.cs | 10 ++-- .../Entities/LivingEntities/Player/Player.cs | 1 + .../Graphics/StaticGraphicsComponent.cs | 4 +- ZoFo/GameCore/Server.cs | 2 +- 18 files changed, 107 insertions(+), 36 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 626f732..325efbc 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -48,7 +48,7 @@ namespace ZoFo.GameCore { InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection, InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection - }); + }); }; } @@ -158,7 +158,7 @@ namespace ZoFo.GameCore var ent = FindEntityById(update.IdEntity); ent.position = (update as UpdatePosition).NewPosition; - DebugHUD.Instance.Log("newPosition " + ent.position); + //DebugHUD.Instance.Log("newPosition " + ent.position); } } diff --git a/ZoFo/GameCore/GUI/DebugHUD.cs b/ZoFo/GameCore/GUI/DebugHUD.cs index 294530a..f7e4b8e 100644 --- a/ZoFo/GameCore/GUI/DebugHUD.cs +++ b/ZoFo/GameCore/GUI/DebugHUD.cs @@ -62,6 +62,10 @@ public class DebugHUD { _text[key] = value; } + public static void DebugSet(string key, string value) + { + Instance._text[key] = value; + } public void Log(string value) { @@ -71,4 +75,12 @@ public class DebugHUD _log.RemoveAt(0); } } + public static void DebugLog(string value) + { + Instance._log.Add(value); + if (Instance._log.Count > 30) + { + Instance._log.RemoveAt(0); + } + } } \ No newline at end of file diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs index fa9f9d6..0209483 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs @@ -43,12 +43,11 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager public GameObject gameObject { get; set; } - bool doesStop; - bool hasCollision; + public bool hasCollision; public Rectangle stopRectangle; // triggers for rectangle - bool isTrigger; + public bool isTrigger; public Rectangle triggerRectangle; //delegate @@ -58,13 +57,21 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager //events DoorInteraction - public event EventHandler OnTriggerEnter; - public event EventHandler OnTriggerZone; - public event EventHandler OnTriggerExit; - - public event EventHandler OnCollision; + public delegate void CollisionAction(Player player); + public event CollisionAction? OnTriggerEnter; + public event CollisionAction? OnTriggerZone; + public event CollisionAction? OnTriggerExit; + + public delegate void CoollisionEvent(GameObject gameObject); + public event CoollisionEvent? OnCollision; + + + + public void PlayerInZone(Player player) => OnTriggerZone?.Invoke(player); + public void PlayerEnter(Player player) => OnTriggerEnter?.Invoke(player); + public void PlayerExit(Player player) => OnTriggerExit?.Invoke(player); + public void OnCollisionWithObject(GameObject gameObject) => OnCollision?.Invoke(gameObject); + - - } } diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs index 25736fe..f1acc92 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -12,6 +12,7 @@ using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.Graphics; +using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; namespace ZoFo.GameCore.GameManagers.CollisionManager { @@ -53,8 +54,9 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager { collidedX = true;// меняем значение соприкосновения на true - entity.OnCollision(item);//подписываем entity на ивент коллизии - + //entity.OnCollision(item);//подписываем entity на ивент коллизии + item.OnCollisionWithObject(entity); + entity.collisionComponent.OnCollisionWithObject(item.gameObject); break;// выход } } @@ -109,35 +111,72 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager entity.velocity = Vector2.Zero; } + public void UpdateTriggerZones(Player player) + { + + var entity = player as LivingEntity; + var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК + currentRect.X += (int)entity.position.X; + currentRect.Y += (int)entity.position.Y; + + + + foreach (var item in ObjectsWithTriggers)//фильтрация + { + if (item.triggerRectangle.SetOrigin(item.gameObject.position).Intersects(currentRect)) + { + item.PlayerInZone(player); + } + } + } + //обновление позиции объекта - public void UpdatePositions() + public void ResolvePhysics() { foreach (var item in EntitiesWithMovements) { CheckComponentCollision(item); } + foreach (var item in AppManager.Instance.server.players) + { + UpdateTriggerZones(item); + } } public CollisionManager() - { - //graphicsComponent - //.ObjectDrawRectangle = new Rectangle(0, 0, 16 * 12, 16 * 16); + { EntitiesWithMovements = new List(); ObjectsWithCollisions = new List(); + ObjectsWithTriggers = new List(); } //регистрация компонента(его коллизии) public void Register(CollisionComponent component) { - ObjectsWithCollisions.Add(component); + if (component.hasCollision) + ObjectsWithCollisions.Add(component); + if (component.isTrigger) + ObjectsWithTriggers.Add(component); if (component.gameObject is LivingEntity) { EntitiesWithMovements.Add(component); } } + public Player[] GetPlayersInZone(Rectangle rectangle) + { + List players = new List(); + foreach (var item in AppManager.Instance.server.players)//фильтрация + { + if (item.collisionComponent.stopRectangle.SetOrigin(item.position).Intersects(rectangle)) + { + players.Add(item); + } + } + return players.ToArray(); + } } public static class ExtentionClass diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs index ba4f1bc..6362c00 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs @@ -8,6 +8,8 @@ using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.Graphics; +using Microsoft.Xna.Framework.Graphics; +using ZoFo.GameCore.GUI; namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { @@ -18,11 +20,19 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { graphicsComponent.ObjectDrawRectangle.Width = 20; graphicsComponent.ObjectDrawRectangle.Height = 20; + + collisionComponent.triggerRectangle = new Rectangle(0, 0, 20, 20); } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { + DebugHUD.DebugLog("collected"); AppManager.Instance.server.AddData(new UpdateLoot("Ammo")); AppManager.Instance.server.DeleteObject(this); } + public override void Draw(SpriteBatch spriteBatch) + { + DrawDebugRectangle(spriteBatch, collisionComponent.triggerRectangle.SetOrigin(position), Color.Blue); + base.Draw(spriteBatch); + } } } diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs index 01f36a9..b8f2bcb 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs @@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("Antiradine")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs index 528f614..77b8cf3 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs @@ -19,7 +19,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("BottleOfWater")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Collectable.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Collectable.cs index 9bb5227..eff3c1f 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Collectable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Collectable.cs @@ -11,7 +11,7 @@ public class Collectable : Interactable { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { // AppManager.Instance.server.AddData(new UpdateLoot()); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs index 5525cfd..c76552d 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs @@ -20,7 +20,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("Peeble")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs index 1523f6e..b0ecc02 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs @@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables public PureBottleOfWater(Vector2 position) : base(position) { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("PureBottleOfWater")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs index 147f30d..c101506 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs @@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("RottenFlesh")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs index 5618236..538f0dd 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs @@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables public Steel(Vector2 position) : base(position) { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("Steel")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs index 85af71b..2db30a5 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs @@ -15,7 +15,7 @@ public class Wood : Collectable { } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { AppManager.Instance.server.AddData(new UpdateLoot("Wood")); AppManager.Instance.server.DeleteObject(this); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs index 23ce44f..06c0898 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs @@ -16,7 +16,7 @@ public class Door : Interactable //graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//���������, ��� ����� ������ ������������� - SD } - public override void OnInteraction(object sender, CollisionComponent e) + public override void OnInteraction(GameObject sender) { //graphicsComponent.AnimationSelect("DoorInteraction", isOpened); //graphicsComponent.AnimationStep(); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs index 270b986..bc61ff9 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs @@ -13,17 +13,19 @@ public class Interactable : Entity public Interactable(Vector2 position) : base(position) { - collisionComponent.OnTriggerEnter += (sender, e) => ChangeInteraction(sender, e, true); - collisionComponent.OnTriggerExit += (sender, e) => ChangeInteraction(sender, e, false); + collisionComponent.isTrigger = true; + collisionComponent.hasCollision = false; + collisionComponent.OnTriggerEnter += (sender) => ChangeInteraction(sender, true); + collisionComponent.OnTriggerExit += (sender) => ChangeInteraction(sender, false); collisionComponent.OnTriggerZone += OnInteraction; } - private void ChangeInteraction(object sender, CollisionComponent e, bool isReady) + private void ChangeInteraction(GameObject sender, bool isReady) { AppManager.Instance.server.AddData(new UpdateInteractionReady((sender as Player).Id, isReady)); } - public virtual void OnInteraction(object sender, CollisionComponent e) + public virtual void OnInteraction(GameObject sender) { } diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 42d89c2..c4617e2 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -40,6 +40,7 @@ public class Player : LivingEntity float t; public void MovementLogic() { + IsTryingToShoot = true; //velocity.X = 3+(float)Math.Sin(t); t++; if (InputPlayerRotation.X > 0.9) diff --git a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs index 3e2fd6c..4c25e2c 100644 --- a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs @@ -48,7 +48,7 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) { - DebugHUD.Instance.Log("draw "); + //DebugHUD.Instance.Log("draw "); destinationRectangle.X -= CameraPosition.X; destinationRectangle.Y -= CameraPosition.Y; @@ -58,7 +58,7 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) { - DebugHUD.Instance.Log("draw "); + //DebugHUD.Instance.Log("draw "); destinationRectangle.X -= CameraPosition.X; destinationRectangle.Y -= CameraPosition.Y; diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index dbbfa94..15d8909 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -155,7 +155,7 @@ namespace ZoFo.GameCore { go.UpdateLogic(); } - collisionManager.UpdatePositions(); + collisionManager.ResolvePhysics(); ticks = 0; networkManager.SendData(); }