Physics: added triggers

This commit is contained in:
SergoDobro 2024-08-18 19:47:03 +03:00
parent 5069d1a4a6
commit 51ece0112c
18 changed files with 107 additions and 36 deletions

View file

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

View file

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

View file

@ -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<CollisionComponent> OnTriggerEnter;
public event EventHandler<CollisionComponent> OnTriggerZone;
public event EventHandler<CollisionComponent> OnTriggerExit;
public delegate void CollisionAction(Player player);
public event CollisionAction? OnTriggerEnter;
public event CollisionAction? OnTriggerZone;
public event CollisionAction? OnTriggerExit;
public event EventHandler<CollisionComponent> OnCollision;
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);
}
}

View file

@ -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<CollisionComponent>();
ObjectsWithCollisions = new List<CollisionComponent>();
ObjectsWithTriggers = new List<CollisionComponent>();
}
//регистрация компонента(его коллизии)
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<Player> players = new List<Player>();
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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ public class Door : Interactable
//graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - SD
}
public override void OnInteraction(object sender, CollisionComponent e)
public override void OnInteraction(GameObject sender)
{
//graphicsComponent.AnimationSelect("DoorInteraction", isOpened);
//graphicsComponent.AnimationStep();

View file

@ -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)
{
}

View file

@ -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)

View file

@ -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;

View file

@ -155,7 +155,7 @@ namespace ZoFo.GameCore
{
go.UpdateLogic();
}
collisionManager.UpdatePositions();
collisionManager.ResolvePhysics();
ticks = 0;
networkManager.SendData();
}